Link to home
Start Free TrialLog in
Avatar of Lukasz Lach
Lukasz Lach

asked on

Turning monitor off

Can someone help me with that code. It seems good, but it turns monitor off when pressing F2 and the it is turned on quickly. Don't realy know why ;/

program WorkHide2;

uses
   Windows, Messages;

var
  wndclass: TWndClass;
  mainwnd: HWnd;
  msg: TMsg;

function MainWindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM):
    LRESULT; stdcall;
begin
        case uMsg of
                WM_DESTROY: PostQuitMessage(0);
                WM_HOTKEY: if WParam = $0001 then PostMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
                else begin
                        Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
                        Exit;
                end;
        end;
        Result := 0;
end;

begin
  wndclass.lpfnWndProc := @MainWindowProc;
  wndclass.hInstance := hInstance;
  wndclass.lpszClassName := 'ALiNoE_WorkHide2';
  if RegisterClass(wndclass) = 0 then Halt(2);
  mainwnd := CreateWindow(wndclass.lpszClassName, nil, 0, 0, 0, 0, 0, 0, 0,
        hInstance, nil);
  if mainwnd = 0 then Halt(2);
  RegisterHotKey(mainwnd, $0001, 0, VK_F2);
  while GetMessage(msg, 0, 0, 0) do DispatchMessage(msg);
  UnregisterHotKey(mainwnd, $0001);
end.
Avatar of DaFox
DaFox

Just a shoot in the dark (not tested):

Change
PostMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
to
PostMessage(GetCurrentProcess, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
or better
SendMessage(GetCurrentProcess, WM_SYSCOMMAND, SC_MONITORPOWER, 1);

Markus
Avatar of Lukasz Lach

ASKER

the same ;-/
ASKER CERTIFIED SOLUTION
Avatar of Munim
Munim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
A for that ;-)
Hi anaKiN ...

I have a laptop so can't test, from what I understand,  turning the monitor off should carry a 0 not a 1 ?

{turns on the monitor}
SendMessage(Application.Handle, wm_SysCommand, SC_MonitorPower, -1);

{turns off the monitor}
SendMessage(Application.Handle, wm_SysCommand, SC_MonitorPower, 0);  <<< use a 0

Does this work any better ?

Steve
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
God daim, i found it not working now ;-/
The problem is with the keyboard, the message to turn off the monitor is sent before the F2 key is up, so that Windows notices input and turns the monitor on again... :-/

If anyone solve that I'll give him that 100 ptk, even again ;-/
Before sending the message, you can sleep the working thread for a millisecond or two... I haven't tested... But it seems it M A Y work...
maybe just insert a flag to test and prevent the repeat msg being processed ?

var
 wndclass: TWndClass;
 mainwnd: HWnd;
 msg: TMsg;
 bMonOff : Boolean; // need to initalise somewhere !

function MainWindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM):
   LRESULT; stdcall;
begin
       case uMsg of
               WM_DESTROY: PostQuitMessage(0);
               WM_HOTKEY:
               begin
                    if bMonOff = FALSE then
                    begin
                         if WParam = $0001 then PostMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
                    bMonOff := TRUE;
                    end;
               end
               else begin
                       Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
                       Exit;
               end;
       end;
       Result := 0;
end;