[VB.net]绘制具有渐变颜色和防锯齿字体的标题

1。新建一个项目
2。添加一个用户控件“PaneCaption.vb”
3。[操作]调整控件大小为150×30
4。打开代码编辑器:

ContractedBlock.gif ExpandedBlockStart.gif
None.gifImports System.Drawing.Drawing2D
None.gif
Imports System.ComponentModel
ExpandedBlockStart.gifContractedBlock.gif
Public Class PaneCaptionClass PaneCaption
InBlock.gif    
' 常量设置
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private Class ConstsClass Consts
InBlock.gif        
Public Const DefaultHeight As Integer = 20
InBlock.gif        
Public Const DefaultFontName As String = "arial"
InBlock.gif        
Public Const DefaultFontSize As Integer = 9
InBlock.gif        
Public Const PosOffset As Integer = 4
ExpandedSubBlockEnd.gif    
End Class

InBlock.gif
InBlock.gif    
' 内部成员
InBlock.gif
    Private _active As Boolean = False
InBlock.gif    
Private _antiAlias As Boolean = True
InBlock.gif    
Private _allowActive As Boolean = True
InBlock.gif
InBlock.gif    
Private _colorActiveText As Color = Color.Black
InBlock.gif    
Private _colorInactiveText As Color = Color.White
InBlock.gif
InBlock.gif    
Private _colorActiveLow As Color = Color.FromArgb(25516578)
InBlock.gif    
Private _colorActiveHigh As Color = Color.FromArgb(255225155)
InBlock.gif    
Private _colorInactiveLow As Color = Color.FromArgb(355145)
InBlock.gif    
Private _colorInactiveHigh As Color = Color.FromArgb(90135215)
InBlock.gif
InBlock.gif    
' 绘图对象
InBlock.gif
    Private _brushActiveText As SolidBrush
InBlock.gif    
Private _brushInactiveText As SolidBrush
InBlock.gif    
Private _brushActive As LinearGradientBrush
InBlock.gif    
Private _brushInactive As LinearGradientBrush
InBlock.gif    
Private _format As StringFormat
InBlock.gif
InBlock.gif
InBlock.gif    
' 公共参数
InBlock.gif

InBlock.gif    
' 控件标题
InBlock.gif
    <Category("Appearance"), Browsable(True), _
InBlock.gif    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
InBlock.gif    Description(
"Text that is displayed in the label.")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shadows Property Text()Property Text() As String
InBlock.gif        
Get
InBlock.gif            
Return MyBase.Text
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            
MyBase.Text = Value
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' 标题栏是否激活
InBlock.gif
    <Description("The active state of the caption, draws the caption with different gradient colors."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(False)> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property Active()Property Active() As Boolean
InBlock.gif        
Get
InBlock.gif            
Return _active
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Boolean)
InBlock.gif            _active 
= value
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' 是否应该包含活动和非活动状态
InBlock.gif
    <Description("True always uses the inactive state colors, false maintains an active and inactive state."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(True)> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property AllowActive()Property AllowActive() As Boolean
InBlock.gif        
Get
InBlock.gif            
Return _allowActive
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Boolean)
InBlock.gif            _allowActive 
= value
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' 防锯齿模式
InBlock.gif
    <Description("If should draw the text as antialiased."), _
