?? generictoolbar.vb
字號:
Public Class GenericToolbar
Inherits System.Windows.Forms.UserControl
'類成員變量
Private m_EditBox As RichTextBox '綁定到的文本框
Private m_iMinFontSize As Integer = 8 '字體下拉列表框中最小字體
Private m_iMaxFontSize As Integer = 22 '字體下拉列表框中最大字體
'綁定到的RichTextBox,如果沒有綁定,則控件為Disabled狀態
Public Property AttathedTextBox() As RichTextBox
Get
Return m_EditBox
End Get
Set(ByVal Value As RichTextBox)
m_EditBox = Value
'更新窗體中控件狀態
UpdateCmdUI()
End Set
End Property
#Region " Windows 窗體設計器生成的代碼 "
Public Sub New()
MyBase.New()
'該調用是 Windows 窗體設計器所必需的。
InitializeComponent()
'在 InitializeComponent() 調用之后添加任何初始化
End Sub
'UserControl 重寫 dispose 以清理組件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗體設計器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下過程是 Windows 窗體設計器所必需的
'可以使用 Windows 窗體設計器修改此過程。
'不要使用代碼編輯器修改它。
Friend WithEvents palToolbox As System.Windows.Forms.Panel
Friend WithEvents cboFontName As System.Windows.Forms.ComboBox
Friend WithEvents cboFontSize As System.Windows.Forms.ComboBox
Friend WithEvents btnColor As System.Windows.Forms.Button
Friend WithEvents btnUnderLine As System.Windows.Forms.Button
Friend WithEvents btnItalic As System.Windows.Forms.Button
Friend WithEvents btnBold As System.Windows.Forms.Button
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(GenericToolbar))
Me.palToolbox = New System.Windows.Forms.Panel
Me.cboFontName = New System.Windows.Forms.ComboBox
Me.cboFontSize = New System.Windows.Forms.ComboBox
Me.btnColor = New System.Windows.Forms.Button
Me.btnUnderLine = New System.Windows.Forms.Button
Me.btnItalic = New System.Windows.Forms.Button
Me.btnBold = New System.Windows.Forms.Button
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
Me.palToolbox.SuspendLayout()
Me.SuspendLayout()
'
'palToolbox
'
Me.palToolbox.BackColor = System.Drawing.Color.Transparent
Me.palToolbox.Controls.Add(Me.cboFontName)
Me.palToolbox.Controls.Add(Me.cboFontSize)
Me.palToolbox.Controls.Add(Me.btnColor)
Me.palToolbox.Controls.Add(Me.btnUnderLine)
Me.palToolbox.Controls.Add(Me.btnItalic)
Me.palToolbox.Controls.Add(Me.btnBold)
Me.palToolbox.Dock = System.Windows.Forms.DockStyle.Top
Me.palToolbox.Location = New System.Drawing.Point(0, 0)
Me.palToolbox.Name = "palToolbox"
Me.palToolbox.Size = New System.Drawing.Size(376, 27)
Me.palToolbox.TabIndex = 34
'
'cboFontName
'
Me.cboFontName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboFontName.Location = New System.Drawing.Point(2, 2)
Me.cboFontName.Name = "cboFontName"
Me.cboFontName.Size = New System.Drawing.Size(134, 20)
Me.cboFontName.TabIndex = 25
'
'cboFontSize
'
Me.cboFontSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboFontSize.Location = New System.Drawing.Point(144, 2)
Me.cboFontSize.Name = "cboFontSize"
Me.cboFontSize.Size = New System.Drawing.Size(54, 20)
Me.cboFontSize.TabIndex = 26
'
'btnColor
'
Me.btnColor.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnColor.Image = CType(resources.GetObject("btnColor.Image"), System.Drawing.Image)
Me.btnColor.Location = New System.Drawing.Point(304, 1)
Me.btnColor.Name = "btnColor"
Me.btnColor.Size = New System.Drawing.Size(22, 22)
Me.btnColor.TabIndex = 30
'
'btnUnderLine
'
Me.btnUnderLine.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnUnderLine.Image = CType(resources.GetObject("btnUnderLine.Image"), System.Drawing.Image)
Me.btnUnderLine.Location = New System.Drawing.Point(272, 1)
Me.btnUnderLine.Name = "btnUnderLine"
Me.btnUnderLine.Size = New System.Drawing.Size(22, 22)
Me.btnUnderLine.TabIndex = 29
'
'btnItalic
'
Me.btnItalic.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnItalic.Image = CType(resources.GetObject("btnItalic.Image"), System.Drawing.Image)
Me.btnItalic.Location = New System.Drawing.Point(240, 1)
Me.btnItalic.Name = "btnItalic"
Me.btnItalic.Size = New System.Drawing.Size(22, 22)
Me.btnItalic.TabIndex = 28
'
'btnBold
'
Me.btnBold.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnBold.Image = CType(resources.GetObject("btnBold.Image"), System.Drawing.Image)
Me.btnBold.Location = New System.Drawing.Point(208, 1)
Me.btnBold.Name = "btnBold"
Me.btnBold.Size = New System.Drawing.Size(22, 22)
Me.btnBold.TabIndex = 27
'
'GenericToolbar
'
Me.Controls.Add(Me.palToolbox)
Me.Name = "GenericToolbar"
Me.Size = New System.Drawing.Size(376, 28)
Me.palToolbox.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
#Region "窗體初始化"
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'初始化字體下拉列表框
InitFontNameCombo(cboFontName)
InitFontSizeCombo(cboFontSize)
UpdateCmdUI()
End Sub
'初始化字體下拉列表框
Private Sub InitFontNameCombo(ByRef cbo As ComboBox)
Dim i, j As Integer
Dim fontFamilies() As FontFamily
Dim FontInstalled As New System.Drawing.Text.InstalledFontCollection
fontFamilies = FontInstalled.Families
cbo.DataSource = fontFamilies
cbo.DisplayMember = "Name"
cbo.ValueMember = "Name"
cbo.SelectedValue = Me.Font.FontFamily.Name
End Sub
'初始化字號下拉列表框
Private Sub InitFontSizeCombo(ByRef cbo As ComboBox)
Dim i As Integer
Dim Ary As New ArrayList
Ary.Capacity = Me.m_iMaxFontSize - Me.m_iMinFontSize + 1
For i = m_iMinFontSize To m_iMaxFontSize
Ary.Add(i)
Next
cbo.DataSource = Ary
cbo.SelectedIndex = 4
End Sub
#End Region
Private Sub UpdateCmdUI()
Dim ctl As Control
Dim bEnabled As Boolean = True
'如果沒有設置AttathedTextBox屬性,則禁用所有控件
If m_EditBox Is Nothing Then
bEnabled = False
End If
'禁用Form中的頂級控件即可
For Each ctl In Me.Controls
ctl.Enabled = bEnabled
Next
End Sub
#Region "文本格式工具箱控件事件響應代碼"
'
'注意!!!!!!!
'此處對m_EditBox.SelectionFont做了簡化處理
'
'
'
'字體格式按鈕響應代碼,包知btnBold, btnItalic, btnUnderLine, btnColor
Private Sub btnFontStyle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnBold.Click, _
btnItalic.Click, _
btnUnderLine.Click, _
btnColor.Click
Dim fs As FontStyle
Dim ft As Font
If m_EditBox Is Nothing Then
Return
End If
'當選中不同種字體時,SelectionFont為Nothing
'對于選中多種字體,這里只做簡單處理
If m_EditBox.SelectionFont Is Nothing Then
fs = FontStyle.Regular
ft = m_EditBox.DefaultFont
Else
fs = m_EditBox.SelectionFont.Style
ft = m_EditBox.SelectionFont
End If
Dim btnSender As Button = DirectCast(sender, Button)
Select Case btnSender.Name
Case btnBold.Name
m_EditBox.SelectionFont = New Font(ft, FontStyle.Bold Xor fs)
Case btnItalic.Name
m_EditBox.SelectionFont = New Font(ft, FontStyle.Italic Xor fs)
Case btnUnderLine.Name
m_EditBox.SelectionFont = New Font(ft, FontStyle.Underline Xor fs)
Case btnColor.Name
If ColorDialog1.ShowDialog() = DialogResult.OK Then
m_EditBox.SelectionColor = ColorDialog1.Color
End If
End Select
m_EditBox.Focus()
End Sub
'字體下拉列表框
Private Sub cboFontName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFontName.SelectedIndexChanged
Dim emSize As Single
If m_EditBox Is Nothing Then
Return
End If
Dim fs As FontStyle
'設定FontSize
'對于選中多種字體,這里只做簡單處理
If m_EditBox.SelectionFont Is Nothing Then
emSize = cboFontSize.SelectedValue
fs = FontStyle.Regular
Else
emSize = m_EditBox.SelectionFont.Size
fs = m_EditBox.SelectionFont.Style
End If
If emSize < 1 Then
emSize = Me.m_iMinFontSize
End If
m_EditBox.SelectionFont = New Font(cboFontName.SelectedItem.ToString, emSize, fs)
m_EditBox.Focus()
End Sub
'字號下拉列表框
Private Sub cboFontSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFontSize.SelectedIndexChanged
Dim emSize As Single
Dim strFontFamilyName As String
If m_EditBox Is Nothing Then
Return
End If
Dim fs As FontStyle
'1) 設定FontFamily
'對于選中多種字體,這里只做簡單處理
If m_EditBox.SelectionFont Is Nothing Then
strFontFamilyName = cboFontName.SelectedValue
fs = FontStyle.Regular
Else
strFontFamilyName = m_EditBox.SelectionFont.FontFamily.Name
fs = m_EditBox.SelectionFont.Style
End If
'2) 設定FontSize
emSize = CSng(cboFontSize.SelectedItem)
If emSize < 1 Then
emSize = m_iMinFontSize
End If
m_EditBox.SelectionFont = New Font(strFontFamilyName, emSize, fs)
m_EditBox.Focus()
End Sub
#End Region
End Class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -