Difference between revisions of "Subclassing in .NET"
From HashVB
m (Fixed code indentation) |
|||
Line 1: | Line 1: | ||
− | 'General class to subclass any window (control,form..etc) | + | 'General class to subclass any window (control,form..etc) |
− | + | Public Class SubclassWindow | |
− | Public Class SubclassWindow | + | Inherits NativeWindow |
− | + | Dim m_Msg As Integer = -1 | |
− | + | Dim bCatchMsg As Boolean = False | |
− | + | Public Event OnProcessMessage(ByRef m As System.Windows.Forms.Message) | |
− | + | Public Property CatchMessage() As Integer | |
− | + | Get | |
− | + | Return m_Msg | |
− | + | End Get | |
− | + | Set(ByVal Value As Integer) | |
− | + | m_Msg = Value | |
− | + | bCatchMsg = True | |
− | + | End Set | |
− | + | End Property | |
− | + | Protected Overloads Overrides Sub WndProc(ByRef m As Message) | |
− | + | If bCatchMsg AndAlso m.Msg = m_Msg Then | |
− | + | RaiseEvent OnProcessMessage(m) | |
− | + | If Not m.Result.Equals(IntPtr.Zero) Then | |
− | + | Return | |
− | + | End If | |
− | + | End If | |
− | + | MyBase.WndProc(m) | |
− | + | End Sub | |
− | + | End Class | |
− | End Class | + | |
− | + | '### Example How to use it #####' | |
− | '### Example How to use it #####' | + | 'declare |
− | 'declare | + | Dim withevents oSubclassTest as SubclassWindow |
− | Dim withevents oSubclassTest as SubclassWindow | + | |
− | + | 'then put the code in InitializeComponent or where you need | |
− | 'then put the code in InitializeComponent or where you need | + | oSubclassTest = New SubclassWindow |
− | oSubclassTest = New SubclassWindow | + | With oSubclassCalendar |
− | With oSubclassCalendar | + | .CatchMessage = WM_LBUTTONDOWN ' or other message |
− | + | .AssignHandle(textbox1.Handle) ' or any control/form..etc | |
− | + | End With | |
− | End With | + | |
− | + | 'then catch the specific message within OnProcessMessage event | |
− | 'then catch the specific message within OnProcessMessage event | + | Private Sub SubclassWindow_OnProcessMessage(ByRef m As System.Windows.Forms.Message) Handles oSubclassTest.OnProcessMessage |
− | Private Sub SubclassWindow_OnProcessMessage(ByRef m As System.Windows.Forms.Message) Handles oSubclassTest.OnProcessMessage | + | 'insert your code here (processing...etc) |
− | + | '''''''''''''' | |
− | + | ||
− | + | 'to stop the message,set Result property: | |
− | + | m.Result = New IntPtr(1) | |
− | + | End Sub | |
− | End Sub | + |
Revision as of 10:56, 5 January 2006
'General class to subclass any window (control,form..etc) Public Class SubclassWindow Inherits NativeWindow Dim m_Msg As Integer = -1 Dim bCatchMsg As Boolean = False Public Event OnProcessMessage(ByRef m As System.Windows.Forms.Message) Public Property CatchMessage() As Integer Get Return m_Msg End Get Set(ByVal Value As Integer) m_Msg = Value bCatchMsg = True End Set End Property Protected Overloads Overrides Sub WndProc(ByRef m As Message) If bCatchMsg AndAlso m.Msg = m_Msg Then RaiseEvent OnProcessMessage(m) If Not m.Result.Equals(IntPtr.Zero) Then Return End If End If MyBase.WndProc(m) End Sub End Class '### Example How to use it #####' 'declare Dim withevents oSubclassTest as SubclassWindow 'then put the code in InitializeComponent or where you need oSubclassTest = New SubclassWindow With oSubclassCalendar .CatchMessage = WM_LBUTTONDOWN ' or other message .AssignHandle(textbox1.Handle) ' or any control/form..etc End With 'then catch the specific message within OnProcessMessage event Private Sub SubclassWindow_OnProcessMessage(ByRef m As System.Windows.Forms.Message) Handles oSubclassTest.OnProcessMessage 'insert your code here (processing...etc) ''''''''' 'to stop the message,set Result property: m.Result = New IntPtr(1) End Sub