用VC++6.0为对话框添加最小化按钮和提示条
{
CDC* pDC = GetWindowDC();
CDC* pMemDC=new CDC;
CBitmap *pBitmap1=new CBitmap;
CBitmap *pBitmap2=new CBitmap;
CBitmap *pOldBitmap;
CRect rtWnd, rtButtons, rtTitle;
//创建一个内存设备环境
pMemDC->CreateCompatibleDC(pDC);
if(!pDC) return;
GetWindowRect(&rtWnd);
//取得标题栏的矩形
rtTitle.left = GetSystemMetrics(SM_CXFRAME);
rtTitle.top = GetSystemMetrics(SM_CYFRAME);
rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
//计算最小化按钮的位置,位图大小为15X15
rtButtons.top=rtTitle.top+1;
rtButtons.bottom=rtButtons.top+15;
rtButtons.right = rtTitle.right-20;
rtButtons.left = rtButtons.right-15;
m_rtButtMin = rtButtons;
pBitmap1->LoadBitmap(IDB_BITMAP1); //调入正常状态按钮的位图
pBitmap2->LoadBitmap(IDB_BITMAP2); //调入按钮按下状态的位图
m_rtButtMin.OffsetRect(rtWnd.TopLeft());//记录最小化按钮的位置
//将按钮位图调入内存设备环境中,m_BMPPressed为TRUE
//调入正常状态按钮的位图
pOldBitmap=pMemDC->SelectObject(m_BMPPressed ? pBitmap1: pBitmap2);
//将按钮位图复制到实际的内存设备环境中
pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(),
rtButtons.Height(), pMemDC, 0, 0, SRCCOPY);
pMemDC->SelectObject(pOldBitmap);//恢复原来的内存设备环境
//释放资源
ReleaseDC(pMemDC);
ReleaseDC(pDC);
delete pMemDC;
delete pBitmap1;
delete pBitmap2;
}
⑷ 对应鼠标左键的WM_NCLBUTTONDOWN消息处理函数
void CMinButtonDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
CDialog::OnNcLButtonDown(nHitTest, point);
//检测是否在最小化按钮区域内按下按钮
if (m_rtButtMin.PtInRect(point))
{
m_BMPPressed=FALSE;//绘制按钮按下时的状态
DrawButton(); //绘制最小化按钮
m_BMPPressed=TRUE;//绘制按钮的正常状态
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y));//发送最小化消息
}
}
⑸ //在最小化按钮区域实现提示文本
BOOL CToolTips::AddToolTip( UINT nID, LPRECT lpRect, LPCTSTR lpszText )
{
TOOLINFO Tips;
memset(&Tips, 0, sizeof(TOOLINFO));
Tips.cbSize = sizeof(TOOLINFO);
Tips.hwnd = GetOwner()->GetSafeHwnd();
Tips.uFlags = TTF_SUBCLASS;
Tips.uId = nID;
memcpy(&Tips.rect, lpRect, sizeof(RECT));
Tips.lpszText = (LPSTR)lpszText
return (BOOL)SendMessage(TTM_ADDTOOL, 0, (LPARAM)&Tips);
}
⑹ 在对话框初始化函数OnInitDialog()中添下列代码:
m_ToolTip.Create(this,TTS_ALWAYSTIP);//创建工具提示控件,TTS_ALWAYSTIP表示,//不论控件所属窗体是否活动,提示条始终有效
GetMinButRect();
ScreenToClient(&m_ButtMinTips);//屏幕坐标转换为窗体坐标
m_ToolTip.AddToolTip(1,&m_ButtMinTips,"最小化");
4 结束语
实例程序的测试环境为Windows 98、PⅡ350,效果良好。