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.