Hwnd Help

BlitzPlus Forums/BlitzPlus Programming/Hwnd Help

Rob(Posted 2003) [#1]
Hi all,

Given just the hwnd handle returned by findwindow etc, how is it possible to find the position of the hwnd window?
Help much appreciated, ta!


Perturbatio(Posted 2003) [#2]
From the win32 SDK:


The GetWindowPlacement function retrieves the show state and the restored, minimized, and maximized positions of the specified window.

BOOL GetWindowPlacement(

HWND hWnd, // handle of window
WINDOWPLACEMENT *lpwndpl // address of structure for position data
);


Parameters

hWnd

Identifies the window.

lpwndpl

Points to the WINDOWPLACEMENT structure that receives the show state and position information.
Before calling GetWindowPlacement, set the length member of the WINDOWPLACEMENT structure to sizeof(
WINDOWPLACEMENT).
GetWindowPlacement fails if lpwndpl->length is not set correctly.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The flags member of WINDOWPLACEMENT retrieved by this function is always zero. If the window identified by the hWnd parameter is maximized, the showCmd member is SW_SHOWMAXIMIZED. If the window is minimized, showCmd is SW_SHOWMINIMIZED. Otherwise, it is SW_SHOWNORMAL.
The length member of WINDOWPLACEMENT must be set to sizeof(WINDOWPLACEMENT). If this member is not set correctly, the function returns FALSE.

See Also

SetWindowPlacement, WINDOWPLACEMENT





The WINDOWPLACEMENT structure contains information about the placement of a window on the screen.

typedef struct _WINDOWPLACEMENT { // wndpl
UINT length;
UINT flags;
UINT showCmd;
POINT ptMinPosition;
POINT ptMaxPosition;
RECT rcNormalPosition;
} WINDOWPLACEMENT;


Members

length

Specifies the length, in bytes, of the structure. Before calling the GetWindowPlacement or SetWindowPlacement functions, set this member to sizeof(WINDOWPLACEMENT).

GetWindowPlacement and SetWindowPlacement fail if this member is not set correctly.

flags

Specifies flags that control the position of the minimized window and the method by which the window is restored. This member can be one or both of the following values:



Value Meaning
WPF_RESTORETOMAXIMIZED
Specifies that the restored window will be maximized, regardless of whether it was maximized before it was minimized. This setting is only valid the next time the window is restored. It does not change the default restoration behavior. This flag is only valid when the SW_SHOWMINIMIZED value is specified for the showCmd member.
WPF_SETMINPOSITION
Specifies that the coordinates of the minimized window may be specified. This flag must be specified if the coordinates are set in the ptMinPosition member.


showCmd

Specifies the current show state of the window. This member can be one of the following values:



Value Meaning
SW_HIDE Hides the window and activates another window.
SW_MINIMIZE Minimizes the specified window and activates the top-level window in the system's list.
SW_RESTORE Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_SHOWNORMAL).
SW_SHOW Activates a window and displays it in its current size and position.
SW_SHOWMAXIMIZED Activates a window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates a window and displays it as an icon.
SW_SHOWMINNOACTIVE Displays a window as an icon. The active window remains active.
SW_SHOWNA Displays a window in its current state. The active window remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_RESTORE).


ptMinPosition

Specifies the coordinates of the window's upper-left corner when the window is minimized.

ptMaxPosition

Specifies the coordinates of the window's upper-left corner when the window is maximized.

rcNormalPosition

Specifies the window's coordinates when the window is in the restored position.



See Also

ShowWindow, POINT, RECT




The POINT structure defines the x- and y- coordinates of a point.

typedef struct tagPOINT { // pt
LONG x;
LONG y;
} POINT;


Members

x

Specifies the x-coordinate of the point.

y

Specifies the y-coordinate of the point.



The GetWindowPlacement function is in user32


soja(Posted 2003) [#3]
Note:
If we're talking about a Blitz window here, it's easiest just to call GadgetX and GadgetY.


Rob(Posted 2003) [#4]
Thanks Perturbatio! now to turn that into a decls...


Rob(Posted 2003) [#5]
;.lib "user32.dll"
;GetWindowPlacement(hwnd%, lpwndpl%) : "GetWindowPlacement"

winx=GetWindowPlacement(hwnd,0)


I'm new to all this, what could I possibly be doing wrong?


EOF(Posted 2003) [#6]
lpwndpl needs to be a pointer to a type structure or a bank. Use * instead of %

I can only get this working with a bank so far.

; .lib "user32.dll"
; GetWindowPlacement%(hwnd%,windowplacementPTR*):"GetWindowPlacement"

; BANK values

; length%                00
; flags%                 04
; window_status%         08
; minimised_pos_X%       12
; minimised_pos_Y%       16
; maximised_pos_X%       20
; maximised_pos_Y%       24
; normal_pos_X%          28
; normal_pos_Y%          32
; normal_pos_W%          36
; normal_pos_H%          40
;     (sizeof)           44

wb=CreateBank(44)
PokeInt wb,0,44

; =================================================

title$="WindowStatus example"
AppTitle title$
win=CreateWindow(title$,60,60,250,90,Desktop(),1+2+8)
lab=CreateLabel("Resize me ..",4,4,170,14,win)
SetGadgetLayout lab,1,1,0,0

bbWin=QueryObject(win,1)

Repeat
	ev=WaitEvent()
	GetWindowPlacement bbWin,wb
	SetGadgetText lab,"'normal' WIDTH = " +Str$(PeekInt(wb,36))
Until ev=$803 Or ev=$103
End



soja(Posted 2003) [#7]
Yes, the second parameter is an OUT parameter, meaning that Windows will try to write to it for you to read after the function call... so essentially you're telling Windows to write to NULL and you end up getting a memory access violation.

What Syntax Error says is right... Here's a modified version that uses a Custom Type instead of a bank. It works quite nicely since all the parameters are 32 bits long.

Type WindowPlacement
	Field length%
	Field flags%
	Field showcmd%
	Field minposX%
	Field minposY%
	Field maxposX%
	Field maxposY%
	Field normalposLeft%
	Field normalposTop%
	Field normalposRight%
	Field normalposBottom%
End Type

wp.WindowPlacement = New WindowPlacement
wp\length = 44 ; Sizeof WindowPlacement structure (bytes)

CreateWindow("GetWindowPlacement Example",100,100,200,200)
w = GetForegroundWindow()
If GetWindowPlacement(w, wp) Then
	Print "Length: " + wp\length
	Print "Flags: " + wp\flags
	Print "Showcmd: " + wp\showcmd
	Print "MinPos.x: " + wp\minposx
	Print "MinPos.y: " + wp\minposy
	Print "MaxPos.x: " + wp\maxposx
	Print "MaxPos.y: " + wp\maxposy
	Print "NormalPos.Left: " + wp\normalposLeft
	Print "NormalPos.Top: " + wp\normalposTop
	Print "NormalPos.Right: " + wp\normalposRight
	Print "NormalPos.Bottom: " + wp\normalposBottom
Else
	Notify "GetWindowPlacement call failed."
EndIf

Notify "Done."

Here are some of the definitions:

; From Winuser.h:
;
;/*
; * ShowWindow() Commands
; */
;#define SW_HIDE 0
;#define SW_SHOWNORMAL 1
;#define SW_NORMAL 1
;#define SW_SHOWMINIMIZED 2
;#define SW_SHOWMAXIMIZED 3
;#define SW_MAXIMIZE 3
;#define SW_SHOWNOACTIVATE 4
;#define SW_SHOW 5
;#define SW_MINIMIZE 6
;#define SW_SHOWMINNOACTIVE 7
;#define SW_SHOWNA 8
;#define SW_RESTORE 9
;#define SW_SHOWDEFAULT 10
;#define SW_FORCEMINIMIZE 11
;#define SW_MAX 11


The documentation is your friend. Check this out and ask if you have any questions about it -- everything necessary to write this function, or know where else to look, is included there: GetWindowPlacement on MSDN

<EDIT> One more thing... Here are the declarations I used:
.lib "user32.dll"
GetForegroundWindow%():"GetForegroundWindow"
GetWindowPlacement%(hwnd%, lpwndpl*):"GetWindowPlacement"



Rob(Posted 2003) [#8]
Thanks syntax and soja! a lifesaver - I'll try it out as soon as possible! :)