Jump to content

Recommended Posts

Posted

Hey, I'm new here and trying to learn some Autoit to create a program to make a user click through a few GUIs without just closing them out I want them to have to actually click a button on the GUI to proceed

So far I've succeeded in preventing the use of the "X" button and trapping the mouse in the GUI (after the first X click) to make them interact with the window and preventing other methods of closing such as right clicking the title bar or double clicking the icon but I can't seem to figure out a way to prevent using alt+f4 to just kill it

I found one almost solution of setting the hotkey with an empty function but it doesn't work when pressing alt+f4 more than once (and sometimes not even for the first press)

I also tried seeing if maybe I could sort of overwrite it using _WinAPI_RegisterHotKey but it doesn't work when adding the ALT modifier to an f4 key press

Any help or Ideas is appreciated

Posted

 

I haven't tried it yet but you can try this suggestion from AI:

You can disable Alt + F4 in an AutoIt GUI using two primary methods.

The most recommended approach is using GUISetOnEvent because it handles all standard window-closing actions.


 

## Method 1: Using GUISetOnEvent (Recommended)

 

This is the best practice because it disables not only Alt + F4 but also the "X" button on the title bar and other standard window-closing methods. This gives you complete control over when the window is allowed to close.

How it works: You register a function to handle the window's close event ($GUI_EVENT_CLOSE). When the user tries to close the GUI, your function is called instead of the default action (closing the window). If your function does nothing or simply Returns, the window will not close.

Example:

#include <GUIConstantsEx.au3>

; Create a simple GUI window
$hGUI = GUICreate("Window that can't be closed by Alt+F4", 400, 300)

; Set a function to handle the close event.
; When the user presses Alt+F4 or the X button, the "onClose" function will be called.
GUISetOnEvent($GUI_EVENT_CLOSE, "onClose")

GUISetState(@SW_SHOW, $hGUI)

; Main loop to keep the program running
While 1
    Sleep(100)
WEnd

; This function will be called on a close event
Func onClose()
    ; You can put a message here to inform the user
    MsgBox(48, "Notice", "Alt + F4 has been disabled!")

    ; Since we don't call Exit() or GUIDelete(),
    ; the window will not close. Just returning is enough.
    Return
EndFunc

 

## Method 2: Using HotKeySet

 

This method specifically blocks only the Alt + F4 key combination. The user can still close the window by clicking the "X" button. This is useful if you only want to disable that specific shortcut.

How it works: You register a hotkey for the !{F4} combination (the ! represents the Alt key). When this key combination is pressed, AutoIt calls the function you specified instead of performing the default operating system action.

Example:

#include <GUIConstantsEx.au3>

; Set a hotkey for Alt+F4, which will call the "doNothing" function when pressed
HotKeySet("!{F4}", "doNothing")

; Create a GUI window
$hGUI = GUICreate("Only Alt+F4 is disabled", 400, 300)
$hLabel = GUICtrlCreateLabel("You can still close this window with the X button.", 30, 130, 340, 20)
GUISetState(@SW_SHOW, $hGUI)

; Main loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE ; This event still works when the user presses the X button
            ExitLoop
    EndSwitch
WEnd

; An empty function to "catch" the Alt+F4 keypress and do nothing
Func doNothing()
    ; No code here, so nothing happens when Alt+F4 is pressed
EndFunc

 

## Summary

 

Method Pros Cons When to Use
GUISetOnEvent Comprehensive: Blocks all standard ways to close a window (Alt+F4, X button, etc.). Cannot distinguish if the user used Alt+F4 or the X button. When you want to prevent the user from closing the window through any normal means. This is the recommended choice.
HotKeySet Specific: Only blocks the Alt + F4 key combination. The user can still close the window in other ways. When you only want to disable the Alt+F4 shortcut but still allow closing via the X button.

Regards,
 

Posted (edited)

Dummy approach

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Misc.au3>

Opt("GUICloseOnESC", 0) ;1=ESC  closes, 0=ESC won't close

Example()

Func Example()
    Local $hGUI = GUICreate("Example", 300, 200, -1, -1, -1, $WS_EX_TOOLWINDOW)
    Local $idDummy = GUICtrlCreateDummy()
    Local $idExit = GUICtrlCreateButton("Exit", 160, 150, 85, 25)
    GUISetState(@SW_SHOW, $hGUI)

    Local $bGreen = False

    While 1
        $aCoords = WinGetPos($hGUI)
        If Not @error Then  _MouseTrap($aCoords[0], $aCoords[1], $aCoords[0] + $aCoords[2], $aCoords[1] + $aCoords[3])

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUICtrlSendToDummy($idDummy)
            Case $idExit
                $bGreen = True
                GUICtrlSendToDummy($idDummy)
            Case $idDummy
                If $bGreen Then ExitLoop
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

 

Edited by ioa747
add #include <Misc.au3>

I know that I know nothing

Posted
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <Misc.au3>

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 400)
    Disable_SC_CLOSE($hGUI) ; if you need this too, add it
    Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idOK
                If _IsPressed("12") And _IsPressed("73") Then ContinueLoop ; Alt + F4
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func Disable_SC_CLOSE($hGUI) ; https://www.autoitscript.com/forum/topic/169277-disable-window-menu/
    Local Const $MF_BYCOMMAND = 0x0, $SC_CLOSE = 0xF060
    Local $hMenu = DllCall("user32.dll", "hwnd", "GetSystemMenu", "hwnd", $hGUI, "int", 0)
    DllCall("user32.dll", "hwnd", "DeleteMenu", "hwnd", $hMenu[0], "int", $SC_CLOSE, "int", $MF_BYCOMMAND)
EndFunc   ;==>Disable_SC_CLOSE

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...