我创建了一个用于保存其他对象的类。我需要每个对象都是可点击的。使用鼠标单击对象时,以下代码有效。但是,我想通过从另一个函数提升mouseclick事件来模拟鼠标单击,但我可以找出语法。
这是我的自定义类:
Public Class MyTab
Inherits System.Windows.Forms.Panel
Public myText As New Label
Public Event OnButtonClick As EventHandler
Public Sub New(TextString)
myText.Font = CustomFont.GetInstance(Main.main_font_size_up3, FontStyle.Regular)
myText.ForeColor = Color.FromArgb(255, 0, 0, 0)
myText.BackColor = Color.Transparent
myText.Text = TextString
Dim textSize As Size = TextRenderer.MeasureText(TextString, myText.Font)
myText.Width = textSize.Width + 15
myText.Height = textSize.Height + 6
myText.UseCompatibleTextRendering = True
myText.BorderStyle = BorderStyle.None
myText.Name = "tab_" & TextString
Me.Width = textSize.Width + 10
Me.Height = 30
Me.BackColor = Color.White
myText.TextAlign = ContentAlignment.MiddleCenter
AddHandler myText.Click, AddressOf OnLabelClick ' Listen for the click on the new label
AddHandler myText.MouseClick, AddressOf OnLabelClick ' Listen for the click on the new label
Controls.Add(myText)
End Sub
Private Sub OnLabelClick(sender As Object, e As EventArgs)
RaiseEvent OnButtonClick(Me, e)
gray_out_tabs()
sender.BackColor = Color.FromArgb(255, 255, 255, 255)
End Sub
Private Sub gray_out_tabs()
' gray out all tabs
For Each item As Object In tab_holder.Controls
If TypeOf item Is MyTab Then
item.myText.BackColor = Color.FromArgb(255, 200, 200, 200)
End If
Next
End Sub
End Class
我正在尝试从另一个类中提升此类上的mouseClick事件,但它无效。
这是我尝试使用的另一个课程:
Public Class myTabHolder
Inherits Panel
Public Function highlight(which)
For Each item As Object In tab_holder.Controls
If TypeOf item Is MyTab Then
If item.mytext.name = "tab_" & which.ToString Then
item.mytext.MouseClick() ' <-- not working
End If
End If
Next
Return Nothing
End Function
End Class
我没有收到错误,但它只是忽略了我的陈述。
答案 0 :(得分:0)
以下是from Pinvoke:
的示例' Clicks left mouse button in current coordinates
Imports System.Runtime.InteropServices
Imports System.Threading
Public Class Form1
Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2 '0x0002
Const MOUSEEVENTF_LEFTUP As UInteger = &H4 '0x0004
<DllImport("user32.dll")> _
Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As UInteger, ByVal dy As UInteger, ByVal dwData As UInteger, ByVal dwExtraInfo As Integer)
End Sub
'Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal dwData As Long, ByVal dwExtraInfo As Long)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Thread.Sleep(100)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
End Class
答案 1 :(得分:0)
如果您不需要实际模拟真正的点击,而是需要执行点击事件的代码,您可以执行以下操作:
OnLabelClick
活动更改为Public
OnLabelClick
函数中调用highlight
子代码,而不是之前的item.mytext.MouseClick()
。