c#获取外部程序的某个按钮句柄并触发点击事件?

无爱大叔 2012-04-19 05:09:35

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

[STAThread]
static void Main(string[] args)
{
string path = "C:\\WINDOWS\\system32\\calc.exe";
Process p = Process.Start(path);
if (p == null)
Console.WriteLine("Warning:process may already exist");


Console.WriteLine("Finding main window handle");
IntPtr mainWindows = FindMainWindowHandle("计算器", 100, 25);
Console.WriteLine("Handle to main window is " + mainWindows);

//有名字控件句柄
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mainWindows, IntPtr.Zero, null, "Backspace");//这里的1是,计算器上名字为1的按钮
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
SendMessage(mainWindows, 0X101, butt, null);


//没有名字或者重名控件
//Console.WriteLine("Findding handle to listbox1");
//IntPtr lb = FindWindowByIndex(mwh, 3);
//if (lb == IntPtr.Zero)
// throw new Exception("Unable to find listbox1");
//else
// Console.WriteLine("Handle to listbox1 is " + lb);

}

//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent, result, null, null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct < index && result != IntPtr.Zero);
return result;
}
}

//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Form has been found");
formFound = true;
}
}

if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
}
}
}


我用windows的计算器做的试验。
这里的0X101是个啥东东?为啥这句话执行了没有任何反应?
SendMessage(mainWindows, 0X101, butt, null);
...全文
2358 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
莪只是个坏人 2014-06-25
  • 打赏
  • 举报
回复
楼主 怎么样 这个解决了么?遇到类似的问题,求分享
「已注销」 2012-07-17
  • 打赏
  • 举报
回复
SendMessage(butt, 0X101, butt, null);
果断是上面找到的butt值才是要点击的窗口句柄
wy811007 2012-04-20
  • 打赏
  • 举报
回复
你消息发错了 童鞋
应该是对按钮1 发消息 第一次参数是按钮1的句柄你写成计算器窗体句柄了
第二个参数是楼上写的WM_LBUTTONDOWN 然后模拟 左键点下 在抬起 就好了
晚上我给你发个代码 你看看吧 现在手头木有
crystal_lz 2012-04-20
  • 打赏
  • 举报
回复
- -!、、遇到这种情况 习惯有PostMessage的路过 不过这里都一样
PostMessage(hWnd,WM_LBUTTONDOWN,0,0);//鼠标左键点下
PostMessage(hWnd,WM_LBUTTONUP,0,0);//鼠标左键抬起
hWnd,你的句柄、、、
还有 那个wParam 和 lParam 话说在c#中采用的是IntPtr类型 反正后面两个是空参数就是了 也就是不用传参数
IntPtr的话 貌似 是IntPtr.zero、、
- -!、、反正 对外界的程序的按钮 要触发他的点击事件、、我是怎么干的、、还有楼主的代码 没有去看完、、所以 不知道是不是你要的、、好久没有看c#的代码了、所以 感觉有点 不习惯了、、
无爱大叔 2012-04-19
  • 打赏
  • 举报
回复

//有名字控件句柄
Console.WriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mainWindows, IntPtr.Zero, "Button", "1");//这里的1是,计算器上名字为1的按钮
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1");
else
Console.WriteLine("Handle to button1 is " + butt);
SendMessage(mainWindows, 0x102, butt, null);
Console.ReadLine();

求指点,SendMessage应该如何写?
无爱大叔 2012-04-19
  • 打赏
  • 举报
回复
没写过c/s这样的,我接触的大多数都是web开发。
我要实现的就是找到 “windows计算器” 窗口,并点击上面的按钮“1”这个按钮就行了
足球中国 2012-04-19
  • 打赏
  • 举报
回复
0x101KEYUP消息。对找到的键钮发送这个消息。
changjiangzhibin 2012-04-19
  • 打赏
  • 举报
回复
难道不能调试 ????
无爱大叔 2012-04-19
  • 打赏
  • 举报
回复
我不太懂,能帮我调试一下吗?
wy811007 2012-04-19
  • 打赏
  • 举报
回复
应该模拟鼠标点击的
wy811007 2012-04-19
  • 打赏
  • 举报
回复
句柄啊 应该可以滴 sendmessage 发消息 API 你后面参数不对吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