tags:

views:

292

answers:

1

Hi,

I'm trying to subclass an external window in C#. I have used something similar before in VB6 without any problem BUT the below code just won't work. Can anybody help me out?

//API

[DllImport("user32")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newProc);

[DllImport("user32")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WinProc newProc);

[DllImport("user32.dll")]
private static extern IntPtr DefWindowProc(IntPtr hWnd, int uMsg, int wParam, int lParam);

[DllImport("user32")]
private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);

private delegate IntPtr WinProc(IntPtr hWnd, int Msg, int wParam, int lParam);

private const int GWL_WNDPROC = -4;

private enum winMessage : int
{
    WM_GETMINMAXINFO = 0x024,
    WM_ENTERSIZEMOVE = 0x231,
    WM_EXITSIZEMOVE = 0x232
}

private WinProc newWndProc = null;
private IntPtr oldWndProc = IntPtr.Zero;
private IntPtr winHook = IntPtr.Zero;

//Implementation

public void hookWindow(IntPtr winHandle)
{
    if (winHandle != IntPtr.Zero)
    {
        winHook = winHandle;

        newWndProc = new WinProc(newWindowProc);
        oldWndProc = SetWindowLong(winHook, GWL_WNDPROC,newWndProc);
    }
}

public void unHookWindow()
{
    if (winHook != IntPtr.Zero)
    {
        SetWindowLong(winHook, GWL_WNDPROC, oldWndProc);
        winHook = IntPtr.Zero;
    }
}

private IntPtr newWindowProc(IntPtr hWnd, int Msg, int wParam, int lParam)
{
     switch (Msg)
     {
         case (int)winMessage.WM_GETMINMAXINFO:
             MessageBox.Show("Moving");
             return DefWindowProc(hWnd, Msg, wParam, lParam);

}
A: 

Seems as if subclassing is only allowed within the own process thread...

Not what I had hoped for...

Jonas