viewpoint-particle

Author Topic: Get Window Handle from Class  (Read 1018 times)

Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Get Window Handle from Class
« on: February 07, 2017, 09:49:03 am »
Is it possible in WBto get a window handle from a window class?

Thanks JW

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #1 on: February 07, 2017, 10:11:35 am »
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Re: Get Window Handle from Class
« Reply #2 on: February 08, 2017, 09:54:22 am »
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

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #3 on: February 08, 2017, 10:22:17 am »
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.

   
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Re: Get Window Handle from Class
« Reply #4 on: February 08, 2017, 10:31:26 am »
Did it return the correct height for the taskbar 40 pixels?

Thanks JW

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #5 on: February 08, 2017, 10:45:46 am »
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Re: Get Window Handle from Class
« Reply #6 on: February 08, 2017, 02:40:37 pm »
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

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #7 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
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Re: Get Window Handle from Class
« Reply #8 on: February 13, 2017, 07:08:21 am »
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

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #9 on: February 13, 2017, 08:08:28 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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Re: Get Window Handle from Class
« Reply #10 on: February 13, 2017, 08:56:28 am »

[/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

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: Get Window Handle from Class
« Reply #11 on: February 13, 2017, 03:01:06 pm »
I suppose it does require some basic understanding of the Cartesian coordinates system.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates