get window handle from point

  • Thread starter Thread starter Logan McKinley
  • Start date Start date
L

Logan McKinley

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:
//---
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
static public extern IntPtr WindowFromPoint(System.Drawing.Point Point);
//...
h = (System.Drawing.Point)hits[iii]; // this is an array of Point
objects
ip = WindowFromPoint(h);
System.Diagnostics.Trace.WriteLine(ip.ToString());
g = Graphics.FromHwnd(ip.Handle);
g.FillEllipse(b,h.X,h.Y,20,20);
//---

but it seems to return sparatic results.
However the following code works consistantly

//---
//...
h = (System.Drawing.Point)hits[iii]; // this is an array of Point
objects
g = Graphics.FromHwnd(this.Handle); // <------------------- change
g.FillEllipse(b,h.X,h.Y,20,20);
//---

The problem with the second code is that the dots are apparently drawn under
the picture box and cannot be seen. I need the dot to be top most so the
user can see it.
Thanks in advance,
~Logan
 
You really need to take some time to learn about GDI+ in .Net, rather
than jumping through hoops to draw a simple ellipse. You'd probably have an
easier time using the Control.CreateGraphics object as well, so that you
could apply the ellipse directly to the control which has the mouse click.

Chris R.
 
As far as I understand it is a bad practice to draw something outside
OnPaint event handler.

Paint is initiated by the system and you have no guarantee system will not
call paint after you have your ellipse drawn and erase it.

If you do not have a possibility to modify the window's WM_PAINT event
handler (which is OnPaint for .NET controls) for some reason you may try to
use the following trick.

1. Place additional control over the control of your interest.
2. Draw additional graphics according to your business logic.
3. Make it (additional control) transparent by using Region property.

Good luck.
 
The code snippet I gave is in a function called from OnPaint but I need to
call it at other times as well to add a point when the window is not being
repainted. I guess I could take everything out of the function and put it
just in the OnPaint and then invalidate the display, but I don't think that
would help my problem.

Unfortunately I cannot have transparent layers set on my application for
outside reasons. I am sorry, I don't think I explained my problem
effectively. I believe the real problem I am having is with the
ip = WindowFromPoint(h);
g = Graphics.FromHwnd(ip);
part of the code not working correctly. The reason I think that is because
when I use
This.Handle
in place of "ip" it will draw correctly on that window but not on the
others.

Thanks,
~Logan

Max said:
As far as I understand it is a bad practice to draw something outside
OnPaint event handler.

Paint is initiated by the system and you have no guarantee system will not
call paint after you have your ellipse drawn and erase it.

If you do not have a possibility to modify the window's WM_PAINT event
handler (which is OnPaint for .NET controls) for some reason you may try to
use the following trick.

1. Place additional control over the control of your interest.
2. Draw additional graphics according to your business logic.
3. Make it (additional control) transparent by using Region property.

Good luck.

Logan McKinley said:
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:
//---
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
static public extern IntPtr WindowFromPoint(System.Drawing.Point Point);
//...
h = (System.Drawing.Point)hits[iii]; // this is an array of Point
objects
ip = WindowFromPoint(h);
System.Diagnostics.Trace.WriteLine(ip.ToString());
g = Graphics.FromHwnd(ip.Handle);
g.FillEllipse(b,h.X,h.Y,20,20);
//---

but it seems to return sparatic results.
However the following code works consistantly

//---
//...
h = (System.Drawing.Point)hits[iii]; // this is an array of Point
objects
g = Graphics.FromHwnd(this.Handle); // <------------------- change
g.FillEllipse(b,h.X,h.Y,20,20);
//---

The problem with the second code is that the dots are apparently drawn under
the picture box and cannot be seen. I need the dot to be top most so the
user can see it.
Thanks in advance,
~Logan
 
Back
Top