CMemDC如何使用?//下面操作怎么无法绘制图形。。

mirroatl332 2015-11-09 10:00:44

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

return FALSE;//CDialogEx::OnEraseBkgnd(pDC);
}


void CDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialogEx::OnPaint()
CRect rct;
GetClientRect(&rct);
CMemDC memDC(dc, rct);
CDC *pDC = &memDC.GetDC();
//填充背景色
pDC->FillSolidRect(&rct, RGB(100, 100, 100));
}
...全文
176 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mirroatl332 2015-11-11
  • 打赏
  • 举报
回复
对话框上的子控件不和父窗口一起刷新,怎么设置控件属性呀?
引用 7 楼 schlafenhamster 的回复:
vc6 没 CMemDC 通常是

class CMemDC : public CDC
{
public:

    // constructor sets up the memory DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);

        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // Create a Memory DC
        {
            pDC->GetClipBox(&m_rect);
            CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
            m_pOldBitmap = SelectObject(&m_bitmap);
            SetWindowOrg(m_rect.left, m_rect.top);
        }
        else        // Make a copy of the relevent parts of the current DC for printing
        {
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
    
    // Destructor copies the contents of the mem DC to the original DC
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // Copy the offscreen bitmap onto the screen.
            m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top, SRCCOPY);

            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
        } else {
            // All we need to do is replace the DC with an illegal value,
            // this keeps us from accidently deleting the handles associated with
            // the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
        }
    }

    // Allow usage as a pointer
    CMemDC* operator->() {return this;}
        
    // Allow usage as a pointer
    operator CMemDC*() {return this;}

private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    CDC*     m_pDC;         // Saves CDC passed in constructor
    CRect    m_rect;        // Rectangle of drawing area.
    BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
};

schlafenhamster 2015-11-11
  • 打赏
  • 举报
回复
"对话框上的子控件不和父窗口一起刷新" 具体一点
worldy 2015-11-09
  • 打赏
  • 举报
回复
BOOL CDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return FALSE;//CDialogEx::OnEraseBkgnd(pDC); //应该返回True,返回false则会自动重绘背景MemDC无法起到防止背景闪烁 } void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); CMemDC memDC(dc, rct); //CDC *pDC = &memDC.GetDC(); //填充背景色 memDC.FillSolidRect(&rct, RGB(100, 100, 100)); dc.Bitblt(0,0,100,100,&MemDC,0,0,SRCCOPY); }
mirroatl187 2015-11-09
  • 打赏
  • 举报
回复
为什么 CMemDC类不行呢???做双缓冲呀!
引用 1 楼 schlafenhamster 的回复:
BOOL CDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return FALSE;//CDialogEx::OnEraseBkgnd(pDC); } return TRUE;// :表示 不 擦 1, 直接用 dc void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); // CMemDC memDC(dc, rct); // CDC *pDC = &memDC.GetDC(); //填充背景色 dc.FillSolidRect(&rct, RGB(100, 100, 100)); } 2 用 ClentDC void CDlg::OnPaint() { // TODO: 在此处添加消息处理程序代码 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); CClientDC dc=GetDC(); // CDC *pDC = &memDC.GetDC(); //填充背景色 dc.FillSolidRect(&rct, RGB(100, 100, 100)); } 3 用 mem DC void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); // CDC memDC; memDC.CreateCompatibleDC(&dc); // draw into memdc CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc,rct.Width(),rct.Height()); memDC.SelectObject(&bitmap); memDC.FillSolidRect(&rct , RGB(100, 100, 100)); // draw into dc dc.BitBlt(0,0,rct.Width(),rct.Height(),&memDC,0,0,SRCCOPY); }
paschen 2015-11-09
  • 打赏
  • 举报
回复
schlafenhamster 2015-11-09
  • 打赏
  • 举报
回复
BOOL CDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return FALSE;//CDialogEx::OnEraseBkgnd(pDC); } return TRUE;// :表示 不 擦 1, 直接用 dc void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); // CMemDC memDC(dc, rct); // CDC *pDC = &memDC.GetDC(); //填充背景色 dc.FillSolidRect(&rct, RGB(100, 100, 100)); } 2 用 ClentDC void CDlg::OnPaint() { // TODO: 在此处添加消息处理程序代码 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); CClientDC dc=GetDC(); // CDC *pDC = &memDC.GetDC(); //填充背景色 dc.FillSolidRect(&rct, RGB(100, 100, 100)); } 3 用 mem DC void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); // CDC memDC; memDC.CreateCompatibleDC(&dc); // draw into memdc CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc,rct.Width(),rct.Height()); memDC.SelectObject(&bitmap); memDC.FillSolidRect(&rct , RGB(100, 100, 100)); // draw into dc dc.BitBlt(0,0,rct.Width(),rct.Height(),&memDC,0,0,SRCCOPY); }
schlafenhamster 2015-11-09
  • 打赏
  • 举报
回复
vc6 没 CMemDC 通常是

class CMemDC : public CDC
{
public:

    // constructor sets up the memory DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);

        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // Create a Memory DC
        {
            pDC->GetClipBox(&m_rect);
            CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
            m_pOldBitmap = SelectObject(&m_bitmap);
            SetWindowOrg(m_rect.left, m_rect.top);
        }
        else        // Make a copy of the relevent parts of the current DC for printing
        {
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
    
    // Destructor copies the contents of the mem DC to the original DC
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // Copy the offscreen bitmap onto the screen.
            m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top, SRCCOPY);

            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
        } else {
            // All we need to do is replace the DC with an illegal value,
            // this keeps us from accidently deleting the handles associated with
            // the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
        }
    }

    // Allow usage as a pointer
    CMemDC* operator->() {return this;}
        
    // Allow usage as a pointer
    operator CMemDC*() {return this;}

private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    CDC*     m_pDC;         // Saves CDC passed in constructor
    CRect    m_rect;        // Rectangle of drawing area.
    BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
};

worldy 2015-11-09
  • 打赏
  • 举报
回复
BOOL CDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return FALSE;//CDialogEx::OnEraseBkgnd(pDC); //应该返回True,返回false则会自动重绘背景MemDC无法起到防止背景闪烁 } void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); CMemDC memDC(dc, rct); //CDC *pDC = &memDC.GetDC(); //填充背景色 memDC.FillSolidRect(&rct, RGB(100, 100, 100)); dc.Bitblt(0,0,100,100,&MemDC,0,0,SRCCOPY); } CMemDC 哪里来的,不是派生于CDC?那就自己加一个FillSolidRect成员函数呗,去CDC类的实现里拷贝出来就行
mirroatl187 2015-11-09
  • 打赏
  • 举报
回复
//填充背景色 memDC.FillSolidRect(&rct, RGB(100, 100, 100)); error: FillSolidRect不是memDC的成员呢。。。
引用 4 楼 worldy 的回复:
BOOL CDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return FALSE;//CDialogEx::OnEraseBkgnd(pDC); //应该返回True,返回false则会自动重绘背景MemDC无法起到防止背景闪烁 } void CDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialogEx::OnPaint() CRect rct; GetClientRect(&rct); CMemDC memDC(dc, rct); //CDC *pDC = &memDC.GetDC(); //填充背景色 memDC.FillSolidRect(&rct, RGB(100, 100, 100)); dc.Bitblt(0,0,100,100,&MemDC,0,0,SRCCOPY); }

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