// mainfrm.h内
class CMainFrame : public CFrameWindowImpl<CMainFrame>,
public CUpdateUI<CMainFrame>, public CMessageFilter, public CIdleHandler
{
public:
DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)
virtual BOOL PreTranslateMessage(MSG* pMsg){
return CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg);
}
virtual BOOL OnIdle(){
UIUpdateToolBar();
return FALSE;
}
BEGIN_UPDATE_UI_MAP(CMainFrame)
// エントリなし
END_UPDATE_UI_MAP()
BEGIN_MSG_MAP_EX(CMainFrame)
MSG_WM_CREATE(OnCreate)
NOTIFY_CODE_HANDLER_EX(TBN_DROPDOWN, OnToolbarDropdown)
COMMAND_ID_HANDLER_EX(ID_BUTTON_MSG, OnButtonMsg)
COMMAND_ID_HANDLER_EX(ID_MENUITEM_HELLOATL, OnMenuHelloATL)
COMMAND_ID_HANDLER_EX(ID_MENUITEM_HELLOWTL, OnMenuHelloWTL)
COMMAND_ID_HANDLER_EX(ID_APP_EXIT, OnMenuExit)
CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
END_MSG_MAP()
LRESULT OnCreate(LPCREATESTRUCT lpcs){
// ツールバーを作成
CreateSimpleToolBar();
UIAddToolBar(m_hWndToolBar);
// ツールバーのスタイル変更
CToolBarCtrl bar = m_hWndToolBar;
bar.SetStyle(bar.GetStyle() | TBSTYLE_FLAT);
bar.SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
// ツールバー上のボタンのスタイルを変更
TBBUTTONINFO bi = {sizeof(bi), TBIF_STYLE};
bar.GetButtonInfo(ID_BUTTON_MSG, &bi);
bi.fsStyle |= TBSTYLE_DROPDOWN;
bar.SetButtonInfo(ID_BUTTON_MSG, &bi);
// メッセージループにメッセージフィルタとアイドルハンドラを追加
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
return 0;
}
LRESULT OnToolbarDropdown(LPNMHDR pnmh){
LPNMTOOLBAR pnmtb = (LPNMTOOLBAR)pnmh;
if(pnmtb->iItem == ID_BUTTON_MSG){
CToolBarCtrl bar = pnmtb->hdr.hwndFrom;
// ドロップダウンメニューを表示する位置を取得
CRect rcButton;
bar.GetRect(ID_BUTTON_MSG, rcButton);
bar.MapWindowPoints(HWND_DESKTOP, rcButton);
TPMPARAMS tpm = {sizeof(tpm)};
tpm.rcExclude = rcButton;
// ドロップダウンメニュー表示
CMenu menuDropdown;
menuDropdown.LoadMenu(IDR_MENU_DROPDOWN);
menuDropdown.GetSubMenu(0).TrackPopupMenuEx(
TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
rcButton.left, rcButton.bottom, m_hWnd, &tpm);
}
return TBDDRET_DEFAULT;
}
void OnButtonMsg(UINT uNotifyCode, int nID, HWND hWndCtl){
MessageBox(_T("Hello World"));
}
void OnMenuHelloATL(UINT uNotifyCode, int nID, HWND hWndCtl){
MessageBox(_T("Hello ATL"));
}
void OnMenuHelloWTL(UINT uNotifyCode, int nID, HWND hWndCtl){
MessageBox(_T("Hello WTL"));
}
void OnMenuExit(UINT uNotifyCode, int nID, HWND hWndCtl){
PostMessage(WM_CLOSE);
}
};
|