自定义控件——自绘

WinForm的一些控件具备自绘功能,这就意味着你可以对这些控件进行自绘,起到意想不到的效果。以下控件就是通过一些简单的控件转变过来的——

1、横向选项卡:

 

这个“横向”对话框其实是通过一个TabControl进行“方向旋转”、重绘控件项等操作进行实现的。步骤如下:

1.1)Alignment:用于控制选项卡的方向(设置为Left)。

1.2)SizeMode:用于调整每个选项卡,默认是Normal(非自绘模式),此处应该设置为Fixed(固定模式),则允许自绘。

1.3)设置ItemSize(注意每一个选项卡因为是“横向”的,但是这些单元卡的Width或者是Height确实按照原来“竖向”的选项卡进行处理的。因此Height其实是横向选项卡的“宽度”,而Width确实选项卡的“高度”,注意不要混淆)。

1.4)最后重绘DrawItem,这一步也就是最重要的(为了显示文字)。每次Draw_Item会在创建了TabPage之后被调用。此时你应该设定绘制文字的起始点(定义X,Y)。代码如下:

[C#]

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
                e.DrawFocusRectangle();
                e.DrawBackground();
                e.Graphics.DrawString("标签" + (e.Index + 1), SystemFonts.DefaultFont, Brushes.Black, new PointF(e.Bounds.X + 5, e.Bounds.Y +
                    5));
        }

[VB.NET]

Private Sub tabControl1_DrawItem(sender As Object, e As DrawItemEventArgs)
    e.DrawFocusRectangle()
    e.DrawBackground()
    e.Graphics.DrawString("标签" & Convert.ToString((e.Index + 1)), SystemFonts.DefaultFont, Brushes.Black, New PointF(e.Bounds.X + 5, e.Bounds.Y + 5))
End Sub

注意:程序的DrawFocusRectangle和DrawBackGound分别是绘制聚焦虚框和选定一个选项卡之后背景变成蓝色。如果省略则无法呈现选中的效果。

2、颜色选项卡:

Combobox和TabControl一样每一个Item都可以重绘。重要属性如下:

2.1)ItemHeight:设置每项项目的重绘高度。

2.2)DrawMode:重绘样式(分为:Normal一般模式,不支持重绘;OwnerDrawFixed:自绘模式,固定高度,OwnerDrawVariable:自绘模式,可以在MesureItem中重新为每一项调整高度进行绘制)。

2.3)重绘Draw_Item。

全部代码如下:

[C#]

public partial class Form1 : Form
    {
        /// <summary>
        /// 绑定下拉列表的Color类
        /// </summary>
        private class ColorInfo
        {
            /// <summary>
            /// 颜色名称
            /// </summary>
            public string ColorName { get; set; }
            /// <summary>
            /// 对应的Color实体
            /// </summary>
            public Color Color { get; set; }

            public static List<ColorInfo> GetAllColors()
            {
                Color c = new Color();
                List<ColorInfo> Colors = new List<ColorInfo>();
                foreach (var item in c.GetType().GetProperties())
                {
                    //排除非颜色的情况
                    if (item.GetValue(c, null) is Color)
                    {
                        Colors.Add(new ColorInfo { ColorName = item.Name, Color = (Color)item.GetValue(c, null) });
                    }
                }
                return Colors;
            }

        }
        

        public Form1()
        {
            InitializeComponent();
            comboBox1.DataSource = ColorInfo.GetAllColors();
            comboBox1.DisplayMember = "ColorName";
            comboBox1.ValueMember = "Color";
        }


        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            //绘制空心矩形框,起始点(0,5),宽度60,高度10
            Rectangle r = new Rectangle(e.Bounds.X, e.Bounds.Y+5, 60, 10);
            //外框是黑色
            e.Graphics.DrawRectangle(new Pen(Color.Black),r);
            //内框用枚举出来的颜色填充
            e.Graphics.FillRectangle(new SolidBrush((comboBox1.DataSource as List<ColorInfo>)[e.Index].Color), r);
            //绘制颜色名称,起始点每项都是Item中(70,5)
            e.Graphics.DrawString((comboBox1.DataSource as List<ColorInfo>)[e.Index].ColorName, SystemFonts.DefaultFont, Brushes.Black, new PointF(e.Bounds.X + 70, e.Bounds.Y + 5));
        }

    }

[VB.NET]

Public Partial Class Form1
    Inherits Form
    ''' <summary>
    ''' 绑定下拉列表的Color类
    ''' </summary>
    Private Class ColorInfo
        ''' <summary>
        ''' 颜色名称
        ''' </summary>
        Public Property ColorName() As String
            Get
                Return m_ColorName
            End Get
            Set
                m_ColorName = Value
            End Set
        End Property
        Private m_ColorName As String
        ''' <summary>
        ''' 对应的Color实体
        ''' </summary>
        Public Property Color() As Color
            Get
                Return m_Color
            End Get
            Set
                m_Color = Value
            End Set
        End Property
        Private m_Color As Color

        Public Shared Function GetAllColors() As List(Of ColorInfo)
            Dim c As New Color()
            Dim Colors As New List(Of ColorInfo)()
            For Each item As var In c.[GetType]().GetProperties()
                '排除非颜色的情况
                If TypeOf item.GetValue(c, Nothing) Is Color Then
                    Colors.Add(New ColorInfo() With { _
                        Key .ColorName = item.Name, _
                        Key .Color = DirectCast(item.GetValue(c, Nothing), Color) _
                    })
                End If
            Next
            Return Colors
        End Function

    End Class


    Public Sub New()
        InitializeComponent()
        comboBox1.DataSource = ColorInfo.GetAllColors()
        comboBox1.DisplayMember = "ColorName"
        comboBox1.ValueMember = "Color"
    End Sub


    Private Sub comboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
        e.DrawBackground()
        e.DrawFocusRectangle()
        '绘制空心矩形框,起始点(0,5),宽度60,高度10
        Dim r As New Rectangle(e.Bounds.X, e.Bounds.Y + 5, 60, 10)
        '外框是黑色
        e.Graphics.DrawRectangle(New Pen(Color.Black), r)
        '内框用枚举出来的颜色填充
        e.Graphics.FillRectangle(New SolidBrush(TryCast(comboBox1.DataSource, List(Of ColorInfo))(e.Index).Color), r)
        '绘制颜色名称,起始点每项都是Item中(70,5)
        e.Graphics.DrawString(TryCast(comboBox1.DataSource, List(Of ColorInfo))(e.Index).ColorName, SystemFonts.DefaultFont, Brushes.Black, New PointF(e.Bounds.X + 70, e.Bounds.Y + 5))
    End Sub
End Class

 

posted @ 2012-07-07 14:08  Serviceboy  阅读(1328)  评论(1)    收藏  举报