C++ 一鍵關閉屏幕

Demo下載地址:http://pan.baidu.com/s/1vN4wFwindows

 

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WindowProc( HWND hwnd,      // handle to window
                             UINT uMsg,      // message identifier
                             WPARAM wParam,  // first message parameter
                             LPARAM lParam   // second message parameter
                            );

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 延時防止雙擊exe關閉屏幕後,又打開屏幕。關閉屏幕的消息,先於系統的雙擊消息;
    Sleep(200);

    ::SendMessage(HWND_BROADCAST, WM_SYSCOMMAND,  SC_MONITORPOWER,  (LPARAM)2);

    static TCHAR szAppName[] = TEXT("HelloWin");

    WNDCLASS wndClass;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WindowProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = LoadIcon(hInstance, (char*)IDI_ICON1);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = szAppName;

    if (!RegisterClass(&wndClass))
    {
        MessageBox(NULL, TEXT("註冊窗口失敗!"), TEXT(""), 0);
        return 0;
    }

    HWND hWnd = CreateWindow(szAppName, TEXT("Hello Program"),
        WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
        NULL, NULL, hInstance, NULL);

    ::SendMessage(hWnd, WM_DESTROY, 0, NULL);
    
    return 0;
}

LRESULT CALLBACK WindowProc( HWND hwnd,      // handle to window
                            UINT uMsg,      // message identifier
                            WPARAM wParam,  // first message parameter
                            LPARAM lParam   // second message parameter
                            )
{
    HDC  hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
相關文章
相關標籤/搜索