我可以将我的窗口代码放在函数或标题中

时间:2014-03-11 08:04:50

标签: c++ function graphics header window

我有这个代码用于设置窗口,我想知道我是否可以将它放在aheader或单独的函数中来优化我的项目。如果是这样,我可以从单独的程序中操纵它的大小和内容代码是:

#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL, "WindowClass1",    
                      "Our First Windowed Program",   
                      WS_OVERLAPPEDWINDOW,    
                      300,    
                      300,    
                      500,    
                      400,    
                      NULL,    
                      NULL,    
                      hInstance,    
                      NULL);    


ShowWindow(hWnd, nCmdShow);

MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

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


return DefWindowProc (hWnd, message, wParam, lParam);
}

0 个答案:

没有答案