在文本框中更改插入符颜色
Change Caret Color in textbox
我使用此代码在文本框中创建插入符号 (vb.net):
Private Declare Function CreateCaret Lib "user32" (ByVal hwnd As Long, ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DestroyCaret Lib "user32" () As Long
Private Declare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As Long
Private Declare Function SetCaretPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Sub txtOutput_GotFocus(sender As Object, e As EventArgs) Handles txtOutput.GotFocus
CreateCaret(txtOutput.Handle, IntPtr.Zero, 9, 12)
ShowCaret(txtOutput.Handle)
End Sub
是否可以更改插入符号的颜色?
您遇到的问题是您的 PInvoke 函数没有正确声明。查找它们并将它们与 http://pinvoke.net/.
匹配
创建插入符号的调用将采用位图句柄。因此,根据插入符号的外观,您只需要创建一个位图并将其传递给 CreateCaret
函数即可更改其颜色。这是我用来更改常规文本框的插入符号颜色的一些(非常)粗略的代码。请注意,我只将 CreateCaret
和 ShowCaret
更改为 http://www.pinvoke.net 中的正确签名。您将不得不更改其余部分。
Public Class Form1
Private Declare Function CreateCaret Lib "user32" (ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As Boolean
Private Declare Function DestroyCaret Lib "user32" () As Long
Private Declare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As Long
Private Declare Function SetCaretPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
Private caretBitmap as Bitmap
Private Sub txtOutput_GotFocus(sender As Object, e As EventArgs) Handles txtOutput.GotFocus
If caretBitmap Is Nothing Then
caretBitmap = CreateCaretBitmap()
End If
CreateCaret(txtOutput.Handle, caretBitmap.GetHbitmap(), 5, 10)
ShowCaret(txtOutput.Handle)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Function CreateCaretBitmap() As Bitmap
Dim flag As New Bitmap(5, 5)
Dim flagGraphics As Graphics = Graphics.FromImage(flag)
flagGraphics.FillRectangle(Brushes.Chartreuse, 0, 0, 5, 10)
Return flag
End Function
End Class
您可以根据需要创建位图。
希望对您有所帮助。
我使用此代码在文本框中创建插入符号 (vb.net):
Private Declare Function CreateCaret Lib "user32" (ByVal hwnd As Long, ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DestroyCaret Lib "user32" () As Long
Private Declare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As Long
Private Declare Function SetCaretPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Sub txtOutput_GotFocus(sender As Object, e As EventArgs) Handles txtOutput.GotFocus
CreateCaret(txtOutput.Handle, IntPtr.Zero, 9, 12)
ShowCaret(txtOutput.Handle)
End Sub
是否可以更改插入符号的颜色?
您遇到的问题是您的 PInvoke 函数没有正确声明。查找它们并将它们与 http://pinvoke.net/.
匹配创建插入符号的调用将采用位图句柄。因此,根据插入符号的外观,您只需要创建一个位图并将其传递给 CreateCaret
函数即可更改其颜色。这是我用来更改常规文本框的插入符号颜色的一些(非常)粗略的代码。请注意,我只将 CreateCaret
和 ShowCaret
更改为 http://www.pinvoke.net 中的正确签名。您将不得不更改其余部分。
Public Class Form1
Private Declare Function CreateCaret Lib "user32" (ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As Boolean
Private Declare Function DestroyCaret Lib "user32" () As Long
Private Declare Function SetCaretBlinkTime Lib "user32" (ByVal wMSeconds As Long) As Long
Private Declare Function SetCaretPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
Private caretBitmap as Bitmap
Private Sub txtOutput_GotFocus(sender As Object, e As EventArgs) Handles txtOutput.GotFocus
If caretBitmap Is Nothing Then
caretBitmap = CreateCaretBitmap()
End If
CreateCaret(txtOutput.Handle, caretBitmap.GetHbitmap(), 5, 10)
ShowCaret(txtOutput.Handle)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Function CreateCaretBitmap() As Bitmap
Dim flag As New Bitmap(5, 5)
Dim flagGraphics As Graphics = Graphics.FromImage(flag)
flagGraphics.FillRectangle(Brushes.Chartreuse, 0, 0, 5, 10)
Return flag
End Function
End Class
您可以根据需要创建位图。
希望对您有所帮助。