请帮忙。
我有一个表单和一个类。
Form - frmTestTool
Class - MainClass
我要做的是每次移动鼠标光标时打印文本。所以情况是,我有一个软件,我嵌入了自定义命令。所以我打开自定义命令然后表单就会弹出,我需要在点击"PlaceText"之前选择软件中的某个地方;按钮。点击"PlaceText"按钮,它将实现btnPlaceText_Click_1,但将不再触发"OnMouseMove"
场景1(WORKING WELL步骤)
-
在软件中选择位置
-
打开自定义命令
-
选择文本位置
-
Move MouseCursor(打印"Hello word";每次鼠标移动)
场景2(NOT WORKING steps)
- 打开自定义命令
- 在软件 中选择位置
- 选择放置文本
- 移动鼠标光标(此时,OnMouseMove不会触发)
下面是代码
Partial Public Class frmTestTool
Inherits Form
Public Sub btnPlaceText_Click_1(sender As Object, e As EventArgs) Handles btnPlaceText.Click
WriteMessage("Hello World")
End Sub
End Class
Public Class TextWizard
Inherits BaseStepCommand
Private Shared ofrmTestTool As frmTestTool = New frmTestTool()
Public Overrides Sub OnSuspend()
MyBase.OnSuspend()
End Sub
Public Overrides Sub OnResume()
MyBase.OnResume()
End Sub
Public Overrides Sub OnStart(ByVal commandID As Integer, ByVal argument As Object)
MyBase.OnStart(commandID, argument)
Try
m_running = True
m_oTxnMgr = ClientServiceProvider.TransactionMgr
ofrmTestTool = New frmTestTool()
ofrmTestTool.Show()
Catch commonException As CmnException
ClientServiceProvider.ErrHandler.ReportError(ErrorHandler.ErrorLevel.Critical, MethodBase.GetCurrentMethod().Name, commonException, commandFailed)
End Try
End Sub
Protected Overrides Sub OnMouseDown(ByVal view As GraphicView, ByVal e As GraphicViewManager.GraphicViewEventArgs, ByVal position As Position)
MyBase.OnMouseDown(view, e, position)
ofrmTestTool.btnPlaceText_Click_1(Nothing, Nothing)
End Sub
End Class
我认为你需要使用RemoveHandler
RemoveHandler Me.MouseMove, AddressOf OnMouseMove
如果您不知道处理程序的地址,那么您将需要使用System.Reflection
来查找和删除它们。
Sub RemoveEvents(Of T As Control)(Target As T, ByVal EventName As String)
Dim oFieldInfo As FieldInfo = GetType(Control).GetField(EventName, BindingFlags.[Static] Or BindingFlags.NonPublic)
Dim oEvent As Object = oFieldInfo.GetValue(Target)
Dim oPropertyInfo As PropertyInfo = GetType(T).GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim oEvenHandlerList As EventHandlerList = CType(oPropertyInfo.GetValue(Target, Nothing), EventHandlerList)
oEvenHandlerList.RemoveHandler(oEvent, oEvenHandlerList(oEvent))
End Sub
像这样调用Sub
:
RemoveEvents(Me, "EventMouseMove")
'这可以用于事件,只需输入字符串"Event<EventName>"
,其中事件名称是您想要在目标上删除所有处理程序的事件。