【发布时间】:2013-10-24 08:30:26
【问题描述】:
我想知道是否可以调整这段代码以在普通类(例如:Form1 类)中使用它,而不需要对控件进行子类化。
代码来自BlueMonkMN这里Capture the option selected by the user in a windows default contextmenu?
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
【问题讨论】:
-
你能解释一下为什么你需要使用氪星文本框而不是普通文本框吗?氪文本框不提供可覆盖的 WndProc 是真的吗?当你需要对它进行如此精细的控制时,我会犹豫是否依赖像氪文本框这样的闭源控件。
标签: .net vb.net events textbox windows-messages