472,780 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Graphics.FromHwnd(_pictureBox.Handle)


I am walking through some of the very first sample code from the book
"Beginning .NET Game Programming" from Apress. I identify his sample
code with //SC

This code puzzles me:

Graphics graph = new Graphics //SC

// first of all there is no constructor for Graphics
// so in the Form_Paint I use instead:
private void Form1_Paint(object sender, PaintEventArgs pe)
{
Graphics graph = pe.Graphics;

//Then he wants to do:

graph = Graphics.FromHwnd(picSource.Image.); // SC
graph.FillRectangle( // SC
new SolidBrush(Color.Red), // SC
30, 20, 13, 7); // SC
This doesn't work at all because .FromHwnd is expecting an IntPtr

Can some one possibly see what the author is trying to do in this code?

Why would he want to acquire a handle from a pictureBox?

He can get it from:

IntPtr hdc = pe.Graphics.GetHdc();

Jul 21 '05 #1
2 3457
First of all that code has serious typos in. Secondly it's a mess that will
lead you down alleyways you don't want to be walking on a dark night.

I haven't read the book in question. Good job I wasn't the technical
reviewer on that one is all I can say.

You are correct in obtaining the Graphics object from the event arguments in
the Paint handler. You can modify the other mess to draw a rectangle for you
like this:

//Remove the line which reads graph=new Graphics.From.......
//just use...
graph.FillRectangle(Brushes.Red,30,20,13,7);

You'll see a red rectangle on the window.

For more details on how when and where to get the Graphics object, details
of what and what not to do with the picturebox plus a bunch of other stuff
that I'm sure you'll find useful check out the GDI+ FAQ.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"John Bailo" <ja*****@earthlink.net> wrote in message
news:%t*****************@newsread3.news.pas.earthl ink.net...

I am walking through some of the very first sample code from the book
"Beginning .NET Game Programming" from Apress. I identify his sample code
with //SC

This code puzzles me:

Graphics graph = new Graphics //SC

// first of all there is no constructor for Graphics
// so in the Form_Paint I use instead:
private void Form1_Paint(object sender, PaintEventArgs pe)
{
Graphics graph = pe.Graphics;

//Then he wants to do:

graph = Graphics.FromHwnd(picSource.Image.); // SC
graph.FillRectangle( // SC
new SolidBrush(Color.Red), // SC
30, 20, 13, 7); // SC
This doesn't work at all because .FromHwnd is expecting an IntPtr

Can some one possibly see what the author is trying to do in this code?

Why would he want to acquire a handle from a pictureBox?

He can get it from:

IntPtr hdc = pe.Graphics.GetHdc();

Jul 21 '05 #2
Bob Powell [MVP] wrote:
First of all that code has serious typos in. Secondly it's a mess that will
lead you down alleyways you don't want to be walking on a dark night.
Thanks for confirming my fears!

This book looks completely /unedited/ right from the start!!!

If I can find the receipt I might return it to Powell's ( $42 bucks !)

I thought Apress was a good publisher, but man, this one is a dog!

I might just read it for the general concepts of gaming math and look
for source on the web....

I haven't read the book in question. Good job I wasn't the technical
reviewer on that one is all I can say.

You are correct in obtaining the Graphics object from the event arguments in
the Paint handler. You can modify the other mess to draw a rectangle for you
like this:

//Remove the line which reads graph=new Graphics.From.......
//just use...
graph.FillRectangle(Brushes.Red,30,20,13,7);

You'll see a red rectangle on the window.

For more details on how when and where to get the Graphics object, details
of what and what not to do with the picturebox plus a bunch of other stuff
that I'm sure you'll find useful check out the GDI+ FAQ.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Martin | last post by:
Hi, I have create a Graphics object from a HWND Both the Window and the Graphics object are created by the same thread, nevertheless when i dispose the object, I get an exception indicating that...
3
by: Logan McKinley | last post by:
I need to draw a dot where ever the user clicks (which will be on either the form or a couple dynamically placed picture boxes). I thought the following code should work: //--- static public...
1
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics...
1
by: DBC User | last post by:
Hi Sharpies, This is a very basic question. I am using .Net IDE 2003 and System.Drawing.dll (1.1.4322). I wrote a simple program like this (ommitting the commons) using System.Drawing; .....
3
by: Bob Steuernagel | last post by:
This code is for a simple drawing program that lets the user draw lines, rectangles, and circles in different colors. When the menu is dropped down to change objects, it erases anything drawn...
2
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph...
1
by: Benny Raymond | last post by:
I've seen both used... FromHwnd can be called like this FromHwnd(this.Handle). FromHdc needs a call to User32.GetWindowDC(this.Handle) - so my assumption is that FromHwnd is better because it's...
1
by: Polaris | last post by:
Hi Experts: I'd like to be able to draw image outside my application. I know I can get a drawing surface for a form by using: Graphics g = Form.CreateGraphics Just wonder, if there is...
10
by: anuking | last post by:
Hi, I made a tool that compares the texts from 2 richtextboxes and then marks the characters that are different in red. An option needs me to overlap these 2 text data to show the exact...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.