我使用此代码在文本框(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
是否可以更改插入符号的颜色?
答案 0 :(得分:2)
您遇到的问题是您的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
您可以根据需要创建位图。
希望有所帮助。