1. 为对话框添加工具栏。
为 CPropertyPage 或者CDialog 的 派生类添加一个成员 CToolBar m_ToolBar. CImageList m_imageList;CBitmap m_toolBarBitmap;
OnInitDialog()中加入:
m_ToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_TOOLTIPS, CRect(0,0,0,0));
// 使用 24位的图片代替工具栏的图片。 将白色作为工具栏底色。
m_ToolBar.LoadToolBar(IDR_TOOLBAR_SCANDATA_IMPORT_DELETE);
//m_ToolBar.SetBitmap((HBITMAP)m_toolBarBitmap);
m_ToolBar.SetSizes(CSize(24 + 7, 24 + 6), CSize(24, 24));
m_toolBarBitmap.LoadBitmap(IDB_TOOLBAR_SCANDATA_IMPORT_DELETE);
m_imageList.Create(24, 24, ILC_COLOR24|ILC_MASK, 5, 1);
m_imageList.Add(&m_toolBarBitmap, RGB(255, 255, 255));
m_ToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)m_imageList.m_hImageList);
// 更新其他控件的位置。
CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);
// To reposition and resize the control bar
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);
CPoint ptOffset(rcClientNow.left - rcClientStart.left,rcClientNow.top-rcClientStart.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
MoveWindow(rcWindow, FALSE);
// And position the control bars
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
// 设置自己的按钮属性。任意设置
m_ToolBar.SetButtonStyle(0, TBBS_CHECKGROUP);
m_ToolBar.SetButtonStyle(1, TBBS_CHECKGROUP);
m_ToolBar.SetButtonStyle(2, TBBS_CHECKGROUP);
m_ToolBar.SetButtonStyle(3, TBBS_SEPARATOR);
m_ToolBar.SetButtonStyle(4, TBBS_CHECKGROUP);
m_ToolBar.SetButtonStyle(5, TBBS_CHECKGROUP);
2. 添加tool tip
添加消息:
afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);
// allow top level routing frame to handle the message
if (GetRoutingFrame() != NULL)
return FALSE;
// need to handle both ANSI and UNICODE versions of the message
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
TCHAR szFullText[256];
CString cstTipText;
CString cstStatusText;
UINT nID = pNMHDR->idFrom;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
// idFrom is actually the HWND of the tool
nID = ((UINT)(WORD)::GetDlgCtrlID((HWND)nID));
}
if (nID != 0) // will be zero on a separator
{
AfxLoadString(nID, szFullText);
// this is the command id, not the button index
AfxExtractSubString(cstTipText, szFullText, 1, '/n');
AfxExtractSubString(cstStatusText, szFullText, 0, '/n');
}
// Non-UNICODE Strings only are shown in the tooltip window...
if (pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn((TCHAR*)(pTTTA->szText), cstTipText,
(sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0])));
else
{
char szStr[256] = {0};
wcstombs(szStr, cstTipText, cstTipText.GetLength());
const char * p = szStr;
_mbstowcsz(pTTTW->szText, p,
(sizeof(pTTTW->szText)/sizeof(pTTTW->szText[0])));
}
*pResult = 0;
// bring the tooltip window above other popup windows
::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
return TRUE; // message was handled
}
在工具栏的资源文件中加入tool tip信息即可。
3 加入 update command ui.
对于普通的CDialog 应用,创建一个CToolBar的派生类, 添加消息
LRESULT OnIdleUpdateCmdUI(WPARAM wParam, LPARAM);
BEGIN_MESSAGE_MAP(CInSpecDlgToolBar, CToolBar)
ON_MESSAGE(WM_IDLEUPDATECMDUI, OnIdleUpdateCmdUI)
END_MESSAGE_MAP()
// CInSpecDlgToolBar message handlers
LRESULT CInSpecDlgToolBar::OnIdleUpdateCmdUI(WPARAM wParam, LPARAM)
{
if (IsWindowVisible())
{
CFrameWnd *pParent = (CFrameWnd *)GetParent();
if (pParent)
OnUpdateCmdUI(pParent, (BOOL)wParam);
}
return 0L;
}
本文介绍如何为CDialog或CPropertyPage派生类添加工具栏,包括设置工具栏样式、加载自定义图标、调整布局以及实现工具提示和命令更新等功能。
1230

被折叠的 条评论
为什么被折叠?



