// MainWindow.h
#pragma once
class CMainWindow : public CWindowImpl<CMainWindow>,
public CMessageFilter, public CIdleHandler
{
public:
// ウィンドウクラス名、メニューバーを登録
static CWndClassInfo& GetWndClassInfo()
{
static CWndClassInfo wc =
{
{
sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
StartWindowProc, 0, 0, NULL, NULL, NULL, (HBRUSH)(COLOR_WINDOW + 1),
MAKEINTRESOURCE(IDR_MAINFRAME), // メニューバー
_T("Hello"), NULL
},
NULL, NULL, IDC_ARROW, TRUE, 0, _T("")
};
return wc;
}
virtual BOOL PreTranslateMessage(MSG* pMsg){
return FALSE;
}
virtual BOOL OnIdle(){
return FALSE;
}
BEGIN_MSG_MAP(CMainWindow)
MSG_WM_PAINT(OnPaint)
MSG_WM_CONTEXTMENU(OnContextMenu)
MSG_WM_CREATE(OnCreate)
MSG_WM_DESTROY(OnDestroy)
COMMAND_ID_HANDLER_EX(ID_APP_EXIT, OnFileExit)
END_MSG_MAP()
void OnPaint(CDCHandle /*dc*/){
CPaintDC dc(m_hWnd);
CRect rect;
GetClientRect(rect);
dc.DrawText(_T("Hello, ATL/WTL"), -1,
rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
void OnContextMenu(CWindow wnd, CPoint point){
// [Shift]+[F10]キーが押された場合は座標をクライアント領域の左上に設定
if(point.x == -1 && point.y == -1){
point.SetPoint(0, 0);
ClientToScreen(&point);
}
// 座標がクライアント領域内の場合のみポップアップメニューを表示
CRect rc;
GetClientRect(&rc);
ClientToScreen(&rc);
if(rc.PtInRect(point)){
CMenu menuPopup;
menuPopup.LoadMenu(IDR_MENU_POPUP);
menuPopup.GetSubMenu(0).TrackPopupMenu(
TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON,
point.x, point.y, m_hWnd);
}else{
SetMsgHandled(false);
}
}
int OnCreate(LPCREATESTRUCT lpCreateStruct){
// メッセージループにメッセージフィルタとアイドルハンドラを追加
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
return 0;
}
void OnDestroy(){
// メッセージループからメッセージフィルタとアイドルハンドラを削除
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->RemoveMessageFilter(this);
pLoop->RemoveIdleHandler(this);
PostQuitMessage(0);
}
void OnFileExit(UINT uNotifyCode, int nID, CWindow wndCtl){
PostMessage(WM_CLOSE);
}
};
|