win32的建立視窗程式碼

迷失在程式碼中發表於2017-07-22

 

 

 

 

 #include"stdafx.h"
HINSTANCE g_hInstance = 0;
//視窗處理函式
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);  //能夠使GetMessage返回0?
    
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);  //給各種訊息做預設處理
}
//註冊視窗類
void Register(LPSTR lpClassName, WNDPROC wndproc){
    WNDCLASS wce = { 0 };
    //wce.cbSize = sizeof(wce);
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    //wce.hIconSm = NULL;
    wce.hInstance = g_hInstance;
    wce.lpfnWndProc = wndproc;
    wce.lpszClassName = _TEXT("lpClassName");
    wce.lpszMenuName = NULL;
    wce.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wce);//將以上所有賦值全部寫入作業系統
}
//建立主視窗
HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName){
    HWND hWnd = CreateWindowEx(0,_TEXT("lpClassName"),_TEXT("lpWndName"),WS_OVERLAPPEDWINDOW,100,100,700,500,NULL,NULL,g_hInstance,NULL);
    return hWnd;
}
//顯示視窗
void Display(HWND hWnd){
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}
//訊息迴圈
void Message(){
    MSG nMsg = { 0 };
    while (GetMessage(&nMsg,NULL,0,0))
    {
        TranslateMessage(&nMsg);
        DispatchMessage(&nMsg);  //交給視窗處理函式
    }
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    //HWND hWnd;
    g_hInstance = hInstance;  //當前程式例項控制程式碼
    Register("Main",WndProc);
    HWND hWnd=CreateMain("Main", "window");
    Display(hWnd);
    Message();
    return 0;
}


以上程式碼自己寫的有錯望糾正!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


相關文章