By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Getting/Setting a Windows Size/Position

P: n/a
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

Jun 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Br*****@gmail.com wrote:
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

--
Hope this helps,
Tom Spink
Jun 28 '06 #2

P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 28 '06 #3

P: n/a
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 Spink wrote:
Br*****@gmail.com wrote:
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

--
Hope this helps,
Tom Spink


Jun 28 '06 #4

P: n/a
Br*****@gmail.com wrote:
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 Spink wrote:
Br*****@gmail.com wrote:
> 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

--
Hope this helps,
Tom Spink


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.

--
Hope this helps,
Tom Spink
Jun 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.