C#仿QQ皮肤-实现原理系列文章导航
http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html
这一次我们先要研究一下系统的是怎么完成的
我们使用Reflector反编译一下GroupBox一起来看看它的内部是怎么实现的。
从类的开始第一行我们可以看得出来它是继承Control这个类而来的,下面是所有引用的命名空间和继承的源


using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Windows.Forms.Internal;
using System.Windows.Forms.Layout;
using System.Windows.Forms.VisualStyles;
[DefaultEvent( " Enter " ),Designer( " System.Windows.Forms.Design.GroupBoxDesigner,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a " ),ClassInterface(ClassInterfaceType.AutoDispatch),DefaultProperty( " Text " ),ComVisible( true ),System.Windows.Forms.SRDescription( " DescriptionGroupBox " )]
public class GroupBox:Control
{
其实这里告诉我们这样几个问题
1.所有引用的命名空间
2.默认的事件 DefaultEvent("Enter"),
3.在实现设计时服务的类 System.Windows.Forms.Design.GroupBoxDesigner
4.为Com指定接口类型ClassInterface
5.默认的属性DefaultProperty("Text")
6.托管类型或是成员的Com可访问性ComVisible
7.继承自public class GroupBox : Control
关于事件的注册
我们以一个AutoSizeChanged事件来说明
用上面的语句指定属性和方法是否在编辑器里的可见方式 在这里我们合作Always代表是编辑器里是始终可见的
然后让它显示在属性窗口中
看一下MS的实现方式吧


[EditorBrowsable(EditorBrowsableState.Always),System.Windows.Forms.SRCategory( " CatPropertyChanged " ),System.Windows.Forms.SRDescription( " ControlOnAutoSizeChangedDescr " ),Browsable( true )]
public event EventHandlerAutoSizeChanged
{
add
{
base .AutoSizeChanged += value;
}
remove
{
base .AutoSizeChanged -= value;
}
}
我们现在再来看Click事件的实现就更为简单了


public event EventHandlerClick
{
add
{
base .Click += value;
}
remove
{
base .Click -= value;
}
}
以后的事件基本上和这个差不多,就不再多说了
具体的实现
我们一起来看看他的构造方法


{
base .SetState2( 0x800 , true );
base .SetStyle(ControlStyles.ContainerControl, true );
base .SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, this .OwnerDraw);
base .SetStyle(ControlStyles.Selectable, false );
this .TabStop = false ;
}
呵呵 ,其实这里跟我用来实现Combox控件时的情况差不了多少。
DrawGroupBox事件的实现方法如果你看过我的Combox的实现的话应该很容易能看明白,


