코드 설명: Visual C++ 장치 프로젝트에서 마법사가 생성한 코드

업데이트: 2007년 11월

Visual C++ 기능과 마법사를 사용하면 다중 플랫폼 장치 응용 프로그램, 구성 파일 및 프로젝트 파일을 생성하는 등과 같은 대부분의 일상적인 작업을 간소화할 수 있습니다. 이 연습에서는 Windows 32 장치 응용 프로그램 마법사에서 자동으로 생성되는 코드에 대해 설명합니다. 이를 통해 Windows 응용 프로그램을 필요에 맞게 확장하고 수정할 수 있습니다.

마법사에서 생성된 Windows 32 장치 응용 프로그램용 코드

Windows(win32) 데스크톱 프로그래밍에 익숙한 개발자라면 마법사에서 생성된 기본 루틴을 쉽게 이해할 수 있을 것입니다.

일반적인 Windows 장치 응용 프로그램의 경우 주 진입점은 <yourprojectname>.cpp 파일에 있습니다. 아래에서 이 파일의 샘플을 볼 수 있습니다.

간단히 살펴보면 응용 프로그램에는 다음과 같은 기본 기능이 있습니다.

  • WinMain 함수

  • MyRegisterClass 함수

  • InitInstance 함수

  • About Dialog 함수

