Jump to content

Lock Windows Device


antmar904
 Share

Recommended Posts

thank you all for your quick response.

one more question.

i cant get a yes or no button to work when the user presses the shutdown button.

here is my code, i know that it can use some fine tuning but hey that's why i am here.

thank you all again.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Dim $Hibernate, $Lock, $Shutdown, $answer
While 1
WEnd
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $btnHibernate
   Shutdown (64)
  Case $btnLock
   DllCall("user32.dll", "int", "LockWorkStation")
  Case $btnShutdown
   $answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
   Switch $answer
    Case 2
    Else
     Case 1
     Shutdown (9)
   EndSwitch
EndSwitch
WEnd
Link to comment
Share on other sites

There are many errors with that code. First, you have an else in the middle of the second Switch statement that doesn't apply to anything so this script won't even run as is.

Second, you're not reading the buttons at all because you have the code for it after the While loop, so it's never run.

Third, look at the help file for what MsgBox returns when various buttons are pressed because your switch statements for the return from that aren't right.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

after changing some things i still cant get it to work, now the gui just opens and then closes.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Dim $Hibernate, $Lock, $Shutdown, $answer
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $btnHibernate
Shutdown (64)
Case $btnLock
DllCall("user32.dll", "int", "LockWorkStation")
Case $btnShutdown
$answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
Switch $answer
Case 6
     Shutdown(9)
Case 7
     MsgBox(0, "Message", "You pressed no")
EndSwitch
EndSwitch
Edited by antmar904
Link to comment
Share on other sites

You removed the While loop, there's nothing to keep the script running. Wrap the section that checks the buttons inside a While loop, look at the help file for GUICreate and you'll see what I am referring to.

Also, try to avoid using the Dim keyword, you should use Global/Local instead.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

i think i got it working.

how does this look?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Hibernate, $Lock, $Shutdown, $answer
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $btnHibernate
   Shutdown (64)
  Case $btnLock
   DllCall("user32.dll", "int", "LockWorkStation")
  Case $btnShutdown
   $answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
   Switch $answer
    Case 6
     Shutdown(9)
    Case 7
   EndSwitch
EndSwitch
WEnd
Link to comment
Share on other sites

Looks ok now, although the Case 7 isn't really needed unless you're planning on using it for something later. Other than that it works as it should.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

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