2009-10-12 110 views
0

我想在C#中創建一個外部窗口的子類。 我以前在VB6中使用過類似的東西,但沒有任何問題,但下面的代碼不起作用。有人可以幫我嗎?在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); 

} 

回答

3

OK IM與編碼完成,但在您的解決方案,你必須有你的形式解決方案和DLL解決方案,它可以工作,如果你想要的代碼讓我知道。但不能在同一個exe文件中進行子類化。因此它可以在所有的C#做,但你需要一個DLL,當我到轉換我的C++項目

都因爲

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