?
Solved

Turning monitor off

Posted on 2003-03-08
11
Medium Priority
?
1,153 Views
Last Modified: 2011-09-20
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
Comment
Question by:Lukasz Lach
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 8094159
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
 
LVL 5

Author Comment

by:Lukasz Lach
ID: 8094181
the same ;-/
0
 
LVL 1

Accepted Solution

by:
Munim earned 400 total points
ID: 8094217
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);


This works fine...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Munim
ID: 8094227
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
0
 
LVL 5

Author Comment

by:Lukasz Lach
ID: 8094235
A for that ;-)
0
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8094236
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
 
LVL 1

Expert Comment

by:Munim
ID: 8094246
Search in Microsoft Platform SDK Documentation for WM_SYSCOMMAND...
0
 
LVL 5

Author Comment

by:Lukasz Lach
ID: 8094305
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
 
LVL 1

Expert Comment

by:Munim
ID: 8094331
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
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8094336
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question