|
Post by metro on Mar 18, 2022 1:42:42 GMT -5
After some advice, I use one of my programs all day , I am constantly finding myself having to let go of the mouse and and press WIN-D to minimize the 6 to 8 windows I have open (other programs not my code). Of course then I have to drag my program up from the taskbar.
Does anyone have any-code that can list the handles of all open or running programs. I want to minimize them (except one) from a button on my program.
I can call #user32 to close the windows if I had a list.
Thanks in advance
|
|
|
Post by Walt Decker on Mar 18, 2022 7:12:54 GMT -5
The EnumWindows function enumerates all top-level windows on the screen by passing the handle of each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc, // pointer to callback function LPARAM lParam // application-defined value );
Parameters
lpEnumFunc
Points to an application-defined callback function. For more information, see the EnumWindowsProc callback function.
lParam
Specifies a 32-bit, application-defined value to be passed to the callback function.
Return Values
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
Remarks
The EnumWindows function does not enumerate child windows. This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.
============================================================================== The EnumDesktopWindows function enumerates all windows in a desktop by passing the handle of each window, in turn, to an application-defined callback function.
BOOL EnumDesktopWindows(
HDESK hDesktop, // handle to desktop to enumerate WNDENUMPROC lpfn, // points to application's callback function LPARAM lParam // 32-bit value to pass to the callback function );
Parameters
hDesktop
Specifies the handle to the desktop whose windows are to be enumerated. The CreateDesktop, OpenDesktop, and GetThreadDesktop functions return a desktop handle.
lpfn
Points to an application-defined EnumWindowsProc callback function.
lParam
Specifies a 32-bit application-defined value to be passed to the callback function.
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 EnumDesktopWindows function repeatedly invokes the lpfn callback function until the last window is enumerated or the callback function returns FALSE.
=============================================================================
The EnumWindowsProc function is an application-defined callback function that receives top-level window handles as a result of a call to the EnumWindows or EnumDesktopWindows function.
BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window LPARAM lParam // application-defined value );
Parameters
hwnd
Identifies a top-level window.
lParam
Specifies the application-defined value given in EnumWindows or EnumDesktopWindows.
Return Values
To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE.
Remarks
The callback function can perform any desired task.
An application must register this callback function by passing its address to EnumWindows or EnumDesktopWindows. EnumWindowsProc is a placeholder for the application-defined function name. The WNDENUMPROC type is a pointer to an EnumWindowsProc function.
|
|
|
Post by metro on Mar 18, 2022 7:23:08 GMT -5
Thanks, Walt. I shall read and digest. It May take a bit to get a handle on it (yep intended) I appreciate the help
metro
|
|
|
Post by Walt Decker on Mar 18, 2022 7:34:11 GMT -5
If you have two monitors the EnumDesktopWindows() function is probably the better choice.
If you have problems setting it up either, do not hesitate to ask.
|
|
|
Post by metro on Mar 18, 2022 8:21:38 GMT -5
I appreciate the help Walt. After reading what you put up I did some digging and found some old code of Carl's. I am still trying to come to grips with it as it is beyond my skill set ATM. I have managed to achieve a result but would like to actually use the windows handles and not the WindowtextA. Still playing............
Also need to test on a windoze machine as I use Linux Mint and native Linux windows do not minimize , however all WINE windows do! With Linux I get strange Window handles for example ExternalHandle{154 1 10 0 }
texteditor #win.te, 10, 10, 250, 250 open "Enum Windows Example" for window as #win print #win, "trapclose [quit]" 'set the variable named address to be the memory address for 'enumWndProc() using types handle and ulong, and set 'the return type of enumWndProc() to be a boolean callback address, enumWndProc(handle, ulong), boolean 'call EnumWindows, which in turn calls back into the 'BASIC function at address. print address calldll #user32, "EnumWindows", _ address as ulong, _ 0 as long, _ result as boolean wait [quit] close #win end function enumWndProc(hwnd, lparam) labelBuffer$ = space$(71) calldll #user32, "GetWindowTextA", _ hwnd as ulong, _ labelBuffer$ as ptr, _ 70 as long, _ result as long '================================================ 'KLUDGE AS I KNOW THE NAME OF MY PROGRAM '================================================ res2=instr(labelBuffer$,"Enum") if res2 <1 then call closeWin hwnd end if '================================================ if left$(labelBuffer$, 1) <> chr$(0) then print hwnd;" ";labelBuffer$ print #win.te, labelBuffer$;" ";hwnd call setCount getCount()+1 end if if getCount() = 15 then enumWndProc = 0 'returning 0 causes EnumWindows to return else enumWndProc = 1 end if end function sub setCount value count(0) = value end sub function getCount() getCount = count(0) end function
SUB closeWin h1 calldll #user32, "CloseWindow",_ h1 as ulong,_ result as boolean END SUB
|
|
|
Post by Walt Decker on Mar 18, 2022 9:17:44 GMT -5
I do not use Linux so I can not help you there. Perhaps Rod or Mr. Iverson can.
Make a global handle and set it to HWND(#YOUR WINDOW). Then compare the returned handles with your handle. Right here: calldll #user32, "GetWindowTextA", _ hwnd as ulong, _ labelBuffer$ as ptr, _ 70 as long, _ '<--- this should be 71 result as long '<--- the number of characters returned in labelBuffer$ Why are you limiting the search to 15 windows?
EDIT: You do not have to kill the apps you do not want visible. You can send the user32.dll funciton ShowWindow() with the argument SW.MINIMIZE E. G.
SW.MINIMIZE = 6
CALLDLL #user32, "ShowWindow", WinHndl AS ULONG, SW.MINIMIZE AS LONG, RetVal AS VOID
If the window you want to work with is minimized you can change the argument to:
SW.SHOWNORMAL = 1 CALLDLL #user32, "ShowWindow", WinHndl AS ULONG, SW.NORMAL AS LONG, RetVal AS VOID
|
|
|
Post by metro on Mar 18, 2022 19:05:12 GMT -5
Thanks for the feedback, Walt. In my first iteration, I did have the handle of #win, however, it did not help as Linux (I think) provides strange results for the handles. I'm not sure what goes on in the background with WINE So what I have done is minimized all windows then brought to the top the window I want. I will investigate your other options and also run the code on my WIN10 laptop to see is the handle results match what I expect they should Sill work in progress.....
SUB bring2Top h1 ShowFlag = _SW_RESTORE calldll #user32, "ShowWindow",_ h1 as ulong,_ ShowFlag as long,_ result As boolean END SUB
EDIT, oops I picked 15 for no particular reason, it is unlikely that I will have more that 6 to 8 windows open
Carl's original code had 5 I believe It is my understanding (could be wrong), CloseWindow will only minimize the window. however to "kill" I need calldll #user32, "DestroyWindow", hwndAC as ulong, re as long
seem the old Russian proverb "More than one way to skin a cat" applies here
|
|
|
Post by Brandon Parker on Mar 18, 2022 20:46:14 GMT -5
Here is a short example that might be worth looking at...
NoMainWin WindowWidth = 475 WindowHeight = 385
Global False : False = 0 Global True : True = 1
Global address 'Dimension this array to the number of 'windows you want to leave open Global numWindows : numWindows = 0 Dim leaveOpen(numWindows)
Button #Main.btnHideindows, "Hide Windows", btnHideWindows, UL, 355, 295, 100, 25 TextEditor #Main.txtEditor, 5, 5, 450, 285 Open "Enum Windows Example" For Window As #Main #Main "TrapClose quit"
'Set up the callback for EnumWndProc CallBack address, enumWndProc(ulong, ulong), long
'Set the array elements to the handle values 'for the windows you do not want to close For i = 0 To numWindows leaveOpen(i) = Hwnd(#Main) Next i Wait
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Sub quit handle$ Close #handle$ End End Sub '_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Sub btnHideWindows handle$ result = enumerateWindows() End Sub
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function enumerateWindows() CallDLL #user32, "EnumWindows", address As ulong, _ _NULL As long, _ enumerateWindows As long End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function enumWndProc(hWnd, lparam) enumWndProc = 1 labelBuffer$ = Trim$(GetWindowText$(hWnd)) If (labelBuffer$ <> "") Then If IsWindowVisible(hWnd) And Not(IsIconic(hWnd)) Then For i = 0 To numWindows If (hWnd = leaveOpen(i)) Then leaveOpen = True : Exit For Next i If (leaveOpen = False) Then result = ShowWindow(hWnd, _SW_MINIMIZE) #Main.txtEditor "!Contents? string$" labelBuffer$ = string$;chr$(13);"Hiding - ";labelBuffer$;", handle - ";hWnd #Main.txtEditor "!Insert labelBuffer$" End If End If End If End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function ShowWindow(hWnd, nCmdShow) CallDLL #user32, "ShowWindow", hWnd As ulong, _ nCmdShow As long, _ ShowWindow As long End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function GetWindowText$(hWnd) GetWindowText$ = Space$(_MAX_PATH) + chr$(0) nMaxCount = Len(GetWindowText$) CallDLL #user32, "GetWindowTextA", hWnd As ulong, _ GetWindowText$ As ptr, _ nMaxCount As long, _ result As long GetWindowText$ = Trim$(GetWindowText$) End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function IsWindowVisible(hWnd) CallDLL #user32, "IsWindowVisible", hWnd As ulong, _ IsWindowVisible As long End Function
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function IsIconic(hWnd) CallDLL #user32, "IsIconic", hWnd As ulong, _ IsIconic As long End Function
{:0)
Brandon Parker
|
|
|
Post by metro on Mar 18, 2022 21:15:37 GMT -5
Here is a short example that might be worth looking at... Dang, I wish I had 1/10 th the talent you lot have(won't list coz I'll miss someone). Thanks once again Brandon. I keep thinking at 67 I'll never master this but then Walt (and others) show it can be done!
I'll slot that into my program.
|
|
|
Post by Walt Decker on Mar 19, 2022 7:16:21 GMT -5
In Mr. Parker's code is the function IsWindowVisible(). I doubt that function is necessary or even desirable. According to the specs:
You are correct concerning the CloseWindow() function. I had one of those senior moments.
To restore a minimized window you can also use the User32.dll OpenIcon() function:
|
|
|
Post by metro on Mar 19, 2022 8:14:21 GMT -5
In Mr. Parker's code is the function IsWindowVisible(). I doubt that function is necessary or even desirable. According to the specs: Thanks Walt, one too many 12-year-old single malts (tonight) to be able to digest what you have posted. I will chase this up tomorrow. Once again thanks for the input
|
|
|
Post by Brandon Parker on Mar 19, 2022 9:13:37 GMT -5
In Mr. Parker's code is the function IsWindowVisible(). I doubt that function is necessary or even desirable. Well, one is free to add/remove/leave out any code that is posted, but I would probably leave the IsWindowVisible() function in unless you want your desktop to look like the image below (the black portions are from me blacking out my desktop for privacy). There are tons of top-level windows that Windows actually has open in the background (not visible) doing all kinds of things. The difference is 8 windows are minimized using the function vs. 156 windows (mostly background process things) being hidden without the function. And, all of the Default IME boxes show up at that point without using the function... And, you will most likely have to restart Windows to restore everything to normal if you run the code without the IsWindowVisible() function... {:0) Brandon Parker
|
|
|
Post by Walt Decker on Mar 19, 2022 9:43:07 GMT -5
Metro, in post #7 you have:
How did you acquire those values? What are the values actually returned by the EnumWindowsProc() callback function?
|
|
|
Post by metro on Mar 19, 2022 10:59:58 GMT -5
Metro, in post #7 you haveHow did you acquire those values? What are the values actually returned by the EnumWindowsProc() callback function? print hwnd;" ";labelBuffer$. This line in the code supplied, the result of which which I guess is from the WINE layer and something I do not understand. I have implemented Brandon's code within my program and it works (functions) flawlessly. Something I have come to accept with blind faith. Thanks again Walt
|
|
|
Post by Walt Decker on Mar 19, 2022 11:46:14 GMT -5
Mr. Parker, I learn something new every day. Thank you.
|
|