Difference between revisions of "Subclassing in .NET"

From HashVB
Jump to: navigation, search
m (Reverted edit of OloerGetva, changed back to last version by Dee)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'General class to subclass any window (control,form..etc)
+
{{VB.NET}}
 +
{{For VB6|subclassing|Subclassing}}
  
Public Class SubclassWindow
+
'General class to subclass any window (control,form..etc)
        Inherits NativeWindow
+
Public Class SubclassWindow
        Dim m_Msg As Integer = -1
+
        Inherits NativeWindow
        Dim bCatchMsg As Boolean = False
+
        Dim m_Msg As Integer = -1
        Public Event OnProcessMessage(ByRef m As System.Windows.Forms.Message)
+
        Dim bCatchMsg As Boolean = False
        Public Property CatchMessage() As Integer
+
        Public Event OnProcessMessage(ByRef m As System.Windows.Forms.Message)
            Get
+
        Public Property CatchMessage() As Integer
                Return m_Msg
+
            Get
            End Get
+
                Return m_Msg
            Set(ByVal Value As Integer)
+
            End Get
                m_Msg = Value
+
            Set(ByVal Value As Integer)
                bCatchMsg = True
+
                m_Msg = Value
            End Set
+
                bCatchMsg = True
        End Property
+
            End Set
        Protected Overloads Overrides Sub WndProc(ByRef m As Message)
+
        End Property
            If bCatchMsg AndAlso m.Msg = m_Msg Then
+
        Protected Overloads Overrides Sub WndProc(ByRef m As Message)
                RaiseEvent OnProcessMessage(m)
+
            If bCatchMsg AndAlso m.Msg = m_Msg Then
                If Not m.Result.Equals(IntPtr.Zero) Then
+
                RaiseEvent OnProcessMessage(m)
                    Return
+
                If Not m.Result.Equals(IntPtr.Zero) Then
                End If
+
                    Return
            End If
+
                End If
            MyBase.WndProc(m)
+
            End If
        End Sub
+
            MyBase.WndProc(m)
End Class
+
        End Sub
 +
End Class
 +
 +
'###  Example How to use it #####'
 +
'declare 
 +
Dim withevents oSubclassTest as SubclassWindow
  
'### Example How to use it #####'
+
  'then put the code in InitializeComponent or where you need
'declare 
+
oSubclassTest = New SubclassWindow
Dim withevents oSubclassTest as SubclassWindow
+
With oSubclassCalendar
 
+
  .CatchMessage = WM_LBUTTONDOWN  ' or other message
'then put the code in InitializeComponent or where you need
+
  .AssignHandle(textbox1.Handle) ' or any control/form..etc
oSubclassTest = New SubclassWindow
+
End With
With oSubclassCalendar
+
.CatchMessage = WM_LBUTTONDOWN  ' or other message
+
'then catch the specific message within OnProcessMessage event
.AssignHandle(textbox1.Handle) ' or any control/form..etc
+
Private Sub SubclassWindow_OnProcessMessage(ByRef m As System.Windows.Forms.Message) Handles oSubclassTest.OnProcessMessage
End With
+
        'insert your code here (processing...etc)
 
+
        ''''''''''''''
'then catch the specific message within OnProcessMessage event
+
Private Sub SubclassWindow_OnProcessMessage(ByRef m As System.Windows.Forms.Message) Handles oSubclassTest.OnProcessMessage
+
        'to stop the message,set Result property:  
        'insert your code here (processing...etc)
+
        m.Result = New IntPtr(1)
        ''''''''''''''
+
End Sub
 
+
        'to stop the message,set Result property:  
+
        m.Result = New IntPtr(1)
+
End Sub
+

Latest revision as of 16:06, 18 July 2008

float
 This article is based on Visual Basic.NET. Find other Visual Basic.NET articles.
 For information on subclassing in Visual Basic 6, see Subclassing.
'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