如何解决如何在VB.NET中正确制作带有水印和嵌入字体的自定义文本框?
我正在制作一个带有水印的自定义文本框(这是我的新消息),该水印使用嵌入的自定义字体,但无法使其正常运行。
这些是问题所在
- 键入时,光标似乎在文本后面
- 当我在文本框中单击或从文本框中选择文本时,该文本显示为没有自定义字体
- 单击后鼠标离开控件时,将返回自定义字体。
这是我正在使用的自定义文本框类(FontManager类工作正常,因此我没有包含它,但是如果您要求我可以这样做)
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Text
Public Class WaterMarkTextBox
Inherits TextBox
Private oldFont As Font = Nothing
Private waterMarkTextEnabled As Boolean = False
Private myFontCollection As PrivateFontCollection = New PrivateFontCollection()
#Region "Attributes"
Private _waterMarkColor As Color = Drawing.Color.Gray
Public Property WaterMarkColor() As Color
Get
Return _waterMarkColor
End Get
Set(ByVal value As Color)
_waterMarkColor = value
Me.Invalidate()
End Set
End Property
Private _waterMarkText As String = "Watermark"
Public Property WaterMarkText() As String
Get
Return _waterMarkText
End Get
Set(ByVal value As String)
_waterMarkText = value
Me.Invalidate()
End Set
End Property
#End Region
Public Sub New()
Dim installedFontsCount = FontManager.AddFontsFromResources(myFontCollection,{"minecraft_font"})
End Sub
Private Sub WaterMark_Toggle()
If Me.Text.Length <= 0 Then
EnableWaterMark()
Else
DisableWaterMark()
End If
End Sub
Private Sub EnableWaterMark()
oldFont = New Font(myFontCollection.Families(0),8,FontStyle.Italic,Font.Unit)
Me.SetStyle(ControlStyles.UserPaint,True)
Me.waterMarkTextEnabled = True
Refresh()
End Sub
Private Sub DisableWaterMark()
Me.waterMarkTextEnabled = False
Me.SetStyle(ControlStyles.UserPaint,True)
If Not oldFont Is Nothing Then
Me.Font = New Font(myFontCollection.Families(0),9,FontStyle.Regular,Font.Unit)
End If
Refresh()
End Sub
Protected Overrides Sub OnHandleCreated(e As EventArgs)
WaterMark_Toggle()
MyBase.OnHandleCreated(e)
End Sub
Protected Overrides Sub OnEnter(ByVal e As EventArgs)
WaterMark_Toggle()
MyBase.OnEnter(e)
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing Then myFontCollection.Dispose()
MyBase.Dispose(disposing)
End Sub
Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
WaterMark_Toggle()
End Sub
Protected Overrides Sub OnLeave(e As EventArgs)
WaterMark_Toggle()
MyBase.OnLeave(e)
End Sub
Protected Overrides Sub OnFontChanged(e As EventArgs)
If waterMarkTextEnabled Then
oldFont = New Font(myFontCollection.Families(0),Font.Unit)
Refresh()
End If
MyBase.OnFontChanged(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim drawFont As Font
If waterMarkTextEnabled = True Then
_waterMarkColor = Drawing.Color.Gray
drawFont = New Font(myFontCollection.Families(0),Font.Unit)
Else
_waterMarkColor = Drawing.Color.Black
drawFont = New Font(myFontCollection.Families(0),Font.Unit)
End If
Dim drawBrush As SolidBrush = New SolidBrush(Me.WaterMarkColor)
e.Graphics.DrawString(IIf(waterMarkTextEnabled,WaterMarkText,Text).ToString(),drawFont,drawBrush,New Point(0,0))
MyBase.OnPaint(e)
End Sub
End Class
此外,我想知道如何始终显示文本框字符串的结尾,不仅是当控件具有焦点或光标时,还是如何在开头添加自动省略号,例如:... ocuments \ Proyect.txt 我正在使用文件和目录,但我不想用\除或只显示文件名,我想在其上放一个工具提示,用户可以从文本框中复制完整地址。 (后者并不重要。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。