All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
通过内存配置来优化SQL Server的性能_[SQL Server教程]
C#中删除XML节点实例
支付宝Payto接口的c#.net实现_[Asp.Net教程]
6种ASP.NET跨页面传值的方法_[Asp.Net教程]
灵活调用xsl来解析xml文档(js异步)_JavaScript技术_编程技术
FreeBSD5.3下安装Apache+PHP+MySQL+Tomcat_php资料_编程技术
细节决定成败,ASP.NET中的蝴蝶效应_[Asp.Net教程]
在ASP.NET中使用AJAX的简单方法_.net资料_编程技术
asp.net实现简单的用户登录_[Asp.Net教程]
c#2.0中动态修改页面标题_.net资料_编程技术
Reading number is top 10 pictures
Perfect small Laurie1
Men don't mature ten sign
Small s breast enhancement demonstration
Summer is most suitable for young people to travel in China2
西游日记2
Ashlynn Brooke a group sexy photo4
Ashlynn Brooke photograph of a group2
网上疯传的一篇小学作文《爸爸也治不了妈妈》
Plump, too plump!2
A man's favorite things11
Download software ranking
Tram sex maniac 2 (H) rar bag18
Unix video tutorial7
Tram sex maniac 2 (H) rar bag7
天龙八部十二宫服务端
The cock of the Grosvenor LTD handsome
Sora aoi, the maid, students' uniforms
尖东毒玫瑰B
Sora aoi, the nurse, uniform ,nursing assistant
Unix video tutorial13
金山office2007
delv published in(发表于) 2014/1/8 7:04:07 Edit(编辑)
对C#中的TreeView添加背景图_[Asp.Net教程]

对C#中的TreeView添加背景图_[Asp.Net教程]

对C#中的TreeView添加背景图_[Asp.Net教程]

在微软的.NET的Forms窗口控件中,比如Treeview和ListView,仅仅是对通用控件的简单封装,因此他们不正常的引发Paint事件。 微软所发布内容中,能看到的唯一建议就是设置控件的ControlStyles.UserPaint类型,然后自己为控件做所有的绘图操作。 (译注:老外提供了一个TreeViewWithPaint控件类,派生自TreeView类,提供了Paint事件的挂接。)

  一、为了解决这个问题,我们在类内部使用了一个基于Bitmap类的Graphics对象。当任何窗口重新定义大小时候,对象都会重建。


//Recreate internal graphics object
protected override void OnResize( System.EventArgs e )
{
 if( internalBitmap == null || internalBitmap.Width != Width || internalBitmap.Height != Height )
 {
  if( Width != 0 && Height != 0 )
  {
   DisposeInternal();
   internalBitmap = new Bitmap( Width, Height );
   internalGraphics = Graphics.FromImage( internalBitmap );
  }
 }
}

  二、重写窗口过程

  当控件收到了WM_PAINT消息时候,将执行下面的三个步骤:

  1. 通过一个内部的WM_PRINTCLIENT消息,让原来的控件过程把图象画到内部的Graphics对象上。


//Draw Internal Graphics
IntPtr hdc = internalGraphics.GetHdc();
Message printClientMessage = Message.Create( Handle, WM_PRINTCLIENT, hdc, IntPtr.Zero );
DefWndProc( ref printClientMessage );
internalGraphics.ReleaseHdc( hdc );

  2. 使用内部的Graphics对象建立PaintEventArgs参数,引发用户的OnPaint()函数。


//Add the missing OnPaint() call
OnPaint( new PaintEventArgs( internalGraphics, Rectangle.FromLTRB(
updateRect.left,
updateRect.top,
updateRect.right,
updateRect.bottom ) ) );

  3. 把内部Graphics对象的位图拷贝到屏幕的Graphics设备上。


//Draw Screen Graphics
screenGraphics.DrawImage( internalBitmap, 0, 0 );

WM_ERASEBKGND消息被过滤掉,什么都不做。 case WM_ERASEBKGND:
//removes flicker
return;

  三、所提供的代码和测试程序能使用Paint事件在TreeNode在被选中的时候,在其边框上画个黄色的边框。但是,其实对于我实际要用的项目来说,需要添加背景图的功能没有实现。而这里离我们的目的还有一步之遥,我们对前文绘图过程2和3之间加一个步骤:


Bitmap temp = new Bitmap(internalBitmap, internalBitmap.Size); // 建立一个临时的位图temp,保存前面绘好的界面

temp.MakeTransparent(Color.White); // 设置白色为透明色
internalGraphics.FillRectangle(Brushes.White, 0, 0, this.Bounds.Width, this.Bounds.Height);
// 在原来的内部位图对象上,用白色重画背景
if (image != null) // 如果设置了背景图,就在内部对象上画背景
internalGraphics.DrawImage (image, 0, 0, image.Width, image.Height);
internalGraphics.DrawImage(temp, 0, 0, temp.Width, temp.Height);// 把前面绘好的界面按白色为透明色复合到内部位图上
screenGraphics.DrawImage( internalBitmap, 0, 0 ); // 把合成的临时位图刷到屏幕上

  其实,这里还存在一个问题:在处理WM_PAINT消息时候,通常的做法是使用BeginPaint和Endpaint函数来操作DC画图的,当树结点展开或者折叠时候,我们收到WM_PAINT消息,并由消息得到的刷新区域或者说刷新矩形。关键就是在于,这里的刷新区域不是整个客户区,背景图会出现重叠的部分而变形。

  解决方法:考虑使用GetDC和ReleaseDC操作,可以避开刷新区域的限制,我们可以把整个客户区重画,而实现背景图的完整性。这里要非常注意的是:BeginPaint和Endpaint函数会自动把需要刷新的区域设为有效,而GetDC和ReleaseDC函数不会,所以我们要自己增加两个操作GetUpdateRect和ValidateRect,也就是自己把需要刷新的区域设置为有效。否则:会不停的得到WM_PAINT消息,和死循环一样,CPU占用达到100%。



图一 测试程序

  四、结束语

  由于使用了Win32的API函数,因此附加了一个Win32内部类,导入了自己需要的函数。
作者:李静南 来源:VCKBASE





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.