Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Disable or remove close, minimize, maximize buttons on any window in runtime !

$
0
0
However, there is a good forum topic on this, it took me several hours to figure out how to do it on other windows in runtime, so I tought it might help others.

So you want to remove buttons like this:
Window_close_only.png
[ autoit ]      
#include <WinAPI.au3> #include <Constants.au3> #include <WindowsConstants.au3> $h = WinGetHandle("Untitled - Note") $iOldStyle = _WinAPI_GetWindowLong($h, $GWL_STYLE) ConsoleWrite("+ old style: " &amp;amp; $iOldStyle &amp; @CR) $iNewStyle = BitXOr($iOldStyle, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX) _WinAPI_SetWindowLong($h, $GWL_STYLE, $iNewStyle) _WinAPI_ShowWindow($h, @SW_SHOW)

or remove even the close button:
Window_no_buttons.PNG
instead of
[ autoit ]      
$iNewStyle = BitXOr($iOldStyle, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX)
use
[ autoit ]      
$iNewStyle = BitXOr($iOldStyle, $WS_SYSMENU)

The cool part about these is that you can still close the window with ALT+F4 or minimize it clicking the tray icon.
The trickiest part caused me the biggest headache that if you just set the window style with a SetWindowLong() API call, but don't run
[ autoit ]      
the following can happen. To show up properly after the style change, I tried redrawing the window, redrawing everything, sending WM_PAINT message to the Window, calling SetWindowLong() with different handles and options, nothing worked, until I found this comment. So it is very important to run it after setting the style. You can find more about Window styles on MSDN or Autoit help

Now, what if you want only disable the buttons, but don't want to remove them ?
Like this:
window_disabled_buttons.PNG
(The close button is "greyed" out, the other two are still visible. but if you click them, nothing happens.)

The following forum topic solution is very good: Disable Close button.

You can disable any of you want with these functions:
[ autoit ]         
; Constants from http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx Const $SC_CLOSE = 0xF060 Const $SC_MOVE = 0xF010 Const $SC_MAXIMIZE = 0xF030 Const $SC_MINIMIZE = 0xF020 Const $SC_SIZE = 0xF000 Const $SC_RESTORE = 0xF120 Func GetMenuHandle($hWindow, $bRevert = False) If $bRevert = False Then $iRevert = 0 Else $iRevert = 1 EndIf $aSysMenu = DllCall("User32.dll", "hwnd", "GetSystemMenu", "hwnd", $hWindow, "int", $iRevert) ConsoleWrite("+ Menu Handle: " &amp;amp; $aSysMenu[0] &amp; @CRLF) Return $aSysMenu[0] EndFunc Func DisableButton($hWindow, $iButton) $hSysMenu = GetMenuHandle($hWindow) DllCall("User32.dll", "int", "RemoveMenu", "hwnd", $hSysMenu, "int", $iButton , "int", 0) DllCall("User32.dll", "int", "DrawMenuBar", "hwnd", $hWindow) EndFunc Func EnableButton($hWindow, $iButton) $hSysMenu = GetMenuHandle($hWindow, True) DllCall("User32.dll", "int", "RemoveMenu", "hwnd", $hSysMenu, "int", $iButton , "int", 0) DllCall("User32.dll", "int", "DrawMenuBar", "hwnd", $hWindow) EndFunc

by calling the functions like this:
[ autoit ]      
DisableButton($handle, $SC_CLOSE) DisableButton($handle, $SC_MAXIMIZE) DisableButton($handle, $SC_RESTORE) DisableButton($handle, $SC_MINIMIZE)
Same with EnableButton().

If you disable $SC_SIZE, the window will not be resizeable with the mouse.
If you disable SC_MOVE, you also cannot move it.

Note: everything was tested on Windows 7 with Aero theme. On other system you might not have the same problem (window becoming a ghost after style update). If you test it on other system, please make a feedback about it !

Hope this helps somebody !

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>