MFC CView改变颜色,字体,内容.

#include "stdafx.h"
#include "NTCollectView.h"

// CNTCollectView
CNTCollectView *CNTCollectView::m_pCollectView = NULL;

IMPLEMENT_DYNCREATE(CNTCollectView, CView)

CNTCollectView::CNTCollectView()
{
    m_pCollectView = this;
    m_globe = false;
}

CNTCollectView::~CNTCollectView()
{
}

BEGIN_MESSAGE_MAP(CNTCollectView, CView)
    ON_WM_SIZE()
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


// CNTCollectView 绘图

void CNTCollectView::OnDraw(CDC *pDC)
{
    CDocument *pDoc = GetDocument();
    // TODO: 在此添加绘制代码

    CRect rect;
    GetClientRect(&rect);
    pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_BgMemDC, 0, 0, SRCCOPY);
}


// CNTCollectView 诊断

#ifdef _DEBUG
void CNTCollectView::AssertValid() const
{
    CView::AssertValid();
}

#ifndef _WIN32_WCE
void CNTCollectView::Dump(CDumpContext &dc) const
{
    CView::Dump(dc);
}
#endif
#endif //_DEBUG


// CNTCollectView 消息处理程序


void CNTCollectView::OnSize(UINT nType, int cx, int cy)
{
    CView::OnSize(nType, cx, cy);

    // TODO: 在此处添加消息处理程序代码

    if (cx == 0 || cy == 0) return;

    InitBgMemDC();


    //将显示区域转换为屏幕坐标
    rectShowToScreen();

    Invalidate();  //这个函数为刷新窗口函数
}


void CNTCollectView::InitBgMemDC()
{
    CRect rect;
    GetClientRect(rect);


    //创建
    if(m_BgMemDC.GetSafeHdc() != NULL)
    {
        m_BgMemBmp.DeleteObject();
        m_BgMemDC.DeleteDC();
    }

    CClientDC dc(this);
    m_BgMemBmp.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
    m_BgMemDC.CreateCompatibleDC(&dc);
    m_BgMemDC.SelectObject(&m_BgMemBmp);


    //设置分割区间
    SetSplitRect(rect);


    //画背景图
    CString strNo;
    //using namespace Gdiplus;
    //Graphics g(m_BgMemDC.m_hDC);

    CFont font;
    font.CreateFont(m_rectShowArray[0].Width() / 10, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("宋体"));

    CFont *pOldFont;
    pOldFont = (CFont *)m_BgMemDC.SelectObject(&font);

    m_BgMemDC.SetBkMode(TRANSPARENT);
    m_BgMemDC.SetTextColor(RGB(255, 255, 255));

    for(INT_PTR i = 0, j = m_rectShowArray.GetSize(); i < j; i++)
    {
        if (m_globe == false)
        {
            //画边框
            m_BgMemDC.DrawEdge(m_rectShowArray[i], EDGE_ETCHED, BF_ADJUST | BF_RECT);

            rect = m_rectShowArray[i];

            CBrush brush(RGB(47, 79, 79));    // Color::DarkSlateGray
            m_BgMemDC.FillRect(rect, &brush);
            brush.DeleteObject();
            //画监视区号
            strNo.Format(_T("第%d区"), i + 1);
            m_BgMemDC.DrawText(strNo, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        }
        else
        {
            //画边框
            m_BgMemDC.DrawEdge(m_rectShowArray[i], EDGE_ETCHED, BF_ADJUST | BF_RECT);

            rect = m_rectShowArray[i];

            CBrush brush(RGB(0, 0, 0));    // Color::DarkSlateGray
            m_BgMemDC.FillRect(rect, &brush);
            brush.DeleteObject();

            strNo.Format(_T("第%d区"), i + 1);
            m_BgMemDC.DrawText(strNo, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        }
    }

    m_BgMemDC.SelectObject(pOldFont);

}

void CNTCollectView::SetSplitRect(CRect rectShow)
{
    //设置分割显示区域
    int nRow, nColumn;

    //    CalcRowCol(CLicControl::GetInstance()->GetMaxNTMonitor(), nRow, nColumn);

    nColumn = 5;
    nRow = 4;
    double  dColWidth	= (double)rectShow.Width() / nColumn;
    double  dRowHeight	= (double)rectShow.Height() / nRow;

    CRect rectSplit;
    m_rectShowArray.RemoveAll();

    for (int i = 0; i < nRow; i++)
    {
        rectSplit.SetRect(rectShow.left, int(rectShow.top + i * dRowHeight), rectShow.left + int(dColWidth), int(rectShow.top + (i + 1)*dRowHeight));

        for (int j = 0; j < nColumn; j++)
        {
            m_rectShowArray.Add(rectSplit);

            rectSplit.left   = rectSplit.right;
            rectSplit.right  = rectShow.left + int(dColWidth * (j + 2));
        }
    }
}

BOOL CNTCollectView::OnEraseBkgnd(CDC *pDC)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    // return CView::OnEraseBkgnd(pDC);
    return TRUE;
}


// 根据信息,改变网格状态
void CNTCollectView::ChangeAreaStatus(const struNTCollectReply &reply)
{
    m_pCollectView->RedrawArea(reply);


}

void CNTCollectView::RedrawArea(const struNTCollectReply &reply)
{
    m_globe = true;

    // ------------ 重绘 -----------
    CRect rect;
    GetClientRect(rect);
    SendMessage(WM_SIZE, (WPARAM)SIZE_RESTORED, MAKELPARAM(rect.Width(), rect.Height()));
}

你可能感兴趣的:(MFC CView改变颜色,字体,内容.)