功能是把标题栏的颜色改变,可是我在XP下运行,出来的不是整条标题栏变色,只是一部分,怎么回事?请指教! #include<afxwin.h> #include<afxext.h>
//派生自己的应用程序类 class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); };
//应用程序主窗口类 class CMyWnd : public CFrameWnd { protected:
//声明主窗口的消息处理函数 afx_msg void OnNcPaint(); afx_msg BOOL OnNcActivate(BOOL bActive); DECLARE_MESSAGE_MAP(); public: void PaintTitleBar(BOOL bActive); };
//初始化应用程序的实例 BOOL CMyApp::InitInstance() { //创建应用程序主窗口 CMyWnd *pWnd = new CMyWnd; pWnd->Create(NULL,"具有五彩标题条的窗口");
//显示应用程序主窗口,并更新客户区 pWnd->ShowWindow(SW_SHOW); pWnd->UpdateWindow(); m_pMainWnd = pWnd; return TRUE; }
//声明应用程序对象 CMyApp MyApp;
//应用程序主窗口的消息映射 BEGIN_MESSAGE_MAP(CMyWnd, CWnd) ON_WM_NCPAINT() ON_WM_NCACTIVATE() END_MESSAGE_MAP()
//绘制窗口的标题条,参数bActive代表窗口的当前激活状态 void CMyWnd::PaintTitleBar(BOOL bActive) { //创建代表整个窗口的现实设备上下对象 CWindowDC dc(this); CRect rc;
//获得窗口矩形及宽度 GetWindowRect(rc); UINT nWidth = rc.Width();
//获得窗口边框的度量 UINT nXFrame = GetSystemMetrics(SM_CXSIZEFRAME); UINT nYFrame = GetSystemMetrics(SM_CYSIZEFRAME);
//获得窗口标题条的高度 UINT nYCaption = GetSystemMetrics(SM_CYCAPTION); COLORREF cr; if(bActive) { //获得当窗口处于激活状态时其标题条的颜色 cr = GetSysColor(COLOR_ACTIVECAPTION); //按从红到绿,再到蓝的渐变规律绘制标题条 for(UINT j=nYFrame; j<=nYFrame+nYCaption; j++) { for(UINT i=nXFrame; i<=nWidth/2; i++) { UINT nLen = nWidth/2 - nXFrame + 1; if(dc.GetPixel(i,j)==cr) { dc.SetPixelV(i,j, RGB(255-255*(i-nXFrame)/nLen,255*(i-nXFrame)/nLen,0)); } }
for(i=nWidth/2+1; i<=nWidth-nXFrame; i++) { UINT nLen = nWidth/2 - nXFrame - 1 + i; if(dc.GetPixel(i,j)==cr) { dc.SetPixelV(i,j, RGB(0,255-255*(i-nWidth/2-1)/nLen,255*(i-nWidth/2-1)/nLen)); } } } } else { //获得窗口处于非激活状态时的标题条颜色 cr = GetSysColor(COLOR_INACTIVECAPTION);
//按从黑到灰,再到黑的渐变规律绘制标题条 for(UINT j=nYFrame; j<=nYFrame+nYCaption-1; j++) { for(UINT i=nXFrame; i<=nWidth/2; i++) { UINT nLen = nWidth/2 - nXFrame; if(dc.GetPixel(i,j)==cr) { dc.SetPixelV(i,j,RGB(192*(i-nXFrame)/nLen, 192*(i-nXFrame)/nLen,192*(i-nXFrame)/nLen)); } } for(i=nWidth/2+1; i<=nWidth-nXFrame; i++) { UINT nLen = nWidth/2 - nXFrame - 1; if(dc.GetPixel(i,j)==cr) { dc.SetPixelV(i,j,RGB(192-192*(i-nWidth/2)/nLen, 192-192*(i-nWidth/2)/nLen,192-192*(i-nWidth/2)/nLen)); } } } } }
//消息WM_NCPAINT的处理成员函数 void CMyWnd::OnNcPaint() { CFrameWnd::OnNcPaint();
//检查当前窗口是否为激活窗口 if(::GetActiveWindow()==GetSafeHwnd()) PaintTitleBar(TRUE); else PaintTitleBar(FALSE); }
//消息WM_NCACTIVE的处理成员函数 BOOL CMyWnd::OnNcActivate(BOOL bActive) { CFrameWnd::OnNcActivate(bActive);
//根据不同的激活状态按不同的方式绘制窗口标题条 if(bActive) { PaintTitleBar(TRUE); return FALSE; } else { PaintTitleBar(FALSE); return FALSE; } }