c#使用FindWindow函数的心得
正在给一个单位写个小程序,原来的想法是把显示曲线的程序主窗体劫持到本程序下,不想Setparent函数使用起来有限制(不能捕获外部程序窗口),遂改变思路,每秒复制目标程序主界面到本程序界面显示,用到FindWindow方法,最后出了点小意外,天黑之前运行无数次都正常,回家再试就不行了,代码如下:
结果发现FindWindow如果只是用第一个参数(WindowClass)就是窗体类名,一直获取不到正确的目标窗体句柄,后续步骤就无法开展,将下列代码:
更改为:
这样做自认为操作更友好,不必使用spy++费劲看窗口类名,可以使用任务管理器开程序名称就成,但仍不能避免多实例运行问题,希望对可能用到FindWindow的朋友有所帮助
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string strclassName, string strWindowText);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC( IntPtr hwnd );
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC目标设备的句柄
int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, // width of destination rectangle目标对象的矩形宽度
int nHeight, // height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, // handle to source DC源设备的句柄
int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop // raster operation code光栅的操作值
);
private const uint SRCCOPY = (uint)0xCC0020;//光栅操作码:复制
hWndSrcForm = FindWindow(strWindowClassName, null);//获取待捕捉窗口句柄
IntPtr hdcSrc = Form1.GetWindowDC(hWndSrcForm);//创建待捕获窗口的设备上下文(绘图句柄)
//RECT windowRect = new Form1.RECT();
//Form1.GetWindowRect(hWndSrcForm, ref windowRect);//获取待捕获窗口大小,含非客户区
//int width = windowRect.right - windowRect.left; //此处不能这样做,应根据测试直接在BitBle函数中赋值
//int height = windowRect.bottom - windowRect.top;
IntPtr handle = this.Handle; //
IntPtr hdDesForm = this.CreateGraphics().GetHdc();
//this.lblLocHandle.Text = handle.ToString();
//this.lblLocDC.Text = hdDesForm.ToString();
//this.lblSrcHandle.Text = hWndSrcForm.ToString();
//this.lblSrcDC.Text = hdcSrc.ToString();
//this.lblClass.Text = Settings.Default.WindowClassName;
Form1.BitBlt(hdDesForm, 100, 100, 250, 65, hdcSrc, 40, 40, Form1.SRCCOPY);//将源DC中的指定位置和大小的图像复制到目标DC
Form1.ReleaseDC(handle, hdDesForm);
Form1.ReleaseDC(hWndSrcForm, hdcSrc);
结果发现FindWindow如果只是用第一个参数(WindowClass)就是窗体类名,一直获取不到正确的目标窗体句柄,后续步骤就无法开展,将下列代码:
hWndSrcForm = FindWindow(strWindowClassName, null);//获取待捕捉窗口句柄更改为:
Process[] locAllProcess = Process.GetProcessesByName(Settings.Default.AppName);
string strMainWindowTitle = locAllProcess[0].MainWindowTitle;
hWndSrcForm = FindWindow(null, strMainWindowTitle);//获取待捕捉窗口句柄
这样做自认为操作更友好,不必使用spy++费劲看窗口类名,可以使用任务管理器开程序名称就成,但仍不能避免多实例运行问题,希望对可能用到FindWindow的朋友有所帮助
...全文
相关推荐
C# findwindow及sendmessage外挂
c# 利用findwindow及sendmessage外挂,也可以程序间通信。但是本代码主要演示外挂方式。
FindWindow.rar
c#利用findwindow点击网页的消息
findwindow_src.zip
类似于spy++,,对于句柄的捕获。c#源代码调用user32.dll实现。 动态获取捕获句柄等信息。如有疑问可以留言,可能不能及时答复,但是看到,有问必答
在C#中调用API函数.
在C#中调用API函数. 在C#中调用API函数. 在C#中调用API函数.
C#关于外挂汉化的一些思考(API函数)
C#关于外挂汉化的一些思考(API函数FindWindow,FindWindowEx,SendMessage)


