如何处理非用户区的消息,列如如果我要在标题栏画上自己的颜色,该怎么办,有人说要处理WM_NCPAINT消息,可是在类向导中找不到这个消息,不知如何处理,能不能给一小段代码的例子。谢谢!

解决方案 »

  1.   

    转的高手的代码:厄,其实画这个不是很难。一般的立体控件,只要你想像左上角有光源照下来就可以了。比如button,左上两个边使用亮色,右下两个边用暗色,这样画出来的一个平面矩形框看起来就是立体的。
    至于标题栏,给你个例子吧,在这个例子里,我在标题栏上多添了一个按钮。
    void CResizableMiniDockFrameWnd::OnNcPaint()
    {
    CWindowDC dc(this); CRect rectClient(0,0,0,0);
    GetClientRect(rectClient);

    CRect rectWindow(0,0,0,0);
    GetWindowRect(rectWindow);
    ScreenToClient(rectWindow);

    rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
    dc.ExcludeClipRect(rectClient);
    rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);

    CSize sizeFrame(GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME));
    CSize sizeBorder(GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER));
    CSize sizeSmIcon(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
    CMemoryDC dcMemory;
    dcMemory.Create(&dc, rectWindow);

    //边框
    dcMemory.DrawEdge(&rectWindow, EDGE_RAISED, BF_RECT);
    CRect rectCaption(rectWindow);
    rectCaption.DeflateRect(sizeFrame.cx, sizeFrame.cy);
    rectCaption.bottom = rectCaption.top + sizeSmIcon.cy - sizeBorder.cy;

    //标题栏底色
    BOOL bGradient = FALSE;
    ::SystemParametersInfo(SPI_GETGRADIENTCAPTIONS, 0, &bGradient, 0);
    COLORREF clrLeft = ::GetSysColor(COLOR_ACTIVECAPTION);
    COLORREF clrRight = ::GetSysColor(COLOR_GRADIENTACTIVECAPTION);
    COLORREF clrGray = ::GetSysColor(COLOR_INACTIVECAPTION);
    if(bGradient && m_bActive)
    {
    int nShift = 6;
    int nSteps = 1 << nShift;
    for(int i=0; i<nSteps; ++i)
    {
    int nRed   = (GetRValue(clrLeft) * (nSteps - i) + GetRValue(clrRight) * i) >> nShift;
    int nGreen = (GetGValue(clrLeft) * (nSteps - i) + GetGValue(clrRight) * i) >> nShift;
    int nBlue  = (GetBValue(clrLeft) * (nSteps - i) + GetBValue(clrRight) * i) >> nShift;

    CRect rect(rectCaption);
    rect.left  = rectCaption.left +((i       * rectCaption.Width()) >> nShift);
    rect.right = rectCaption.left +(((i + 1) * rectCaption.Width()) >> nShift);

    if(rect.Width() > 0)
    {
    dcMemory.FillSolidRect(rect, RGB(nRed, nGreen, nBlue));
    }
    }
    }
    else
    {
    dcMemory.FillSolidRect(&rectCaption, m_bActive ? clrLeft:clrGray);
    }
    LOGFONT fontCaption = {0};
    ::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &fontCaption, 0);
    fontCaption.lfWeight = FW_BOLD;
    HFONT hCaptionFont = CreateFontIndirect(&fontCaption);
    HGDIOBJ hOldFont = dcMemory.SelectObject(hCaptionFont);
    dcMemory.SetTextColor(::GetSysColor(COLOR_CAPTIONTEXT));
    dcMemory.SetBkMode(TRANSPARENT);

    CString strCaption(_T(""));
    GetWindowText(strCaption);
    if(strCaption.IsEmpty())
    {
    strCaption = _T("ToolBar");
    }
    CSize sizeText(dcMemory.GetTextExtent(strCaption));

    //输出文本(右端扣除Button所占的位置)
    CRect rectText(rectCaption);
    rectText.left += sizeBorder.cx * 2;
    rectText.right -= sizeSmIcon.cx * 2 + sizeBorder.cx;
    dcMemory.DrawText(strCaption, &rectText, DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS);
    dcMemory.SelectObject(hOldFont);
    DeleteObject(hCaptionFont);

    DrawButton(&dcMemory, rectCaption);

    dcMemory.Flush();
    }
    void CResizableMiniDockFrameWnd::DrawButton(CDC* pDC, CRect& rectCaption)
    {
    ASSERT_VALID(pDC);

    CRect rectClose = rectCaption;
    rectClose.top += 1;
    rectClose.bottom = rectClose.top + 12;
    rectClose.right -= 1;
    rectClose.left = rectClose.right - 12;

    CRect rectExpand = rectClose;
    rectExpand.OffsetRect(-14,0);

    //background
    pDC->FillSolidRect(&rectClose, afxData.clrBtnFace);
    pDC->FillSolidRect(&rectClose, afxData.clrBtnFace);

    //close button
    DrawFrameControl(pDC->GetSafeHdc(), rectClose, DFC_BUTTON, 
    DFCS_BUTTONPUSH|(m_btnClose.IsPushed()?DFCS_PUSHED:0));
    CPoint pt = rectClose.TopLeft();
    pt.Offset(m_btnClose.IsPushed()?2:1, m_btnClose.IsPushed()?2:1);
    m_ImageList.Draw(pDC, m_btnClose.GetIcon(), pt, ILD_TRANSPARENT); //expand button
    DrawFrameControl(pDC->GetSafeHdc(), rectExpand, DFC_BUTTON,
    DFCS_BUTTONPUSH|(m_btnExpand.IsPushed()?DFCS_PUSHED:0));
    pt = rectExpand.TopLeft();
    pt.Offset(1,1);
    m_ImageList.Draw(pDC, m_btnExpand.GetIcon(), pt, ILD_TRANSPARENT);
    }
      

  2.   

    WM_NCPAINT
    The WM_NCPAINT message is sent to a window when its frame must be painted. A window receives this message through its WindowProc function. LRESULT CALLBACK WindowProc(
      HWND hwnd,       // handle to window
      UINT uMsg,       // WM_NCPAINT
      WPARAM wParam,   // handle to update region (HRGN)
      LPARAM lParam    // not used
    );
    Parameters
    wParam 
    Handle to the update region of the window. The update region is clipped to the window frame. When wParam is 1, the entire window frame needs to be updated. 
    lParam 
    This parameter is not used. 
    Return Values
    An application returns zero if it processes this message. Res
    The DefWindowProc function paints the window frame. An application can intercept the WM_NCPAINT message and paint its own custom window frame. The clipping region for a window is always rectangular, even if the shape of the frame is altered. The wParam value can be passed to GetDCEx as in the following example.case WM_NCPAINT:
    {
        HDC hdc;
        hdc = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
        // Paint into this DC
        ReleaseDC(hwnd, hdc);
    }
      

  3.   

    http://www.vckbase.com/document/viewdoc/?id=317
      

  4.   

    这个OnNcPaint()是在类向导中加入的吗?我怎么也找不到WM_NCPAINT这个消息呀