修改TreeView背景TreeViewWithPaint

来源:互联网 发布:淘宝开店商品选择 编辑:程序博客网 时间:2024/06/08 20:13
using System; 
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;

namespace NewControls {

///
/// Displays a hierarchical collection of labeled items, each represented by a System.Windows.Forms.TreeNode.
///
[
//Use the same attibutes as TreeView
DesignerAttribute("System.Windows.Forms.Design.TreeViewDesigner, System.Design, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
DefaultPropertyAttribute("Nodes"),
DefaultEventAttribute("AfterSelect")
]
public class TreeViewWithPaint :TreeView {

Bitmap internalBitmap = null;
Graphics internalGraphics = null;
Image image = null;

private void DisposeInternal() {
if( internalGraphics != null )
internalGraphics.Dispose();
if( internalBitmap != null )
internalBitmap.Dispose();

}
/// Releases resources.
/// true = Both managed and unmanaged, false = Unmanaged only.
protected override void Dispose(bool disposing) {
if( disposing )
DisposeInternal();
base.Dispose (disposing);
}

/// Occurs when window is resized.
/// A System.EventArgs.Empty.
/// Recreates 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 );
}
}
}

/// Occurs when a Windows message is dispatched.
/// Message to process.
/// Overrides WM_PAINT, WM_ERASEBKGND.
public void setBackGround(Image bk)
{
image = bk;
}

/// Occurs when a Windows message is dispatched.
/// Message to process.
/// Overrides WM_PAINT, WM_ERASEBKGND.
protected override void WndProc(ref Message message)
{
const int WM_PAINT = 0x000F;
const int WM_PRINTCLIENT = 0x0318;
const int WM_ERASEBKGND = 0x0014;

switch( message.Msg ) {

case WM_ERASEBKGND:
return;

case WM_PAINT:
// The designer host does not call OnResize()
if( internalGraphics == null )
OnResize( EventArgs.Empty );

//Set up
Win32.RECT updateRect = new Win32.RECT();
if( Win32.GetUpdateRect( message.HWnd, ref updateRect, false) == 0 )
break;

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

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


Win32.PAINTSTRUCT paintStruct = new Win32.PAINTSTRUCT();
//IntPtr screenHdc1 = Win32.BeginPaint(message.HWnd, ref paintStruct);
IntPtr screenHdc = Win32.GetDC(message.HWnd);
using( Graphics screenGraphics = Graphics.FromHdc( screenHdc ) )
{
//Draw Screen Graphics

Bitmap temp = new Bitmap(internalBitmap, internalBitmap.Size);
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 );
}

//Tear down
int ret = Win32.ReleaseDC(message.HWnd, screenHdc);
Win32.ValidateRect(message.HWnd, ref updateRect);
//Win32.EndPaint( message.HWnd, ref paintStruct );
return;
}
base.WndProc(ref message);
}

/// Occurs when the control is redrawn.
/// Re enable browsing attributes for the Paint Event.
[
EditorBrowsableAttribute( EditorBrowsableState.Always ),
BrowsableAttribute(true)
]
public new event PaintEventHandler Paint {
add { base.Paint += value; }
remove{ base.Paint -= value; }
}
}
}
原创粉丝点击