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