一个程序通过窗体句柄控制另一个窗体
接下来,我们来演示不同程序中,怎么通过窗体句柄控制其它程序窗体。
演示的例子是:控制系统的记事本窗体,先移动,再最大化,最后关闭
要求:桌面上有一个记事本,名称是:测试文本.txt - 记事本,保证是打开的
界面只有一个按钮,btnCloseText
引用的windowapi函数dll如下:
/// <summary>
/// 获取窗体的句柄函数
/// </summary>
/// <param name="lpClassName">窗口类名</param>
/// <param name="lpWindowName">窗口标题名</param>
/// <returns>返回句柄</returns>
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 移动窗体的函数
/// </summary>
/// <param name="hWnd">窗体句柄</param>
/// <param name="x">窗体新位置x轴坐标</param>
/// <param name="y">窗体新位置y轴坐标</param>
/// <param name="nWidth">窗体新位置宽度</param>
/// <param name="nHeight">窗体新位置高度</param>
/// <param name="BRePaint">是否刷新窗体</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
/// <summary>
/// 通过句柄,窗体显示函数
/// </summary>
/// <param name="hWnd">窗体句柄</param>
/// <param name="cmdShow">显示方式</param>
/// <returns>返工成功与否</returns>
[DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)]
public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
/// <summary>
/// 发送消息到窗体函数
/// </summary>
/// <param name="hwnd">窗体句柄</param>
/// <param name="wMsg">消息类型</param>
/// <param name="wParam">附带消息</param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
实现代码:
//wMsg类型,关闭窗体
private const int WM_CLOSE = 0x10;
private void btnCloseText_Click(object sender, EventArgs e)
{
IntPtr textHandle = FindWindow(null, "测试文本.txt - 记事本");
if (textHandle != IntPtr.Zero)
{
//移动记事本窗体
MoveWindow(textHandle, 0, 100, 800, 600, true);
//延时3秒后
Thread.Sleep(3000);
//设置窗体最大化显示
ShowWindow(textHandle, 3);
//延时3秒后
Thread.Sleep(3000);
//向记事本程序窗体发送关闭信息
SendMessage(textHandle, WM_CLOSE,0,0);
}
}
整体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 获取窗体的句柄函数
/// </summary>
/// <param name="lpClassName">窗口类名</param>
/// <param name="lpWindowName">窗口标题名</param>
/// <returns>返回句柄</returns>
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 移动窗体的函数
/// </summary>
/// <param name="hWnd">窗体句柄</param>
/// <param name="x">窗体新位置x轴坐标</param>
/// <param name="y">窗体新位置y轴坐标</param>
/// <param name="nWidth">窗体新位置宽度</param>
/// <param name="nHeight">窗体新位置高度</param>
/// <param name="BRePaint">是否刷新窗体</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
/// <summary>
/// 通过句柄,窗体显示函数
/// </summary>
/// <param name="hWnd">窗体句柄</param>
/// <param name="cmdShow">显示方式</param>
/// <returns>返工成功与否</returns>
[DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)]
public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
/// <summary>
/// 发送消息到窗体函数
/// </summary>
/// <param name="hwnd">窗体句柄</param>
/// <param name="wMsg">消息类型</param>
/// <param name="wParam">附带消息</param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
//wMsg类型,关闭窗体
private const int WM_CLOSE = 0x10;
private void btnCloseText_Click(object sender, EventArgs e)
{
IntPtr textHandle = FindWindow(null, "测试文本.txt - 记事本");
if (textHandle != IntPtr.Zero)
{
//移动记事本窗体
MoveWindow(textHandle, 0, 100, 800, 600, true);
//延时3秒后
Thread.Sleep(3000);
//设置窗体最大化显示
ShowWindow(textHandle, 3);
//延时3秒后
Thread.Sleep(3000);
//向记事本程序窗体发送关闭信息
SendMessage(textHandle, WM_CLOSE,0,0);
}
}
}
}
演示结果如下:
1)刚开始运行结果(图片太大,不截图)
2)窗体移动后结果(图片太大,不截图)
3)最大化窗体结果(图片太大,不截图)
4)关闭记事本结果(图片太大,不截图)
平时多记记,到用时才能看看,记录你的进步,分享你的成果