MFC设置对话框背景和边框颜色

 对于对话框,只需要重载默认的消息处理函数就行了:

// 重载默认的消息处理函数,主要处理WM_MOVE WM_PAINT WM_NCPAINT WM_NCACTIVATE WM_NOTIFY这// 几个消息就可以了

LRESULT CMyDialog::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)
            DrawBorder(pWinDC);
        ReleaseDC(pWinDC);
    }
    return lrst;
   
    //return CDialog::DefWindowProc(message, wParam, lParam);
}
// 重画边框的具体代码
void CMyDialog::DrawBorder(CDC *pDC)
{
    if (m_hWnd )
    {
        CBrush Brush(RGB(255, 0, 0));
        CBrush* pOldBrush = pDC->SelectObject(&Brush);

        CRect rtWnd;
        GetWindowRect(&rtWnd);
       
        CPoint point;
        //填充顶部框架
        point.x = rtWnd.Width();
        point.y = GetSystemMetrics(SM_CYFRAME)+1;
        pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
        //填充左侧框架
        point.x = GetSystemMetrics(SM_CXFRAME);
        point.y = rtWnd.Height();
        pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
        //填充底部框架
        point.x = rtWnd.Width();
        point.y = GetSystemMetrics(SM_CYFRAME) + 1;
        pDC->PatBlt(0, rtWnd.Height()-point.y, point.x, point.y, PATCOPY);
        //填充右侧框架
        point.x = GetSystemMetrics(SM_CXFRAME);
        point.y = rtWnd.Height();
        pDC->PatBlt(rtWnd.Width()-point.x, 0, point.x, point.y, PATCOPY);
    }
}

 

以上代码虽然是针对对话框的,不过,对于一般的窗口,稍微修改一下,估计也是可以的。
如果出現对话框的边框颜色没有修改,可以用:

InvalidateRect(NULL); // 在边框改变的对话框中

或者如果有父窗口,在父窗口中调用:

RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN );

 

这样应该就差不多了。

---------------------------------------------------------------------------------------------------------------------

以下为修改对话框背景颜色方法:

重载OnPaint(),即WM_PAINT消息。有关代码如下(以上例工程为准):   

void   CExampleDlgDlg::OnPaint()   
{ 
if   (IsIconic()) 
… 
else 
{ 
CRect   rect; 
CPaintDC   dc(this); 
GetClientRect(rect); 
dc.FillSolidRect(rect,RGB(0,255,0));   //设置为绿色背景 
CDialog::OnPaint(); 
} 

 

或者在OnInitDialog函数中调用

SetBackgroundColor(RGB(0,255,0));

 

 

posted @ 2016-10-23 17:33  程序员大叔  阅读(9736)  评论(0编辑  收藏  举报