http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 15443|回复: 1

[C#皮肤] C#皮肤-GroupBox 控件实现

[复制链接]
发表于 2013-3-8 11:57:43 | 显示全部楼层 |阅读模式
C#皮肤-GroupBox 控件实现

导读部分
-------------------------------------------------------------------------------------------------------------
C#皮肤-实现原理系列文章导航
http://www.sufeinet.com/thread-2-1-1.html

这一次我们先要研究一下系统的是怎么完成的


           我们使用Reflector反编译一下GroupBox一起来看看它的内部是怎么实现的。
从类的开始第一行我们可以看得出来它是继承Control这个类而来的,下面是所有引用的命名空间和继承的源
[C#] 纯文本查看 复制代码
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



其实这里告诉我们这样几个问题
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事件来说明
[C#] 纯文本查看 复制代码
EditorBrowsable(EditorBrowsableState.Always)


用上面的语句指定属性和方法是否在编辑器里的可见方式  在这里我们合作Always代表是编辑器里是始终可见的
然后让它显示在属性窗口中
[C#] 纯文本查看 复制代码
Browsable(true)

看一下MS的实现方式吧
[C#] 纯文本查看 复制代码
 [EditorBrowsable(EditorBrowsableState.Always), System.Windows.Forms.SRCategory("CatPropertyChanged"), System.Windows.Forms.SRDescription("ControlOnAutoSizeChangedDescr"), Browsable(true)]
        public event EventHandler AutoSizeChanged
        {
            add
            {
                base.AutoSizeChanged += value;
            }
            remove
            {
                base.AutoSizeChanged -= value;
            }
        }

我们现在再来看Click事件的实现就更为简单了
[C#] 纯文本查看 复制代码
[Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler Click
        {
            add
            {
                base.Click += value;
            }
            remove
            {
                base.Click -= value;
            }
        }


以后的事件基本上和这个差不多,就不再多说了

具体的实现

我们一起来看看他的构造方法
[C#] 纯文本查看 复制代码
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;
        }

呵呵 ,其实这里跟我用来实现Combox控件时的情况差不了多少。
          DrawGroupBox事件的实现方法如果你看过我的Combox的实现的话应该很容易能看明白,
[C#] 纯文本查看 复制代码
 private void DrawGroupBox(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle clientRectangle = base.ClientRectangle;
            int num = 8;
            Color disabledColor = base.DisabledColor;
            Pen pen = new Pen(ControlPaint.Light(disabledColor, 1f));
            Pen pen2 = new Pen(ControlPaint.Dark(disabledColor, 0f));
            clientRectangle.X += num;
            clientRectangle.Width -= 2 * num;
            try
            {
                Size size;
                int num2;
                if (this.UseCompatibleTextRendering)
                {
                    using (Brush brush = new SolidBrush(this.ForeColor))
                    {
                        using (StringFormat format = 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 (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(graphics))
                {
                    IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
                    if (!this.ShowKeyboardCues)
                    {
                        flags |= IntTextFormatFlags.HidePrefix;
                    }
                    if (this.RightToLeft == RightToLeft.Yes)
                    {
                        flags |= IntTextFormatFlags.RightToLeft;
                        flags |= IntTextFormatFlags.Right;
                    }
                    using (WindowsFont font = 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的方法没有多少高明之处啊,不过还是值得我敬佩的
我把所有的代码都放上来大家参考一下吧,然后再接着说我的控件是怎么实现的
[C#] 纯文本查看 复制代码
namespace System.Windows.Forms
{
    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 Font cachedFont;
        private System.Windows.Forms.FlatStyle flatStyle = 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 EventHandler AutoSizeChanged
        {
            add
            {
                base.AutoSizeChanged += value;
            }
            remove
            {
                base.AutoSizeChanged -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler Click
        {
            add
            {
                base.Click += value;
            }
            remove
            {
                base.Click -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler DoubleClick
        {
            add
            {
                base.DoubleClick += value;
            }
            remove
            {
                base.DoubleClick -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event KeyEventHandler KeyDown
        {
            add
            {
                base.KeyDown += value;
            }
            remove
            {
                base.KeyDown -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event KeyPressEventHandler KeyPress
        {
            add
            {
                base.KeyPress += value;
            }
            remove
            {
                base.KeyPress -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event KeyEventHandler KeyUp
        {
            add
            {
                base.KeyUp += value;
            }
            remove
            {
                base.KeyUp -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseClick
        {
            add
            {
                base.MouseClick += value;
            }
            remove
            {
                base.MouseClick -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseDoubleClick
        {
            add
            {
                base.MouseDoubleClick += value;
            }
            remove
            {
                base.MouseDoubleClick -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseDown
        {
            add
            {
                base.MouseDown += value;
            }
            remove
            {
                base.MouseDown -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler MouseEnter
        {
            add
            {
                base.MouseEnter += value;
            }
            remove
            {
                base.MouseEnter -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler MouseLeave
        {
            add
            {
                base.MouseLeave += value;
            }
            remove
            {
                base.MouseLeave -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseMove
        {
            add
            {
                base.MouseMove += value;
            }
            remove
            {
                base.MouseMove -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseUp
        {
            add
            {
                base.MouseUp += value;
            }
            remove
            {
                base.MouseUp -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler TabStopChanged
        {
            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 AccessibleObject CreateAccessibilityInstance()
        {
            return new GroupBoxAccessibleObject(this);
        }

        private void DrawGroupBox(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle clientRectangle = base.ClientRectangle;
            int num = 8;
            Color disabledColor = base.DisabledColor;
            Pen pen = new Pen(ControlPaint.Light(disabledColor, 1f));
            Pen pen2 = new Pen(ControlPaint.Dark(disabledColor, 0f));
            clientRectangle.X += num;
            clientRectangle.Width -= 2 * num;
            try
            {
                Size size;
                int num2;
                if (this.UseCompatibleTextRendering)
                {
                    using (Brush brush = new SolidBrush(this.ForeColor))
                    {
                        using (StringFormat format = 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 (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(graphics))
                {
                    IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
                    if (!this.ShowKeyboardCues)
                    {
                        flags |= IntTextFormatFlags.HidePrefix;
                    }
                    if (this.RightToLeft == RightToLeft.Yes)
                    {
                        flags |= IntTextFormatFlags.RightToLeft;
                        flags |= IntTextFormatFlags.Right;
                    }
                    using (WindowsFont font = 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 Size GetPreferredSizeCore(Size proposedSize)
        {
            Size size2 = (this.SizeFromClientSize(Size.Empty) + new Size(0, this.fontHeight)) + base.Padding.Size;
            return (this.LayoutEngine.GetPreferredSize(this, proposedSize - size2) + size2);
        }

        protected override void OnFontChanged(EventArgs e)
        {
            this.fontHeight = -1;
            this.cachedFont = null;
            base.Invalidate();
            base.OnFontChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if ((Application.RenderWithVisualStyles && (base.Width >= 10)) && (base.Height >= 10))
            {
                GroupBoxState state = base.Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled;
                TextFormatFlags flags = 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)
                {
                    Color textColor = 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(SizeF factor, BoundsSpecified specified)
        {
            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 Message m)
        {
            System.Windows.Forms.NativeMethods.RECT rect = new System.Windows.Forms.NativeMethods.RECT();
            System.Windows.Forms.SafeNativeMethods.GetClientRect(new HandleRef(this, base.Handle), ref rect);
            using (Graphics graphics = Graphics.FromHdcInternal(m.WParam))
            {
                using (Brush brush = 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 Message m)
        {
            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.AutoSizeMode AutoSizeMode
        {
            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.CreateParams CreateParams
        {
            [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
            get
            {
                System.Windows.Forms.CreateParams createParams = 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 Padding DefaultPadding
        {
            get
            {
                return new Padding(3);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(200, 100);
            }
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Size clientSize = 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;
                }
                Padding padding = 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.FlatStyle FlatStyle
        {
            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.GroupBox owner) : base(owner)
            {
            }

            public override AccessibleRole Role
            {
                get
                {
                    AccessibleRole accessibleRole = base.Owner.AccessibleRole;
                    if (accessibleRole != AccessibleRole.Default)
                    {
                        return accessibleRole;
                    }
                    return AccessibleRole.Grouping;
                }
            }
        }
    }
}

我先拿两个事件做为比较吧,如果大家不细细的看的话应当是没有什么分别的
[C#] 纯文本查看 复制代码
// Events
        [Category("CatPropertyChanged"), EditorBrowsable(EditorBrowsableState.Always), Description("ControlOnAutoSizeChangedDescr"), Browsable(true)]
        public event EventHandler AutoSizeChanged
        {
            add
            {
                base.AutoSizeChanged += value;
            }
            remove
            {
                base.AutoSizeChanged -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler Click
        {
            add
            {
                base.Click += value;
            }
            remove
            {
                base.Click -= value;
            }
        }

下面就是我的控件的实现方法,大家可以做一个比较和参考
[C#] 纯文本查看 复制代码
using System;
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
{
    //public class GroupBox : System.Windows.Forms.GroupBox
    //{
    //    public GroupBox()
    //        : base()
    //    {
    //        this.BackColor = Color.Transparent;
    //    }

    //    [DefaultValue(typeof(Color), "Transparent")]   
    //    public override System.Drawing.Color BackColor
    //    {
    //        get
    //        {
    //            return base.BackColor;
    //        }
    //        set
    //        {
    //            base.BackColor = value;
    //        }
    //    }

    //    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    //    {
    //        base.OnPaint(e);

    //    //e.Graphics.DrawString(this.Text, this.Font, Brushes.Red, 10, 1);
    //    e.Graphics.DrawLine(new Pen(Shared.ControlBorderBackColor), 1, 7, 8, 7);
    //    e.Graphics.DrawLine(new Pen(Shared.ControlBorderBackColor), 90, 7, this.Width - 2, 7);
    //    e.Graphics.DrawLine(new Pen(Shared.ControlBorderBackColor), 1, 7, 1, this.Height - 2);
    //    e.Graphics.DrawLine(new Pen(Shared.ControlBorderBackColor), 1, this.Height - 2, this.Width - 2, this.Height - 2);
    //    e.Graphics.DrawLine(new Pen(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 Font cachedFont;
        private FlatStyle flatStyle = FlatStyle.Standard;
        private int fontHeight = -1;

        // Events
        [Category("CatPropertyChanged"), EditorBrowsable(EditorBrowsableState.Always), Description("ControlOnAutoSizeChangedDescr"), Browsable(true)]
        public event EventHandler AutoSizeChanged
        {
            add
            {
                base.AutoSizeChanged += value;
            }
            remove
            {
                base.AutoSizeChanged -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler Click
        {
            add
            {
                base.Click += value;
            }
            remove
            {
                base.Click -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler DoubleClick
        {
            add
            {
                base.DoubleClick += value;
            }
            remove
            {
                base.DoubleClick -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event KeyEventHandler KeyDown
        {
            add
            {
                base.KeyDown += value;
            }
            remove
            {
                base.KeyDown -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event KeyPressEventHandler KeyPress
        {
            add
            {
                base.KeyPress += value;
            }
            remove
            {
                base.KeyPress -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event KeyEventHandler KeyUp
        {
            add
            {
                base.KeyUp += value;
            }
            remove
            {
                base.KeyUp -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event MouseEventHandler MouseClick
        {
            add
            {
                base.MouseClick += value;
            }
            remove
            {
                base.MouseClick -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event MouseEventHandler MouseDoubleClick
        {
            add
            {
                base.MouseDoubleClick += value;
            }
            remove
            {
                base.MouseDoubleClick -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseDown
        {
            add
            {
                base.MouseDown += value;
            }
            remove
            {
                base.MouseDown -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler MouseEnter
        {
            add
            {
                base.MouseEnter += value;
            }
            remove
            {
                base.MouseEnter -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event EventHandler MouseLeave
        {
            add
            {
                base.MouseLeave += value;
            }
            remove
            {
                base.MouseLeave -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseMove
        {
            add
            {
                base.MouseMove += value;
            }
            remove
            {
                base.MouseMove -= value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public event MouseEventHandler MouseUp
        {
            add
            {
                base.MouseUp += value;
            }
            remove
            {
                base.MouseUp -= value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
        public event EventHandler TabStopChanged
        {
            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 AccessibleObject CreateAccessibilityInstance()
        {
            return new GroupBoxAccessibleObject(this);
        }

        private void DrawGroupBox(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle clientRectangle = base.ClientRectangle;
            int num = 8;
            Color disabledColor = SystemColors.Control; // base.DisabledColor;
            //Pen pen = new Pen(ControlPaint.Light(disabledColor, 1f));
            Pen pen = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black : Shared.ControlBorderBackColor, 0.3f);
            //Pen pen2 = new Pen(ControlPaint.Dark(disabledColor, 0f));
            Pen pen2 = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black : Shared.ControlBorderBackColor, 0f);
            clientRectangle.X += num;
            clientRectangle.Width -= 2 * num;
            try
            {
                Size size;
                int num2;
               

                IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak;
                if (!this.ShowKeyboardCues)
                {
                    flags |= IntTextFormatFlags.HidePrefix;
                }
                if (this.RightToLeft == RightToLeft.Yes)
                {
                    flags |= IntTextFormatFlags.RightToLeft;
                    flags |= IntTextFormatFlags.Right;
                }

                Graphics graphics2 = e.Graphics;

                size = graphics2.MeasureString(this.Text, this.Font).ToSize();
                if (base.Enabled)
                {
                   // graphics2.DrawString(this.Text, this.Font, new SolidBrush(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(EventArgs e)
        {
            this.fontHeight = -1;
            this.cachedFont = null;
            base.Invalidate();
            base.OnFontChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if ((Application.RenderWithVisualStyles && (base.Width >= 10)) && (base.Height >= 10))
            {
                GroupBoxState state = base.Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled;
                TextFormatFlags flags = 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(SizeF factor, BoundsSpecified specified)
        {
            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 Message m)
        {
            Win32.RECT rect = new Win32.RECT();
            Win32.GetClientRect(new HandleRef(this, base.Handle), ref rect);
            using (Graphics graphics = Graphics.FromHdcInternal(m.WParam))
            {
                using (Brush brush = 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 Message m)
        {
            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 AutoSizeMode AutoSizeMode
        {
            get
            {
                return base.GetAutoSizeMode();
            }
            set
            {
                if (!ClientUtils.IsEnumValid(value, (int)value, 0, 1))
                {
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(AutoSizeMode));
                }
               
            }
        }

        internal virtual Control ParentInternal
        {
            get
            {
                return this.Parent;
            }
            set
            {
                if (this.Parent != value)
                {
                    if (value != null)
                    {
                        value.Controls.Add(this);
                    }
                    else
                    {
                        this.Parent.Controls.Remove(this);
                    }
                }
            }
        }

        protected override CreateParams CreateParams
        {
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
            get
            {
                CreateParams createParams = 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 Padding DefaultPadding
        {
            get
            {
                return new Padding(3);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(200, 100);
            }
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Size clientSize = 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;
                }
                Padding padding = 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 FlatStyle FlatStyle
        {
            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);
            }
        }


        // Nested Types
        [ComVisible(true)]
        internal class GroupBoxAccessibleObject : Control.ControlAccessibleObject
        {
            // Methods
            internal GroupBoxAccessibleObject(GroupBox owner)
                : base(owner)
            {
            }

            // Properties
            public override AccessibleRole Role
            {
                get
                {
                    AccessibleRole accessibleRole = base.Owner.AccessibleRole;
                    if (accessibleRole != AccessibleRole.Default)
                    {
                        return accessibleRole;
                    }
                    return AccessibleRole.Grouping;
                }
            }
        }

        internal IntPtr SendMessage(int msg, int wparam, int lparam)
        {
            return new IntPtr(Win32.SendMessage(this.Handle, msg, wparam, lparam));
        }
    }
}

本帖被以下淘专辑推荐:

  • · 皮肤|主题: 15, 订阅: 0


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-12-28 22:53:50 | 显示全部楼层
感谢您的无私奉献,真是帮了我的大忙了
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-13 15:37

© 2014-2021

快速回复 返回顶部 返回列表