Monitor Off

Just starting out? Need help? Post your questions and find answers here.
SharkyEXE
User
User
Posts: 35
Joined: Sat Feb 26, 2022 6:33 pm

Monitor Off

Post by SharkyEXE »

Hello

On Microsoft Windows 7 Professional Service Pack 1 code work perfectly - after run code, monitor is off, if i click any button on keyboard - monitor is on, in tray i see icon

Code: Select all

#PHYSICAL_MONITOR_DESCRIPTION_SIZE = 128

Structure PHYSICAL_MONITOR
  hPhysicalMonitor.i
  szPhysicalMonitorDescription.u[#PHYSICAL_MONITOR_DESCRIPTION_SIZE]
EndStructure

Prototype.l GetNumberOfPhysicalMonitorsFromHMONITOR_(hMonitor.i, *pdwNumberOfPhysicalMonitors.Long)
Prototype.l GetPhysicalMonitorsFromHMONITOR_(hMonitor.i, dwPhysicalMonitorArraySize.l, *pPhysicalMonitorArray.PHYSICAL_MONITOR)
Prototype.l DestroyPhysicalMonitors_(dwPhysicalMonitorArraySize.l, *pPhysicalMonitorArray.PHYSICAL_MONITOR)
Prototype.l SetVCPFeature_( hMonitor.i, bVCPCode.a, dwNewValue.i)
  

Global GetNumberOfPhysicalMonitorsFromHMONITOR_.GetNumberOfPhysicalMonitorsFromHMONITOR_
Global GetPhysicalMonitorsFromHMONITOR_.GetPhysicalMonitorsFromHMONITOR_
Global DestroyPhysicalMonitors_.DestroyPhysicalMonitors_
Global SetVCPFeature_.SetVCPFeature_

Global hMonitor.i
Global PhysicalMonitorArrayIndex.l
Global PhysicalMonitorArraySize.l
Global Dim PhysicalMonitorArray.PHYSICAL_MONITOR(0)
Global MonitorCapabilities.l
OpenLibrary(0, "Dxva2.dll")
  GetNumberOfPhysicalMonitorsFromHMONITOR_ = GetFunction(0, "GetNumberOfPhysicalMonitorsFromHMONITOR")
  GetPhysicalMonitorsFromHMONITOR_ = GetFunction(0, "GetPhysicalMonitorsFromHMONITOR")
  DestroyPhysicalMonitors_ = GetFunction(0, "DestroyPhysicalMonitors")
  SetVCPFeature_ =GetFunction(0, "SetVCPFeature")
;  hMonitor = MonitorFromPoint_(0, #MONITOR_DEFAULTTOPRIMARY)
;  If hMonitor
;       If GetNumberOfPhysicalMonitorsFromHMONITOR_(hMonitor, @PhysicalMonitorArraySize) And PhysicalMonitorArraySize > 0
;         ReDim PhysicalMonitorArray(PhysicalMonitorArraySize - 1)
;          If GetPhysicalMonitorsFromHMONITOR_(hMonitor, PhysicalMonitorArraySize, @PhysicalMonitorArray())
;            For PhysicalMonitorArrayIndex = 0 To PhysicalMonitorArraySize - 1  
;              ;SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$D6,4 )
;              SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$E1,0 )
;            Next
;            DestroyPhysicalMonitors_(PhysicalMonitorArraySize, @PhysicalMonitorArray())
;          EndIf
;        EndIf
;      EndIf
 
Global TimeOFF, Messaga.s,EscON
TimeOFF =60
MessagaON = 0
EscON = 0
ParameterCount.i = CountProgramParameters()                                                         
If ParameterCount.i <>  0                                                                           
  If Val(ProgramParameter(0)) > 0: TimeOFF =  Val(ProgramParameter(0)): EndIf
  If Val(ProgramParameter(1)) > 0: MessagaON = 1: EndIf  
  If Val(ProgramParameter(2)) > 0: EscON = 1: EndIf 
