Subclassing a external window in C# .NET

时光总嘲笑我的痴心妄想 提交于 2019-12-08 10:32:12

问题


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);

}

回答1:


ok im done with the coding, but in your solution you have to have your form solution and a dll solution and it can work, if you want that code let me know. but you cannot subclass within a same exe. so it can all be done in c# but you do need that dll, when i got down to converting my c++ project

all because of

BOOL WINAPI DllMain(HANDLE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
{
    switch(fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            {
                hInstance=(HINSTANCE)hinstDLL;
            }
            break;
        case DLL_PROCESS_DETACH:
            {
                if((int)hndll>1)
                {
                    SetWindowLong(hndll,GWL_WNDPROC,OldWndHndl);   //Set back the old window procedure
                    return 1;
                }       
            }
    }
}



回答2:


It's impossible with C#. Only unmanaged C/C++ can do it..

oldWndProc = SetWindowLong(winHook, GWL_WNDPROC,newWndProc); will always return 0(which means failed) if winHook is from another process.

Reference: https://social.msdn.microsoft.com/Forums/vstudio/en-US/8dd657b5-647b-443b-822d-ebe03ca4033c/change-wndproc-of-another-process-in-c



来源:https://stackoverflow.com/questions/1554674/subclassing-a-external-window-in-c-sharp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!