ホーム ATL/WTL
スケルトン
ドキュメント種別 ATL/WTL に関する文書
最終更新日 2007/03/18
PR
 これまで ATL/WTL Application Wizard を使用せずに Win32 プロジェクト として プログラムを作成してきましたが、ここでは ATL/WTL Application Wizard を使用してプロジェクトを作成します。

次に示すのは ATL/WTL Application Wizard で作成したモーダルダイアログのスケルトン(雛形)です。 作成するには、ATL/WTL Application Wizard を実行して、[Application Type] 設定画面で [Dialog Based] と [Modal Dialog] を選択し、[Finish] ボタンを押します。 なお、改行は元のソースから変更している部分があります。

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#pragma once

// Change these values to use different versions
#define WINVER      0x0500
#define _WIN32_WINNT  0x0501
#define _WIN32_IE   0x0501
#define _RICHEDIT_VER   0x0200

#include <atlbase.h>
#include <atlapp.h>

extern CAppModule _Module;

#include <atlwin.h>

#if defined _M_IX86
  #pragma comment(linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
  #pragma comment(linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
  #pragma comment(linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
  #pragma comment(linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
			

// stdafx.cpp : source file that includes just the standard includes
//  SampleProject.pch will be the pre-compiled header
//  stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

#if (_ATL_VER < 0x0700)
#include <atlimpl.cpp>
#endif //(_ATL_VER < 0x0700)
			

// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

class CMainDlg : public CDialogImpl<CMainDlg>
{
public:
    enum { IDD = IDD_MAINDLG };

    BEGIN_MSG_MAP(CMainDlg)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
        COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
        COMMAND_ID_HANDLER(IDOK, OnOK)
        COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
    END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//  LRESULT MessageHandler(
//      UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//  LRESULT CommandHandler(
//      WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//  LRESULT NotifyHandler(
//      int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

    LRESULT OnInitDialog(
        UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
        // center the dialog on the screen
        CenterWindow();

        // set icons
        HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), 
            MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 
            ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), 
            LR_DEFAULTCOLOR);
        SetIcon(hIcon, TRUE);
        HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), 
            MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 
            ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 
            LR_DEFAULTCOLOR);
        SetIcon(hIconSmall, FALSE);

        return TRUE;
    }

    LRESULT OnAppAbout(
        WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        CSimpleDialog<IDD_ABOUTBOX, FALSE> dlg;
        dlg.DoModal();
        return 0;
    }

    LRESULT OnOK(
        WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        // TODO: Add validation code 
        EndDialog(wID);
        return 0;
    }

    LRESULT OnCancel(
        WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        EndDialog(wID);
        return 0;
    }
};
			

// SampleProject.cpp : main source file for SampleProject.exe
//

#include "stdafx.h"

#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>

#include "resource.h"

#include "MainDlg.h"

CAppModule _Module;

int WINAPI _tWinMain(HINSTANCE hInstance, 
    HINSTANCE /*hPrevInstance*/, LPTSTR /*lpstrCmdLine*/, int /*nCmdShow*/)
{
    HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
//  HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
    ATLASSERT(SUCCEEDED(hRes));

    // this resolves ATL window thunking problem when 
    // Microsoft Layer for Unicode (MSLU) is used
    ::DefWindowProc(NULL, 0, 0, 0L);

    AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls

    hRes = _Module.Init(NULL, hInstance);
    ATLASSERT(SUCCEEDED(hRes));

    int nRet = 0;
    // BLOCK: Run application
    {
        CMainDlg dlgMain;
        nRet = dlgMain.DoModal();
    }

    _Module.Term();
    ::CoUninitialize();

    return nRet;
}
			

 stdafx.hファイルでは次のコードが生成されます。

  • バージョン定義マクロ
  • ATL/WTLヘッダファイルのインクルード
  • マニフェストファイルのリンカオプション

 stdafx.cppファイルでは、ATL7.0より古いバージョンを使っている場合にatlimpl.cppファイルがインクルードされます。 atlimpl.cppは、_ATL_MIN_CRTが定義されている場合に、 独自のWinMainCRTStartup()などを呼び出します。 これにより、ATLは可能な限りCランタイムライブラリ(CRT)を使用しなくなり、 プログラムのファイルサイズが小さくなります。 なお、ATL7.0以上ではatlimpl.cppファイルをインクルードしなくても、 _ATL_MIN_CRTが定義されている場合は同様にプログラムのファイルサイズが小さくなります。

次に示すのは、上記のプログラムを_ATL_MIN_CRTを定義してビルドした場合と、 定義せずにビルドした場合のプログラムサイズです。

_ATL_MIN_CRT定義 _ATL_MIN_CRT未定義
44KB 76KB

ATL/WTL Application Wizard で作成したプロジェクトのリリースビルドでは、 デフォルトで_ATL_MIN_CRTが定義されています。

 MainDlg.hファイルでは、メインウィンドウとなるCMainDlgクラスが定義され、 そのクラス内で次のメッセージハンドラが定義されます。

  • WM_INITDIALOGメッセージハンドラ
  • [OK]ボタンコマンドメッセージハンドラ
  • [Cancel]ボタンコマンドメッセージハンドラ
  • [About]ボタンコマンドメッセージハンドラ

 SampleProject.cppファイルの_tWinMain()では、 モーダルダイアログを作成するコードのほかに、COM初期化関数の呼び出しやMSLUのためのコードが生成されます。