2 u012777971 u012777971 于 2015.05.28 21:26 提问

MFC中 使用CPen类GDI一直慢慢增加,求助?

以下是我的代码,大神看看我的代码有什么问题?GDI没有释放?

CRect rect;
GetDlgItem(IDC_STATIC_Status)->GetClientRect(&rect);
FillRect((GetDlgItem(IDC_STATIC_Status)->GetDC())->GetSafeHdc(),&rect,CBrush(GetSysColor(COLOR_3DFACE)));

CDC *pDC=GetDC();

GetDlgItem(IDC_STATIC_Status)->GetWindowRect(&rect);
ScreenToClient(&rect);

CPen pen1;
pen1.CreatePen(PS_SOLID,30,RGB(48,128,20));
pDC->SelectObject(&pen1);
pDC->MoveTo(int(rect.left+rect.Width()/4.0),int(rect.top+rect.Height()/3.0));
pDC->LineTo(int(rect.left+rect.Width()/4.0),int(rect.top+rect.Height()/3.0));
pen1.DeleteObject();

pen1.CreatePen(PS_SOLID,30,RGB(255,215,0));
pDC->SelectObject(&pen1);
pDC->MoveTo(int(rect.left+rect.Width()*2.0/4.0),int(rect.top+rect.Height()/3.0));
pDC->LineTo(int(rect.left+rect.Width()*2.0/4.0),int(rect.top+rect.Height()/3.0));
pen1.DeleteObject();

pen1.CreatePen(PS_SOLID,30,RGB(255,0,0));
pDC->SelectObject(&pen1);
pDC->MoveTo(int(rect.left+rect.Width()*3.0/4.0),int(rect.top+rect.Height()/3.0));
pDC->LineTo(int(rect.left+rect.Width()*3.0/4.0),int(rect.top+rect.Height()/3.0));
pen1.DeleteObject();

CFont font;
font.CreatePointFont(150,"宋体");
pDC->SelectObject(&font);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkMode(TRANSPARENT);

pDC->TextOutA(int(rect.left+rect.Width()/4.0-25),int(rect.top+rect.Height()*2.0/3.0),"正常");
pDC->TextOutA(int(rect.left+rect.Width()*2.0/4.0-25),int(rect.top+rect.Height()*2.0/3.0),"预警");
pDC->TextOutA(int(rect.left+rect.Width()*3.0/4.0-25),int(rect.top+rect.Height()*2.0/3.0),"危险");

font.DeleteObject();

ReleaseDC(pDC);

2个回答

oyljerry
oyljerry   Ds   Rxr 2015.05.28 22:02

selectobject要保存旧的object。用完就要还原。

http://blog.csdn.net/winnyrain/article/details/6676841

91program
91program   Ds   Rxr 2015.05.29 07:45

当 GDI 对象,如 pen1 被选入 DC,但未选出时对它调用 DeleteObject 是无法删除成功的。虽然 DeleteObject 函数返回的是成功,但实际并没有删除。
必须先选出,所谓选出就是选入其它 GDI 对象。如 SelectObject 时保存返回值,返回值就是选出的对象;最后再将此选出的对象选入即可。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!