$$$listctrl滚动条问题

hnzlk 2009-12-03 04:46:47

void CCoolListCtrl::OnPaint()
{
// device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CListCtrl::OnPaint()
CPaintDC dc(this);

if(m_IsLoadImage)
{
BOOL bFocused = (GetFocus()==this);
CRect rectIcon(0,0,0,0);
CRect rectLabel(0,0,0,0);
CRect rectItem(0,0,0,0);

//建立背景画刷

CBrush brush;
CBitmap hBitmap;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
hBitmap.CreateCompatibleBitmap(&dc,GridSize*2,GridSize*2);
CBitmap *pOldBit=MemDC.SelectObject(&hBitmap);
MemDC.FillSolidRect(0,0,GridSize*2,GridSize*2,RGB(255,255,255));
MemDC.FillSolidRect(0,0,GridSize,GridSize,RGB(236,233,200)); //236,233,216
MemDC.FillSolidRect(GridSize,GridSize,GridSize,GridSize,RGB(236,233,200));
brush.CreatePatternBrush(&hBitmap);

for(int i=0; i<GetItemCount(); ++i)
{
CPoint pt;
//GetItemPosition(i,&pt);
//画外框
GetItemRect(i, &rectItem, LVIR_BOUNDS);
CPen pen, *pOldPen;
pen.CreatePen(PS_SOLID,0,RGB(241,239,0)); //226
pOldPen=dc.SelectObject(&pen);
dc.Rectangle(rectItem);
dc.SelectObject(pOldPen);
//画底图

dc.SelectObject(&brush);
GetItemRect(i,&rectIcon,LVIR_ICON);
rectIcon.DeflateRect(50,0,50,0);
dc.FillRect(rectIcon,&brush);
//dc.StretchBlt(rectIcon.left,rectIcon.top,rectIcon.Width(),rectIcon.Height(),&MemDC,0,0,20,20,SRCCOPY);
//dc.FillSolidRect(rectIcon,RGB(237,235,219));

//画缩略图
LVITEM lvItem = {0};
lvItem.iItem = i;
lvItem.mask = LVIF_IMAGE|LVIF_PARAM|LVIF_STATE;
GetItem(&lvItem);
GetItemRect(i,&rectIcon,LVIR_ICON);
m_ImageListThumb.Draw(&dc, lvItem.iImage, CPoint(rectIcon.left+5,rectIcon.top), ILD_TRANSPARENT);

}
MemDC.DeleteDC();
hBitmap.DeleteObject();
}
}


为什么当我拖动滚动条的时候,底图显示变形?如下图:
正常:
不正常:

还有,dc.FillRect(rectIcon,&brush);无论我把rectIcon设成多大,填充的区域仍然不会改变,不知道为什么?
...全文
350 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
没有超出范围啊,我就在原来的基础上改了你说的那两句就成这样了。
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
分我会给的,别急嘛。
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
只能看到图片其它的都看不到了。
zhwlsr-softdrink 2009-12-04
  • 打赏
  • 举报
回复
说得越来越离奇了,第二个问题超出了OnPanit()函数的范围,没看到代码,不好定位啊:)

先给了分再说................
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
那我现在onpaint里面绘的东西看不到了,只有拖滚动条的时候才能看到。那这样也不行嘛!Invalidate()也看不到。
zhwlsr-softdrink 2009-12-04
  • 打赏
  • 举报
回复
The CPaintDC class is a device-context class derived from CDC. It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint at destruction time.
A CPaintDC object can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function.

The CClientDC class is derived from CDC and takes care of calling the Windows functions GetDC at construction time and ReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window.

所以CPaintDC用于响应WM_PAINT消息,只用于OnPaint()函数中,重画区域为客户区中的无效区域;
而CClientDC可用于更广泛的地方,重画区域对应整个客户区。



hnzlk 2009-12-04
  • 打赏
  • 举报
回复
但是还有一个问题,第一次载入图片的时候,没有响应onpaint,而且窗口改变大小的时候调用Invalidate()重绘的时候,那些背景也看不到了。只能看到图片。在onpaint里绘制的东西都不见了,只有拉滚动条的时候才会出来。为什么?
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
没有人知道吗?
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
顶!
zhwlsr-softdrink 2009-12-04
  • 打赏
  • 举报
回复
把工程发给我,我来帮你解决,应该是很简单的问题,我上起QQ了
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
我传到csdn相册了。

wwgddx 2009-12-04
  • 打赏
  • 举报
回复
看不到图,帮顶
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
我能看到啊,你刷新一下!
fandh 2009-12-04
  • 打赏
  • 举报
回复
看不到图呀!
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
顶!
zhwlsr-softdrink 2009-12-04
  • 打赏
  • 举报
回复
把工程发给我仔细定位,QQ43460271
hnzlk 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 zhwylh 的回复:]
把CClientDC dc(this)放到if语句里面去试试
[/Quote]

还是一样的。
zhwlsr-softdrink 2009-12-04
  • 打赏
  • 举报
回复
把CClientDC dc(this)放到if语句里面去试试
hnzlk 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zhwylh 的回复:]
函数最后应该调用CListCtrl::OnPaint()哦

[/Quote]
行了,再问一下,一般onpaint里都用CPaint dc(this),为什么要用CClientDC dc(this)才行呢?稍后会给你分的。
zhwlsr-softdrink 2009-12-03
  • 打赏
  • 举报
回复
函数最后应该调用CListCtrl::OnPaint()哦
加载更多回复(10)

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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