본문 바로가기
프로그래밍/Windows

winAPI 메시지 처리 함수 정리하기

by 즉흥 2014. 3. 28.
728x90
반응형

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdParam,int nCmdShow){

HWND hWnd;

MSG Message;

WNDCLASS WndClass;

g_hInst=hInstance;


//유니코드 출력을 위해 로케일 설정

_wsetlocale( LC_ALL, L"korean" );


WndClass.cbClsExtra=0;

WndClass.cbWndExtra=0;

WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);

WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

WndClass.hInstance=hInstance;

WndClass.lpfnWndProc=(WNDPROC)WndProc;

WndClass.lpszClassName=L"test";

WndClass.lpszMenuName=NULL;

WndClass.style=CS_HREDRAW | CS_VREDRAW;

RegisterClass(&WndClass);


hWnd=CreateWindow(L"test",L"스케쥴러",WS_CAPTION | WS_SYSMENU,

50,50,950,850,

 NULL,(HMENU)NULL,hInstance,NULL);

ShowWindow(hWnd,nCmdShow);

while(GetMessage(&Message,0,0,0)) {

TranslateMessage(&Message);

DispatchMessage(&Message);

}

return Message.wParam;

}



LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam){

int i;

static MESSAGEMAP MessageMaps[] = { //콜백 함수 정리

{WM_CREATE, OnCreate},

{WM_COMMAND, OnCommand},

{WM_CTLCOLOREDIT, OnCtlColorEdit},

{WM_PAINT, OnPaint},

{WM_DESTROY, OnDestroy}

};

for(i = 0 ; i < sizeof(MessageMaps) / sizeof(MessageMaps[0]) ; ++i){

if(MessageMaps[i].iMessage == iMessage){

return (*MessageMaps[i].lpfnMsgProc)(hWnd, wParam, lParam);

}

}

return(DefWindowProc(hWnd, iMessage, wParam, lParam));

}


길게 안 늘어뜨리고 각각의 함수로 정리할 수 있어서 좋은 듯.


LRESULT OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam){

hdc=BeginPaint(hWnd,&ps);

SetBkMode(hdc,TRANSPARENT);

EndPaint(hWnd,&ps);

return 0;

}


LRESULT OnDestroy(HWND hWnd, WPARAM wParam, LPARAM lParam){

PostQuitMessage(0);

DeleteObject(hBitmap);

DeleteObject(hBrush);

return 0;

}


LRESULT OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam){

}


이런 식으로 메시지 처리 함수들을 만들어주면 된다.

728x90
반응형

댓글