C++/summary

SDK - 윈도우창을 여러개 띄워보자.

gandus 2010. 11. 15. 10:48

                        인스턴스
                     ↓            ↓
윈도우 프로시저 - 윈도클래스     윈도우 프로시저 - 윈도클래스
                   ↓                                 ↓
       윈도우1, 윈도우2                        윈도우3, 윈도우4


이렇게 구성이 되어있다.

그리고 윈도우 카운터를 통해서 모든창이 꺼질때 까지
프로세스를 종료하지 않는다.


소스코드는 다음과 같다.


#include
<windows.h>

 

 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);

 

static int wndCount =0;

 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

                                 LPSTR lpCmdLine, int nCmdShow)

{

        WNDCLASS wndclass, wndclass2;

        HWND hwnd, hwnd2, hwnd3, hwnd4;

        MSG msg;

 

       

        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 = "HelloClass";

 

       

        wndclass2 = wndclass;

        wndclass2.lpszClassName = "h2";

        wndclass2.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

        wndclass2.lpfnWndProc = WndProc2;

 

 

 

        if(!RegisterClass(&wndclass)) return 1;

        if(!RegisterClass(&wndclass2)) return 1;

 

 

       

        hwnd = CreateWindow("HelloClass", "h1", WS_OVERLAPPEDWINDOW,

               0, 0, 500, 500,

               NULL, NULL, hInstance, NULL);

 

        hwnd2 = CreateWindow("HelloClass", "h2", WS_OVERLAPPEDWINDOW,

               500, 0, 500, 500,

               NULL, NULL, hInstance, NULL);

       

        hwnd3 = CreateWindow("h2", "h3", WS_OVERLAPPEDWINDOW,

               0, 500, 500, 500,

               NULL, NULL, hInstance, NULL);

 

        hwnd4 = CreateWindow("h2", "h4", WS_OVERLAPPEDWINDOW,

               500, 500, 500, 500,

               NULL, NULL, hInstance, NULL);

 

       

        ShowWindow(hwnd, nCmdShow);

        ShowWindow(hwnd2, nCmdShow);

        ShowWindow(hwnd3, nCmdShow);

        ShowWindow(hwnd4, nCmdShow);

 

       

        while(GetMessage(&msg, NULL, 0, 0) > 0){

               TranslateMessage(&msg);

               DispatchMessage(&msg);

        }

 

        return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message,

                                              WPARAM wParam, LPARAM lParam)

{

        HDC hdc;

        PAINTSTRUCT ps;

        char *str = "Hello, SDK";

 

        switch(message){

        case WM_CREATE:

               wndCount++;

               return 0;

                                

        case WM_LBUTTONDOWN:

               MessageBox(hwnd, "마우스를클릭했습니다.", "마우스메시지", MB_OK);

               return 0;

 

        case WM_PAINT:

               hdc = BeginPaint(hwnd, &ps);

               TextOut(hdc, 100, 100, str, lstrlen(str));

               EndPaint(hwnd, &ps);

               return 0;

 

        case WM_DESTROY:

 

               wndCount--;

               if(wndCount == 0){

                       PostQuitMessage(0);

               }

               return 0;

        }

       

        return DefWindowProc(hwnd, message, wParam, lParam);

}

 

 

LRESULT CALLBACK WndProc2(HWND hwnd, UINT message,

                                              WPARAM wParam, LPARAM lParam)

{

        HDC hdc;

        PAINTSTRUCT ps;

        char *str = "Hello, SDK";

 

        switch(message){

        case WM_CREATE:

               wndCount++;

               return 0;

                                

        case WM_LBUTTONDOWN:

               MessageBox(hwnd, "마우스를클릭했습니다.", "마우스메시지", MB_OK);

               return 0;

 

        case WM_PAINT:

               hdc = BeginPaint(hwnd, &ps);

               TextOut(hdc, 100, 100, str, lstrlen(str));

               EndPaint(hwnd, &ps);

               return 0;

 

        case WM_DESTROY:

 

               wndCount--;

               if(wndCount == 0){

                       PostQuitMessage(0);

               }

               return 0;

        }

       

        return DefWindowProc(hwnd, message, wParam, lParam);

}