首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

为什么dc画出的位图是黑框呢?该如何解决

2012-02-21 
为什么dc画出的位图是黑框呢?先帖一下代码吧:CRectrtthis- GetClientRect(&rt)m_dcMemory.CreateCompat

为什么dc画出的位图是黑框呢?
先帖一下代码吧:
CRect   rt;
this-> GetClientRect(&rt);
m_dcMemory.CreateCompatibleDC(pdc);
//   为屏幕DC创建兼容的内存DC
m_Bmp.CreateCompatibleBitmap(&m_dcMemory,   rt.Width(),   rt.Height());

//   相当于选择画布
::SelectObject(m_dcMemory.GetSafeHdc(),   m_Bmp);
m_dcMemory.FillSolidRect(&rt,   0x00FFFFFF);

CBitmap   bitmap;
    CString   fileName   =  
  "C:\\graphcom\\normal2.bmp ";
    bitmap.m_hObject   =   (HBITMAP)::LoadImage(NULL,
    fileName,
    IMAGE_BITMAP,
    0   ,   0,
    LR_CREATEDIBSECTION   |   LR_LOADFROMFILE);
CDC   *mdc   =   new   CDC;
mdc-> CreateCompatibleDC(pdc);
mdc-> SelectObject(&bitmap);
#if   1
m_dcMemory.BitBlt(0,
0,
50,
50,mdc,0,0,SRCCOPY);
pdc-> BitBlt(0,   0,   rt.Width(),   rt.Height(),   &m_dcMemory,   0,   0,   SRCCOPY);
this-> ReleaseDC(pdc);
#else
pdc-> BitBlt(20,
20,
200,
200,mdc,0,0,SRCCOPY);
this-> ReleaseDC(pdc);
#endif
用if和else之间的代码编译就是黑框,用else之后的编译是正常的,请大侠们帮我看看吧,不胜感激。。

[解决办法]
1:是你画在内存图片上
2:直接画在设备上了。
1使用的方法不正确
我这里有个封装的类

#ifndef __MEMDC_H__
#define __MEMDC_H__

#pragma once

#ifdef USEMOSKINNAMESPACE
namespace UMoSkin
{
#endif

class CMemDC : public CDC {
private:
CBitmapm_bitmap;// Offscreen bitmap
CBitmap*m_oldBitmap;// bitmap originally found in CMemDC
CDC*m_pDC;// Saves CDC passed in constructor
CRectm_rect;// Rectangle of drawing area.
BOOLm_bMemDC;// TRUE if CDC really is a Memory DC.
public:

CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
{
ASSERT(pDC != NULL);

// Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC-> IsPrinting();

// Get the rectangle to draw
if (pRect == NULL) {
pDC-> GetClipBox(&m_rect);
} else {
m_rect = *pRect;
}

if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC-> LPtoDP(&m_rect);

m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);

SetMapMode(pDC-> GetMapMode());

SetWindowExt(pDC-> GetWindowExt());
SetViewportExt(pDC-> GetViewportExt());

pDC-> DPtoLP(&m_rect);
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;
}

// Fill background
FillSolidRect(m_rect, pDC-> GetBkColor());
}

~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_oldBitmap);
} 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;
}
};

#ifdef USEMOSKINNAMESPACE
}
#endif

#endif

使用方法
CMemDC menmdc(dc,rect);
直接在menmdc绘制
[解决办法]
关键是这里
pDC-> LPtoDP(&m_rect);
[解决办法]
m_Bmp.CreateCompatibleBitmap(&m_dcMemory, rt.Width(), rt.Height());

这句是不是该创建与设备DC有关的内存
m_Bmp.CreateCompatibleBitmap(pdc, rt.Width(), rt.Height());

试试看呢?

热点排行