Getting window position from IntPtr window handle

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

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 ) but this
does not work for non .NET windows.

I am expecting to need to use SendMessage somewhere along the line,
but cannot find the correct message to get the window position. Also,
is there a way to perform simple operations on a window such as
BringToFront?

Any help would be very much appreciated!
Many Thanks

Matt
 
Matt,

If you want to get the current position of any window, given the handle,
you can call GetWindowRect through the P/Invoke layer to get the position of
the window relative to the screen.

As for calling BringToFront, I would actually try and get the Control
instance for the window handle. If it exists, then call BringToFront,
otherwise, I would then cobble together code that does it manually (the .NET
control might have an override of BringToFront which has functionality you
want to preserve).

If you have to, you can probably call SetWindowPos to set the Z-order of
the window so that it is at the top of the stack.

Hope this helps.
 
Back
Top