2008年(909)
分类:
2008-05-06 21:21:15
下面我们来探讨具体的实现过程
一、定义资源:
添加系统按钮的位图资源(本例中使用十个位图,分别显示帮助、最小化、最大化、恢复及关闭按钮的普通状态和焦点状态,当然可随程序的要求自定义添加)
二、定义全局变量及函数:
CRect m_rtButtExit; //关闭按钮位置 CRect m_rtButtMax; //最大化按钮位置 CRect m_rtButtMin; //最小化按钮位置 CRect m_rtButtHelp; //帮助按钮位置 CRect m_rtIcon; //图标位置 void DrawTitleBar(CDC *pDC); //画非客户区主函数三、函数实现:
void CTitleBarDlg::DrawTitleBar(CDC *pDC) { if (m_hWnd) { CBrush Brush(RGB(0,100,255)); CBrush* pOldBrush = pDC->SelectObject(&Brush); CRect rtWnd, rtTitle, rtButtons; GetWindowRect(&rtWnd); …………………………….. //因代码过长,未贴,见源程序 ……………………………... } }//将消息进行过滤,对可能影响界面初始及更新的消息,产生重画消息执行DrawTitleBar函数
LRESULT CTitleBarDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) { LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam); if (!::IsWindow(m_hWnd)) return lrst; if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT|| message== WM_NCACTIVATE ||message == WM_NOTIFY) { CDC* pWinDC = GetWindowDC(); if (pWinDC) DrawTitleBar(pWinDC); ReleaseDC(pWinDC); } return lrst; }//实现标题栏按钮的鼠标移动效果
void CTitleBarDlg::OnNcMouseMove(UINT nHitTest, CPoint point) { CDC* pDC = GetWindowDC(); if (pDC) { …………………………….. //因代码过长,未贴,见源程序 ……………………………... } ReleaseDC(pDC); CDialog::OnNcMouseMove(nHitTest, point); }//实现非客户区,用户鼠标点击操作
void CTitleBarDlg::OnNcLButtonDown(UINT nHitTest, CPoint point) { //检测各按钮是否按到 if (m_rtIcon.PtInRect(point)) AfxMessageBox("欢迎访问FlySnowStdio主页"); else if (m_rtButtHelp.PtInRect(point)) SendMessage(WM_HELP); else if (m_rtButtExit.PtInRect(point)) SendMessage(WM_CLOSE); else if (m_rtButtMin.PtInRect(point)) SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y)); else if (m_rtButtMax.PtInRect(point)) { if (IsZoomed()) SendMessage(WM_SYSCOMMAND, SC_RESTORE, MAKELPARAM(point.x, point.y)); else { SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, MAKELPARAM(point.x, point.y)); Invalidate(); } } else if (!IsZoomed()) Default(); }四、总结