{
Graphicsgraphics = e.Graphics;
RectangleclientRectangle = base .ClientRectangle;
int num = 8 ;
ColordisabledColor = base .DisabledColor;
Penpen = new Pen(ControlPaint.Light(disabledColor,1f));
Penpen2 = new Pen(ControlPaint.Dark(disabledColor,0f));
clientRectangle.X += num;
clientRectangle.Width -= 2 * num;
try
{
Sizesize;
int num2;
if ( this .UseCompatibleTextRendering)
{
using (Brushbrush = new SolidBrush( this .ForeColor))
{
using (StringFormatformat = new StringFormat())
{
format.HotkeyPrefix = this .ShowKeyboardCues ? HotkeyPrefix.Show:HotkeyPrefix.Hide;
if ( this .RightToLeft == RightToLeft.Yes)
{
format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
size = Size.Ceiling(graphics.MeasureString( this .Text, this .Font,clientRectangle.Width,format));
if ( base .Enabled)
{
graphics.DrawString( this .Text, this .Font,brush,clientRectangle,format);
}
else
{
ControlPaint.DrawStringDisabled(graphics, this .Text, this .Font,disabledColor,clientRectangle,format);
}
}
goto Label_01E7;
}
}
using (WindowsGraphicsgraphics2 = WindowsGraphics.FromGraphics(graphics))
{
IntTextFormatFlagsflags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
if ( ! this .ShowKeyboardCues)
{
flags |= IntTextFormatFlags.HidePrefix;
}
if ( this .RightToLeft == RightToLeft.Yes)
{
flags |= IntTextFormatFlags.RightToLeft;
flags |= IntTextFormatFlags.Right;
}
using (WindowsFontfont = WindowsGraphicsCacheManager.GetWindowsFont( this .Font))
{
size = graphics2.MeasureText( this .Text,font, new Size(clientRectangle.Width, 0x7fffffff ),flags);
if ( base .Enabled)
{
graphics2.DrawText( this .Text,font,clientRectangle, this .ForeColor,flags);
}
else
{
ControlPaint.DrawStringDisabled(graphics2, this .Text, this .Font,disabledColor,clientRectangle,(TextFormatFlags)flags);
}
}
}
Label_01E7:
num2 = num;
if ( this .RightToLeft == RightToLeft.Yes)
{
num2 += clientRectangle.Width - size.Width;
}
int num3 = Math.Min(( int )(num2 + size.Width),( int )( base .Width - 6 ));
int num4 = base .FontHeight / 2 ;
graphics.DrawLine(pen, 1 ,num4, 1 , base .Height - 1 );
graphics.DrawLine(pen2, 0 ,num4, 0 , base .Height - 2 );
graphics.DrawLine(pen, 0 , base .Height - 1 , base .Width, base .Height - 1 );
graphics.DrawLine(pen2, 0 , base .Height - 2 , base .Width - 1 , base .Height - 2 );
graphics.DrawLine(pen2, 0 ,num4 - 1 ,num2,num4 - 1 );
graphics.DrawLine(pen, 1 ,num4,num2,num4);
graphics.DrawLine(pen2,num3,num4 - 1 , base .Width - 2 ,num4 - 1 );
graphics.DrawLine(pen,num3,num4, base .Width - 1 ,num4);
graphics.DrawLine(pen,( int )( base .Width - 1 ),( int )(num4 - 1 ),( int )( base .Width - 1 ),( int )( base .Height - 1 ));
graphics.DrawLine(pen2, base .Width - 2 ,num4, base .Width - 2 , base .Height - 2 );
}
finally
{
pen.Dispose();
pen2.Dispose();
}
}
在这里不得不说MS的方法没有多少高明之处啊,不过还是值得我敬佩的
我把所有的代码都放上来大家参考一下吧,然后再接着说我的控件是怎么实现的


{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Windows.Forms.Internal;
using System.Windows.Forms.Layout;
using System.Windows.Forms.VisualStyles;
[DefaultEvent( " Enter " ),Designer( " System.Windows.Forms.Design.GroupBoxDesigner,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a " ),ClassInterface(ClassInterfaceType.AutoDispatch),DefaultProperty( " Text " ),ComVisible( true ),System.Windows.Forms.SRDescription( " DescriptionGroupBox " )]
public class GroupBox:Control
{
private FontcachedFont;
private System.Windows.Forms.FlatStyleflatStyle = System.Windows.Forms.FlatStyle.Standard;
private int fontHeight = - 1 ;
[EditorBrowsable(EditorBrowsableState.Always),System.Windows.Forms.SRCategory( " CatPropertyChanged " ),System.Windows.Forms.SRDescription( " ControlOnAutoSizeChangedDescr " ),Browsable( true )]
public event EventHandlerAutoSizeChanged
{
add
{
base .AutoSizeChanged += value;
}
remove
{
base .AutoSizeChanged -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event EventHandlerClick
{
add
{
base .Click += value;
}
remove
{
base .Click -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event EventHandlerDoubleClick
{
add
{
base .DoubleClick += value;
}
remove
{
base .DoubleClick -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event KeyEventHandlerKeyDown
{
add
{
base .KeyDown += value;
}
remove
{
base .KeyDown -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event KeyPressEventHandlerKeyPress
{
add
{
base .KeyPress += value;
}
remove
{
base .KeyPress -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event KeyEventHandlerKeyUp
{
add
{
base .KeyUp += value;
}
remove
{
base .KeyUp -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseClick
{
add
{
base .MouseClick += value;
}
remove
{
base .MouseClick -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseDoubleClick
{
add
{
base .MouseDoubleClick += value;
}
remove
{
base .MouseDoubleClick -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseDown
{
add
{
base .MouseDown += value;
}
remove
{
base .MouseDown -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerMouseEnter
{
add
{
base .MouseEnter += value;
}
remove
{
base .MouseEnter -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event EventHandlerMouseLeave
{
add
{
base .MouseLeave += value;
}
remove
{
base .MouseLeave -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseMove
{
add
{
base .MouseMove += value;
}
remove
{
base .MouseMove -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseUp
{
add
{
base .MouseUp += value;
}
remove
{
base .MouseUp -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerTabStopChanged
{
add
{
base .TabStopChanged += value;
}
remove
{
base .TabStopChanged -= value;
}
}
public GroupBox()
{
base .SetState2( 0x800 , true );
base .SetStyle(ControlStyles.ContainerControl, true );
base .SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, this .OwnerDraw);
base .SetStyle(ControlStyles.Selectable, false );
this .TabStop = false ;
}
protected override AccessibleObjectCreateAccessibilityInstance()
{
return new GroupBoxAccessibleObject( this );
}
private void DrawGroupBox(PaintEventArgse)
{
Graphicsgraphics = e.Graphics;
RectangleclientRectangle = base .ClientRectangle;
int num = 8 ;
ColordisabledColor = base .DisabledColor;
Penpen = new Pen(ControlPaint.Light(disabledColor,1f));
Penpen2 = new Pen(ControlPaint.Dark(disabledColor,0f));
clientRectangle.X += num;
clientRectangle.Width -= 2 * num;
try
{
Sizesize;
int num2;
if ( this .UseCompatibleTextRendering)
{
using (Brushbrush = new SolidBrush( this .ForeColor))
{
using (StringFormatformat = new StringFormat())
{
format.HotkeyPrefix = this .ShowKeyboardCues ? HotkeyPrefix.Show:HotkeyPrefix.Hide;
if ( this .RightToLeft == RightToLeft.Yes)
{
format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
size = Size.Ceiling(graphics.MeasureString( this .Text, this .Font,clientRectangle.Width,format));
if ( base .Enabled)
{
graphics.DrawString( this .Text, this .Font,brush,clientRectangle,format);
}
else
{
ControlPaint.DrawStringDisabled(graphics, this .Text, this .Font,disabledColor,clientRectangle,format);
}
}
goto Label_01E7;
}
}
using (WindowsGraphicsgraphics2 = WindowsGraphics.FromGraphics(graphics))
{
IntTextFormatFlagsflags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
if ( ! this .ShowKeyboardCues)
{
flags |= IntTextFormatFlags.HidePrefix;
}
if ( this .RightToLeft == RightToLeft.Yes)
{
flags |= IntTextFormatFlags.RightToLeft;
flags |= IntTextFormatFlags.Right;
}
using (WindowsFontfont = WindowsGraphicsCacheManager.GetWindowsFont( this .Font))
{
size = graphics2.MeasureText( this .Text,font, new Size(clientRectangle.Width, 0x7fffffff ),flags);
if ( base .Enabled)
{
graphics2.DrawText( this .Text,font,clientRectangle, this .ForeColor,flags);
}
else
{
ControlPaint.DrawStringDisabled(graphics2, this .Text, this .Font,disabledColor,clientRectangle,(TextFormatFlags)flags);
}
}
}
Label_01E7:
num2 = num;
if ( this .RightToLeft == RightToLeft.Yes)
{
num2 += clientRectangle.Width - size.Width;
}
int num3 = Math.Min(( int )(num2 + size.Width),( int )( base .Width - 6 ));
int num4 = base .FontHeight / 2 ;
graphics.DrawLine(pen, 1 ,num4, 1 , base .Height - 1 );
graphics.DrawLine(pen2, 0 ,num4, 0 , base .Height - 2 );
graphics.DrawLine(pen, 0 , base .Height - 1 , base .Width, base .Height - 1 );
graphics.DrawLine(pen2, 0 , base .Height - 2 , base .Width - 1 , base .Height - 2 );
graphics.DrawLine(pen2, 0 ,num4 - 1 ,num2,num4 - 1 );
graphics.DrawLine(pen, 1 ,num4,num2,num4);
graphics.DrawLine(pen2,num3,num4 - 1 , base .Width - 2 ,num4 - 1 );
graphics.DrawLine(pen,num3,num4, base .Width - 1 ,num4);
graphics.DrawLine(pen,( int )( base .Width - 1 ),( int )(num4 - 1 ),( int )( base .Width - 1 ),( int )( base .Height - 1 ));
graphics.DrawLine(pen2, base .Width - 2 ,num4, base .Width - 2 , base .Height - 2 );
}
finally
{
pen.Dispose();
pen2.Dispose();
}
}
internal override SizeGetPreferredSizeCore(SizeproposedSize)
{
Sizesize2 = ( this .SizeFromClientSize(Size.Empty) + new Size( 0 , this .fontHeight)) + base .Padding.Size;
return ( this .LayoutEngine.GetPreferredSize( this ,proposedSize - size2) + size2);
}
protected override void OnFontChanged(EventArgse)
{
this .fontHeight = - 1 ;
this .cachedFont = null ;
base .Invalidate();
base .OnFontChanged(e);
}
protected override void OnPaint(PaintEventArgse)
{
if ((Application.RenderWithVisualStyles && ( base .Width >= 10 )) && ( base .Height >= 10 ))
{
GroupBoxStatestate = base .Enabled ? GroupBoxState.Normal:GroupBoxState.Disabled;
TextFormatFlagsflags = TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
if ( ! this .ShowKeyboardCues)
{
flags |= TextFormatFlags.HidePrefix;
}
if ( this .RightToLeft == RightToLeft.Yes)
{
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
if ( this .ShouldSerializeForeColor() || ! base .Enabled)
{
ColortextColor = base .Enabled ? this .ForeColor:TextRenderer.DisabledTextColor( this .BackColor);
GroupBoxRenderer.DrawGroupBox(e.Graphics, new Rectangle( 0 , 0 , base .Width, base .Height), this .Text, this .Font,textColor,flags,state);
}
else
{
GroupBoxRenderer.DrawGroupBox(e.Graphics, new Rectangle( 0 , 0 , base .Width, base .Height), this .Text, this .Font,flags,state);
}
}
else
{
this .DrawGroupBox(e);
}
base .OnPaint(e);
}
[UIPermission(SecurityAction.LinkDemand,Window = UIPermissionWindow.AllWindows)]
protected internal override bool ProcessMnemonic( char charCode)
{
if ( ! Control.IsMnemonic(charCode, this .Text) || ! this .CanProcessMnemonic())
{
return false ;
}
System.Windows.Forms.IntSecurity.ModifyFocus.Assert();
try
{
base .SelectNextControl( null , true , true , true , false );
}
finally
{
CodeAccessPermission.RevertAssert();
}
return true ;
}
protected override void ScaleControl(SizeFfactor,BoundsSpecifiedspecified)
{
if ((factor.Width != 1f) && (factor.Height != 1f))
{
this .fontHeight = - 1 ;
this .cachedFont = null ;
}
base .ScaleControl(factor,specified);
}
public override string ToString()
{
return ( base .ToString() + " ,Text: " + this .Text);
}
private void WmEraseBkgnd( ref Messagem)
{
System.Windows.Forms.NativeMethods.RECTrect = new System.Windows.Forms.NativeMethods.RECT();
System.Windows.Forms.SafeNativeMethods.GetClientRect( new HandleRef( this , base .Handle), ref rect);
using (Graphicsgraphics = Graphics.FromHdcInternal(m.WParam))
{
using (Brushbrush = new SolidBrush( this .BackColor))
{
graphics.FillRectangle(brush,rect.left,rect.top,rect.right - rect.left,rect.bottom - rect.top);
}
}
m.Result = (IntPtr) 1 ;
}
[SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc( ref Messagem)
{
if ( this .OwnerDraw)
{
base .WndProc( ref m);
}
else
{
int msg = m.Msg;
if (msg != 20 )
{
if (msg == 0x3d )
{
base .WndProc( ref m);
if ((( int )(( long )m.LParam)) == - 12 )
{
m.Result = IntPtr.Zero;
}
return ;
}
if (msg != 0x318 )
{
base .WndProc( ref m);
return ;
}
}
this .WmEraseBkgnd( ref m);
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public override bool AllowDrop
{
get
{
return base .AllowDrop;
}
set
{
base .AllowDrop = value;
}
}
[Browsable( true ),EditorBrowsable(EditorBrowsableState.Always),DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override bool AutoSize
{
get
{
return base .AutoSize;
}
set
{
base .AutoSize = value;
}
}
[System.Windows.Forms.SRCategory( " CatLayout " ),Localizable( true ),System.Windows.Forms.SRDescription( " ControlAutoSizeModeDescr " ),Browsable( true ),DefaultValue( 1 )]
public System.Windows.Forms.AutoSizeModeAutoSizeMode
{
get
{
return base .GetAutoSizeMode();
}
set
{
if ( ! System.Windows.Forms.ClientUtils.IsEnumValid(value,( int )value, 0 , 1 ))
{
throw new InvalidEnumArgumentException( " value " ,( int )value, typeof (System.Windows.Forms.AutoSizeMode));
}
if ( base .GetAutoSizeMode() != value)
{
base .SetAutoSizeMode(value);
if ( this .ParentInternal != null )
{
if ( this .ParentInternal.LayoutEngine == DefaultLayout.Instance)
{
this .ParentInternal.LayoutEngine.InitLayout( this ,BoundsSpecified.Size);
}
LayoutTransaction.DoLayout( this .ParentInternal, this ,PropertyNames.AutoSize);
}
}
}
}
protected override System.Windows.Forms.CreateParamsCreateParams
{
[SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.UnmanagedCode)]
get
{
System.Windows.Forms.CreateParamscreateParams = base .CreateParams;
if ( ! this .OwnerDraw)
{
createParams.ClassName = " BUTTON " ;
createParams.Style |= 7 ;
}
else
{
createParams.ClassName = null ;
createParams.Style &= - 8 ;
}
createParams.ExStyle |= 0x10000 ;
return createParams;
}
}
protected override PaddingDefaultPadding
{
get
{
return new Padding( 3 );
}
}
protected override SizeDefaultSize
{
get
{
return new Size( 200 , 100 );
}
}
public override RectangleDisplayRectangle
{
get
{
SizeclientSize = base .ClientSize;
if ( this .fontHeight == - 1 )
{
this .fontHeight = this .Font.Height;
this .cachedFont = this .Font;
}
else if ( ! object .ReferenceEquals( this .cachedFont, this .Font))
{
this .fontHeight = this .Font.Height;
this .cachedFont = this .Font;
}
Paddingpadding = base .Padding;
return new Rectangle(padding.Left, this .fontHeight + padding.Top,Math.Max(clientSize.Width - padding.Horizontal, 0 ),Math.Max((clientSize.Height - this .fontHeight) - padding.Vertical, 0 ));
}
}
[System.Windows.Forms.SRDescription( " ButtonFlatStyleDescr " ),System.Windows.Forms.SRCategory( " CatAppearance " ),DefaultValue( 2 )]
public System.Windows.Forms.FlatStyleFlatStyle
{
get
{
return this .flatStyle;
}
set
{
if ( ! System.Windows.Forms.ClientUtils.IsEnumValid(value,( int )value, 0 , 3 ))
{
throw new InvalidEnumArgumentException( " value " ,( int )value, typeof (System.Windows.Forms.FlatStyle));
}
if ( this .flatStyle != value)
{
bool ownerDraw = this .OwnerDraw;
this .flatStyle = value;
bool flag2 = this .OwnerDraw != ownerDraw;
base .SetStyle(ControlStyles.ContainerControl, true );
base .SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserMouse | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, this .OwnerDraw);
if (flag2)
{
base .RecreateHandle();
}
else
{
this .Refresh();
}
}
}
}
private bool OwnerDraw
{
get
{
return ( this .FlatStyle != System.Windows.Forms.FlatStyle.System);
}
}
internal override bool SupportsUseCompatibleTextRendering
{
get
{
return true ;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public bool TabStop
{
get
{
return base .TabStop;
}
set
{
base .TabStop = value;
}
}
[Localizable( true )]
public override string Text
{
get
{
return base .Text;
}
set
{
bool visible = base .Visible;
try
{
if (visible && base .IsHandleCreated)
{
base .SendMessage( 11 , 0 , 0 );
}
base .Text = value;
}
finally
{
if (visible && base .IsHandleCreated)
{
base .SendMessage( 11 , 1 , 0 );
}
}
base .Invalidate( true );
}
}
[System.Windows.Forms.SRDescription( " UseCompatibleTextRenderingDescr " ),DefaultValue( false ),System.Windows.Forms.SRCategory( " CatBehavior " )]
public bool UseCompatibleTextRendering
{
get
{
return base .UseCompatibleTextRenderingInt;
}
set
{
base .UseCompatibleTextRenderingInt = value;
}
}
[ComVisible( true )]
internal class GroupBoxAccessibleObject:Control.ControlAccessibleObject
{
internal GroupBoxAccessibleObject(System.Windows.Forms.GroupBoxowner): base (owner)
{
}
public override AccessibleRoleRole
{
get
{
AccessibleRoleaccessibleRole = base .Owner.AccessibleRole;
if (accessibleRole != AccessibleRole.Default)
{
return accessibleRole;
}
return AccessibleRole.Grouping;
}
}
}
}
}
我先拿两个事件做为比较吧,如果大家不细细的看的话应当是没有什么分别的


[Category( " CatPropertyChanged " ),EditorBrowsable(EditorBrowsableState.Always),Description( " ControlOnAutoSizeChangedDescr " ),Browsable( true )]
public event EventHandlerAutoSizeChanged
{
add
{
base .AutoSizeChanged += value;
}
remove
{
base .AutoSizeChanged -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerClick
{
add
{
base .Click += value;
}
remove
{
base .Click -= value;
}
}
下面就是我的控件的实现方法,大家可以做一个比较和参考


using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms.VisualStyles;
using System.Security.Permissions;
using CRD.Common;
namespace CRD.WinUI.Misc
{
// publicclassGroupBox:System.Windows.Forms.GroupBox
// {
// publicGroupBox()
// :base()
// {
// this.BackColor=Color.Transparent;
// }
// [DefaultValue(typeof(Color),"Transparent")]
// publicoverrideSystem.Drawing.ColorBackColor
// {
// get
// {
// returnbase.BackColor;
// }
// set
// {
// base.BackColor=value;
// }
// }
// protectedoverridevoidOnPaint(System.Windows.Forms.PaintEventArgse)
// {
// base.OnPaint(e);
// // e.Graphics.DrawString(this.Text,this.Font,Brushes.Red,10,1);
// e.Graphics.DrawLine(newPen(Shared.ControlBorderBackColor),1,7,8,7);
// e.Graphics.DrawLine(newPen(Shared.ControlBorderBackColor),90,7,this.Width-2,7);
// e.Graphics.DrawLine(newPen(Shared.ControlBorderBackColor),1,7,1,this.Height-2);
// e.Graphics.DrawLine(newPen(Shared.ControlBorderBackColor),1,this.Height-2,this.Width-2,this.Height-2);
// e.Graphics.DrawLine(newPen(Shared.ControlBorderBackColor),this.Width-2,7,this.Width-2,this.Height-2);
// }
// }
[Flags]
public enum IntTextFormatFlags
{
Bottom = 8 ,
CalculateRectangle = 0x400 ,
Default = 0 ,
EndEllipsis = 0x8000 ,
ExpandTabs = 0x40 ,
ExternalLeading = 0x200 ,
HidePrefix = 0x100000 ,
HorizontalCenter = 1 ,
Internal = 0x1000 ,
Left = 0 ,
ModifyString = 0x10000 ,
NoClipping = 0x100 ,
NoFullWidthCharacterBreak = 0x80000 ,
NoPrefix = 0x800 ,
PathEllipsis = 0x4000 ,
PrefixOnly = 0x200000 ,
Right = 2 ,
RightToLeft = 0x20000 ,
SingleLine = 0x20 ,
TabStop = 0x80 ,
TextBoxControl = 0x2000 ,
Top = 0 ,
VerticalCenter = 4 ,
WordBreak = 0x10 ,
WordEllipsis = 0x40000
}
// [Designer("System.Windows.Forms.Design.GroupBoxDesigner,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"),Description("DescriptionGroupBox"),ComVisible(true),ClassInterface(ClassInterfaceType.AutoDispatch),DefaultEvent("Enter"),DefaultProperty("Text")]
public class GroupBox:Control
{
// Fields
private FontcachedFont;
private FlatStyleflatStyle = FlatStyle.Standard;
private int fontHeight = - 1 ;
// Events
[Category( " CatPropertyChanged " ),EditorBrowsable(EditorBrowsableState.Always),Description( " ControlOnAutoSizeChangedDescr " ),Browsable( true )]
public event EventHandlerAutoSizeChanged
{
add
{
base .AutoSizeChanged += value;
}
remove
{
base .AutoSizeChanged -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerClick
{
add
{
base .Click += value;
}
remove
{
base .Click -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event EventHandlerDoubleClick
{
add
{
base .DoubleClick += value;
}
remove
{
base .DoubleClick -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event KeyEventHandlerKeyDown
{
add
{
base .KeyDown += value;
}
remove
{
base .KeyDown -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event KeyPressEventHandlerKeyPress
{
add
{
base .KeyPress += value;
}
remove
{
base .KeyPress -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event KeyEventHandlerKeyUp
{
add
{
base .KeyUp += value;
}
remove
{
base .KeyUp -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event MouseEventHandlerMouseClick
{
add
{
base .MouseClick += value;
}
remove
{
base .MouseClick -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event MouseEventHandlerMouseDoubleClick
{
add
{
base .MouseDoubleClick += value;
}
remove
{
base .MouseDoubleClick -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseDown
{
add
{
base .MouseDown += value;
}
remove
{
base .MouseDown -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerMouseEnter
{
add
{
base .MouseEnter += value;
}
remove
{
base .MouseEnter -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event EventHandlerMouseLeave
{
add
{
base .MouseLeave += value;
}
remove
{
base .MouseLeave -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseMove
{
add
{
base .MouseMove += value;
}
remove
{
base .MouseMove -= value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public event MouseEventHandlerMouseUp
{
add
{
base .MouseUp += value;
}
remove
{
base .MouseUp -= value;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public event EventHandlerTabStopChanged
{
add
{
base .TabStopChanged += value;
}
remove
{
base .TabStopChanged -= value;
}
}
// Methods
public GroupBox()
{
// base.SetState2(0x800,true);
base .SetStyle(ControlStyles.ContainerControl, true );
base .SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, this .OwnerDraw);
base .SetStyle(ControlStyles.Selectable, false );
this .TabStop = false ;
}
protected override void OnCreateControl()
{
base .OnCreateControl();
this .BackColor = Color.Transparent;
}
protected override AccessibleObjectCreateAccessibilityInstance()
{
return new GroupBoxAccessibleObject( this );
}
private void DrawGroupBox(PaintEventArgse)
{
Graphicsgraphics = e.Graphics;
RectangleclientRectangle = base .ClientRectangle;
int num = 8 ;
ColordisabledColor = SystemColors.Control; // base.DisabledColor;
// Penpen=newPen(ControlPaint.Light(disabledColor,1f));
Penpen = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black:Shared.ControlBorderBackColor, 0.3f );
// Penpen2=newPen(ControlPaint.Dark(disabledColor,0f));
Penpen2 = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black:Shared.ControlBorderBackColor,0f);
clientRectangle.X += num;
clientRectangle.Width -= 2 * num;
try
{
Sizesize;
int num2;
IntTextFormatFlagsflags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
if ( ! this .ShowKeyboardCues)
{
flags |= IntTextFormatFlags.HidePrefix;
}
if ( this .RightToLeft == RightToLeft.Yes)
{
flags |= IntTextFormatFlags.RightToLeft;
flags |= IntTextFormatFlags.Right;
}
Graphicsgraphics2 = e.Graphics;
size = graphics2.MeasureString( this .Text, this .Font).ToSize();
if ( base .Enabled)
{
// graphics2.DrawString(this.Text,this.Font,newSolidBrush(Shared.ControlBorderBackColor==SystemColors.Control?Color.Black:Shared.ControlBorderBackColor),8,0);
graphics2.DrawString( this .Text, this .Font, new SolidBrush(Shared.FontColor), 8 , 0 );
}
else
{
ControlPaint.DrawStringDisabled(graphics2, this .Text, this .Font,disabledColor,clientRectangle,(TextFormatFlags)flags);
}
Label_01E7:
num2 = num;
if ( this .RightToLeft == RightToLeft.Yes)
{
num2 += size.Width;
}
int num3 = Math.Min(( int )(num2 + size.Width),( int )( base .Width - 6 ));
int num4 = base .FontHeight / 2 ;
graphics.DrawLine(pen, 1 ,num4, 1 , base .Height - 1 );
// graphics.DrawLine(pen2,0,num4,0,base.Height-2);
graphics.DrawLine(pen, 1 , base .Height - 1 , base .Width - 1 , base .Height - 1 );
// graphics.DrawLine(pen2,0,base.Height-2,base.Width-1,base.Height-2);
graphics.DrawLine(pen2, 1 ,num4 - 1 ,num2 - 1 ,num4 - 1 );
/// /graphics.DrawLine(pen,1,num4,num2,num4);
graphics.DrawLine(pen2,num3,num4 - 1 , base .Width - 2 ,num4 - 1 );
/// /graphics.DrawLine(pen,num3,num4,base.Width-1,num4);
graphics.DrawLine(pen,( int )( base .Width - 1 ),( int )(num4 - 1 ),( int )( base .Width - 1 ),( int )( base .Height - 1 ));
/// /graphics.DrawLine(pen2,base.Width-2,num4,base.Width-2,base.Height-2);
}
finally
{
pen.Dispose();
pen2.Dispose();
}
}
protected override void OnFontChanged(EventArgse)
{
this .fontHeight = - 1 ;
this .cachedFont = null ;
base .Invalidate();
base .OnFontChanged(e);
}
protected override void OnPaint(PaintEventArgse)
{
if ((Application.RenderWithVisualStyles && ( base .Width >= 10 )) && ( base .Height >= 10 ))
{
GroupBoxStatestate = base .Enabled ? GroupBoxState.Normal:GroupBoxState.Disabled;
TextFormatFlagsflags = TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;
if ( ! this .ShowKeyboardCues)
{
flags |= TextFormatFlags.HidePrefix;
}
if ( this .RightToLeft == RightToLeft.Yes)
{
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
}
else
{
this .DrawGroupBox(e);
}
base .OnPaint(e);
}
protected override void ScaleControl(SizeFfactor,BoundsSpecifiedspecified)
{
if ((factor.Width != 1f) && (factor.Height != 1f))
{
this .fontHeight = - 1 ;
this .cachedFont = null ;
}
base .ScaleControl(factor,specified);
}
public override string ToString()
{
return ( base .ToString() + " ,Text: " + this .Text);
}
private void WmEraseBkgnd( ref Messagem)
{
Win32.RECTrect = new Win32.RECT();
Win32.GetClientRect( new HandleRef( this , base .Handle), ref rect);
using (Graphicsgraphics = Graphics.FromHdcInternal(m.WParam))
{
using (Brushbrush = new SolidBrush( this .BackColor))
{
graphics.FillRectangle(brush,rect.Left,rect.Top,rect.Right - rect.Left,rect.Bottom - rect.Top);
}
}
m.Result = (IntPtr) 1 ;
}
[SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc( ref Messagem)
{
if ( this .OwnerDraw)
{
base .WndProc( ref m);
}
else
{
int msg = m.Msg;
if (msg != 20 )
{
if (msg == 0x3d )
{
base .WndProc( ref m);
if ((( int )(( long )m.LParam)) == - 12 )
{
m.Result = IntPtr.Zero;
}
return ;
}
if (msg != 0x318 )
{
base .WndProc( ref m);
return ;
}
}
this .WmEraseBkgnd( ref m);
}
}
// Properties
[EditorBrowsable(EditorBrowsableState.Advanced),Browsable( false )]
public override bool AllowDrop
{
get
{
return base .AllowDrop;
}
set
{
base .AllowDrop = value;
}
}
[EditorBrowsable(EditorBrowsableState.Always),DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),Browsable( true )]
public override bool AutoSize
{
get
{
return base .AutoSize;
}
set
{
base .AutoSize = value;
}
}
[Category( " CatLayout " ),Localizable( true ),Browsable( true ),DefaultValue( 1 ),Description( " ControlAutoSizeModeDescr " )]
public AutoSizeModeAutoSizeMode
{
get
{
return base .GetAutoSizeMode();
}
set
{
if ( ! ClientUtils.IsEnumValid(value,( int )value, 0 , 1 ))
{
throw new InvalidEnumArgumentException( " value " ,( int )value, typeof (AutoSizeMode));
}
}
}
internal virtual ControlParentInternal
{
get
{
return this .Parent;
}
set
{
if ( this .Parent != value)
{
if (value != null )
{
value.Controls.Add( this );
}
else
{
this .Parent.Controls.Remove( this );
}
}
}
}
protected override CreateParamsCreateParams
{
[SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.UnmanagedCode)]
get
{
CreateParamscreateParams = base .CreateParams;
if ( ! this .OwnerDraw)
{
createParams.ClassName = " BUTTON " ;
createParams.Style |= 7 ;
}
else
{
createParams.ClassName = null ;
createParams.Style &= - 8 ;
}
createParams.ExStyle |= 0x10000 ;
return createParams;
}
}
protected override PaddingDefaultPadding
{
get
{
return new Padding( 3 );
}
}
protected override SizeDefaultSize
{
get
{
return new Size( 200 , 100 );
}
}
public override RectangleDisplayRectangle
{
get
{
SizeclientSize = base .ClientSize;
if ( this .fontHeight == - 1 )
{
this .fontHeight = this .Font.Height;
this .cachedFont = this .Font;
}
else if ( ! object .ReferenceEquals( this .cachedFont, this .Font))
{
this .fontHeight = this .Font.Height;
this .cachedFont = this .Font;
}
Paddingpadding = base .Padding;
return new Rectangle(padding.Left, this .fontHeight + padding.Top,Math.Max(clientSize.Width - padding.Horizontal, 0 ),Math.Max((clientSize.Height - this .fontHeight) - padding.Vertical, 0 ));
}
}
// [Description("ButtonFlatStyleDescr"),Category("CatAppearance"),DefaultValue(2)]
public FlatStyleFlatStyle
{
get
{
return this .flatStyle;
}
set
{
if ( ! ClientUtils.IsEnumValid(value,( int )value, 0 , 3 ))
{
throw new InvalidEnumArgumentException( " value " ,( int )value, typeof (FlatStyle));
}
if ( this .flatStyle != value)
{
bool ownerDraw = this .OwnerDraw;
this .flatStyle = value;
bool flag2 = this .OwnerDraw != ownerDraw;
base .SetStyle(ControlStyles.ContainerControl, true );
base .SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserMouse | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, this .OwnerDraw);
if (flag2)
{
base .RecreateHandle();
}
else
{
this .Refresh();
}
}
}
}
private bool OwnerDraw
{
get
{
return ( this .FlatStyle != FlatStyle.System);
}
}
public bool SupportsUseCompatibleTextRendering
{
get
{
return true ;
}
}
[Browsable( false ),EditorBrowsable(EditorBrowsableState.Advanced)]
public bool TabStop
{
get
{
return base .TabStop;
}
set
{
base .TabStop = value;
}
}
[Localizable( true )]
public override string Text
{
get
{
return base .Text;
}
set
{
bool visible = base .Visible;
try
{
if (visible && base .IsHandleCreated)
{
this .SendMessage( 11 , 0 , 0 );
}
base .Text = value;
}
finally
{
if (visible && base .IsHandleCreated)
{
this .SendMessage( 11 , 1 , 0 );
}
}
base .Invalidate( true );
}
}
// NestedTypes
[ComVisible( true )]
internal class GroupBoxAccessibleObject:Control.ControlAccessibleObject
{
// Methods
internal GroupBoxAccessibleObject(GroupBoxowner)
: base (owner)
{
}
// Properties
public override AccessibleRoleRole
{
get
{
AccessibleRoleaccessibleRole = base .Owner.AccessibleRole;
if (accessibleRole != AccessibleRole.Default)
{
return accessibleRole;
}
return AccessibleRole.Grouping;
}
}
}
internal IntPtrSendMessage( int msg, int wparam, int lparam)
{
return new IntPtr(Win32.SendMessage( this .Handle,msg,wparam,lparam));
}
}
}