|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Turn Off Monitor
Hi! I'm very new to VB, and I would appreciate it very much if someone could help me a little here. I am trying to make a simple app that will turn off the computer's monitor when a button is pressed. I was given this code, but I'm not exactly sure what to do with it:
Code:
Option Explicit Private Declare Function SendMessage Lib _ "user32" Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long Const SC_MONITORPOWER = &HF170& Public Const MONITOR_ON = -1& Public Const MONITOR_OFF = 2& Const WM_SYSCOMMAND = &H112 'Turn Monitor on: 'SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON ' 'Turn Monitor off: 'SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF Thanks in advance. Zach Last edited by NASCAR6Frd : July 25th, 2003 at 12:41 AM. |
|
#2
|
|||
|
|||
|
Can anybody help me?
|
|
#3
|
|||
|
|||
|
Hi,
as I can understand it send specific set of command to the User32 library.... so to know what those commands are you have to get documentation for user32.dll (have no Idea how you can get it ... , but you can brows the dll, to do this go to View => othere window => object browser=> then your dll |
|
#4
|
|||
|
|||
|
This is a rather simple API call that WILL turn off (or on) your computer monitor
1) To use this API call in Visual Basic, or at least try it out, simply create a blank form. Then in the declarations section of the form (under the Option Explicit) simply copy and paste the following code: Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long Const SC_MONITORPOWER = &HF170& Const MONITOR_ON = -1& Const MONITOR_OFF = 2& Const WM_SYSCOMMAND = &H112 Note: You can place this code into a module but then be sure to make the two Constants MONITOR_ON and MONITOR_OFF as Public Contstants: Public Const MONITOR_ON = -1& Public Const MONITOR_OFF = 2& End Note: 2) Now, place a Timer control into the form and name it: Timer1. Make sure the 'Interval' property for this control is set to 0 (zero). Next, copy and paste the following code into the 'Timer1_Timer' event: SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON (all one line). This API function tells the monitor to turn ON. We have simply placed it into a timer for the sole purpose of turning the monitor on again after a specific time period (which will be 10 seconds). We have no real control once the monitor turns off unless we use a key press or a timing mechanism of some kind to do the job. How and when you want the monitor to turn back on again is entirely up to you but for this example, we are going to use a Timer control. 3) Now you must place a Comand button into your new form. Do so now, then in the caption property of this button, enter "Monitor OFF" (no quotes). For now, make sure the name of your button is 'Command1'. Now...place the following code into the 'Command1_Click' event of your button: '(*All one Line*) This API function call will turn the monitor OFF. SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF ' Here, we set the Timer1 control Interval to 10000. ' By doing so, we are telling the timer to start and ' after 10000 miliseconds (10 seconds), fire the ' Timer1_Timer event and run the code in that event. Me.Timer1.Interval = 10000 'End of Code When you start your example program, you will see a blank form with just on command button on it that says "Monitor OFF". Once you click on this button, the monitor will shut down (if your monitor supports suspend mode). Then, after a 10 second time period, the monitor will turn back on again. Increasing the Timer1.Interval will increase the length of time the monitor stays off (keep in mind: 1 second = 1000 interval ticks). I hope this helps. |
|
#5
|
|||
|
|||
|
So detailed explain! Thank CyberLynx
! |
|
#6
|
|||
|
|||
|
Thanks so much! Works wonderfully.
|
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Turn Off Monitor |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|