#define ID_BUTTONS 501
CToolBar m_toolbar; //工具栏
CImageList m_imageList; //工具栏图片
CImageList m_hotImageList; //工具栏热点图片
CReBar m_Rebar; //可以在位图上显示子窗口口 用来显示背景
CString str; //用于鼠标停留时的消息
//大小要一致
m_imageList.Create(70, 70, ILC_COLOR24 | ILC_MASK, 1, 1);
m_hotImageList.Create(70, 70, ILC_COLOR24 | ILC_MASK, 1, 1);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP2);
m_imageList.Add(&bmp, RGB(255, 255, 255));
bmp.DeleteObject();
bmp.LoadBitmap(IDB_BITMAP3);
m_hotImageList.Add(&bmp, RGB(255, 255, 255));
bmp.DeleteObject();
//按钮ID列表
UINT nArray[1];
nArray[0] = ID_BUTTONS;
m_toolbar.CreateEx(this);
m_toolbar.EnableToolTips(); //开启消息提示
m_toolbar.SetButtons(nArray,1); //注入按钮
m_toolbar.SetButtonText(0, _T("0")); //设置索引为0的按钮文本为"0"
m_toolbar.SetSizes(CSize(90, 90),CSize(80, 80)); //设置工具栏的大小 前一个CSize必须比后一个大!!!
m_toolbar.GetToolBarCtrl().SetImageList(&m_imageList); //设置图片列表
m_toolbar.GetToolBarCtrl().SetHotImageList(&m_hotImageList); //设置热点图片列表
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST0,0); //自适应放置工具栏
先在Dlg.h中增加函数声明
BOOL OnDisplay(UINT id,NMHDR* pNMHDR,LRESULT* pResult);
再Dlg.cpp中实现
TOOLTIPTEXT *pTTT=(TOOLTIPTEXT *)pNMHDR;
UINT nID=pNMHDR->idFrom;//获取工具栏按钮ID
UINT nIndex=m_toolbar.CommandToIndex(nID);//根据按钮ID获取索引
str.Format(_T("工具栏按钮%d"),nIndex);
pTTT->lpszText=str.GetBuffer(str.GetLength());//设置按钮提示信息
pTTT->hinst=AfxGetResourceHandle();
return TRUE;
增加消息映射宏开启提示
ON_NOTIFY_EX(TTN_NEEDTEXT,0,OnDisplay)
#define MESSAGE_CAD WM_USER+100
#define ID_CHILD_BUTTON_CAD 1001
CToolBar m_child_toolbar;
CImageList m_child_imageList;
void WorkForCAD();
m_child_imageList.Create(50,28,ILC_COLOR24 | ILC_MASK , 1, 1);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP2);
m_child_imageList.Add(&bmp,RGB(255,255,255));
bmp.DeleteObject();
UINT nArray[1];
for(int i = 0 ; i < 1; i++){
nArray[1] = ID_CHILD_BUTTON_CAD + i;
}
m_child_toolbar.CreateEx(this);
m_child_toolbar.SetButtons(nArray,1);
m_child_toolbar.SetButtonText(0, _T("0"));
m_child_toolbar.SetSizes(CSize(80,80),CSize(70,70));
m_child_toolbar.GetToolBarCtrl().SetImageList(&m_child_imageList);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
void OnBnClickForCAD1();
CString s = _T("成功");
//向主界面发送MESSAGE_CAD消息
::PostMessage(AfxGetMainWnd()->m_hWnd , MESSAGE_CAD,(WPARAM)_T(""),(LPARAM)&s);
ON_COMMAND(ID_CHILD_BUTTON_CAD,&CChildDlg::OnBnClickForCAD1)
#include "ChildDlg.h"
friend UINT ThreadForCAD(LPVOID lpParameter);
CMyToolbarDlg* pDlg = (CMyToolbarDlg*)lpParameter;
CChildDlg* pTestDlg = new CChildDlg();
pTestDlg->Create(IDD_DIALOG1,NULL);
pTestDlg->SetWindowTextW((LPCTSTR)_T("0"));
pTestDlg->WorkForCAD();
pTestDlg->MoveWindow(680,420,600,200,FALSE);
pTestDlg->ShowWindow(SW_SHOW);
MSG msg = {0};
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#define MESSAGE_CAD WM_USER+100
if(message == MESSAGE_CAD){
AfxMessageBox(_T("成功"));
//接收到消息后就发送关闭线程消息,将窗口关闭
PostThreadMessage(m_thread->m_nThreadID,WM_QUIT,0,0);
}
return CDialogEx::WindowProc(message,wParam,lParam);
CEvent g_hEvent;
CWinThread* m_thread;
ON_COMMAND(ID_BUTTONS,&CMyToolbarDlg::OnBnClickForCAD);
SetEvent(g_hEvent);
if(m_thread==NULL){
m_thread = AfxBeginThread(ThreadForCAD, (LPVOID)this,THREAD_PRIORITY_NORMAL,0 , NULL,NULL);
}
else{
//如果上一个线程还没有关闭,则发送关闭线程消息,先将前一个窗口关闭,再生成新的窗口
PostThreadMessage(m_thread->m_nThreadID,WM_QUIT,0,0);
m_thread = AfxBeginThread(ThreadForCAD, (LPVOID)this,THREAD_PRIORITY_NORMAL,0 , NULL,NULL);
}
WaitForSingleObject(g_hEvent,INFINITE);
MSG msg;
while(::PeekMessageA(&msg, m_hWnd,WM_USER + 100, WM_USER +100,PM_REMOVE)){
if(msg.message == MESSAGE_CAD){
WindowProc(msg.message,msg.wParam,msg.lParam);
break;
}
}
ON_COMMAND(ID_BUTTONS,&CMyToolbarDlg::OnBnClickForCAD);
TIP: