📄 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 + -