ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Win32 윈도우 메시지맵
    Programming/WPF, WIN32API, MFC 2008. 12. 16. 09:22
    /**********************************************************
    *     main.h
    **********************************************************/



    #define dim(x) (sizeof(x) / sizeof(x[0]))       // 배열 원소가 몇개인지 반환

    //---------------------------------------------------- 데이터 타입 정의 시작

    /* 메시지와 함수를 대응시키는 구조체 */
    struct decodeUINT
    {                         
      UINT Code;                              
      LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
    }; 

    /* 메뉴 ID와 함수를 대응시키는 구조체 */
    struct decodeCMD
    {                            
      UINT Code;                                
      LRESULT (*Fxn)(HWND, WORD, HWND, WORD);   
    };
    //---------------------------------------------------- 데이터 타입 정의 끝



    //---------------------------------------------------- 함수 선언 시작

    HWND InitInstance (HINSTANCE, LPWSTR, int);
    int TermInstance (HINSTANCE, int);

    /* 윈도우 프로시저 */
    LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

    /* 메시지 핸들러 */
    LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
    LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);



    //---------------------------------------------------- 함수 선언 끝




                                                                    


    /**********************************************************
    *    main.cpp
    **********************************************************/


    #include <windows.h>
    #include "main.h"


    //---------------------------------------------------- 전역 데이터 정의 시작

    const TCHAR szAppName[] = TEXT("HelloCE");        // 윈도우 클래스 이름
    HINSTANCE hInst;                    // 프로그램 인스턴스 핸들

    /* MainWindowProc를 위한 메시지 분배 테이블 */
    const struct decodeUINT MainMessages[] = {
                          WM_PAINT, DoPaintMain,
                          WM_DESTROY, DoDestroyMain,
                         };


    //---------------------------------------------------- 전역 데이터 정의 끝



    //##################################################### 프로그램 시작점
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) 
    {
      MSG msg;
      int rc = 0;
      HWND hwndMain;

      /* 인스턴스 초기화 */
      hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
      if (hwndMain == 0return 0x10;

      /* 메시지 루프 */
      while (GetMessage (&msg, NULL, 00)) {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
      }
      /* 인스턴스 소거 */
      return TermInstance (hInstance, msg.wParam);
    }






    //---------------------------------------------------- InitInstance - 인스턴스 초기화
    HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) 
    {
      WNDCLASS wc;
      HWND hWnd;

      /* 전역 변수에 프로그램 인스턴스 핸들 보관 */
      hInst = hInstance;


      /* 주 윈도우 클래스 등록 */
      wc.style      = 0;                    // 윈도우 스타일
      wc.lpfnWndProc    = MainWndProc;                // 콜백 함수 
      wc.cbClsExtra    = 0;                    // 추가 클래스 데이터
      wc.cbWndExtra    = 0;                    // 추가 윈도우 데이터
      wc.hInstance    = hInstance;                // 소유자 핸들
      wc.hIcon      = NULL,                    // 아이콘
      wc.hCursor      = LoadCursor (NULL, IDC_ARROW);        // 디폴트 커서
      wc.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH);  // 배경색
      wc.lpszMenuName    = NULL;                    // 메뉴이름 
      wc.lpszClassName  = szAppName;                // 윈도우 클래스 이름

      if (RegisterClass (&wc) == 0return 0;

      /* 주 윈도우 생성 */
      hWnd = CreateWindow (szAppName,                  // 윈도우 클래스
                TEXT("HelloCE"),              // 윈도우 창제목
                WS_VISIBLE | WS_CAPTION | WS_SYSMENU,    // 윈도우 스타일
                CW_USEDEFAULT,                // 시작 x좌표
                CW_USEDEFAULT,                // 시작 y좌표
                CW_USEDEFAULT,                // 최초 너비
                CW_USEDEFAULT,                // 최초 높이
                NULL,                    // 부모윈도우
                NULL,                    // 메뉴(NULL)
                hInstance,                  // 프로그램 인스턴스
                NULL);                    // 생성 매개변수 포인터

      if (!IsWindow (hWnd)) return 0;  

      /* 표준적인 윈도우 표시/갱신 */
      ShowWindow (hWnd, nCmdShow);
      UpdateWindow (hWnd);
      return hWnd;
    }












    //---------------------------------------------------- Terminstance - 프로그램 소거
    int TermInstance (HINSTANCE hInstance, int nDefRC) 
    {
      return nDefRC;
    }




    //##################################################### 주 윈도우를 위한 메시지 처리 프로시저

    //---------------------------------------------------- MainWndProc - 주 윈도우의 콜백 함수
    LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,  LPARAM lParam) 
    {
      INT i;

      /* 메시지 목록을 검사하여 목록에 있는 메시지라면 해당 프로시저 호출 */
      for (i = 0; i < dim(MainMessages); i++) 
      {
        if (wMsg == MainMessages[i].Code)
          return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
      }
      return DefWindowProc (hWnd, wMsg, wParam, lParam);
    }





    //##################################################### 메시지 처리 프로시저's

    LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) 
    {
      PAINTSTRUCT ps;
      RECT rect;
      HDC hdc;

      // Get the size of the client rectangle
      GetClientRect (hWnd, &rect);

      hdc = BeginPaint (hWnd, &ps); 
      Rectangle(hdc, 1010200200);
      EndPaint (hWnd, &ps); 
      return 0;
    }




    //##################################################### 창 파괴 메시지

    //---------------------------------------------------- DoPaintMain - WM_PAINT메시지 처리
    LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) 
    {
      PostQuitMessage (0);
      return 0;



    댓글

Designed by Tistory.