Jump to content

Monitor Control


James
 Share

Recommended Posts

Nothing special I made a tray program for my friend so his monitor will turn off after a set time then turn back on after another time.

Here is the Monitor control part:

Opt("WinTitleMatchMode", 4)

Monitor("off")

Func Monitor($io_control = "on")
    Local $WM_SYSCommand = 274
    Local $SC_MonitorPower = 61808
    Local $HWND = WinGetHandle("classname=Progman")
    Switch StringUpper($io_control)
        Case "OFF"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand, "int", $SC_MonitorPower, "int", 2)
        Case "ON"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand, "int", $SC_MonitorPower, "int", -1)
        Case Else
            MsgBox(32, @ScriptName, "Command usage: on/off")
    EndSwitch
EndFunc

The commands were from the feature request, I just made it into a function. :)

Link to comment
Share on other sites

  • 1 year later...

Hey, great function James. I hope you don't mind, but I added the "powersave" functionality, along with turning on the screensaver.

;===============================================================================
; Function Name:   _Monitor()
; Description:   Changes the state of the monitor or turns on screensaver.
; Syntax:         _Monitor ( [$sIo_control] )
;
; Parameter(s): $sIo_control = The command to send (default = "on").
;                 "on" turns on monitor.
;                 "off" turns off monitor.
;                 "powersave" puts monitor(s) in powersave mode.
;                  "screensaver" turns on screensaver, if one has been selected in Windows.
;
; Requirement(s):  External:   = user32.dll (it's already in system32).
;                 Internal:   = None.
;
; Author(s):       Original function by JamesBrooks (on/off). Modified by TrystianSky (added powersave/screensaver).
;
; Example(s):
;   _Monitor("powersave")
;===============================================================================

Func _Monitor($sIo_control = "on")
    Local $iWM_SYSCommand = 274
    Local $iSC_MonitorPower = 61808
    Local $iSC_ScreenSave = 61760
    Local $hWnd = WinGetHandle("[CLASS:Progman]")
    Switch StringUpper($sIo_control)
        Case "OFF"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 2)
        Case "ON"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", -1)
        Case "POWERSAVE"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 1)
        Case "SCREENSAVER"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_ScreenSave, "int", 0)
        Case Else
            MsgBox(64, @ScriptName, "Command usage: on/off/powersave/screensaver")
    EndSwitch

EndFunc;==>_Monitor

Edit: Changed WinGetHandle call. Thanks Authenticity.

Edited by TrystianSky
Link to comment
Share on other sites

Hey, great function James. I hope you don't mind, but I added the "powersave" functionality, along with turning on the screensaver.

;===============================================================================
; Function Name:   _Monitor()
; Description:   Changes the state of the monitor or turns on screensaver.
; Syntax:         _Monitor ( [$sIo_control] )
;
; Parameter(s): $sIo_control = The command to send (default = "on").
;                 "on" turns on monitor.
;                 "off" turns off monitor.
;                 "powersave" puts monitor(s) in powersave mode.
;                  "screensaver" turns on screensaver, if one has been selected in Windows.
;
; Requirement(s):  External:   = user32.dll (it's already in system32).
;                 Internal:   = None.
;
; Author(s):       Original function by JamesBrooks (on/off). Modified by TrystianSky (added powersave/screensaver).
;
; Example(s):
;   _Monitor("powersave")
;===============================================================================

Func _Monitor($sIo_control = "on")
    Local $iWM_SYSCommand = 274
    Local $iSC_MonitorPower = 61808
    Local $iSC_ScreenSave = 61760
    Local $hWnd = WinGetHandle("classname=Progman")
    Local $iRet = 1
    Switch StringUpper($sIo_control)
        Case "OFF"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 2)
        Case "ON"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", -1)
        Case "POWERSAVE"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 1)
        Case "SCREENSAVER"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_ScreenSave, "int", 0)
        Case Else
            MsgBox(64, @ScriptName, "Command usage: on/off/powersave/screensaver")
    EndSwitch

EndFunc ;==>_Monitor
nothing happens on my monitor with any of the parameters.

I'm using WinXP SP3 and AI 3

Link to comment
Share on other sites

Thanks for the feedback dmob. I forgot to change the winhandle call.

Either insert in the header Opt("WinTitleMatchMode", 4) or change WinGetHandle("classname=Progman") to WinGetHandle("[CLASS:Progman]")

Thanks Authenticity. Decided to use WinGetHandle("[CLASS:Progman]") instead of Opt("WinTitleMatchMode", 4) to make the function play well with other functions. =)
Link to comment
Share on other sites

guys, can we add wake on hot key support?

i mean can this work like a pc on monitor off mode, a mouse movement or keyboard sroke will wake up the monitor?

Of course.

HotKeySet("!{F7}", "On")
HotKeySet("!{F7}", "Off")

While 1
    Sleep(500)
Wend

Func Off()
    _Monitor("off")
EndFunc

Func On()
    _Monitor()
EndFunc

Func _Monitor($sIo_control = "on")
    Local $iWM_SYSCommand = 274
    Local $iSC_MonitorPower = 61808
    Local $iSC_ScreenSave = 61760
    Local $hWnd = WinGetHandle("[CLASS:Progman]")
    Switch StringUpper($sIo_control)
        Case "OFF"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 2)
        Case "ON"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", -1)
        Case "POWERSAVE"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_MonitorPower, "int", 1)
        Case "SCREENSAVER"
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "int", $iWM_SYSCommand, "int", $iSC_ScreenSave, "int", 0)
        Case Else
            MsgBox(64, @ScriptName, "Command usage: on/off/powersave/screensaver")
    EndSwitch

EndFunc;==>_Monitor
Link to comment
Share on other sites

  • 2 weeks later...

Hello,

if I run the script part from Post#1

it switches the monitor "Off".

But if I move the mouse, the monitor switches "On".

I want, that the monitor remains in "Off"- status and switches "On"

if I push a key or a mouse button.

If I run the script part from Post#15 and push "Alt"+"F7"

the monitor switches "Off" for a second, then switches "On".

It should show the same behavior as I wrote before.

I don't know, how to realize this in a script.

I want this for my notebook (laptop),

which has no switch to turn off the display.

Can anybody help me please?

Perhaps the author?

Thanks :)

Link to comment
Share on other sites

  • 4 weeks later...

Hello. anyone can add a handle or something to select which monitor to turn off (primary secondary)

What I should look to change it?

I want to make a camera controled powersave mode. I have made the camera/motion detect issue, but I don't know anything about the monitor :D

Link to comment
Share on other sites

Hello. anyone can add a handle or something to select which monitor to turn off (primary secondary)

What I should look to change it?

I want to make a camera controled powersave mode. I have made the camera/motion detect issue, but I don't know anything about the monitor :D

Hmm, I never thought about multi monitors. I'll take a look around.
Link to comment
Share on other sites

  • 3 years later...

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