WinForm自定義控件應(yīng)用實例
C#的WinForm有一些控件具備自繪的功能,這就意味著你可以對這些控件進行自繪,可以起到意想不到的視覺效果。本文所述的以下控件就是通過一些簡單的控件轉(zhuǎn)變過來的。具體示例如下:
1、橫向選項卡重繪:
這里的“橫向”對話框其實是通過一個TabControl進行“方向旋轉(zhuǎn)”、重繪控件項等操作進行實現(xiàn)的。步驟如下:
①.Alignment:用于控制選項卡的方向(設(shè)置為Left)。
②.SizeMode:用于調(diào)整每個選項卡,默認(rèn)是Normal(非自繪模式),此處應(yīng)該設(shè)置為Fixed(固定模式),則允許自繪。
③.設(shè)置ItemSize(注意每一個選項卡因為是“橫向”的,但是這些單元卡的Width或者是Height確實按照原來“豎向”的選項卡進行處理的。因此Height其實是橫向選項卡的“寬度”,而Width確實選項卡的“高度”,注意不要混淆)。
④.最后重繪DrawItem,這一步也就是最重要的(為了顯示文字)。每次Draw_Item會在創(chuàng)建了TabPage之后被調(diào)用。此時你應(yīng)該設(shè)定繪制文字的起始點(定義X,Y)。
具體實現(xiàn)代碼如下:
C#部分代碼:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) { e.DrawFocusRectangle(); e.DrawBackground(); e.Graphics.DrawString("標(biāo)簽" + (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("標(biāo)簽" & Convert.ToString((e.Index + 1)), SystemFonts.DefaultFont, Brushes.Black, New PointF(e.Bounds.X + 5, e.Bounds.Y + 5)) End Sub
注意:程序的DrawFocusRectangle和DrawBackGound分別是繪制聚焦虛框和選定一個選項卡之后背景變成藍(lán)色。如果省略則無法呈現(xiàn)選中的效果。
2、顏色選項卡重繪:
Combobox和TabControl一樣每一個Item都可以重繪。重要屬性如下:
①.ItemHeight:設(shè)置每項項目的重繪高度。
②.DrawMode:重繪樣式(分為:Normal一般模式,不支持重繪;OwnerDrawFixed:自繪模式,固定高度,OwnerDrawVariable:自繪模式,可以在MesureItem中重新為每一項調(diào)整高度進行繪制)。
③.重繪Draw_Item。
全部代碼如下:
C#部分代碼:
public partial class Form1 : Form { /// <summary> /// 綁定下拉列表的Color類 /// </summary> private class ColorInfo { /// <summary> /// 顏色名稱 /// </summary> public string ColorName { get; set; } /// <summary> /// 對應(yīng)的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); //內(nèi)框用枚舉出來的顏色填充 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> ''' 對應(yīng)的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) '內(nèi)框用枚舉出來的顏色填充 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
相關(guān)文章
C#基礎(chǔ)語法:結(jié)構(gòu)和類區(qū)別詳解
這篇文章主要介紹了C#基礎(chǔ)語法:結(jié)構(gòu)和類詳解,本文總結(jié)了一些結(jié)構(gòu)和類的不同之處并給出了測試區(qū)別特性代碼,需要的朋友可以參考下2015-06-06Unity 實現(xiàn)框選游戲戰(zhàn)斗單位的思路詳解
這篇文章主要介紹了Unity 如何實現(xiàn)框選游戲戰(zhàn)斗單位,本文簡單介紹如何實現(xiàn)即時戰(zhàn)略游戲中框選戰(zhàn)斗單位的功能,需要的朋友可以參考下2022-12-12c#實現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
這篇文章主要介紹了c#實現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志的示例,代碼分為客戶端和服務(wù)端,客戶端可安裝為本地服務(wù)形式啟動2014-01-01Unity中 ShaderGraph 實現(xiàn)超級炫酷的溶解效果入門級教程
這篇文章主要介紹了Unity中的 ShaderGraph 實現(xiàn)超級炫酷的溶解效果入門級教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07c# 如何使用結(jié)構(gòu)體實現(xiàn)共用體
這篇文章主要介紹了c# 如何使用結(jié)構(gòu)體實現(xiàn)共用體,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04C#利用Random得隨機數(shù)求均值、方差、正態(tài)分布的方法
這篇文章主要介紹了C#利用Random得隨機數(shù)求均值、方差、正態(tài)分布的方法,涉及C#數(shù)學(xué)運算及概率統(tǒng)計的相關(guān)技巧,需要的朋友可以參考下2015-05-05