473,473 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3544
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.