InBlock.gif     Category(
"Appearance"), DefaultValue(True)> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property AntiAlias()Property AntiAlias() As Boolean
InBlock.gif        
Get
InBlock.gif            
Return _antiAlias
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Boolean)
InBlock.gif            _antiAlias 
= value
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
颜色属性#Region " 颜色属性 "
InBlock.gif
InBlock.gif    
<Description("Color of the text when active."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(GetType(Color), "Black")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property ActiveTextColor()Property ActiveTextColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorActiveText
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.Black
InBlock.gif            _colorActiveText 
= Value
InBlock.gif            _brushActiveText 
= New SolidBrush(_colorActiveText)
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("Color of the text when inactive."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(GetType(Color), "White")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property InactiveTextColor()Property InactiveTextColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorInactiveText
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.White
InBlock.gif            _colorInactiveText 
= Value
InBlock.gif            _brushInactiveText 
= New SolidBrush(_colorInactiveText)
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("Low color of the active gradient."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(GetType(Color), "255, 165, 78")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property ActiveGradientLowColor()Property ActiveGradientLowColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorActiveLow
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.FromArgb(25516578)
InBlock.gif            _colorActiveLow 
= Value
InBlock.gif            CreateGradientBrushes()
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("High color of the active gradient."), _
InBlock.gif    Category(
"Appearance"), DefaultValue(GetType(Color), "255, 225, 155")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property ActiveGradientHighColor()Property ActiveGradientHighColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorActiveHigh
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.FromArgb(255225155)
InBlock.gif            _colorActiveHigh 
= Value
InBlock.gif            CreateGradientBrushes()
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("Low color of the inactive gradient."), _
InBlock.gif      Category(
"Appearance"), DefaultValue(GetType(Color), "3, 55, 145")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Public Property InactiveGradientLowColor()Property InactiveGradientLowColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorInactiveLow
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.FromArgb(355145)
InBlock.gif            _colorInactiveLow 
= Value
InBlock.gif            CreateGradientBrushes()
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("High color of the inactive gradient."), _
InBlock.gif      Category(
"Appearance"), DefaultValue(GetType(Color), "90, 135, 215")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Public Property InactiveGradientHighColor()Property InactiveGradientHighColor() As Color
InBlock.gif        
Get
InBlock.gif            
Return _colorInactiveHigh
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Color)
InBlock.gif            
If Value.Equals(Color.Empty) Then Value = Color.FromArgb(90135215)
InBlock.gif            _colorInactiveHigh 
= Value
InBlock.gif            CreateGradientBrushes()
InBlock.gif            Invalidate()
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
InBlock.gif    
' 内部属性
InBlock.gif

InBlock.gif    
' 绘制标题的画刷
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private ReadOnly Property TextBrush()Property TextBrush() As SolidBrush
InBlock.gif        
Get
InBlock.gif            
Return DirectCast(IIf(_active AndAlso _allowActive, _
InBlock.gif             _brushActiveText, _brushInactiveText), SolidBrush)
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' 背景渐变色
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private ReadOnly Property BackBrush()Property BackBrush() As LinearGradientBrush
InBlock.gif        
Get
InBlock.gif            
Return DirectCast(IIf(_active AndAlso _allowActive, _
InBlock.gif             _brushActive, _brushInactive), LinearGradientBrush)
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub New()Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
' 窗体设计器调用
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
' 设置双缓冲样式
InBlock.gif
        Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or _
InBlock.gif         ControlStyles.AllPaintingInWmPaint 
Or ControlStyles.ResizeRedraw, True)
InBlock.gif
InBlock.gif        
' 初始化高度
InBlock.gif
        Me.Height = Consts.DefaultHeight
InBlock.gif
InBlock.gif        
' 文本的格式
InBlock.gif
        _format = New StringFormat
InBlock.gif        _format.FormatFlags 
= StringFormatFlags.NoWrap
InBlock.gif        _format.LineAlignment 
= StringAlignment.Center
InBlock.gif        _format.Trimming 
= StringTrimming.EllipsisCharacter
InBlock.gif
InBlock.gif        
' 初始化字体
InBlock.gif
        Me.Font = New Font(Consts.DefaultFontName, Consts.DefaultFontSize, FontStyle.Bold)
InBlock.gif
InBlock.gif        
' 创建GDI对象
InBlock.gif
        Me.ActiveTextColor = _colorActiveText
InBlock.gif        
Me.InactiveTextColor = _colorInactiveText
InBlock.gif
InBlock.gif        
' 创建渐变颜色效果画刷
InBlock.gif
        CreateGradientBrushes()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' 内部成员
InBlock.gif

InBlock.gif    
' 需要重绘标题
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnPaint()Sub OnPaint(ByVal e As PaintEventArgs)
InBlock.gif        DrawCaption(e.Graphics)
InBlock.gif        
MyBase.OnPaint(e)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' 绘制标题
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private Sub DrawCaption()Sub DrawCaption(ByVal g As Graphics)
InBlock.gif        
' 背景
InBlock.gif
        g.FillRectangle(Me.BackBrush, Me.DisplayRectangle)
InBlock.gif
InBlock.gif        
' 标题
InBlock.gif
        If _antiAlias Then
InBlock.gif            g.TextRenderingHint 
= Drawing.Text.TextRenderingHint.AntiAlias
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
' 使用省略号时需要一个矩形
InBlock.gif
        Dim bounds As RectangleF = New RectangleF(Consts.PosOffset, 0, _
InBlock.gif         
Me.DisplayRectangle.Width - Consts.PosOffset, Me.DisplayRectangle.Height)
InBlock.gif
InBlock.gif        g.DrawString(
Me.Text, Me.Font, Me.TextBrush, bounds, _format)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' 重写该函数,处理单击标题产生的事件
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnMouseDown()Sub OnMouseDown(ByVal e As MouseEventArgs)
InBlock.gif        
MyBase.OnMouseDown(e)
InBlock.gif        
If Me._allowActive Then Me.Focus()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overrides Sub OnSizeChanged()Sub OnSizeChanged(ByVal e As System.EventArgs)
InBlock.gif        
MyBase.OnSizeChanged(e)
InBlock.gif
InBlock.gif        
' 重绘
InBlock.gif
        CreateGradientBrushes()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub CreateGradientBrushes()Sub CreateGradientBrushes()
InBlock.gif        
' 允许重绘的高度和宽度条件
InBlock.gif
        If Me.Width > 0 AndAlso Me.Height > 0 Then
InBlock.gif            
If Not (_brushActive Is NothingThen _brushActive.Dispose()
InBlock.gif            _brushActive 
= New LinearGradientBrush(Me.DisplayRectangle, _
InBlock.gif             _colorActiveHigh, _colorActiveLow, LinearGradientMode.Vertical)
InBlock.gif
InBlock.gif            
If Not (_brushInactive Is NothingThen _brushInactive.Dispose()
InBlock.gif            _brushInactive 
= New LinearGradientBrush(Me.DisplayRectangle, _
InBlock.gif              _colorInactiveHigh, _colorInactiveLow, LinearGradientMode.Vertical)
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class


5。按F5编译并保存
6。 建立一个基类 “BasePane.vb”
该基类将使用到前面定义的面板标题类,也属于用户控件。提供对绘图、改变大小、焦点控制等事件进行处理的基本功能。
7。[操作]双击之前生成的用户控件PaneCaption加入到工作区,该操作将定义一个PaneCaption1的实例,改名为"caption"。注意:新的组建要在重新编译后才能在工具栏中出现。
8。打开代码编辑器:

None.gif '  Base class for the three panes. Draws caption at top with using
None.gif'
 a different gradient fill if the pane is active or inactive.
None.gif

None.gif
Imports  System.ComponentModel
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class BasePane Class BasePane
InBlock.gif    
Inherits System.Windows.Forms.UserControl
InBlock.gif
InBlock.gif    
' events
InBlock.gif
    ' raise when the pane becomes active
InBlock.gif
    Public Event PaneActive(ByVal sender As ObjectByVal e As EventArgs)
InBlock.gif
InBlock.gif    
' internal members
InBlock.gif
    ' pane caption
InBlock.gif
    Private caption As PaneCaption
InBlock.gif
InBlock.gif    
' properties
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected ReadOnly Property CaptionControl()Property CaptionControl() As PaneCaption
InBlock.gif        
Get
InBlock.gif            
Return caption
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
<Description("The pane caption."), Category("Appearance")> _
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property CaptionText()Property CaptionText() As String
InBlock.gif        
Get
InBlock.gif            
Return caption.Text
InBlock.gif        
End Get
InBlock.gif
InBlock.gif        
Set(ByVal value As String)
InBlock.gif            caption.Text 
= value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property Active()Property Active() As Boolean
InBlock.gif        
Get
InBlock.gif            
Return caption.Active
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' ctor
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub New()Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
' set double buffer styles
InBlock.gif
        Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or _
InBlock.gif         ControlStyles.AllPaintingInWmPaint 
Or ControlStyles.ResizeRedraw, True)
InBlock.gif
InBlock.gif        
' This call is required by the Windows.Forms Form Designer.
InBlock.gif
        InitializeComponent()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Windows Form Designer generated code#Region " Windows Form Designer generated code "
InBlock.gif    
Private components As System.ComponentModel.Container = Nothing
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif        
If disposing Then
InBlock.gif            
If Not (components Is NothingThen
InBlock.gif                components.Dispose()
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
MyBase.Dispose(disposing)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub InitializeComponent()Sub InitializeComponent()
InBlock.gif        
Me.caption = New FotoVision.PaneCaption
InBlock.gif        
Me.SuspendLayout()
InBlock.gif        
'
InBlock.gif
        'caption
InBlock.gif
        '
InBlock.gif
        Me.caption.Dock = System.Windows.Forms.DockStyle.Top
InBlock.gif        
Me.caption.Font = New System.Drawing.Font("Arial"9.0!, System.Drawing.FontStyle.Bold)
InBlock.gif        
Me.caption.Location = New System.Drawing.Point(11)
InBlock.gif        
Me.caption.Name = "caption"
InBlock.gif        
Me.caption.Size = New System.Drawing.Size(21420)
InBlock.gif        
Me.caption.TabIndex = 0
InBlock.gif        
'
InBlock.gif
        'BasePane
InBlock.gif
        '
InBlock.gif
        Me.Controls.Add(Me.caption)
InBlock.gif        
Me.Name = "BasePane"
InBlock.gif        
Me.Padding = New System.Windows.Forms.Padding(1)
InBlock.gif        
Me.Size = New System.Drawing.Size(216248)
InBlock.gif        
Me.ResumeLayout(False)
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
InBlock.gif    
' internal methods
InBlock.gif

InBlock.gif    
' received focus, make this the active pane
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnEnter()Sub OnEnter(ByVal e As System.EventArgs)
InBlock.gif        
MyBase.OnEnter(e)
InBlock.gif        caption.Active 
= True
InBlock.gif        
RaiseEvent PaneActive(Me, EventArgs.Empty)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' lost focus, not the active pane
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnLeave()Sub OnLeave(ByVal e As System.EventArgs)
InBlock.gif        
MyBase.OnLeave(e)
InBlock.gif        caption.Active 
= False
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' draw border around the pane
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnPaint()Sub OnPaint(ByVal e As PaintEventArgs)
InBlock.gif        
Dim rc As New Rectangle(00Me.Width - 1Me.Height - 1)
InBlock.gif        rc.Inflate(
-Me.DockPadding.All + 1-Me.DockPadding.All + 1)
InBlock.gif        e.Graphics.DrawRectangle(SystemPens.ControlDark, rc)
InBlock.gif        
MyBase.OnPaint(e)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overrides Sub OnResize()Sub OnResize(ByVal e As System.EventArgs)
InBlock.gif        
MyBase.OnResize(e)
InBlock.gif
InBlock.gif        
' manually resize the caption width if in the visual designer
InBlock.gif
        If Me.DesignMode Then
InBlock.gif            caption.Width 
= Me.Width
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif


9。按F5编译保存
10。新建一个用户控件"testPane.vb"
11。加入代码:

ExpandedBlockStart.gif ContractedBlock.gif Public   Class testPane Class testPane
InBlock.gif    
Inherits FotoVision.BasePane
ExpandedBlockEnd.gif
End Class
12。将testPane控件拖到Form1.vb里面编译,就可以看到具有渐变色风格的标题的面板啦,多拖几个可以切换焦点。
PaneCaption.jpg

转载于:https://www.cnblogs.com/Denfo/archive/2007/02/08/644923.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值