# Visual Basic > Visual Basic .NET >  vb.net How to use Wndproc in Module?

## elmagek

hello
vb.net How to use Wndproc in Module or how to addhandler Wndproc from module to form
or anyway to handle Wndproc from module to form
all forms pass me by one module

----------


## jmcilhinney

Generally speaking, if you're creating the form then you should be overriding the WndProc method in that form. If you can't do that for some reason or it's not your form, you can use the NativeWindow class to basically attach to a window and intercept its WndProc function.

----------


## elmagek

i have 482 form
so i need to handle them from module because i already pass forms to sub in that module

----------


## jmcilhinney

I don't know what "handle" means but the fact that you have 482 forms, while concerning, does not mean that you can't override their WndProc methods. You could create a single form class, override WndProc in that and then have all your other forms inherit it. Otherwise, do what I already told you.

----------


## elmagek

> I don't know what "handle" means but the fact that you have 482 forms, while concerning, does not mean that you can't override their WndProc methods. You could create a single form class, override WndProc in that and then have all your other forms inherit it. Otherwise, do what I already told you.


vb.net How to use Wndproc in Module or how to addhandler Wndproc from module to form
or anyway to handle Wndproc from module to form
all forms pass me by one module 
i have 482 form
so i need to handle them from module because i already pass forms to sub in that module

someone great chat with me about

Public Class SubClassing
    Inherits NativeWindow

    Public Delegate Sub CallBackProcEventHandler(ByRef m As Message)
    Public Event CallBackProc As CallBackProcEventHandler
    Private m_Subclassed As Boolean = False

    Public Sub New(ByVal handle As IntPtr)
        MyBase.AssignHandle(handle)
    End Sub

    Public Property SubClass As Boolean
        Get
            Return m_Subclassed
        End Get
        Set(ByVal value As Boolean)
            m_Subclassed = value
        End Set
    End Property

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m_Subclassed Then
            RaiseEvent CallBackProc(m)
        End If

        MyBase.WndProc(m)
    End Sub
End Class

dim x as new SubClassing(form1.Handle)
AddHandler x.CallBackProc, CallBackProc
x.SubClass = True

but i am still beginner so i can't understand hot to merge with the sub i need
and this is my sub i want to use

Protected Overrides Sub WndProc(ByRef message As Message)
        MyBase.WndProc(message)
        If message.Msg = WM_NCHITTEST AndAlso message.Result.ToInt32() = HTCLIENT Then
            message.Result = New IntPtr(HTCAPTION)
        End If
        If message.Msg = WM_NCHITTEST Then
            Dim x As Integer = message.LParam.ToInt32() And &HFFFF
            Dim y As Integer = message.LParam.ToInt32() >> 16

            Dim pt As New Point(x, y)
            pt = PointToClient(pt)

            If pt.X >= ClientSize.Width - BORDER_WIDTH AndAlso pt.Y >= ClientSize.Height - BORDER_WIDTH Then
                message.Result = New IntPtr(HTBOTTOMRIGHT)
            ElseIf pt.X <= BORDER_WIDTH AndAlso pt.Y >= ClientSize.Height - BORDER_WIDTH Then
                message.Result = New IntPtr(HTBOTTOMLEFT)
            ElseIf pt.X <= BORDER_WIDTH AndAlso pt.Y <= BORDER_WIDTH Then
                message.Result = New IntPtr(HTTOPLEFT)
            ElseIf pt.X >= ClientSize.Width - BORDER_WIDTH AndAlso pt.Y <= BORDER_WIDTH Then
                message.Result = New IntPtr(HTTOPRIGHT)
            ElseIf pt.X <= BORDER_WIDTH Then
                message.Result = New IntPtr(HTLEFT)
            ElseIf pt.X >= ClientSize.Width - BORDER_WIDTH Then
                message.Result = New IntPtr(HTRIGHT)
            ElseIf pt.Y <= BORDER_WIDTH Then
                message.Result = New IntPtr(HTTOP)
            ElseIf pt.Y >= ClientSize.Height - BORDER_WIDTH Then
                message.Result = New IntPtr(HTBOTTOM)
            End If
        End If
    End Sub


so please anyone can help me with that

----------


## elmagek

from all forms in formload
i use sub from module
now i want to pass this sub Protected Overrides Sub WndProc(ByRef message As Message)
from module to the form were call module

----------


## Peter Swinkels

@elmagek:
Please use the "#" button when posting to wrap code in code tags.

----------

