1.10 开发技巧与难点分析(4)
(14)在对话框的WM_SIZE消息中添加代码,当对话框的大小改变时重新绘制对话框的标题、边框及按钮。代码如下:
- CPoint pos(30,1);
- m_MinRect.CopyRect(CRect(m_RTitleRc.left+pos.x
,(m_TitleDrawHeight - +2*m_BorderHeight -m_ButtonHeight)/2+pos.y,
m_ButtonWidth,m_ButtonHeight)); //最小化按钮区域 - pos.x = 55;
- m_MaxRect.CopyRect(CRect(m_RTitleRc.left+pos.x,
(m_TitleDrawHeight - +2*m_BorderHeight -m_ButtonHeight)/2+pos.y,m_
ButtonWidth,m_ButtonHeight)); //最大化按钮区域 - pos.x = 80;
- m_CloseRect.CopyRect(CRect(m_RTitleRc.left+pos.x,
(m_TitleDrawHeight - +2*m_BorderHeight -m_ButtonHeight)/2+pos.y,m_
ButtonWidth,m_ButtonHeight)); //关闭按钮区域 - Invalidate();
(15)处理对话框的WM_NCMOUSEMOVE消息,当鼠标移动到标题栏按钮上时,绘制标题栏按钮的热点效果;当鼠标离开标题栏按钮时,恢复标题栏按钮原来的效果。代码如下:
- void CCaptureDlg::OnNcMouseMove(UINT nHitTest, CPoint point)
- {
- CRect tempMin,tempMax,tempClose,ClientRect;
- CWindowDC WindowDC (this);
//CWindowDC类对象 - CDC memDC;
- memDC.CreateCompatibleDC(&WindowDC);
//创建内存设备环境 - BITMAPINFO bInfo;
- CBitmap LeftLine;
- int x,y;
- GetWindowRect(ClientRect);
- tempMin.CopyRect(CRect(m_MinRect.left+ ClientRect.left,ClientRect.top
- +m_MinRect.top,m_MinRect.right+m_MinRect.left+ ClientRect.left,
- m_MinRect.bottom+m_MinRect.top+ClientRect.top));
//设置最小化按钮区域 - tempMax.CopyRect(CRect(m_MaxRect.left+ ClientRect.left,ClientRect.top
- +m_MaxRect.top,m_MaxRect.right+m_MaxRect.left+ ClientRect.left,
- m_MaxRect.bottom+m_MaxRect.top+ClientRect.top));
//设置最大化按钮区域 - tempClose.CopyRect(CRect(m_CloseRect.left+ ClientRect.left,ClientRect.top
- +m_CloseRect.top,m_CloseRect.right+m_CloseRect.left+ ClientRect.left,
- m_CloseRect.bottom+m_CloseRect.top+ClientRect.top));
//设置关闭按钮区域 - if(tempMin.PtInRect(point))
//鼠标在最小化按钮上移动时,更改按钮显示的位图 - {
- if(m_ButtonState != bsMin)
- {
- LeftLine.LoadBitmap(IDB_MINHOTBT) ;
//加载最小化按钮位图资源 - LeftLine.GetObject(sizeof(bInfo),&bInfo);
- x = bInfo.bmiHeader.biWidth;
//获得位图宽度 - y = bInfo.bmiHeader.biHeight;
//获得位图高度 - memDC.SelectObject(&LeftLine);
- WindowDC.StretchBlt(m_MinRect.left,
m_MinRect.top,m_MinRect.right, - m_MinRect.bottom,&memDC,0,0,x,y,SRCCOPY);
//绘制最小化按钮 - m_IsDrawForm = FALSE;
- m_ButtonState = bsMin;
- LeftLine.Detach();
- }
- }
- else if(tempMax.PtInRect(point))
- {
- if(m_ButtonState!=bsMax && m_ButtonState!=bsRes)
- {
- LeftLine.LoadBitmap(IDB_MAXHOTBT);
//加载最大化按钮位图资源 - LeftLine.GetObject(sizeof(bInfo),&bInfo);
- x = bInfo.bmiHeader.biWidth;
//获得位图宽度 - y = bInfo.bmiHeader.biHeight;
//获得位图高度 - memDC.SelectObject(&LeftLine);
- WindowDC.StretchBlt(m_MaxRect.left,m_MaxRect.
top,m_MaxRect.right, - m_MaxRect.bottom,&memDC,0,0,x,y,SRCCOPY);
//绘制最大化按钮 - m_IsDrawForm = FALSE;
- if (m_IsMax)
- {
- m_ButtonState = bsMax;
- }
- else
- {
- m_ButtonState = bsRes;
- }
- LeftLine.Detach();
- }
- }
- else if(tempClose.PtInRect(point))
- {
- if(m_ButtonState != bsClose)
- {
- LeftLine.LoadBitmap(IDB_CLOSEHOTBT);
//加载关闭按钮位图资源 - LeftLine.GetObject(sizeof(bInfo),&bInfo);
- x = bInfo.bmiHeader.biWidth;
//获得位图宽度 - y = bInfo.bmiHeader.biHeight;
//获得位图高度 - memDC.SelectObject(&LeftLine);
- WindowDC.StretchBlt(m_CloseRect.left,
m_CloseRect.top,m_CloseRect.right, - m_CloseRect.bottom,&memDC,0,0,x,y,SRCCOPY); //绘制关闭按钮
- m_IsDrawForm = FALSE;
- m_ButtonState = bsClose;
- LeftLine.Detach();
- }
- }
- else
- {
- if(m_IsDrawForm == FALSE)
- {
- if(m_ButtonState == bsMin)
- DrawDialog( FMINBUTTON);
//重绘最小化按钮 - else if(m_ButtonState == bsClose)
- DrawDialog( FCLOSEBUTTON); //重绘关闭按钮
- else if(m_ButtonState == bsMax ||
m_ButtonState==bsRes) - DrawDialog( FMAXBUTTON); //重绘最大化按钮
- }
- m_ButtonState = bsNone;
- }
- LeftLine.DeleteObject();
- ReleaseDC(&memDC);
- CDialog::OnNcMouseMove(nHitTest, point);
- }