Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在此类中调整windows消息_.net_Vb.net_Events_Textbox_Windows Messages - Fatal编程技术网

.net 在此类中调整windows消息

.net 在此类中调整windows消息,.net,vb.net,events,textbox,windows-messages,.net,Vb.net,Events,Textbox,Windows Messages,我想知道这段代码是否可以修改为在普通类(例如:Form1类)中使用它,从而不需要对控件进行子类化 代码来自这里的BlueMonkMN 为了更好地理解我的问题,我将向您展示我尝试适应它的方法: Public Class Form1 Public Enum ContextCommands WM_CUT = &H300 WM_COPY = &H301 WM_PASTE = &H302 End Enum

我想知道这段代码是否可以修改为在普通类(例如:Form1类)中使用它,从而不需要对控件进行子类化

代码来自这里的BlueMonkMN

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

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

对于VB开发人员来说,这不是一项简单的任务,但我希望这能有所帮助。代码不再与特定的文本框相关,但您应该能够通过比较
cwp.hWnd
textbox.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
导入System.Runtime.InteropServices
公开课表格1
受保护的覆盖子加载(e作为事件参数)
MyBase.OnLoad(e)
EditMenuHook.Enable(真)
端接头
已关闭的受保护覆盖子对象(e作为事件参数)
EditMenuHook.Enable(False)
MyBase.OnClosed(e)
端接头
末级
好友类编辑手册
公共结构
作为IntPtr的公共lParam
作为IntPtr的公共wParam
公共信息作为UInt32
公共硬件作为IntPtr
端部结构
公共委托函数回调(_
ByVal nCode作为整数_
ByVal wParam作为IntPtr_
ByVal lParam(作为IntPtr)作为整数
共享hHook作为整数=0
'为SetWindowsHookEx函数导入。
_
公共重载共享函数SetWindowsHookEx_
(ByVal idHook作为整数,ByVal HookProc作为回调_
ByVal hInstance作为IntPtr,ByVal wParam作为Integer)作为Integer
端函数
'为CallNextHookEx函数导入。
_
公共重载共享函数CallNextHookEx_
(ByVal idHook作为整数,ByVal nCode作为整数_
ByVal wParam作为IntPtr,ByVal lParam作为IntPtr)作为整数
端函数
'为UnhookWindowsHookEx函数导入。
_
公共重载共享函数UnhookWindowsHookEx_
(ByVal idHook作为整数)作为布尔值
端函数
私有常量WH_CALLWNDPROC=4
公共枚举TextCommandMessage
WM_切割=&H300
WM_COPY=&H301
WM_粘贴=&H302
结束枚举
'保留引用,以便不会对委托进行垃圾收集。
私有共享hookproc作为回调
共享子启用(以布尔值启用)
如果hHook=0且启用=True,则
hookproc=EditCommandHook的地址
hHook=SetWindowsHookEx(WH_CALLWNDPROC_
hookproc_
IntPtr.Zero_
AppDomain.GetCurrentThreadId())
如果hHook.等于(0),则
MsgBox(“SetWindowsHookEx失败”)
返回
如果结束
ElseIf hHook 0和Also enable=False然后
Dim ret作为布尔值=UnhookWindowsHookEx(hHook)
如果ret.Equals(False),则
MsgBox(“UnhookWindowsHookEx失败”)
返回
其他的
hHook=0
如果结束
如果结束
端接头
公共共享函数EditCommandHook(_
ByVal nCode作为整数_
ByVal wParam作为IntPtr_
ByVal lParam(作为IntPtr)作为整数
如果nCode<0,则
Return CallNextHookEx(hHook、nCode、wParam、lParam)
如果结束
Dim cwp=DirectCast(System.Runtime.InteropServices.Marshal.PtrToStructure(lParam,GetType(CWPSTRUCT)),CWPSTRUCT)
选择Case cwp.message
Case TextCommandMessage.WM\u CUT
System.Diagnostics.Debug.WriteLine(String.Format(“Cut{0}”,cwp.hWnd))
案例文本CommandMessage.WM\u副本
System.Diagnostics.Debug.WriteLine(String.Format(“Copy{0}”,cwp.hWnd))
Case TextCommandMessage.WM_粘贴
System.Diagnostics.Debug.WriteLine(String.Format(“粘贴{0}”,cwp.hWnd))
结束选择
Return CallNextHookEx(hHook、nCode、wParam、lParam)
端函数
末级

您能解释一下为什么需要使用氪文本框而不是普通文本框吗?氪文本框不提供可重写的WndProc是真的吗?当您需要对其进行如此精细的控制时,我会犹豫是否要依赖像krypton文本框这样的封闭源代码控制。请您解释一下如何使用它好吗?我找不到需要传递文本框句柄的位置。我刚刚把它放在加载事件
EditMenuHook.Enable(True)
中打开它,但我不知道现在该怎么办。当您从文本框的上下文菜单中选择命令时,您是否可以在调试输出窗口中看到EditCommandHook的Debug.WriteLine输出?如果是这样的话,只需将Debug.WriteLine代码替换为选中该命令时要执行的任何操作即可。如果你不在乎是哪个文本框触发了事件,你就不需要使用句柄。与KryptonTextBox无关?我不明白你在问什么,“与KryptonTextBox无关?”对不起,我的意思是,如果你知道如何将解决方案的代码用于Krypton文本框,因为KryptonTextbox使用默认的windows编辑菜单(或者至少看起来是这样),但是钩子不能与KryptonTextbox一起工作。
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