Code:
#COMPILE EXE '#Win 7.00# #DIM ALL #INCLUDE "win32api.inc" '2002/12/03 'We will use this home brewed type to send and extract info 'from both EnumwindowProc and EnumControl, since we can 'only send one long, we are going to use a pointer to a 'variable of this type. TYPE enumtype Hndl AS DWORD zClass AS ASCIIZ * %max_path zCaption AS ASCIIZ * %max_path id AS DWORD hParent AS DWORD Count AS DWORD END TYPE '______________________________________________________________________________ 'retreive windows controls via class FUNCTION EnumControl(BYVAL hWindowProc AS LONG, BYVAL lParam AS LONG) AS LONG '--------------------------------------------------------------------------- 'this function is called via the code line... ' enumchildwindows EnumTry1.hndl, codeptr(EnumControl), varptr(EnumTry2) ' EnumTry1.hndl refer to the parent window ' EnumTry2 is our variable to get/set info we need. 'the way it work is that windows will do a kind of do-loop with us, 'so this function will be executed many times by windows, 'each time putting a new child (control) handle in the hWindowProc variable, 'the loop will continue until every child (control) handle 'are enumerated if the function return %true. 'if the function return %false then the enumeration 'is stopped, it's our job to set the function to return '%false when we have what we where searching for. 'the code in this function depend of what you want to achieve, 'usually it's retrieving handle, caption text, class, control id... '--------------------------------------------------------------------------- LOCAL LenClass AS LONG LOCAL LenCaption AS LONG LOCAL zClass AS ASCIIZ * %max_path LOCAL zCaption AS ASCIIZ * %max_path LOCAL EnumTryptr AS enumtype POINTER STATIC Looper AS LONG 'Retreive the pointer of EnumTry2 so we can read and change it EnumTryptr = lParam 'Get the class of the control LenClass = getclassname(hWindowProc, zClass, %max_path) 'Get the text of the control sendmessage hWindowProc, %wm_gettext, %max_path, VARPTR(zCaption) 'Did we found our control class? IF UCASE$(zClass) = UCASE$(@EnumTryptr.zClass) THEN INCR Looper 'We want the third edit control IF Looper = @EnumTryptr.count THEN 'Found the "interface prefix" text box @EnumTryptr.hndl = hWindowProc 'Put handle in EnumTry2 @EnumTryptr.zCaption = zCaption 'Put caption in EnumTry2 @EnumTryptr.zClass = zClass 'Put class in EnumTry2 @EnumTryptr.id = getdlgctrlid(hWindowProc) 'Get the control id and put it EnumTry2 FUNCTION = %false : EXIT FUNCTION 'Stop enumeration and exit END IF END IF FUNCTION = %true 'True, so function will recall itself until last window found END FUNCTION '______________________________________________________________________________ FUNCTION EnumwindowProc(BYVAL hWindowProc AS LONG, BYVAL lParam AS LONG) AS LONG 'Retreive windows handle via caption '--------------------------------------------------------------------------- 'This function is called via the code line... ' EnumWindows codeptr(EnumwindowProc), varptr(EnumTry1) ' EnumTry1 is our variable to get/set info we need. 'the way it work is that windows will do a kind of do-loop with us, 'so this function will be executed many times by windows, 'each time putting a new window handle in the hWindowProc variable, 'the loop will continue until every window handle 'are enumerated if the function return %true. 'if the function return %false then the enumeration 'is stopped, it's our job to set the function to return '%false when we have what we where searching for. 'The code in this function depend of what you want to achieve, 'usually it's retrieving a window handle by it's caption text or class name. '--------------------------------------------------------------------------- LOCAL zCaption AS ASCIIZ * %max_path LOCAL zClass AS ASCIIZ * %max_path LOCAL EnumTryptr AS enumtype POINTER LOCAL LenClass AS LONG LOCAL LenCaption AS LONG 'retreive the pointer of EnumTry1 so we can read and change it EnumTryptr = lParam IF iswindowvisible(hWindowProc) THEN 'get only visible windows IF getparent(hWindowProc) = @EnumTryptr.hparent THEN 'under desktop if 0 or under parent IF iswindowenabled(hWindowProc) THEN 'get only enabled windows LenClass = getclassname (hWindowProc, zClass, %max_path) 'get the class of the dialog LenCaption = getwindowtext(hWindowProc, zCaption, %max_path) 'get the caption of the dialog 'did we found a dialog with our text? IF UCASE$(zCaption) = UCASE$(@EnumTryptr.zCaption) THEN @EnumTryptr.hndl = hWindowProc 'put handle in EnumTry1 @EnumTryptr.zCaption = zCaption 'put caption in EnumTry1 @EnumTryptr.zClass = zClass 'put class in EnumTry1 FUNCTION = %false : EXIT FUNCTION 'stop enumeration and exit END IF END IF END IF END IF FUNCTION = %true 'true, so function will recall itself until last window found END FUNCTION '______________________________________________________________________________ FUNCTION PBMAIN LOCAL sometext AS ASCIIZ * 50 LOCAL EnumTry1 AS enumtype LOCAL EnumTry2 AS enumtype 'The dialog title that we want to find EnumTry1.zCaption = "powerbasic com browser" 'Zero mean that we want a window under the desktop EnumTry1.hparent = 0 'If window exist EnumTry will be filled with window handle EnumWindows CODEPTR(EnumwindowProc), VARPTR(EnumTry1) 'EnumTry1.hndl <- window handle 'EnumTry1.zCaption <- window caption 'EnumTry1.zClass <- window class 'Maybe we now have the window handle IF EnumTry1.hndl THEN 'not zero, so we found it 'We want a control with a "edit" class EnumTry2.zClass = "edit" 'We want the third control with a "edit" class EnumTry2.count = 3 'try it with one or two for other edit control 'Start the search for controls EnumChildWindows EnumTry1.hndl, CODEPTR(EnumControl), VARPTR(EnumTry2) 'Did we find it ? IF EnumTry2.hndl THEN 'not zero, so we found it 'EnumTry2.id <- control id 'EnumTry2.hndl <- control handle 'EnumTry2.zClass <- class name 'EnumTry2.zCaption <- caption text 'write text to this control sometext = "Play it again Sam... at " & TIME$ sendmessage EnumTry2.hndl, %WM_SETTEXT, 0 ,VARPTR(sometext) MSGBOX "Done!" & $CRLF & _ "The text was: " & $TAB & EnumTry2.zCaption & $CRLF & _ "The new text is: " & $TAB & sometext & $CRLF & _ "Control handle is: " & $TAB & HEX$(EnumTry2.hndl) & " hex" & $CRLF & _ "Control id is: " & $TAB & FORMAT$(EnumTry2.id) & " dec" & $CRLF & _ "Control class is: " & $TAB & EnumTry2.zClass & $CRLF & _ "Window handle is: " & $TAB & HEX$(EnumTry1.hndl) & " hex" & $CRLF & _ "Window caption is: " & $TAB & EnumTry1.zCaption & $CRLF & _ "Window class is: " & $TAB & EnumTry1.zClass, _ %MB_ICONINFORMATION OR %MB_OK OR %MB_TOPMOST, sometext ELSE MSGBOX "sorry, edit control handle not found!", _ %MB_ICONEXCLAMATION OR %MB_OK OR %MB_TOPMOST, "we have a problem !" END IF ELSE MSGBOX "Did you started the ""PowerBASIC com browser"" ?" & $CRLF & _ "In pb ide: tool, PowerBASIC com browser", _ %MB_ICONQUESTION OR %MB_OK OR %MB_TOPMOST, "Handle not found!" END IF END FUNCTION '______________________________________________________________________________
Comment