Get Window Handle from Class

Started by Jeremy Whilde, February 07, 2017, 09:49:03 AM

Previous topic - Next topic

Jeremy Whilde

Is it possible in WBto get a window handle from a window class?

Thanks JW

td

The short answer is mostly, yes in is possible sometimes.  The long answer is that often multiple windows have the same class name so it is not the most reliable of approaches.  There are several functions within the Control Manager Extender that can be used to obtain a window handle from a class name.  You might want to take a look at the cFindByClass function in the Consolidated WIL Help file as a starting point.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde

OK Can get the handle for the windows class using the suggested extender but any idea why this code would not provide the width and height?
The height of the windows taskbar should be 40 pixels but the following code returns 1080 in the last RECT parameter!

Thanks JW

AddExtender("wwctl44i.dll")
handle  = cFindbyClass("Shell_TrayWnd")
;
;--------------------------------------------------------------------------------;
;GetWindowPos : Gets window position in real screen x,y                          ;
;--------------------------------------------------------------------------------;
;handle : dialog handle of window                                                ;
;--------------------------------------------------------------------------------;
;Returns: string = "xpos,ypos,widht,height"                                      ;
;--------------------------------------------------------------------------------;
#DefineFunction GetWindowPos(handle)
   user32=StrCat(DirWindows(1),"user32.dll")
   lpRect = BinaryAlloc(16)
   ret = -1
   If DllCall(user32,long:"GetWindowRect",long:handle,lpbinary:lpRect) Then
      ret = StrCat(BinaryPeek4(lpRect,0),",",BinaryPeek4(lpRect,4),",",BinaryPeek4(lpRect,8),",",BinaryPeek4(lpRect,12))
   EndIf
   BinaryFree(lpRect)
   Return ret
#EndFunction
rect=GetWindowPos(handle)
Message("xpos,ypos,widht,height",rect)

td

Works perfectly fine on my Windows 10 notebook.  Try calling 'DllLastError()' when/if  the GetWindowRect function fails to get the system error code.  You can find the text for system error code in the Tech Database or on MSFT's MSDN Website.

   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde

Did it return the correct height for the taskbar 40 pixels?

Thanks JW

td

Mmmm.  Me thinks there may be some confusion about what the GetWindowRect function returns in the members of the RECT structure.  It returns the upper-left and low-right coordinates of the window's rectangle.  For example, on my system the upper value is 1050 and the lower value is 1080 so 1080-1050 = 30 which is the correct height of the task bar on my system.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde

OK yes the RECT structure is returning left x, top y, right x, bottom y

So the height is bottom y - top y so in my case 1080 - 1040 = 40 which is the correct taskbar height obtained from the taskbar class of  Shell_TrayWnd.

Seems whoever originally posted the GetWindowPos function forgot to put in the calculations for width and height!

Thanks pointing out what the RECT structure was returning, maybe this can be put into the support database for GetWindowRect use in WB, I did search around but did not find a fully functional example like this from  a Windows class.

Thanks JW

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde

Quote from: td on February 08, 2017, 08:49:09 PM
Before you use a Win32 function you have not used before, it is best to get the how-it-works info straight from horse's mouth:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633519(v=vs.85).aspx

and

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162897(v=vs.85).aspx

Yes I had looked at both of these links already but what was unclear was in the WB UDF I had found and tried to adapt was how did the RECT structure work, in the UDF header it has implied that Returns: string = "xpos,ypos,widht,height"   so that is why I could not work out what was happening. So I have cleaned up the UDF and code example as below:

Thanks JW

;=======================================================
;GetWindowPos : Gets window position in real screen x,y
;handle : dialog handle of window                       
;Returns: string = "xtop,ytop,xbot,ybot"               
;=======================================================
#DefineFunction GetWindowPos(handle)
   user32=StrCat(DirWindows(1),"user32.dll")
   lpRect = BinaryAlloc(16)
   ret = -1
   If DllCall(user32,long:"GetWindowRect",long:handle,lpbinary:lpRect) Then
      ret = StrCat(BinaryPeek4(lpRect,0),",",BinaryPeek4(lpRect,4),",",BinaryPeek4(lpRect,8),",",BinaryPeek4(lpRect,12))
                EndIf
   BinaryFree(lpRect)
                Return ret
#EndFunction

AddExtender("wwctl44i.dll")
winclass = "Shell_TrayWnd"        ; class of window to find
handle  = cFindbyClass(winclass)

rect=GetWindowPos(handle)     ; Get target window position

; Window height
ybot = ItemExtract( 4, rect, "," )
ytop = ItemExtract( 2, rect, "," )
height = ybot - ytop

; Window width
xbot = ItemExtract( 3, rect, "," )
xtop = ItemExtract( 1, rect, "," )
width = xbot - xtop

Message("Target Window Rectangle", StrCat("Xtop,Ytop,Xbot,Ybot: ", rect, "%@CRLF%%@CRLF%","Height: ", height, " ", "Width: ", width))

exit

td

Quote from: Jeremy Whilde on February 13, 2017, 07:08:21 AM

Yes I had looked at both of these links already but what was unclear was in the WB UDF I had found and tried to adapt was how did the RECT structure work, in the UDF header it has implied that Returns: string = "xpos,ypos,widht,height"   so that is why I could not work out what was happening. So I have cleaned up the UDF and code example as below:


The MSFT documentation is very clear about the contents of the RECT structure and the UDF simply extract that contents.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeremy Whilde


[/quote]

The MSFT documentation is very clear about the contents of the RECT structure and the UDF simply extract that contents.
[/quote]

Yes you are correct it is my limited understanding that was the issue, the MS documentation is very clear if you are experienced or have an understanding of whats going on.

Thanks JW

td

I suppose it does require some basic understanding of the Cartesian coordinates system.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade