The .Net framework has a class, System.Windows.Forms.NativeWindow, which provides low-level encapsulation for a window. I would like to use the class to override a window's WndProc function. I would need to inherit the class in order to override the protected method WndProc(). The problem is that I need to use this class for a pre-existing window from another program, which means that I can not use the constructor, but instead I need to use the NativeWindow.FromHandle method, which will not return my inherited class, but rather a NativeWindow, hence my override will not be used. There is a function AssignHandle, but I used this function and my WndProc is not being called.
Does anyone know how to use the NativeWindow class to override an existing window's WndProc?
Another solution to override the WndProc would be to use the SetWindowLong API function, with which you can specify a new WndProc (I am using C#, I am just showing VB code for those who aren't bilingual):
When I try this, however, I always get a null pointer for a return value, signifying an error.
If anyone could figure out what I am doing wrong here, that would solve all my problems, too.
Does anyone know how to use the NativeWindow class to override an existing window's WndProc?
Another solution to override the WndProc would be to use the SetWindowLong API function, with which you can specify a new WndProc (I am using C#, I am just showing VB code for those who aren't bilingual):
C#:
//Marshalled as 32-bit function pointer
delegate int WndProc(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SetWindowLongA")]
static extern IntPtr SetWindowLong(
IntPtr hWnd,
int Index,
WndProc dwNewLong);
Visual Basic:
'Marshalled as 32-bit function pointer
Delegate Function WndProc(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _
ByVal hwnd As IntPtr, _
ByVal nIndex As Int32, _
ByVal dwNewLong As WndProc) As Int32
C#:
IntPtr OldWndProc;
void SetWndProc(IntPtr hWnd) {
const int GWL_WNDPROC = -4;
OldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, MyWndProc);
}
static int MyWndProc(IntPtr hWnd, int Msg, int wParam, int lParam) {
}
Visual Basic:
Dim OldWndProc As IntPtr
Private Sub SetWndProc(hWnd As IntPtr)
Const GWL_WNDPROC As Integer = -4
OldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, MyWndProc);
End Sub
Private Shared Function MyWndProc(hWnd As IntPtr, Msg As Integer, _
wParam As Integer, lParam As Integer) As Integer
End Function
Last edited: