×

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

Kalet Kalet 发表于2009-03-20 12:00:14 浏览163 评论0

抢沙发发表评论


1。新建一个项目
2。添加一个用户控件“PaneCaption.vb”[VB.net]绘制具有渐变颜色和防锯齿字体的标题
3。[操作]调整控件大小为150×30
4。打开代码编辑器:




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


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

Imports System.ComponentModel

Public Class BasePane
Inherits System.Windows.Forms.UserControl

' events
' raise when the pane becomes active
Public Event PaneActive(ByVal sender As Object, ByVal e As EventArgs)

' internal members
' pane caption
Private caption As PaneCaption

' properties

Protected ReadOnly Property CaptionControl() As PaneCaption
Get
Return caption
End Get
End Property


<Description("The pane caption."), Category("Appearance")> _
Public Property CaptionText() As String
Get
Return caption.Text
End Get

Set(ByVal value As String)
caption.Text
= value
End Set
End Property


Public ReadOnly Property Active() As Boolean
Get
Return caption.Active
End Get
End Property


' ctor
Public Sub New()
MyBase.New()

' set double buffer styles
Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or _
ControlStyles.AllPaintingInWmPaint
Or ControlStyles.ResizeRedraw, True)

' This call is required by the Windows.Forms Form Designer.
InitializeComponent()
End Sub


Windows Form Designer generated code

' internal methods

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


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


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


Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)

' manually resize the caption width if in the visual designer
If Me.DesignMode Then
caption.Width
= Me.Width
End If
End Sub


End Class



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


Public Class testPane
Inherits FotoVision.BasePane
End Class
12。将testPane控件拖到Form1.vb里面编译,就可以看到具有渐变色风格的标题的面板啦,多拖几个可以切换焦点。[VB.net]绘制具有渐变颜色和防锯齿字体的标题


群贤毕至

访客