Jump to content

Msgbox with cancel button only


Recommended Posts

Hi guys,

I'm trying to create a shutdown application and it will automatically shutdown the pc after working hours. But if the user is still working overtime, they will be able to cancel it. Is it possible to have only the cancel button on the message box? Please see below script for reference.

$x = MsgBox(17+256+4096,"Shutdown","In accordance with company power saving policies, your computer will be shutdown in 1 minute." & @CRLF & @CRLF & "If you wish to abort this process, click the Cancel button below.",60)

If $x = -1 Then
    ; Code 13 = 1 (shutdown) + 4 (force) + 8 (power down)
    Shutdown(13)
Else
    MsgBox(0,"Shutdown Aborted","Please remember to power off your computer when you are done.")
EndIf

 

Link to post
Share on other sites

@igana

Since a "Cancel only" MsgBox seems not be available, you could use the style $MB_OKCANCEL, and maybe a timeout.

If the user presses "OK" or the Timeout has been reached, than shutdown the PC; else, if the user presses "Cancel", then don't shutdown the PC and display the other MsgBox :)

Alternatively, you have to create your own MsgBox, or (I don't know if it would be the case), an amazing UDF by Melba23 :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to post
Share on other sites
1 hour ago, FrancescoDiMuro said:

@igana

Since a "Cancel only" MsgBox seems not be available, you could use the style $MB_OKCANCEL, and maybe a timeout.

If the user presses "OK" or the Timeout has been reached, than shutdown the PC; else, if the user presses "Cancel", then don't shutdown the PC and display the other MsgBox :)

Alternatively, you have to create your own MsgBox, or (I don't know if it would be the case), an amazing UDF by Melba23 :)

Thanks @FrancescoDiMuro! I was able to create it. However, I tested the close button on the window and my pc shuts down lol. Do you know how to fix this? Sorry I'm not really good with programming :)

Link to post
Share on other sites

@igana

Could you please post the code so we can take a look :)

EDIT: just use the reply to this topic instead of quoting the whole reply, so the thread structure remains more fluid and less long ;)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to post
Share on other sites

Hahaha my apologies. Noted on that Mr.

_ExtMsgBoxSet(1, 4, -1, -1, 11)

$sMsg = "In accordance with company power saving policies, your computer will be shutdown in 1 minute." & @CRLF & @CRLF & "If you wish to abort this process, click the Cancel button below."

$x = _ExtMsgBox($EMB_ICONSTOP, "Cancel", "Shutdown", $sMsg,60)

If $x = 1 Then
   MsgBox(0,"Shutdown Aborted","Please remember to power off your computer when you are done.")
Else
    ; Code 13 = 1 (shutdown) + 4 (force) + 8 (power down)
    Shutdown(13)
EndIf

 

Link to post
Share on other sites
  • Moderators

igana.

With that code only pressing the "Cancel" button will prevent shutdown - closing the ExtMsgBox with [X] or allowing the timeout will run the shutdown code.

If you want ONLY the timeout to shutdown then you need this:

If $x = 9 Then
    MsgBox($MB_SYSTEMMODAL, 0, "Shutdown", "Shutting down")
    ; shutdown code
Else
    MsgBox(0, "Shutdown Aborted", "Please remember to power off your computer when you are done.")
EndIf

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites

But if you still want to have a single button "Cancel", here how you could do it with MsgBox :

#include <WindowsConstants.au3>
#include <Constants.au3>
#include <SendMessage.au3>
#include <WinAPIError.au3>

$sCommand = " /AutoIt3ExecuteLine ""MsgBox(0, 'Shutdown'," & _
  "'In accordance with company power saving policies, your computer will be shutdown in 1 minute.' & @CRLF & @CRLF & " & _
  "'If you wish to abort this process, click the Cancel button below.', 60)"""
ConsoleWrite ($sCommand & @CRLF)
Run(@AutoItExe & $sCommand)
$hWnd = WinWait("Shutdown")
$hCtrl = ControlGetHandle($hWnd, "", "Button1")
ConsoleWrite ($hWnd & "/" & $hCtrl & @CRLF)

$tString = DllStructCreate("char text[7]")
$tString.text = "Cancel"
_SendMessageA($hCtrl, $WM_SETTEXT, 0, DllStructGetPtr($tString))
ConsoleWrite (_WinAPI_GetLastError() & @CRLF)

 

Link to post
Share on other sites

An other way...

#include <Constants.au3>
#include <WinAPI.au3>

Local $hProcMsgBox = DllCallbackRegister("CbtHookProcMsgBox", "int", "int;int;int")
Local $TIDMsgBox = _WinAPI_GetCurrentThreadId()
Local $hHookMsgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProcMsgBox), 0, $TIDMsgBox)

Local $iRet = MsgBox(0, "test", "test")

_WinAPI_UnhookWindowsHookEx($hHookMsgBox)
DllCallbackFree($hProcMsgBox)


Func CbtHookProcMsgBox($nCode, $wParam, $lParam)
    Local $RET = 0
    If $nCode < 0 Then
        $RET = _WinAPI_CallNextHookEx($hHookMsgBox, $nCode, $wParam, $lParam)
        Return $RET
    EndIf
    Switch $nCode
        Case 5 ;5=HCBT_ACTIVATE
        DllCall('user32.dll', "int", "SetDlgItemText", "hwnd", $wParam, "int", 1, "str", "Cancel")
    EndSwitch
    Return
EndFunc   ;==>CbtHookProcMsgBox

 

Link to post
Share on other sites
Link to post
Share on other sites

@Nine
Usually I use this in a custom _Msgbox function to set text/position of the Msgbox

Func _MsgBox($flag, $title, $text)
    Local $hProcMsgBox = DllCallbackRegister("CbtHookProcMsgBox", "int", "int;int;int")
    Local $TIDMsgBox = _WinAPI_GetCurrentThreadId()
    Local $hHookMsgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProcMsgBox), 0, $TIDMsgBox)
    Local $iRet = MsgBox($flag, $title, $text)
    _WinAPI_UnhookWindowsHookEx($hHookMsgBox)
    DllCallbackFree($hProcMsgBox)
    Return $iRet 
EndFunc

Maybe I'm wrong but in this case calling _WinAPI_CallNextHookEx at the end of the callback function seems useless

Link to post
Share on other sites

Hmm, you got a good point there.  On the other hand, calling it doesn't hurt for sure.  But then calling it if code < 0 should also be considered useless, no ?

Link to post
Share on other sites

Maybe. I chose to keep on following lazily MS instructions  :idiot:
If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx

Link to post
Share on other sites

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...