Inheriting NativeWindow

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Messages
2,097
Location
USA
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):
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
When I try this, however, I always get a null pointer for a return value, signifying an error.
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
If anyone could figure out what I am doing wrong here, that would solve all my problems, too.
 
Last edited:
Hi,

so what you are tryin to do is to subclass a window in another process? if that is the case, then you need to use Win32 API calls to subclass a specific window. Another way is to use a third party component to help you capture windows messages. I use one called SpyWorks from Desaware. Here is a quote from their website:

Subclassing refers to the process of intercepting Windows messages that are normally processed behind the scenes. The .NET System.Windows.Forms namespace makes it unnecessary to subclass your own forms (you have direct access to the windows message function), however it is less trivial to subclass controls (or 3 rd party controls). There is no mechanism in .NET to subclass forms in other processes or AppDomains.

I hope this information helps you. If you don't want to pay for the component you can always subclass using Win32 API calls.

kmg
 
Well, I tried the Win Api calls (my second code example) and for some reason it didn't work as expected, but the NativeWindow class is there for the purpose of subclassing windows, and from what I have seen and read, should work on your windows or those of another application, but again, it didn't work as expected.

I actually gave up with the subclassing, and used another method that isn't quite so nifty, but works.
 
Back
Top