帮我查查内存泄漏(应该是资源泄漏)

CBEMA 2008-05-08 03:11:25
void CExButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC*pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
UINT state = lpDrawItemStruct->itemState;

CRect leftRect, rightRect,Rect,focusRect; //将控件分成两个区域,显示两种颜色
leftRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
rightRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
focusRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
Rect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
leftRect.right = (leftRect.right+leftRect.left)/2;//重新设置控件的左区域的右侧位置
rightRect.left = leftRect.right;//重新设置控件的右区域的左侧位置
//设置虚线框
focusRect.left +=2;
focusRect.right -=2;
focusRect.top +=2;
focusRect.bottom -=2;

//按钮标题
TCHAR buffer[512];

GetWindowText(buffer,512);

//绘制按钮
DrawFilledRect(pDC, leftRect, GetBGColor_left());
DrawFilledRect(pDC, rightRect, GetBGColor_right());
DrawFrame(pDC,Rect);
//如果按纽处于选中状态则在其上绘制选中虚线框
if(state&ODS_FOCUS)
{
DrawFocusRect(lpDrawItemStruct->hDC,(LPRECT)&focusRect);
}
DrawButtonText(pDC,Rect,buffer);//绘制按钮上的文本

ReleaseDC(pDC);
}

void CExButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{

B.CreateSolidBrush(color);
DC->FillRect(R,&B);
B.DeleteObject();
}

COLORREF CExButton::GetBGColor_left()
{
return m_leftBG;
}

COLORREF CExButton::GetBGColor_right()
{
return m_rightBG;
}

void CExButton::DrawFrame(CDC *DC, CRect R)
{
int nLeft = R.left;
int nTop = R.top;
int nBottom = R.bottom;
int nRight = R.right-1;

pen.CreatePen(PS_SOLID,1,RGB(0,0,255));//创建黑色画笔
::SelectObject(DC->GetSafeHdc(),pen);//将画笔选入设备
DC->SetBkMode(TRANSPARENT);
//左边竖线
DC->MoveTo(nLeft,nTop);
DC->LineTo(nLeft,nTop);
DC->LineTo(nLeft,nBottom);
//右边竖线
DC->MoveTo(nRight,nTop);
DC->LineTo(nRight,nTop);
DC->LineTo(nRight,nBottom);
//上边横线
DC->MoveTo(nRight,nTop);
DC->LineTo(nRight,nTop);
DC->LineTo(nLeft,nTop);
//下边横线
DC->MoveTo(nRight,nBottom);
DC->LineTo(nRight,nBottom);
DC->LineTo(nLeft,nBottom);
pen.DeleteObject();
}

void CExButton::DrawButtonText(CDC *DC, CRect R, const TCHAR *Buf)
{
DC->SetBkMode(TRANSPARENT);
DC->DrawText(Buf,lstrlen(Buf),R,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}

void CExButton::SetBGColor(COLORREF color_left, COLORREF color_Right)
{
m_rightBG = color_Right;
m_leftBG = color_left;
Invalidate();
}

void CExButton::PreSubclassWindow()
{
// Add BS_OWNERDRAW style
SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
CButton::PreSubclassWindow();
} // End of PreSubclassWindow
...全文
102 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
S_S_Ge 2009-07-24
//pen.DeleteObject();
//DC->SelectObject(pen);

这两句不要杠掉吧
  • 打赏
  • 举报
回复
dbger 2008-05-08
看着累,LZ自己仔细检查下SelectObject后要存储旧的资源,使用完毕把旧的资源设置回去。
调试时看看DeleteObject都成功了嘛
  • 打赏
  • 举报
回复
CBEMA 2008-05-08

这是修改过的,仍然有泄漏,窗口重绘一次,内存减少4k.
CExButton::CExButton()
{
m_rightBG = RGB(0,255,0);
m_leftBG = RGB(0,255,0);
B_red.CreateSolidBrush(RGB(255,0,0));
B_green.CreateSolidBrush(RGB(0,255,0));
B_Yellow.CreateSolidBrush(RGB(255,255,0));
}

CExButton::~CExButton()
{
B_red.DeleteObject();
B_green.DeleteObject();
B_Yellow.DeleteObject();
}


BEGIN_MESSAGE_MAP(CExButton, CButton)
//{{AFX_MSG_MAP(CExButton)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExButton message handlers

void CExButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item

pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
UINT state = lpDrawItemStruct->itemState;

CRect leftRect, rightRect,Rect,focusRect; //将控件分成两个区域,显示两种颜色
leftRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
rightRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
focusRect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
Rect.CopyRect(&lpDrawItemStruct->rcItem);//将控件的区域复制给它
leftRect.right = (leftRect.right+leftRect.left)/2;//重新设置控件的左区域的右侧位置
rightRect.left = leftRect.right;//重新设置控件的右区域的左侧位置
//设置虚线框
focusRect.left +=2;
focusRect.right -=2;
focusRect.top +=2;
focusRect.bottom -=2;

//按钮标题
TCHAR buffer[512];

GetWindowText(buffer,512);

//绘制按钮

DrawFilledRect(pDC, leftRect, GetBGColor_left());
DrawFilledRect(pDC, rightRect, GetBGColor_right());
DrawFrame(pDC,Rect);
//如果按纽处于选中状态则在其上绘制选中虚线框
if(state&ODS_FOCUS)
{
DrawFocusRect(lpDrawItemStruct->hDC,(LPRECT)&focusRect);
}
DrawButtonText(pDC,Rect,buffer);//绘制按钮上的文本

ReleaseDC(pDC);
}

void CExButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
if(color==RGB(255,255,0))
DC->FillRect(R,&B_Yellow);
if(color==RGB(0,255,0))
DC->FillRect(R,&B_green);
if(color==RGB(255,0,0))
DC->FillRect(R,&B_red);
}

COLORREF CExButton::GetBGColor_left()
{
return m_leftBG;
}

COLORREF CExButton::GetBGColor_right()
{
return m_rightBG;
}

void CExButton::DrawFrame(CDC *DC, CRect R)
{
int nLeft = R.left;
int nTop = R.top;
int nBottom = R.bottom;
int nRight = R.right-1;

//pen.CreatePen(PS_SOLID,1,RGB(0,0,255));//创建黑色画笔
//OldPen=DC->SelectObject(pen);//将画笔选入设备
DC->SetBkMode(TRANSPARENT);
//左边竖线
DC->MoveTo(nLeft,nTop);
DC->LineTo(nLeft,nTop);
DC->LineTo(nLeft,nBottom);
//右边竖线
DC->MoveTo(nRight,nTop);
DC->LineTo(nRight,nTop);
DC->LineTo(nRight,nBottom);
//上边横线
DC->MoveTo(nRight,nTop);
DC->LineTo(nRight,nTop);
DC->LineTo(nLeft,nTop);
//下边横线
DC->MoveTo(nRight,nBottom);
DC->LineTo(nRight,nBottom);
DC->LineTo(nLeft,nBottom);
//pen.DeleteObject();
//DC->SelectObject(pen);
}

void CExButton::DrawButtonText(CDC *DC, CRect R, const TCHAR *Buf)
{
DC->SetBkMode(TRANSPARENT);
DC->DrawText(Buf,lstrlen(Buf),R,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}

void CExButton::SetBGColor(COLORREF color_left, COLORREF color_Right)
{
m_rightBG = color_Right;
m_leftBG = color_left;
Invalidate();
}

void CExButton::PreSubclassWindow()
{
// Add BS_OWNERDRAW style
SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
CButton::PreSubclassWindow();
} // End of PreSubclassWindow
  • 打赏
  • 举报
回复
dbger 2008-05-08
void CExButton::DrawFrame(CDC *DC, CRect R)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));//创建黑色画笔

HPEN hOld = ::SelectObject(DC->GetSafeHdc(),pen);//将画笔选入设备

// 使用完要把原来的设置回去
::SelectObject(DC->GetSafeHdc(),hOld);
pen.DeleteObject();
}
  • 打赏
  • 举报
回复
相关推荐
发帖
界面
加入

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2008-05-08 03:11
社区公告
暂无公告