WinForm自界說控件運用實例。本站提示廣大學習愛好者:(WinForm自界說控件運用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm自界說控件運用實例正文
C#的WinForm有一些控件具有自繪的功效,這就意味著你可以對這些控件停止自繪,可以起到意想不到的視覺後果。本文所述的以下控件就是經由過程一些簡略的控件改變過去的。詳細示例以下:
1、橫向選項卡重繪:

這裡的“橫向”對話框實際上是經由過程一個TabControl停止“偏向扭轉”、重繪控件項等操作停止完成的。步調以下:
①.Alignment:用於掌握選項卡的偏向(設置為Left)。
②.SizeMode:用於調劑每一個選項卡,默許是Normal(非自繪形式),此處應當設置為Fixed(固定形式),則許可自繪。
③.設置ItemSize(留意每個選項卡由於是“橫向”的,然則這些單位卡的Width或許是Height確切依照本來“豎向”的選項卡停止處置的。是以Height實際上是橫向選項卡的“寬度”,而Width確切選項卡的“高度”,留意不要混雜)。
④.最初重繪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都可以重繪。主要屬性以下:
①.ItemHeight:設置每項項目標重繪高度。
②.DrawMode:重繪款式(分為:Normal普通形式,不支撐重繪;OwnerDrawFixed:自繪形式,固定高度,OwnerDrawVariable:自繪形式,可以在MesureItem中從新為每項調劑高度停止繪制)。
③.重繪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