igana Posted January 31, 2021 Share Posted January 31, 2021 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 comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 31, 2021 Share Posted January 31, 2021 @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 igana 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
igana Posted January 31, 2021 Author Share Posted January 31, 2021 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 comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 31, 2021 Share Posted January 31, 2021 (edited) @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 January 31, 2021 by FrancescoDiMuro igana 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
igana Posted January 31, 2021 Author Share Posted January 31, 2021 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 comment Share on other sites More sharing options...
Moderators Melba23 Posted January 31, 2021 Moderators Share Posted January 31, 2021 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 FrancescoDiMuro and igana 2 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Nine Posted January 31, 2021 Share Posted January 31, 2021 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) FrancescoDiMuro and igana 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted January 31, 2021 Share Posted January 31, 2021 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 comment Share on other sites More sharing options...
Nine Posted January 31, 2021 Share Posted January 31, 2021 @mikell Nice solution. But shouldn't you call _WinAPI_CallNextHookEx at the end of the callback function ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted February 1, 2021 Share Posted February 1, 2021 @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 comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 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 ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted February 1, 2021 Share Posted February 1, 2021 Maybe. I chose to keep on following lazily MS instructions 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 comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now