CTrueToolBar类(一位大鸟写的)已经封装了真彩色工具栏的创建,所需要做的是在对话框里面显示工具栏,在对话框的OnInitDialog函数里添加以下代码
if (!m_colorToolBar.CreateEx(this) ||!m_colorToolBar.LoadToolBar(IDR_MAINFRAME))
?{
??TRACE0("Failed to create toolbar/n");
??return -1;????? // fail to create
?}
?m_colorToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS); //设置工具栏的下拉箭头风格
?m_colorToolBar.LoadTrueColorToolBar( 24, IDB_TOOLBAR_NORMAL, IDB_TOOLBAR_NORMAL, IDB_TOOLBAR_DISABLED);//加载真彩色图片资源
?m_colorToolBar.SetButtonInfo(0,ID_TOOLBAR_BTN_NEW,TBSTYLE_BUTTON | TBSTYLE_DROPDOWN,0);//设置工具栏的第一个按钮(ID_TOOLBAR_BTN_NEW)显示下拉箭头,最后一个参数指定按钮的图片序号
?CRect rcClient;
?CRect rcChild;
?RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClient);//预定位工具栏,但是还没有显示工具栏,将工具栏的预定位置信息保存在rcClient里
?CWnd *pwnd=(CWnd *)GetDlgItem(IDC_MSG_SHOW);
?pwnd->GetWindowRect(rcChild);
?rcClient.top += rcChild.Height() -2;
?rcClient.bottom = rcClient.top + 32;
?rcClient.left += 5;
?m_colorToolBar.MoveWindow(rcClient,false);//把工具栏移至任何想要放置的位置并显示
附录:CTrueToolBar类的实现代码
/***=========================================================================
====???????????????????????????????????????????????????????????????????? ====
====????????????????????????? D C U t i l i t y????????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
=============================================================================
====???????????????????????????????????????????????????????????????????? ====
====??? File name?????????? :? TrueColorToolBar.h??????????????????????? ====
====??? Project name??????? :? Tester??????????????????????????????????? ====
====??? Project number????? :? ---?????????????????????????????????????? ====
====??? Creation date?????? :? 13/1/2003???????????????????????????????? ====
====??? Author(s)?????????? :? Dany Cantin?????????????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
====????????????????? Copyright ?DCUtility? 2003??????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
=============================================================================
===========================================================================*/
#ifndef TRUECOLORTOOLBAR_H_
#define TRUECOLORTOOLBAR_H_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include
/////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar
class CTrueColorToolBar : public CToolBar
{
// Construction
public:
?CTrueColorToolBar();
// Attributes
private:
?BOOL m_bDropDown;
?struct stDropDownInfo {
?public:
??UINT? uButtonID;
??UINT? uMenuID;
??CWnd* pParent;
?};
?
?CArray m_lstDropDownButton;
?
// Operations
public:
?BOOL LoadTrueColorToolBar(int? nBtnWidth,
???????? UINT uToolBar,
???????? UINT uToolBarHot??= 0,
???????? UINT uToolBarDisabled = 0);
?void AddDropDownButton(CWnd* pParent, UINT uButtonID, UINT uMenuID);
private:
?BOOL SetTrueColorToolBar(UINT uToolBarType,
?????????????????????? UINT uToolBar,
?????????? int? nBtnWidth);
// Overrides
?// ClassWizard generated virtual function overrides
?//{{AFX_VIRTUAL(CTrueColorToolBar)
?//}}AFX_VIRTUAL
// Implementation
public:
?virtual ~CTrueColorToolBar();
?// Generated message map functions
protected:
?//{{AFX_MSG(CTrueColorToolBar)
?afx_msg void OnToolbarDropDown(NMTOOLBAR* pnmh, LRESULT* plRes);
?//}}AFX_MSG
?DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // TRUECOLORTOOLBAR_H_
/***=========================================================================
====???????????????????????????????????????????????????????????????????? ====
====????????????????????????? D C U t i l i t y????????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
=============================================================================
====???????????????????????????????????????????????????????????????????? ====
====??? File name?????????? :? TrueColorToolBar.cpp????????????????????? ====
====??? Project name??????? :? Tester??????????????????????????????????? ====
====??? Project number????? :? ---?????????????????????????????????????? ====
====??? Creation date?????? :? 13/1/2003???????????????????????????????? ====
====??? Author(s)?????????? :? Dany Cantin?????????????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
====????????????????? Copyright ?DCUtility? 2003??????????????????????? ====
====???????????????????????????????????????????????????????????????????? ====
=============================================================================
===========================================================================*/
#include "stdafx.h"
#include "TrueColorToolBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar
CTrueColorToolBar::CTrueColorToolBar()
{
?m_bDropDown = FALSE;
}
CTrueColorToolBar::~CTrueColorToolBar()
{
}
BEGIN_MESSAGE_MAP(CTrueColorToolBar, CToolBar)
?//{{AFX_MSG_MAP(CTrueColorToolBar)
?ON_NOTIFY_REFLECT(TBN_DROPDOWN, OnToolbarDropDown)
?//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTrueColorToolBar message handlers
BOOL CTrueColorToolBar::LoadTrueColorToolBar(int? nBtnWidth,
??????????? UINT uToolBar,
??????????? UINT uToolBarHot,
??????????? UINT uToolBarDisabled)
{
?if (!SetTrueColorToolBar(TB_SETIMAGELIST, uToolBar, nBtnWidth))
??return FALSE;
?
?if (uToolBarHot) {
??if (!SetTrueColorToolBar(TB_SETHOTIMAGELIST, uToolBarHot, nBtnWidth))
???return FALSE;
?}
?if (uToolBarDisabled) {
??if (!SetTrueColorToolBar(TB_SETDISABLEDIMAGELIST, uToolBarDisabled, nBtnWidth))
???return FALSE;
?}
?return TRUE;
}
BOOL CTrueColorToolBar::SetTrueColorToolBar(UINT uToolBarType,
??????????? ???????? UINT uToolBar,
????????????? int? nBtnWidth)
{
?CImageList?cImageList;
?CBitmap??cBitmap;
?BITMAP??bmBitmap;
?
?if (!cBitmap.Attach(LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uToolBar),
????????? IMAGE_BITMAP, 0, 0,
????????? LR_DEFAULTSIZE|LR_CREATEDIBSECTION)) ||
???? !cBitmap.GetBitmap(&bmBitmap))
??return FALSE;
?CSize??cSize(bmBitmap.bmWidth, bmBitmap.bmHeight);
?int???nNbBtn?= cSize.cx/nBtnWidth;
?RGBTRIPLE*?rgb??= (RGBTRIPLE*)(bmBitmap.bmBits);
?COLORREF?rgbMask?= RGB(rgb[0].rgbtRed, rgb[0].rgbtGreen, rgb[0].rgbtBlue);
?
?if (!cImageList.Create(nBtnWidth, cSize.cy, ILC_COLOR24|ILC_MASK, nNbBtn, 0))
??return FALSE;
?
?if (cImageList.Add(&cBitmap, rgbMask) == -1)
??return FALSE;
?SendMessage(uToolBarType, 0, (LPARAM)cImageList.m_hImageList);
?cImageList.Detach();
?cBitmap.Detach();
?
?return TRUE;
}
void CTrueColorToolBar::AddDropDownButton(CWnd* pParent, UINT uButtonID, UINT uMenuID)
{
?if (!m_bDropDown) {
??GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_DRAWDDARROWS);
??m_bDropDown = TRUE;
?}
?SetButtonStyle(CommandToIndex(uButtonID), TBSTYLE_DROPDOWN);
?stDropDownInfo DropDownInfo;
?DropDownInfo.pParent?= pParent;
?DropDownInfo.uButtonID?= uButtonID;
?DropDownInfo.uMenuID?= uMenuID;
?m_lstDropDownButton.Add(DropDownInfo);
}
void CTrueColorToolBar::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)
{
?for (int i = 0; i < m_lstDropDownButton.GetSize(); i++) {
??
??stDropDownInfo DropDownInfo = m_lstDropDownButton.GetAt(i);
??if (DropDownInfo.uButtonID == UINT(pnmtb->iItem)) {
???CMenu menu;
???menu.LoadMenu(DropDownInfo.uMenuID);
???CMenu* pPopup = menu.GetSubMenu(0);
???
???CRect rc;
???SendMessage(TB_GETRECT, (WPARAM)pnmtb->iItem, (LPARAM)&rc);
???ClientToScreen(&rc);
???
???pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL,
?????????????????????? rc.left, rc.bottom, DropDownInfo.pParent, &rc);
???break;
??}
?}
}
博客介绍了CTrueToolBar类封装真彩色工具栏的创建,给出在对话框中显示工具栏的代码,包括设置下拉箭头风格、加载图片资源等操作,还提供了CTrueToolBar类的实现代码,涵盖头文件和源文件的具体内容。
2983

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



