-1

我想知道是否可以调整此代码以在普通类(示例:Form1 类)中使用它,而不需要对控件进行子类化。

代码来自BlueMonkMN这里捕获用户在 Windows 默认上下文菜单中选择的选项?

Class MyTextBox : Inherits TextBox
  Public Enum ContextCommands
     WM_CUT = &H300
     WM_COPY = &H301
     WM_PASTE = &H302
  End Enum

  Public Class ContextCommandEventArgs
     Inherits EventArgs
     Public Property Command As ContextCommands
  End Class

  Event OnCut(sender As Object, e As ContextCommandEventArgs)
  Event OnCopy(sender As Object, e As ContextCommandEventArgs)
  Event OnPaste(sender As Object, e As ContextCommandEventArgs)

  Protected Overrides Sub WndProc(ByRef m As Message)
     MyBase.WndProc(m)
     Select Case m.Msg
        Case ContextCommands.WM_CUT
           RaiseEvent OnCut(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_CUT})
        Case ContextCommands.WM_COPY
           RaiseEvent OnCopy(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_COPY})
        Case ContextCommands.WM_PASTE
           RaiseEvent OnPaste(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_PASTE})
     End Select
  End Sub
End Class

为了更好地理解我的问题,我将向您展示我尝试适应它的方法:

Public Class Form1

    Public Enum ContextCommands
        WM_CUT = &H300
        WM_COPY = &H301
        WM_PASTE = &H302
    End Enum

    Public Class ContextCommandEventArgs
        Inherits EventArgs
        Public Property Command As ContextCommands
    End Class

    Event OnCut(TextBox1 As Object, e As ContextCommandEventArgs)
    Event OnCopy(TextBox1 As Object, e As ContextCommandEventArgs)
    Event OnPaste(TextBox1 As Object, e As ContextCommandEventArgs)

    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        Select Case m.Msg
            Case ContextCommands.WM_CUT
                RaiseEvent OnCut(TextBox1, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_CUT})
            Case ContextCommands.WM_COPY
                RaiseEvent OnCopy(TextBox1, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_COPY})
            Case ContextCommands.WM_PASTE
                RaiseEvent OnPaste(TextBox1, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_PASTE})
        End Select
    End Sub

Private Sub TextBox1_OnTextCommand() Handles TextBox1.TextChanged
    If WM_CUT then...
    Elseif WM_COPY then...
    Elseif WM_Paste then...
    End if
End Sub

Private Sub TextBox1_OnTextCommand() Handles Me.OnPaste, Me.OnCopy, Me.OnCut
    MsgBox("Activated")
End Sub

End Class
4

1 回答 1

1

对于 VB 开发人员来说,这不是一项简单的任务,但我希望这会有所帮助。该代码不再与特定的文本框相关,但如果需要,您应该能够通过cwp.hWndTextBox.Handle值进行比较来识别哪个文本框导致了事件。EditMenuHook.Enable应该只在应用程序开始时调用一次以打开挂钩,并在结束时将其关闭。

Imports System.Runtime.InteropServices

Public Class Form1
   Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      EditMenuHook.Enable(True)
   End Sub

   Protected Overrides Sub OnClosed(e As EventArgs)
      EditMenuHook.Enable(False)
      MyBase.OnClosed(e)
   End Sub
End Class

Friend Class EditMenuHook
   Public Structure CWPSTRUCT
      Public lParam As IntPtr
      Public wParam As IntPtr
      Public message As UInt32
      Public hWnd As IntPtr
   End Structure

   Public Delegate Function CallBack( _
       ByVal nCode As Integer, _
       ByVal wParam As IntPtr, _
       ByVal lParam As IntPtr) As Integer

   Shared hHook As Integer = 0

   'Import for the SetWindowsHookEx function.
   <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
   Public Overloads Shared Function SetWindowsHookEx _
          (ByVal idHook As Integer, ByVal HookProc As CallBack, _
           ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
   End Function

   'Import for the CallNextHookEx function.
   <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
   Public Overloads Shared Function CallNextHookEx _
          (ByVal idHook As Integer, ByVal nCode As Integer, _
           ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
   End Function

   'Import for the UnhookWindowsHookEx function.
   <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
   Public Overloads Shared Function UnhookWindowsHookEx _
              (ByVal idHook As Integer) As Boolean
   End Function

   Private Const WH_CALLWNDPROC = 4

   Public Enum TextCommandMessage
      WM_CUT = &H300
      WM_COPY = &H301
      WM_PASTE = &H302
   End Enum

   'Keep the reference so that the delegate is not garbage collected.
   Private Shared hookproc As CallBack

   Shared Sub Enable(enable As Boolean)
      If hHook = 0 AndAlso enable = True Then
         hookproc = AddressOf EditCommandHook
         hHook = SetWindowsHookEx(WH_CALLWNDPROC, _
                                  hookproc, _
                                  IntPtr.Zero, _
                                  AppDomain.GetCurrentThreadId())
         If hHook.Equals(0) Then
            MsgBox("SetWindowsHookEx Failed")
            Return
         End If
      ElseIf hHook <> 0 AndAlso enable = False Then
         Dim ret As Boolean = UnhookWindowsHookEx(hHook)

         If ret.Equals(False) Then
            MsgBox("UnhookWindowsHookEx Failed")
            Return
         Else
            hHook = 0
         End If
      End If
   End Sub

   Public Shared Function EditCommandHook( _
      ByVal nCode As Integer, _
      ByVal wParam As IntPtr, _
      ByVal lParam As IntPtr) As Integer

      If nCode < 0 Then
         Return CallNextHookEx(hHook, nCode, wParam, lParam)
      End If

      Dim cwp = DirectCast(System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, GetType(CWPSTRUCT)), CWPSTRUCT)

      Select Case cwp.message
         Case TextCommandMessage.WM_CUT
            System.Diagnostics.Debug.WriteLine(String.Format("Cut {0}", cwp.hWnd))
         Case TextCommandMessage.WM_COPY
            System.Diagnostics.Debug.WriteLine(String.Format("Copy {0}", cwp.hWnd))
         Case TextCommandMessage.WM_PASTE
            System.Diagnostics.Debug.WriteLine(String.Format("Paste {0}", cwp.hWnd))
      End Select

      Return CallNextHookEx(hHook, nCode, wParam, lParam)
   End Function
End Class
于 2013-10-24T15:53:27.453 回答