WTLでは標準コントロールのスタティックコントロールをCStaticT
というクラステンプレートでカプセル化しています。
テンプレート引数にはクラスを指定しますが、
そのクラスはCStaticTの基底クラスとして使用されます。
atlctrls.hヘッダではtypedefによって次のように宣言されています。
// atlctrls.h
typedef CStaticT<ATL::CWindow> CStatic;
|
これは、CStaticクラスはCWindowクラスの派生クラスであることを意味します。
WTLのCStaticクラスは、MFCの同名のクラスと同等の機能を備えています。
以下に示すのは、CStaticクラスを使用して、
クライアント領域上でタップした位置の座標を表示する例です。
// stdafx.h
#pragma once
#define WINVER 0x0420
#include <atlbase.h>
#if _ATL_VER == 0x900
#define _SECURE_ATL 1
#endif
#define _WTL_USE_CSTRING
#include <atlapp.h>
extern CAppModule _Module;
#include <atlwin.h>
#include <tpcshell.h>
#include <aygshell.h>
#pragma comment(lib, "aygshell.lib")
#include <atlcrack.h>
#include <atlmisc.h>
#include <atlframe.h>
#include <atlctrls.h>
#define _WTL_CE_NO_ZOOMSCROLL
#define _WTL_CE_NO_FULLSCREEN
#include <atlwince.h>
|
// SampleProjectDialog.h
#pragma once
class CSampleProjectDialog :
public CAppStdDialogImpl<CSampleProjectDialog>,
public CUpdateUI<CSampleProjectDialog>,
public CMessageFilter, public CIdleHandler
{
public:
DECLARE_APP_DLG_CLASS(NULL, IDR_MAINFRAME, L"Software\\WTL")
enum { IDD = IDD_MAINDLG };
CStatic m_static_pos; // 座標表示用
virtual BOOL PreTranslateMessage(MSG* pMsg){
return CWindow::IsDialogMessage(pMsg);
}
virtual BOOL OnIdle(){
return FALSE;
}
BEGIN_UPDATE_UI_MAP(CSampleProjectDialog)
END_UPDATE_UI_MAP()
BEGIN_MSG_MAP(CSampleProjectDialog)
MSG_WM_LBUTTONDOWN(OnLButtonDown)
MSG_WM_INITDIALOG(OnInitDialog)
MSG_WM_DESTROY(OnDestroy)
CHAIN_MSG_MAP(CUpdateUI<CSampleProjectDialog>)
CHAIN_MSG_MAP(CAppStdDialogImpl<CSampleProjectDialog>)
END_MSG_MAP()
void OnLButtonDown(UINT nFlags, CPoint point){
SHRGINFO info;
info.cbSize = sizeof(SHRGINFO);
info.hwndClient = m_hWnd;
info.ptDown.x = point.x;
info.ptDown.y = point.y;
info.dwFlags = SHRG_RETURNCMD;
if(SHRecognizeGesture(&info) == GN_CONTEXTMENU){
CString strPos;
strPos.Format(_T("X座標: %d, Y座標: %d"), point.x, point.y);
m_static_pos.SetWindowText(strPos);
}
}
BOOL OnInitDialog(CWindow wndFocus, LPARAM lInitParam){
AtlCreateEmptyMenuBar(m_hWnd);
// コントロール設定
m_static_pos = GetDlgItem(IDC_STATIC_POS);
// メッセージループにメッセージフィルタとアイドルハンドラを追加
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
SetMsgHandled(false);
return TRUE;
}
void OnDestroy(){
// メッセージループからメッセージフィルタとアイドルハンドラを削除
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->RemoveMessageFilter(this);
pLoop->RemoveIdleHandler(this);
}
};
|
// SampleProject.cpp
#include "stdafx.h"
#include "resourceppc.h"
#include "SampleProjectDialog.h"
CAppModule _Module;
int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
HRESULT hRes =
CSampleProjectDialog::ActivatePreviousInstance(hInstance, lpstrCmdLine);
if(FAILED(hRes) || S_FALSE == hRes){
return hRes;
}
hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));
AtlInitCommonControls(ICC_DATE_CLASSES);
SHInitExtraControls();
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
int nRet = CSampleProjectDialog::AppRun(lpstrCmdLine, nCmdShow);
_Module.Term();
::CoUninitialize();
return nRet;
}
|
まず、リソースを作成します。ダイアログに各スタティックコントロールを配置し、
それぞれの[ID]と[Caption]を次のように設定します。
| コントロール名 |
ID |
Caption |
備考 |
| スタティック |
IDC_STATIC |
タップ位置 : |
|
| スタティック |
IDC_STATIC_POS |
|
座標表示用 |
次に、stdafx.hヘッダでは、CStaticクラスを使用するためにatlctrls.hヘッダをインクルードします。
CSampleProjectDialogクラスでは、まず、座標表示用にCStaticクラスのインスタンスをメンバ変数として宣言します。
これを使うためには、WM_INITDIALOGメッセージハンドラでコントロールのハンドルを代入する必要があります。
最後に、WM_LBUTTONDOWNメッセージハンドラを追加し、
クライアント領域上でタップした位置をスタティックコントロールのキャプションとして設定します。
|