子类在C#.NET中的一个对外窗口 [英] Subclassing a external window in C# .NET
问题描述
我试图子类在C#中的对外窗口。
我已经使用之前,VB6类似的东西,没有任何问题,但下面的代码是行不通的。任何人可以帮助我吗?
// API
函数[DllImport(USER32)]
私人静态外部的IntPtr SetWindowLong函数(IntPtr的的HWND,国际nIndex,IntPtr的的NewProc);
函数[DllImport(USER32)]
私人静态外部的IntPtr SetWindowLong函数(IntPtr的的HWND,国际nIndex,Winproc传的NewProc);
函数[DllImport(user32.dll中)]
私人静态外部的IntPtr DefWindowProc函数(IntPtr的的HWND,INT uMsg,诠释的wParam,lParam的INT);
函数[DllImport(USER32)]
私人静态外部的IntPtr CallWindowProc的(IntPtr的lpPrevWndFunc,IntPtr的的HWND,INT消息,诠释的wParam,lParam的INT);
私人委托的IntPtr Winproc传(IntPtr的的HWND,INT消息,诠释的wParam,lParam的INT);
私人const int的GWL_WNDPROC = -4;
私人枚举winMessage:INT
{
WM_GETMINMAXINFO =量0x024,
WM_ENTERSIZEMOVE = 0x231,
WM_EXITSIZEMOVE = 0x232
}
私人Winproc传newWndProc = NULL;
私人的IntPtr oldWndProc = IntPtr.Zero;
私人的IntPtr winHook = IntPtr.Zero;
//执行
公共无效hookWindow(IntPtr的窗口句柄)
{
如果(窗口句柄!= IntPtr.Zero)
$ { b $ b winHook =窗口句柄;
newWndProc =新Winproc传(newWindowProc);
oldWndProc = SetWindowLong函数(winHook,GWL_WNDPROC,newWndProc);
}
}
公共无效unHookWindow()
{
如果(winHook!= IntPtr.Zero)
{
SetWindowLong函数(winHook,GWL_WNDPROC,oldWndProc);
winHook = IntPtr.Zero;
}
}
私人的IntPtr newWindowProc(IntPtr的的HWND,INT消息,诠释的wParam,诠释lParam的)
{
开关(MSG)
{
的情况下(INT)winMessage.WM_GETMINMAXINFO:
MessageBox.Show(动中通);
返回DefWindowProc函数(HWND,味精,的wParam,lParam的);
}
玉任与编码完成,但在你的解决方案,你必须有你的表单解决方案,一个DLL解决方案,它可以工作,如果你想要的代码让我知道。但你不能在同一EXE中继承。所以它都可以在C#中可以做到,但你确实需要该DLL,当我下到转换++项目
都因为
我的C
BOOL WINAPI的DllMain(HANDLE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
开关(fdwReason)
{
情况下DLL_PROCESS_ATTACH:
{
的hInstance =(HINSTANCE)hinstDLL;
}
中断;
壳体DLL_PROCESS_DETACH:
{
如果((int)的hndll→1)
{
SetWindowLong函数(hndll,GWL_WNDPROC,OldWndHndl); //设置回旧的窗口过程
返回1;
}
}
}
}
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);
}
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;
}
}
}
}
这篇关于子类在C#.NET中的一个对外窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!