如何模拟在应用程序中按按钮

nattystyle 2010-07-03 11:08:39
现有一个应用程序Application1,上面有一个Button按钮

我如何用我自己的程序去模拟点击按下Button。

求实现方法,还有源代码
...全文
177 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
nattystyle 2010-07-05
  • 打赏
  • 举报
回复
人生如梦兄给的代码很有用

不过对FindWindowEx这个不是很理解

IntPtr hwnd = FindWindowEx(hwndCalc, 0,null, "1");


我想找到Button名为"搜索"然后按下代码该怎么写?
Wude_S 2010-07-04
  • 打赏
  • 举报
回复
缺10分
sxldfang 2010-07-04
  • 打赏
  • 举报
回复
mark,学习学习
qqzhangmin520 2010-07-04
  • 打赏
  • 举报
回复
路过 看看
amandag 2010-07-04
  • 打赏
  • 举报
回复
1. 如果应用程序是.NET写的,可以用反射模拟实现
2. 否则需要调用windows api函数
Kevin-yi 2010-07-04
  • 打赏
  • 举报
回复
打酱油的
libinlink 2010-07-04
  • 打赏
  • 举报
回复
参考《.net 自动化测试》一书吧。

.net 的方法来处理。
yatou1990 2010-07-04
  • 打赏
  • 举报
回复
学习了
熙风 2010-07-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
[DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
[DllImport ( "user32.dll", EntryPoint……
[/Quote]






学习
leavemaple 2010-07-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyazhe 的回复:]
如梦真快……
xp下可能可以,win7下无效。不过只做例子,已经很明确了。
[/Quote]
Win7的计算机不可以,是因为他的按钮和主窗体不在一个Form里,按钮嵌套在另外的Form里,
用EnumChildWindows继续查找一层即可。
wuyq11 2010-07-03
  • 打赏
  • 举报
回复
[DllImport ( "user32.dll", EntryPoint = "FindWindow", 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 )
const uint BM_CLICK = 0xF5;
IntPtr hwndCalc = FindWindow ( null, "计算器" );
if ( hwndCalc != IntPtr.Zero )
{
IntPtr hwnd = FindWindowEx ( hwndCalc, 0, null, "1" );
SetForegroundWindow ( hwndCalc );
SendMessage (hwnd , BM_CLICK, 0, 0 );
}
hellosm 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
FindWindow找到窗体,FindWindowEx找到对应控件
激活对应窗体,机会对应控件,然后SendKeys.Send("Enter");
[/Quote]
用API??说真的,API我还不怎么会使用。之前装的msdn又搜索不到相关资料···
求赐教~
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
你用什么操作系统?我写个小例子好了。模拟点击计算器的1
nattystyle 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
FindWindow找到窗体,FindWindowEx找到对应控件
激活对应窗体,机会对应控件,然后SendKeys.Send("Enter");
[/Quote]

能不能说的具体一点
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
FindWindow找到窗体,FindWindowEx找到对应控件
激活对应窗体,机会对应控件,然后SendKeys.Send("Enter");
zgke 2010-07-03
  • 打赏
  • 举报
回复
button1.PerformClick();
jianuMan 2010-07-03
  • 打赏
  • 举报
回复
http://www.cnblogs.com/jianu/archive/2010/06/28/1767048.html
FindWindows("窗体标题")
SendMessage 发送鼠标左键点击消息
你需要测出按钮中间位置的坐标
兔子-顾问 2010-07-03
  • 打赏
  • 举报
回复
如梦真快……
xp下可能可以,win7下无效。不过只做例子,已经很明确了。

110,578

社区成员

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

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

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