EndIf                                                                                           
Procedure MonicOff()
   hMonitor = MonitorFromPoint_(0, #MONITOR_DEFAULTTOPRIMARY)
   If hMonitor
      If GetNumberOfPhysicalMonitorsFromHMONITOR_(hMonitor, @PhysicalMonitorArraySize) And PhysicalMonitorArraySize > 0
        ReDim PhysicalMonitorArray(PhysicalMonitorArraySize - 1)
         If GetPhysicalMonitorsFromHMONITOR_(hMonitor, PhysicalMonitorArraySize, @PhysicalMonitorArray())
           For PhysicalMonitorArrayIndex = 0 To PhysicalMonitorArraySize - 1  
             ;SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$D6,4 )
             ; sams SetVCPFeature,[esi+PHYSICAL_MONITOR.hPhysicalMonitor],0E1h,0

             SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$E1,0 )
           Next
           DestroyPhysicalMonitors_(PhysicalMonitorArraySize, @PhysicalMonitorArray())
         EndIf
       EndIf
     EndIf
   ;SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
   GetCursorPos_(pt.POINT)
      Mx = pt\x
      My = pt\y
      offM = 0
 Repeat
    If EscON = 1     
     If GetAsyncKeyState_(27)=-32767
       offM = 1; esc = 27
     Else
       ;SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
     EndIf
    Else 
   For i = 5 To 255
     If GetAsyncKeyState_(i)=-32767
       offM = 1
     Else
      ; SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
     EndIf 
   Next
 EndIf
 
;     GetCursorPos_(pt.POINT)
;     If Mx <> pt\x Or My = pt\y
;       ;SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
;       Mx = pt\x 
;       My = pt\y
;     EndIf

    Delay(1)  
  Until offM = 1
  hMonitor = MonitorFromPoint_(0, #MONITOR_DEFAULTTOPRIMARY)
   If hMonitor
      If GetNumberOfPhysicalMonitorsFromHMONITOR_(hMonitor, @PhysicalMonitorArraySize) And PhysicalMonitorArraySize > 0
        ReDim PhysicalMonitorArray(PhysicalMonitorArraySize - 1)
         If GetPhysicalMonitorsFromHMONITOR_(hMonitor, PhysicalMonitorArraySize, @PhysicalMonitorArray())
           For PhysicalMonitorArrayIndex = 0 To PhysicalMonitorArraySize - 1  
             ;SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$D6,1 )
             SetVCPFeature_(PhysicalMonitorArray(PhysicalMonitorArrayIndex)\hPhysicalMonitor,$E1,1 )
           Next
           DestroyPhysicalMonitors_(PhysicalMonitorArraySize, @PhysicalMonitorArray())
         EndIf
       EndIf
     EndIf
  
  
  ;SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, - 1)
EndProcedure
Procedure GetIdleTime()
  Protected lipi.LASTINPUTINFO
  lipi\cbSize = SizeOf(LASTINPUTINFO)
  GetLastInputInfo_(@lipi)
  sek.f = (GetTickCount_() - lipi\dwTime) / 1000
  If sek>=TimeOFF
    MonicOff()
  EndIf
  SetGadgetText(0, "Монитор будет потушен автоматически после " + StrD((TimeOFF - sek), 1) + " сек. простоя/бездействия! ")
 
  
EndProcedure

