473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

get window handle from point

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
Nov 15 '05 #1
3 10027
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.

"Logan McKinley" <lo***@globalweb.net> wrote in message
news:#2**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
Max
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" <lo***@globalweb.net> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #3
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" <Pl****@reply.to.news.group> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <lo***@globalweb.net> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #4

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

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
0
by: Matt Warner | last post by:
Hi guys, A couple of people have already posted questions about similar issues but haven't had any response. Occasionally, sometimes after running the app for a few hours, it bombs out saying...
4
by: Brad Jones | last post by:
<Previously posted in microsoft.public.dotnet.framework.windowsforms> Hi all. Any suggestions here would be appreciated. Thanks for reading. I'm primarly a C++ developer but I've been trying to...
2
by: Simon Wigzell | last post by:
I use a subroutine to popup windows in my sebsite. It looks like this : function poplink(filename) { filestring = filename + "htm" LinkWindow = window.open(filestring,'LinkWindow',''); } ...
1
by: Arne | last post by:
Hi, I keep getting "Error creating window handle." now and then while running my C# application. I've tried to figure out why, but I'm stuck. What is the usual cause of this error ? The error...
1
by: Matt | last post by:
Hi I am looking for a way in C# to use a IntPtr handle to a (possibly unmanaged) windows form to get the current position of the window on the desktop. I have tried using Form.FromHandle( handle...
4
by: harvie wang | last post by:
Hi, I use API GetDesktopWindow to get desktop window handle and compare with WindowFromPoint(x,y) to get window handle. GetDesktopWindow return 65556 WindowFromPoint return 65700 but I use...
2
by: Nathaniel | last post by:
I play an online game application that displays how many points I have earned throught my game play. I want the number of points I have earned to appear as a string in a text box in the program I...
0
by: msdnuniv | last post by:
Hello! I have an application running since 2004 on win98 to Server 2003 SP1 and Vista without problems. But on Server 2003 with SP2 sometimes window handles of opened, but hidden windows...
0
by: Sasie7679 | last post by:
Dear All, We have a requirement to open any document with a specific application and display it within a OCX control (Example - word, excel, adobe reader, MSPaint, Photoshop etc). We are using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.