다음 예제에서는 위의 각 함수에 대해 설명합니다.

  • WinMain 함수: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow). 자세한 내용은 WinMain Function을 참조하십시오.

    int WINAPI WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
    {
          MSG msg;
    
          // Perform application initialization:
          if (!InitInstance(hInstance, nCmdShow)) 
          {
                return FALSE;
          }
    
  • Win32 플랫폼. 자세한 내용은 Win32 Platforms을 참조하십시오.

    #ifndef WIN32_PLATFORM_WFSP
            HACCEL hAccelTable;
            hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32SMARTDEVICE);
    #endif // !WIN32_PLATFORM_WFSP
    
  • 메시지 루프. 자세한 내용은 Message Handling and Command Targets을 참조하십시오.

            // Main message loop:
            while (GetMessage(&msg, NULL, 0, 0)) 
            {
    #ifndef WIN32_PLATFORM_WFSP
                if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    #endif // !WIN32_PLATFORM_WFSP
                {
                      TranslateMessage(&msg);
                      DispatchMessage(&msg);
                }
          }
    
          return (int) msg.wParam;
    }
    
  • MyRegisterClass 함수는 Windows 응용 프로그램을 등록합니다.

    //  FUNCTION: MyRegisterClass()
    ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
    {
          WNDCLASS wc;
    
          wc.style         = CS_HREDRAW | CS_VREDRAW;
          wc.lpfnWndProc   = (WNDPROC)WndProc;
          wc.cbClsExtra    = 0;
          wc.cbWndExtra    = 0;
          wc.hInstance     = hInstance;
          wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32SMARTDEVICE));
          wc.hCursor       = 0;
          wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
          wc.lpszMenuName  = 0;
          wc.lpszClassName = szWindowClass;
    
          return RegisterClass(&wc);
    }
    
    
  • InitInstance: FUNCTION InitInstance(HANDLE, int). 자세한 내용은 InitInstance Member Function를 참조하십시오.

    WIN32_PLATFORM_WFSP는 Smartphone의 조건으로 사용되고 WIN32_PLATFORM_PSPC는 Pocket PC의 조건으로 사용됩니다. 더 차별화하려면 조건을 언제라도 직접 정의할 수 있습니다.

    // FUNCTION: InitInstance(HANDLE, int)
    // PURPOSE: Saves instance handle and creates main window.
    // COMMENTS:
    // In this function, we save the instance handle in a global 
    // variable and create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
        HWND hWnd;
        TCHAR szTitle[MAX_LOADSTRING];        
    // title bar text
        TCHAR szWindowClass[MAX_LOADSTRING];  
    // main window class name
    
        g_hInst = hInstance; 
    // Store instance handle in your global variable.
    
    #ifdef WIN32_PLATFORM_PSPC
        // SHInitExtraControls should be called once during your application's initialization to initialize any
        // of the Pocket PC special controls such as CAPEDIT and SIPPREF.
        SHInitExtraControls();
    #endif // WIN32_PLATFORM_PSPC
    
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
        LoadString(hInstance, IDC_WIN32SMARTDEVICE, szWindowClass, MAX_LOADSTRING);
    
    #if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
        //If it is already running, then focus on the window, and exit.
        hWnd = FindWindow(szWindowClass, szTitle);  
        if (hWnd) 
        {
            // Set the focus to the foremost child window.
            // The "| 0x00000001" is used to bring any owned windows
            //to the foreground and activate them.
            SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
            return 0;
        } 
    #endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
    
        if (!MyRegisterClass(hInstance, szWindowClass))
        {
          return FALSE;
        }
    
        hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
        if (!hWnd)
        {
            return FALSE;
        }
    
    #ifdef WIN32_PLATFORM_PSPC
        // When the main window is created using CW_USEDEFAULT, 
        // the height of the menubar is not taken into account. 
        // So the generated code resizes the window after creating it.
        if (g_hWndMenuBar)
        {
            RECT rc;
            RECT rcMenuBar;
    
            GetWindowRect(hWnd, &rc);
            GetWindowRect(g_hWndMenuBar, &rcMenuBar);
            rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
    
            MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
        }
    #endif // WIN32_PLATFORM_PSPC
    
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
    
        return TRUE;
    }
    
    
  • 다른 대화 상자를 빌드하는 방법을 보여 주는 예제로 응용 프로그램에 대한 정보 대화 상자도 생성됩니다. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

    // win32smartdevice.cpp : Defines the entry point for the application.
    #include "stdafx.h"
    #include "win32smartdevice.h"
    #include <windows.h>
    #include <commctrl.h>
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE               g_hInst;                
    // Current instance:
    HWND                    g_hWndMenuBar;          
    // Menu bar handle
    
    // Forward declarations of functions included in this code module:
    ATOM              MyRegisterClass(HINSTANCE, LPTSTR);
    BOOL              InitInstance(HINSTANCE, int);
    LRESULT CALLBACK  WndProc(HWND, UINT, WPARAM, LPARAM);
    #ifndef WIN32_PLATFORM_WFSP
    LRESULT CALLBACK  About(HWND, UINT, WPARAM, LPARAM);
    #endif 
    // !WIN32_PLATFORM_WFSP
    //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
    //  PURPOSE:  Processes messages for the main window.
    //  WM_COMMAND    - process the application menu
    //  WM_PAINT      - Paint the main window
    //  WM_DESTROY    - post a quit message and return
    
  • WM_COMMAND와 같은 일부 기본 메시지는 확장성을 위해 이미 포함되어 있습니다. WinProc은 시스템 및 사용자 입력 메시지를 처리하기 위해 포함되어 있습니다. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam. WinProc에 대한 자세한 내용은 Windows Overview를 참조하십시오.

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
            HGDIOBJ  hbrWhite, hbrGray; 
            POINT aptStar[6] = {50,2, 2,98, 98,33, 2,33, 98,98, 50,2}; 
    
    #if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
        static SHACTIVATEINFO s_sai;
    #endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
        switch (message) 
        {
            case WM_COMMAND:
                wmId    = LOWORD(wParam); 
                wmEvent = HIWORD(wParam); 
                // Parse the menu selections:
                switch (wmId)
                {
    #ifndef WIN32_PLATFORM_WFSP
                    case IDM_HELP_ABOUT:
                        DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                        break;
    #endif // !WIN32_PLATFORM_WFSP
    #ifdef WIN32_PLATFORM_WFSP
                    case IDM_OK:
                        DestroyWindow(hWnd);
                        break;
    #endif // WIN32_PLATFORM_WFSP
    #ifndef WIN32_PLATFORM_WFSP
                    case IDM_OK:
                        SendMessage (hWnd, WM_CLOSE, 0, 0);                       
                        break;
    #endif // !WIN32_PLATFORM_WFSP
                    default:
                        return DefWindowProc(hWnd, message, wParam, lParam);
                }
                break;
            case WM_CREATE:
    #ifdef SHELL_AYGSHELL
                SHMENUBARINFO mbi;
    
                memset(&mbi, 0, sizeof(SHMENUBARINFO));
                mbi.cbSize     = sizeof(SHMENUBARINFO);
                mbi.hwndParent = hWnd;
                mbi.nToolBarId = IDR_MENU;
                mbi.hInstRes   = g_hInst;
    
                if (!SHCreateMenuBar(&mbi)) 
                {
                    g_hWndMenuBar = NULL;
                }
                else
                {
                    g_hWndMenuBar = mbi.hwndMB;
                }
    
    #ifndef WIN32_PLATFORM_WFSP
                // Initialize the shell activate info structure
                memset(&s_sai, 0, sizeof (s_sai));
                s_sai.cbSize = sizeof (s_sai);
    #endif // !WIN32_PLATFORM_WFSP
    #endif // SHELL_AYGSHELL
          hbrWhite = GetStockObject(WHITE_BRUSH); 
            hbrGray  = GetStockObject(GRAY_BRUSH); 
            return 0L; 
    break;
            case WM_PAINT:
             RECT  rc;
           hdc = BeginPaint(hWnd, &ps); 
           GetClientRect(hWnd, &rc); 
           Polyline(hdc, aptStar, 6); 
           EndPaint(hWnd, &ps); 
           return 0L; 
    
                break;
            case WM_DESTROY:
    #ifdef SHELL_AYGSHELL
                CommandBar_Destroy(g_hWndMenuBar);
    #endif // SHELL_AYGSHELL
                PostQuitMessage(0);
                break;
    
    #if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
            case WM_ACTIVATE:
                // Notify shell of your activate message.
                SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
                break;
            case WM_SETTINGCHANGE:
                SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
                break;
    #endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
    
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    #ifndef WIN32_PLATFORM_WFSP
    // Message handler for about box.
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_INITDIALOG:
    #ifdef SHELL_AYGSHELL
                {
                    // Create a Done button and size it.  
                    SHINITDLGINFO shidi;
                    shidi.dwMask = SHIDIM_FLAGS;
                    shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                    shidi.hDlg = hDlg;
                    SHInitDialog(&shidi);
                }
    #endif // SHELL_AYGSHELL
    
                return TRUE;
    
            case WM_COMMAND:
    #ifdef SHELL_AYGSHELL
                if (LOWORD(wParam) == IDOK)
    #endif
                {
                    EndDialog(hDlg, LOWORD(wParam));
                    return TRUE;
                }
                break;
    
            case WM_CLOSE:
                EndDialog(hDlg, message);
                return TRUE;
    
    #ifdef _DEVICE_RESOLUTION_AWARE
            case WM_SIZE:
                {
                DRA::RelayoutDialog(
                      g_hInst, 
                      hDlg, 
                      DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
                }
                break;
    #endif
        }
        return FALSE;
    }
    #endif // !WIN32_PLATFORM_WFSP
    
    

참고 항목

참조

옵션 대화 상자, 장치 도구, 일반

도구 상자

기타 리소스

Visual C++ 장치 프로젝트 개발