Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1181
  • Last Modified:

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.
0
Lukasz Lach
Asked:
Lukasz Lach
  • 4
  • 4
  • 2
  • +1
1 Solution
 
DaFoxCommented:
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
0
 
Lukasz LachAuthor Commented:
the same ;-/
0
 
MunimCommented:
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);


This works fine...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MunimCommented:
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
0
 
Lukasz LachAuthor Commented:
A for that ;-)
0
 
steve_hskCommented:
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
0
 
MunimCommented:
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
0
 
Lukasz LachAuthor Commented:
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 ;-/
0
 
MunimCommented:
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...
0
 
steve_hskCommented:
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;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now