Turn off the monitor Applet

Source: Internet
Author: User
I started to follow many articles on the Internet.
Sendmessagege (getasktopwindow (), wm_syscommand, SC _monitorpower, 2 );
I found that I couldn't close the monitor. I haven't found any problem for a long time. Because many articles on the Internet say this in unison, there is no doubt that the code here is faulty.

Later I found a post on Delphi and found that the first parameter when calling this function (that is, the target window for message sending is the main window of the program itself), so I tried to change the code
Sendmessagege (hwnd, wm_syscommand, SC _monitorpower, 2 );
Haha, the monitor turned off.

At first, I thought it was necessary for the program to capture the corresponding message in the message loop to exit, and then I realized that this was all about it. After the monitor is closed, the program can exit. If the keyboard and the mouse are used, the monitor will automatically turn on.

The current program is still a small problem, that is, after the program is run, it is opened after a small close, it should be because the keyboard or mouse message when the program is running is captured by windows, so add a time delay before sending the message. OK!

The complete procedure is as follows:

# Include <windows. h>

Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow)
{
Static tchar szappname [] = text ("lcddown ");
Hwnd;
Wndclass;

Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = defwindowproc;
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance;
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname;

If (! Registerclass (& wndclass ))
{
MessageBox (null, text ("this program requires Windows NT! "), Szappname,
Mb_iconerror );
Return 0;
}

Hwnd = createwindow (szappname, text ("lcddown"), ws_overlappedwindow,
Cw_usedefault, null,
Null, hinstance, null );

Showwindow (hwnd, 0 );
Updatewindow (hwnd );

Sleep (1000 );
Sendmessage (hwnd, wm_syscommand, SC _monitorpower, 2 );

Return 0;
}

Lesson: it seems that even if everyone says so, it may not be true. Believe in books as well as have no books. practice is the only criterion for testing truth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.