MonicOff()
Wh=50
Ww=250
OpenWindow(0, 0, 0, Ww, Wh, "", #PB_Window_BorderLess|#PB_Window_ScreenCentered)
SetClassLongPtr_(WindowID(0), #GCL_STYLE, $00020000)
TextGadget(0, 0, 0, Ww-0, Wh-0, "", #PB_Text_Center)
AddWindowTimer(0, 1, 200)
HideWindow(0, #True)
; Добавление значка в область уведомлений (SysTray) 
  AddSysTrayIcon(0, WindowID(0), CatchImage(0, ?Logo))
GetIdleTime()
 If CreatePopupMenu(0)      ; Начинается создание всплывающего Меню...
   MenuItem(1, "Выход")
   MenuBar()
   MenuItem(2, "Погасить экран") 
EndIf
 StickyWindow(0,1)
 If MessagaON>0
   HideWindow(0, #False) ;  Мессага для уведомления
 EndIf
 
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow : Break
    Case #PB_Event_Timer  
      Debug Timer
      GetIdleTime()
    Case #PB_Event_SysTray
      If EventType()=#PB_EventType_RightClick 
         DisplayPopupMenu(0, WindowID(0))   
       EndIf 
    Case #PB_Event_Menu        
          Select EventMenu()    
            Case 1 :End
            Case 2 
              MonicOff()  
          EndSelect
  EndSelect
ForEver
CloseLibrary(0)
DataSection
    Logo: 
      IncludeBinary "offmon.ico"
  EndDataSection
But on Microsoft Windows 2004 (20H1) code not work : after run code, monitor is off, if i click any button on keyboard - monitor do not on, to turn on the monitor, I have to press the physical power button on the monitor, then the monitor will turn on, in tray i do not see icon

Please, help me, why code do not properly work on Microsoft Windows 2004 (20H1)?

Thank You!
User avatar
mk-soft
Always Here
Always Here
Posts: 5338
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Monitor Off

Post by mk-soft »

Code: Select all

;-TOP
; Hilfsfunktion Montitor ausschalen (mk-soft)
; Autor: Michael Kastner
; Version: v1.0
; Date: 15.04.2013

If OpenWindow(0,0,0,0,0, "MonitorOff", #PB_Window_NoGadgets | #PB_Window_Invisible)

  SendMessage_(WindowID(0), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Monitor Off

Post by BarryG »

You don't even need to create a window. Just do the API call like this:

Code: Select all

SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
SharkyEXE
User
User
Posts: 35
Joined: Sat Feb 26, 2022 6:33 pm

Re: Monitor Off

Post by SharkyEXE »

mk-soft
BarryG


Hello

Please, give me work code

I compile your code and run exe file

I write, work or not

Thank You!
User avatar
mk-soft
Always Here
Always Here
Posts: 5338
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Monitor Off

Post by mk-soft »

:?:

Both codes can be compiled and executed :!:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Monitor Off

Post by netmaestro »

The code of BarryG runs as is as does that of mk-soft. Windows 11.
BERESHEIT
SharkyEXE
User
User
Posts: 35
Joined: Sat Feb 26, 2022 6:33 pm

Re: Monitor Off

Post by SharkyEXE »

mk-soft
netmaestro


I have code, if i run program, monitor is off

If I move the mouse or press the buttons on the mouse, the monitor will NOT turn on

If I press any buttons on the keyboard, the monitor will NOT turn on

If I press the ESC button twice on the keyboard, the monitor will turn on

When the monitor turns on, if I don't do anything, I won't move the mouse, I won't press buttons on the mouse, I won't press buttons on the keyboard - the monitor will turn off again
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Monitor Off

Post by ar-s »

Code: Select all

#MONITOR_OFF = 2
PostMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND,#SC_MONITORPOWER, #MONITOR_OFF)
End
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Monitor Off

Post by BarryG »

@ar-s: Been there; posted that. Hehe.
User avatar
mk-soft
Always Here
Always Here
Posts: 5338
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Monitor Off

Post by mk-soft »

SharkyEXE wrote: Fri Jul 08, 2022 3:51 am mk-soft
netmaestro


I have code, if i run program, monitor is off

If I move the mouse or press the buttons on the mouse, the monitor will NOT turn on

If I press any buttons on the keyboard, the monitor will NOT turn on

If I press the ESC button twice on the keyboard, the monitor will turn on

When the monitor turns on, if I don't do anything, I won't move the mouse, I won't press buttons on the mouse, I won't press buttons on the keyboard - the monitor will turn off again
Either a hardware problem or something is wrong with the system.
My Monitor Off has been running for almost 10 years without any problems.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Blue
Addict
Addict
Posts: 868
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Monitor Off

Post by Blue »

Thanks, BarryG.
What a useful tidbit to keep in the drawer !

Any quick suggestion on how NOT to turn the monitor ON unless a specific key sequence is detected ?
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
mk-soft
Always Here
Always Here
Posts: 5338
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Monitor Off

Post by mk-soft »

Write your own screen saver with the appropriate input to finish
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply