您的位置:逆风者 VC++ 正文
 添加时间:2007-09-01 原文发表:2007-08-31 人气:75 来源:vckbase.com

本文章共2012字,分2页,当前第1页,快速翻页:
 

对C#中的TreeView加背景图

翻译整理:李静南

下载源代码

逆风编程精品
原文地址:http://www.codeproject.com/cs/miscctrl/genmissingpaintevent.asp

  序:在微软的.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 );               // 把合成的临时位图刷到屏幕上

       
 
本文章更多内容1 - 2 - 下一页>>
相关文章

如何在 Windows NT、Windows 2000 和 Windo
用VC 创建自定义向导程序
IE 控件一些高级使用方法
多功能标签CLabelEx
自己编的SQL服务端加客户端
简单快速的哈夫曼编码
谈谈软件项目管理的重要性
检测:.NET中强大的检测选项让你有信心建立
一个生成公章图片的简易工具
KVIP考勤系统
static_cast<>揭密
自己写 FILTER 改变画面颜色
如何去掉浮动工具条中的“关闭”按钮
ResizeParentToFit,destructors及其他
Javascript - Prototype Based Language
函数功能流程图,in as,line feed
使用VC ATL实现Office的COM插件
VC实现波形不闪烁动态绘图
封装ADO之MFC应用
如何利用Xerces C 正确处理XML文档中的中文

相关评论


本文章所属分类:首页 VC++

  热门关键字: