当前位置:网站首页>通过Win32API调用另一界面的按钮
通过Win32API调用另一界面的按钮
2022-06-29 09:26:00 【zlbcdn】
功能说明
有两个团队创建的两个相互独立的winform界面,其中一个团队在未知源码的前提下,操作另一个团队的界面的button。
展示
主界面如下: 
子界面如下: 
子界面有一按钮(测试),点击该按钮,弹出“字窗体的提示内容”提示框。
当点击父窗体(Form1)上的按钮(button1)时,可以操作子窗体(子窗体的窗体)的按钮(测试),并出现提示框
代码
子窗体的代码:
namespace TestForm
{
public partial class MyTestForm : Form
{
public MyTestForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("子窗体的提示内容!");
}
}
}父窗体的代码
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindowA", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Process.Start("TestForm.exe");
const int WM_CLICK = 0x00F5;//鼠标点击消息,各种消息的数值,可以参考MSDN
string lpszName_TestChild = "子窗体的窗体";//子窗体的标题(Text属性值)
string lpszName_btnYes = "测试";//子窗体上button的标题(Text属性值)
IntPtr hwndTestChild = new IntPtr();//子窗体的句柄
IntPtr hwndbtnYes = new IntPtr();//子窗体上button的句柄
hwndTestChild = FindWindow(null, lpszName_TestChild);//获取子窗体的句柄
hwndbtnYes = FindWindowEx(hwndTestChild, 0, null, lpszName_btnYes);//获取子窗体上button的句柄
if (hwndTestChild != IntPtr.Zero)
{
SendMessage(hwndbtnYes, WM_CLICK, 0, 0);//给子窗体上button发送鼠标点击消息,
}
else
{
MessageBox.Show("未找到!");
}
}
catch (System.Exception ex)
{
throw ex;
}
}
}其他说明
1、方法说明中:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
此处EntryPoint = “FindWindow”或EntryPoint = “FindWindowA”均可。
本人环境VS2010,Win7旗舰。之前FindWindow的返回结果一直是0。但不知为何又好了。
2、关于session0的相关问题:
1、关于部分WIN32 API 在WIN 7下失效的问题
2、Session 0 Isolation
3、FindWindow始终返回0
3、参考资料
边栏推荐
猜你喜欢
随机推荐
Simulation problem of two stacks
Codeforces Round #652 (Div. 2)
L2-3 is this a binary search tree- The explanation is wonderful
Codeforces Round #657 Div. 2
这个开源项目超哇塞,手写照片在线生成
Beautiful ruins around Kiev -- a safe guide to Chernobyl!
Nacos registry cluster
Dynamic planning summary
Seaweedfs security configuration
HDU 4578 Transformation(线段树+有技巧的懒标记下放)
September 29, 2020 non commodity templating code level rapidjson Library
同花顺炒股软件可靠吗,安全吗?
Virtual machine port scanning
2019.10.23训练总结
Recyclerview universal adapter package
To 3 -- the last programming challenge
L1-009 sum of N numbers (20 points)
Picture verification code control
Wandering -- the last programming challenge
JVM method return address









