關於Windows programming的基本觀念,可以參閱這裡

這篇並不會解釋太多windows programming上的觀念,純粹只是分享過去的程式作品。在這裡可以看到的是,我怎麼對window創建的程序進行封裝以及對於loop迴圈的設計方法。本程式在執行之後會跳出一個白色的視窗佔住整個營幕並鎖定其他操作,按下F4可以逃出鎖定,其中包含以下幾個檔案: WinPro.h WinPro.cpp main.cpp:包含主程式main(),為程式的進入點

WinPro.h:

#pragma once

#include <iostream>
#include <windows.h>
#include <stdlib.h>

using namespace std; #ifndef WIN_PRO #define WIN_PRO

class WinPro
{

public: WinPro(void); ~WinPro(void); public://初始函式

    bool init();
    int start(int nCmdShow);

public://靜態視窗函式回傳true代表結束迴圈,以正常方式離開程式 static bool frameFunc();

public://功能函式 void lockWindows(bool bLOCK);

private://windows系統變數 WNDCLASSEX wc; HWND hwnd; MSG Msg; int wnd_W; int wnd_H; }; #endif

WinPro.cpp:

#include “WinPro.h”

WinPro::WinPro(void) {

}

WinPro::~WinPro(void) { }

LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){ //PAINTSTRUCT ps; //HDC hMyDC;

//int nLen = (sizeof(text)-1) * 2;
    if(WinPro::frameFunc()){
        msg = WM_DESTROY;
    }
    switch(msg)
    {
    case WM_PAINT:
   // hMyDC=BeginPaint(hwnd, &ps);
  //  TextOut(hMyDC,100,100, text,nLen);
           // EndPaint(hwnd, &ps);
    break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
    break;
}
//Sleep(10);
     return DefWindowProcW(hwnd, msg, wParam, lParam);
}

bool WinPro::init(){ wchar_t* szAppName = L”LockWindows”; wnd_W=400; wnd_H=300; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = NULL; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = szAppName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        return 0;
    }
hwnd = 
    CreateWindowEx(
    WS_EX_TOPMOST,
    szAppName,
    L"LockWindows",
    WS_MAXIMIZE|WS_POPUP|WS_DLGFRAME|WS_VISIBLE,
    0, 0,
    1280, 1024,
    NULL, NULL, NULL, NULL);
 
    if(hwnd == NULL)
    {
        MessageBox(NULL, L"視窗建立失敗!", L"發生錯誤!",
            MB_ICONEXCLAMATION | MB_OK);
        return false;
    }
    return true;
}

int WinPro::start(int nCmdShow){

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}

void WinPro::lockWindows(bool bLOCK){ EnableWindow(FindWindow(L”Shell_TrayWnd”, NULL), !bLOCK); EnableWindow(FindWindow(L”ProgMan”,NULL), !bLOCK); EnableWindow(FindWindow(NULL, L”Program Manager”), !bLOCK); EnableWindow(FindWindow(NULL, L”LockWindows”), !bLOCK); }

main:

#include “WinPro.h” /* 其中都是去找 Shell 所建立的 Windows Handle,如: 工作列: FindWindow(“Shell_TrayWnd”,NULL) 桌面: FindWindow(“ProgMan”,NULL) FindWindow(NULL,“Program Manager”) / static bool lock; WinPro lockWin;

void atExit(){
    lock = false;
    lockWin->lockWindows(false);
}

bool WinPro::frameFunc(){ lockWinlockWindows(lock);

//程式的逃生出口,用以正常結束程式時使用F4
    if(GetAsyncKeyState(VK_F4) < 0){
            return true;
    }   
    atexit(atExit);//在視窗正常關閉時會做什麼事,此為函式指標的用法()如
               //強迫結束process時此函數沒用處。
     if(FindWindow(NULL, L"Windows 工作管理員") != 0){
            CloseWindow(FindWindow(NULL, L"Windows 工作管理員"));
     }
    // Sleep(30);
    return false;
}

//程式進入點 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

lockWin = new WinPro();   
    if(!lockWin->init()){
        MessageBox(NULL, L"視窗類別登記失敗!", L"發生錯誤!",
           MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    lock = true;
lockWin->start(nCmdShow);//程式進入frameFunc迴圈,直至結束
    return 0;
}