Getting/Setting a Windows Size/Position

  • Thread starter Thread starter BrianKE
  • Start date Start date
B

BrianKE

I am attempting to create an app that will run "on top" of another app.
My app will create a transparent window over the other app for the
purpose of displaying information.

I am able to create my transparent app but cannot get the size and
position of the other app. I can get the handle of the other app using
the DllImport("user32")...FindWindow method. However, when I try to
get this size/position of this other window using
DllImport("gdi32")...GetBoundsRect it does not assign any value to the
Rectangle object referenced by GetBoundsRect.

I believe my problem lies in the 'flags' attribute of GetBoundsRect.
The method signature is as follows:

[DllImport("gdi32.dll")]
public static extern int GetBoundsRect(int hWnd, // handle to window
ref Rectangle rect, // bounding rectangle
int flags); // function options


All of the documentation I have found on this method refer to only one
valid value for the 'flags' variable: DCB_RESET (Clears the bounding
rectangle after returning it. If this flag is not set, the bounding
rectangle will not be cleared.) I cannot figure out how to set or
retrieve this value. Do I need to get this value from somewhere or do
I need to initialize in my program before using? Any help would be
appreciated.

I am fairly new to C# (coming from a Java background) and am using
Visual Studio 2005 Professional.

TIA,
Brian Enderle
 
Brian,
However, when I try to
get this size/position of this other window using
DllImport("gdi32")...GetBoundsRect it does not assign any value to the
Rectangle object referenced by GetBoundsRect.

GetBoundsRect isn't the function you want to use (it works on device
contexts, not windows). Have a look at GetWindowRect instead.

And you can't use a System.Drawing.Rectangle as the parameter type,
since it doesn't have the same layout as the GDI RECT struct.


Mattias
 
I am attempting to create an app that will run "on top" of another app.
My app will create a transparent window over the other app for the
purpose of displaying information.

I am able to create my transparent app but cannot get the size and
position of the other app. I can get the handle of the other app using
the DllImport("user32")...FindWindow method. However, when I try to
get this size/position of this other window using
DllImport("gdi32")...GetBoundsRect it does not assign any value to the
Rectangle object referenced by GetBoundsRect.

I believe my problem lies in the 'flags' attribute of GetBoundsRect.
The method signature is as follows:

[DllImport("gdi32.dll")]
public static extern int GetBoundsRect(int hWnd, // handle to window
ref Rectangle rect, // bounding rectangle
int flags); // function options


All of the documentation I have found on this method refer to only one
valid value for the 'flags' variable: DCB_RESET (Clears the bounding
rectangle after returning it. If this flag is not set, the bounding
rectangle will not be cleared.) I cannot figure out how to set or
retrieve this value. Do I need to get this value from somewhere or do
I need to initialize in my program before using? Any help would be
appreciated.

I am fairly new to C# (coming from a Java background) and am using
Visual Studio 2005 Professional.

TIA,
Brian Enderle

Hi Brian,

Instead, try using GetWindowRect:
http://tinyurl.com/bd7eb
 
Thanks for the quick replies. GetWindowRect works great but brings up
another question: I would like to have my transparent app lie within
the client area of the other app. I can use GetClientRect to get the
size of this area but how do I properly position it with respect to the
other app, ie; how do I account for the frame, toolbars, etc.?

GetClientRect returns the size of the client rectangle but not the
position of the rectangle within the window. Using Spy++ Client Rect.
shows the coordinates of the client rectangle relative to the window
but when I use the method (GetClientRect) within my program all I get
is the rectangle size (598, 0, 799, 0).

Thanks again,
Brian Enderle

Tom said:
I am attempting to create an app that will run "on top" of another app.
My app will create a transparent window over the other app for the
purpose of displaying information.

I am able to create my transparent app but cannot get the size and
position of the other app. I can get the handle of the other app using
the DllImport("user32")...FindWindow method. However, when I try to
get this size/position of this other window using
DllImport("gdi32")...GetBoundsRect it does not assign any value to the
Rectangle object referenced by GetBoundsRect.

I believe my problem lies in the 'flags' attribute of GetBoundsRect.
The method signature is as follows:

[DllImport("gdi32.dll")]
public static extern int GetBoundsRect(int hWnd, // handle to window
ref Rectangle rect, // bounding rectangle
int flags); // function options


All of the documentation I have found on this method refer to only one
valid value for the 'flags' variable: DCB_RESET (Clears the bounding
rectangle after returning it. If this flag is not set, the bounding
rectangle will not be cleared.) I cannot figure out how to set or
retrieve this value. Do I need to get this value from somewhere or do
I need to initialize in my program before using? Any help would be
appreciated.

I am fairly new to C# (coming from a Java background) and am using
Visual Studio 2005 Professional.

TIA,
Brian Enderle

Hi Brian,

Instead, try using GetWindowRect:
http://tinyurl.com/bd7eb
 
Thanks for the quick replies. GetWindowRect works great but brings up
another question: I would like to have my transparent app lie within
the client area of the other app. I can use GetClientRect to get the
size of this area but how do I properly position it with respect to the
other app, ie; how do I account for the frame, toolbars, etc.?

GetClientRect returns the size of the client rectangle but not the
position of the rectangle within the window. Using Spy++ Client Rect.
shows the coordinates of the client rectangle relative to the window
but when I use the method (GetClientRect) within my program all I get
is the rectangle size (598, 0, 799, 0).

Thanks again,
Brian Enderle

Tom said:
I am attempting to create an app that will run "on top" of another app.
My app will create a transparent window over the other app for the
purpose of displaying information.

I am able to create my transparent app but cannot get the size and
position of the other app. I can get the handle of the other app using
the DllImport("user32")...FindWindow method. However, when I try to
get this size/position of this other window using
DllImport("gdi32")...GetBoundsRect it does not assign any value to the
Rectangle object referenced by GetBoundsRect.

I believe my problem lies in the 'flags' attribute of GetBoundsRect.
The method signature is as follows:

[DllImport("gdi32.dll")]
public static extern int GetBoundsRect(int hWnd, // handle to window
ref Rectangle rect, // bounding rectangle
int flags); // function options


All of the documentation I have found on this method refer to only one
valid value for the 'flags' variable: DCB_RESET (Clears the bounding
rectangle after returning it. If this flag is not set, the bounding
rectangle will not be cleared.) I cannot figure out how to set or
retrieve this value. Do I need to get this value from somewhere or do
I need to initialize in my program before using? Any help would be
appreciated.

I am fairly new to C# (coming from a Java background) and am using
Visual Studio 2005 Professional.

TIA,
Brian Enderle

Hi Brian,

Instead, try using GetWindowRect:
http://tinyurl.com/bd7eb

Hi Brian,

Are you still trying to pass a System.Drawing.Rectangle as the parameter?
This won't work, because GetWindowRect and related functions using a 'Rect'
structure are not the right layout. You'll need to do something like this:

///
[StructLayout(LayoutKind.Sequential)]
public struct NativeRect
{
public int left;
public int top;
public int right;
public int bottom;
}
///

(This is all from memory, I think I got the attribute right)

Then pass a NativeRect in as the parameter, this should give you the correct
rectangle co-ordinates.
 
Back
Top