RegisterClass和RegisterClassEx有什麼區別?

微wx笑發表於2015-02-09

引言


繼上篇文章“VC CreateWindow 找不到視窗類”中將RegisterClassEx改為RegisterClass問題就解決了,究竟是什麼原因,RegisterClass和RegisterClassEx有什麼區別呢?
經過一番查詢和測試,終於找到了答案:

不同之處:


The RegisterClass function has been superseded by the RegisterClassEx function. You can still use RegisterClass, however, if you do not need to set the class small icon. In addition,their parameters are different.
E文看不太懂,經過測試就是一個引數的區別,如果使用RegisterClassEx 則WNDCLASSEX的hIconSm引數是必須設定的。

相關程式碼片:


//函式:MyRegisterClass
//作用:註冊視窗類
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wndclass;
    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    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;
    //將下面這條語句註釋,“CreateWindow”時就會出現“找不到視窗類”的錯誤;
    wndclass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    return RegisterClassEx(&wndclass);
}
//函式:InitInstance
//作用:初始化應用程式
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hwnd = NULL;
    hwnd = CreateWindow(szAppName, _T("按鈕設計"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);
    if (hwnd == NULL)
    {
        TCHAR szBuf[128];
        LPVOID lpMsgBuf;
        DWORD dw = GetLastError();
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
            NULL,
            dw,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );
        MessageBox(NULL, (LPTSTR)lpMsgBuf, "hwnd", MB_OK);
    }
    BOOL ret = ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    return TRUE;
}

這裡寫圖片描述

相關文章