重绘问题

denglei265 2007-04-16 09:15:57
我要实现一个类似Windows任务管理器中CPU使用的界面,但是实现得不理想,请教原因:
创建了一个CView视图,定义了一个私有成员CRect rect;
在OnPaint()中如下,
CPaintDC dc(this);

GetWindowRect(rect);
ScreenToClient(rect);

CDC MemDC; //首先定义一个显示设备对象
MemDC.CreateCompatibleDC(&dc);

CBitmap MemBitmap;//定义一个位图对象
MemBitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
MemDC.SelectObject(&MemBitmap);

static int i=0;
MemDC.BitBlt(0,0,rect_2.Width(),rect_2.Height(),&dc,i,0,SRCCOPY);
MemDC.StretchBlt(i,0,rect_2.Width(),rect_2.Height(),&dc,0,0,rect_2.Width()-i,rect_2.Height(),SRCCOPY);

DrawWave(&MemDC); //绘制网格线

dc.BitBlt(0,0,rect_2.Width(),rect_2.Height(),&MemDC,0,0,SRCCOPY);
i+=8;

//绘图完成后的清理
MemBitmap.DeleteObject();
MemDC.DeleteDC();

在OnTimer()函数中,
CDC *pDC=GetDC();
DrawWave(pDC);

InvalidateRect(rect);
RedrawWindow();

在DrawWave(CDC *pDC)函数中
for(int i = rect.left ; i < rect.right; i += 13)
{
pDC->MoveTo(i, rect.top);
pDC->LineTo(i, rect.bottom);
}
for(int j = rect.top ; j < rect.bottom; j += 13)
{
pDC->MoveTo(rect.left , j);
pDC->LineTo(rect.right, j);
}

结果却是:图像不断左移,但是网格线却是没有绘画,请问是为什么啊?
...全文
164 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
denglei265 2007-04-19
  • 打赏
  • 举报
回复
实验过了,还是那个样子.不过谢谢了,先结帖再说吧.
denglei265 2007-04-17
  • 打赏
  • 举报
回复
各位,能不能加我MSN或者QQ呢,呵呵,这么写比较慢.我的MSN是:dengelei265@163.com
QQ:4813730,谢谢了.
菜牛 2007-04-17
  • 打赏
  • 举报
回复
在OnPaint()中如下,
{
CPaintDC dc(this);
CRect rect; //不用成员变量
GetClientRect(&rect);

CDC MemDC; //首先定义一个显示设备对象
MemDC.CreateCompatibleDC(&dc);

CBitmap MemBitmap;//定义一个位图对象
MemBitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
CBitmap *pBmpOld = MemDC.SelectObject(&MemBitmap);

DrawWave(&MemDC, rect); //绘制网格线

dc.BitBlt(0, 0,rect.Width(), rect.Height(), &MemDC, 0, 0, SRCCOPY);

//绘图完成后的清理
MemDC.SelectObject(pBmpOld);
MemBitmap.DeleteObject();
MemDC.DeleteDC();
}

DrawWave(CDC *pDC)改为DrawWave(CDC *pDC, const CRect& rect)

OnTimer()函数改为
{
static int i=0;
Invalidate(FALSE);
i+=8;
}

基本代码就是这样了,还有问题就是DrawWave本身的问题了,贴出代码来看看。
denglei265 2007-04-17
  • 打赏
  • 举报
回复
对不起,上面的代码有个小问题,rect_2全改为rect;

另外,我修改了一下,
在OnPaint()中如下,
{
CPaintDC dc(this);
GetClientRect(&rect);

CDC MemDC; //首先定义一个显示设备对象
MemDC.CreateCompatibleDC(&dc);

CBitmap MemBitmap;//定义一个位图对象
MemBitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
MemDC.SelectObject(&MemBitmap);

static int i=0;
DrawWave(&MemDC,rect); //绘制网格线

dc.BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
i+=8;

//绘图完成后的清理
MemBitmap.DeleteObject();
MemDC.DeleteDC();
}

DrawWave(CDC *pDC)改为DrawWave(CDC *pDC,CRect rect)

OnTimer()函数改为
{
InvalidateRect(rect);
RedrawWindow();
}
但是结果还是一样.
菜牛 2007-04-16
  • 打赏
  • 举报
回复
你的代码是比较诡异,搞不懂几个DC拷来拷去的干什么。所有的绘图代码(背景、网格、数据)都绘制到MemDC中,然后BitBlt一次就行了呀。在OnTimer中,只需要刷新就好了,也不要调用别的东西。绘图时的矩形,应该用GetClientRect得到客户矩形。另外,矩形可以作为参数传入DrawWave中,而不是作为类的成员变量,这样通用性更好。。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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