- 26
- 0
- 约2.5千字
- 约 3页
- 2017-06-08 发布于河南
- 举报
对C#中的TreeView添加背景图
对C#中的TreeView添加背景图
发表时间:2006-07-14内容来源:VCKBASE作者:李静南
在微软的.NET的Forms窗口控件中,比如Treeview和ListView,仅仅是对通用控件的简单封装,因此他们不正常的引发Paint事件。 微软所发布内容中,能看到的唯一建议就是设置控件的ControlStyles.UserPaint类型,然后自己为控件做所有的绘图操作。 (译注:老外提供了一个TreeViewWithPaint控件类,派生自TreeView类,提供了Paint事件的挂接。) 一、为了解决这个问题,我们在类内部使用了一个基于Bitmap类的Graphics对象。当任何窗口重新定义大小时候,对象都会重建。?
//Recreate internal graphics objectprotected 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 GraphicsIntPtr 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 callOnPaint new PaintEventArgs internalGraphics, Rectangle.FromLTRB ?updateRect.left,updateRect.top,updateRect.right,updateRect.bottom ; 3. 把内部Graphics对象的位图拷贝到屏幕的Graphics设备上。
//Draw Screen GraphicsscreenGraphics.DrawImage internalBitmap, 0, 0 ;WM_ERASEBKGND消息被过滤掉,什么都不做。 case WM_ERASEBKGND://removes flickerreturn; 三、所提供的代码和测试程序能使用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.Draw
您可能关注的文档
最近下载
- 珍惜战友情谊 密切内部关系.docx VIP
- PowerFlex 700变频器用户手册(中文).pdf
- 2025年厦门事业编考试真题及答案 .pdf VIP
- 2025年湖南科技大学中国近现代史纲要期末考试模拟题必考题.docx VIP
- 2025年宁夏大学微生物学专业《微生物学》期末试卷及答案.docx VIP
- 宁夏大学土壤学笔记.docx VIP
- 2025年山东劳动职业技术学院单招语文测试模拟题库附答案.docx VIP
- 2025(人教2019版)化学必修第二册 第五章单元解读课件.pptx
- 厦门市同安区事业单位招聘考试题目及答案2025.docx VIP
- 建筑工程论文8000字.pdf VIP
原创力文档

文档评论(0)