|
各位大侠,我现在遇到以下困惑,请求帮助。
我编了一个实施曲线的小程序,用于实时显示数据曲线,其中共四天曲线,分为不同的颜色,开始运行时显示都很正常,运行一段时间以后,曲线的样式和颜色都没有了,变成了黑色,但是程序还在运行,我猜是资源释放的问题,但是一直也没找到问题所在,求大侠帮忙。
- void CHuaTuStatic::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: 在此处添加消息处理程序代码
- // 不为绘图消息调用 CStatic::OnPaint()
- CDC m_dc;
- CRect rect;
- GetClientRect(rect);
- m_dc.CreateCompatibleDC(&dc);
- CBitmap *bt, *bitmap;
- bitmap = new CBitmap;
- bitmap->CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
- bt = m_dc.SelectObject(bitmap);
- m_dc.FillSolidRect(rect, RGB(255, 255, 255));
- m_dc.SetBkMode(TRANSPARENT);
- m_dc.SetTextColor(RGB(100, 200, 50));
- CPen *oldpen;
- CPen pen;
- pen.CreatePen(PS_DASH, 1, RGB(100, 200, 50));
- oldpen = m_dc.SelectObject(&pen);
- int x, y;
- CRect rc;
- CString s;
- rc.left = rect.left + 5;
- rc.right = rect.right - 5;
- y = rect.top + 5;
- m_dc.MoveTo(rect.left, y);
- for (int i = 0; i <= 5; i++)
- {
- m_dc.LineTo(rect.right, y);
- rc.top = y + 5;
- if (i == 5)
- {
- rc.bottom = rc.top;
- rc.top = rc.bottom - 30;
- }
- else
- rc.bottom = rc.top + 30;
- s.Format(_T("%.3f"), 350 - i * 350 / 5.0);
- m_dc.DrawText(s, rc, DT_SINGLELINE | DT_TOP | DT_LEFT);
- s.Format(_T("%.3f"), 30 - i * 30 / 5.0);
- m_dc.DrawText(s, rc, DT_SINGLELINE | DT_TOP | DT_RIGHT);
- y += (rect.Height() - 10) / 5.0;
- m_dc.MoveTo(rect.left, y);
- }
- pen.Detach();
- pen.CreatePen(PS_SOLID, 2, RGB(10, 10, 10));
- oldpen = m_dc.SelectObject(&pen);
- if (m_shishi1[0] != -1)
- {
- x = rect.right;
- y = rect.bottom - 5 - m_shishi1[0] * (rect.Height() - 10) / 30.0;
- m_dc.MoveTo(x, y);
- for (int i = 0; i < m_shishi1.GetSize(); i++)
- {
- if (m_shishi1[i] == -1)
- break;
- else
- {
- x = rect.right - i * rect.Width() / (float)(m_shishi1.GetSize());
- y = rect.bottom - 5 - m_shishi1[i] * (rect.Height() - 10) / 30.0;
- m_dc.LineTo(x, y);
- m_dc.MoveTo(x, y);
- }
- }
- }
- pen.Detach();
- pen.CreatePen(PS_SOLID, 2, RGB(200, 10, 10));
- oldpen = m_dc.SelectObject(&pen);
- if (m_shishi2[0] != -1)
- {
- x = rect.right;
- y = rect.bottom - 5 - m_shishi2[0] * (rect.Height() - 10) / 30.0;
- m_dc.MoveTo(x, y);
- for (int i = 0; i < m_shishi2.GetSize(); i++)
- {
- if (m_shishi2[i] == -1)
- break;
- else
- {
- x = rect.right - i * rect.Width() / (float)(m_shishi2.GetSize());
- y = rect.bottom - 5 - m_shishi2[i] * (rect.Height() - 10) / 30.0;
- m_dc.LineTo(x, y);
- m_dc.MoveTo(x, y);
- }
- }
- }
- pen.Detach();
- pen.CreatePen(PS_SOLID, 2, RGB(0, 235, 10));
- oldpen = m_dc.SelectObject(&pen);
- if (m_fuhe1[0] != -1)
- {
- x = rect.right;
- y = rect.bottom - 5 - m_fuhe1[0] * (rect.Height() - 10) / 350.0;
- m_dc.MoveTo(x, y);
- for (int i = 0; i < m_fuhe1.GetSize(); i++)
- {
- if (m_fuhe1[i] == -1)
- break;
- else
- {
- x = rect.right - i * rect.Width() / (float)(m_fuhe1.GetSize());
- y = rect.bottom - 5 - m_fuhe1[i] * (rect.Height() - 10) / 350.0;
- m_dc.LineTo(x, y);
- m_dc.MoveTo(x, y);
- }
- }
- }
- pen.Detach();
- pen.CreatePen(PS_SOLID, 2, RGB(145, 13, 110));
- oldpen = m_dc.SelectObject(&pen);
- if (m_fuhe2[0] != -1)
- {
- x = rect.right;
- y = rect.bottom - 5 - m_fuhe2[0] * (rect.Height() - 10) / 350.0;
- m_dc.MoveTo(x, y);
- for (int i = 0; i < m_fuhe2.GetSize(); i++)
- {
- if (m_fuhe2[i] == -1)
- break;
- else
- {
- x = rect.right - i * rect.Width() / (float)(m_fuhe2.GetSize());
- y = rect.bottom - 5 - m_fuhe2[i] * (rect.Height() - 10) / 350.0;
- m_dc.LineTo(x, y);
- m_dc.MoveTo(x, y);
- }
- }
- }
- dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &m_dc, 0, 0, rect.Width(), rect.Height(), SRCCOPY);
- pen.Detach();
- pen.DeleteObject();
- m_dc.SelectObject(bt);
- m_dc.SelectObject(oldpen);
- m_dc.DeleteDC();
- delete bitmap;
- }
复制代码
GDI 泄漏
除了 CreateCompatibleDC ,所有create都应该手动释放,pen 和bitmap
告诉你个办法,打开任务管理器,查看-> 选择列 ->GDI对象选中 ,然后调试,
理论上来说,进出Onpaint的GDI对象应该是一致的,如果有增加就是GDI泄漏了,好像是超过1万GDI之后,绘图就会出现问题了
|
-
-
上一篇: winhttp智能指针能访问https的网页吗?下一篇: 求助一下程序运行崩溃是什么原因?
|