2009-10-12 4 views
0

J'essaie de sous-classer une fenêtre externe en C#. J'ai utilisé quelque chose de similaire auparavant dans VB6 sans aucun problème MAIS le code ci-dessous ne fonctionnera pas. Quelqu'un peut-il m'aider?Sous-classement d'une fenêtre externe en C# .NET

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

} 

Répondre

3

im ok fait avec le codage, mais dans votre solution, vous devez avoir votre solution de forme et une solution dll et il peut fonctionner, si vous voulez que le code laissez-moi savoir. mais vous ne pouvez pas sous-classer dans un même exe. il peut être fait en C#, mais vous avez besoin que dll, quand je suis descendu à la conversion de mon C++ projet

à cause de

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; 
       }  
      } 
    } 
} 
0

Il est impossible avec C#. Seul le C/C++ non géré peut le faire.

oldWndProc = SetWindowLong(winHook, GWL_WNDPROC,newWndProc);oldWndProc = SetWindowLong(winHook, GWL_WNDPROC,newWndProc);oldWndProc = SetWindowLong(winHook, GWL_WNDPROC,newWndProc); retournera toujours 0 (ce qui signifie qu'il a échoué) si winHook provient d'un autre processus.

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

+0

bas électeur, s'il vous plaît expliquer. –