读书人

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

发布时间: 2012-05-21 18:04:41 作者: rapoo

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

C# code
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);




[解决办法]
句柄啊 应该可以滴 sendmessage 发消息 API 你后面参数不对吧
[解决办法]
应该模拟鼠标点击的
[解决办法]
难道不能调试 ????
[解决办法]
0x101KEYUP消息。对找到的键钮发送这个消息。
[解决办法]
- -!、、遇到这种情况 习惯有PostMessage的路过 不过这里都一样
PostMessage(hWnd,WM_LBUTTONDOWN,0,0);//鼠标左键点下
PostMessage(hWnd,WM_LBUTTONUP,0,0);//鼠标左键抬起
hWnd,你的句柄、、、
还有 那个wParam 和 lParam 话说在c#中采用的是IntPtr类型 反正后面两个是空参数就是了 也就是不用传参数
IntPtr的话 貌似 是IntPtr.zero、、
- -!、、反正 对外界的程序的按钮 要触发他的点击事件、、我是怎么干的、、还有楼主的代码 没有去看完、、所以 不知道是不是你要的、、好久没有看c#的代码了、所以 感觉有点 不习惯了、、
[解决办法]
你消息发错了 童鞋
应该是对按钮1 发消息 第一次参数是按钮1的句柄你写成计算器窗体句柄了
第二个参数是楼上写的WM_LBUTTONDOWN 然后模拟 左键点下 在抬起 就好了
晚上我给你发个代码 你看看吧 现在手头木有

读书人网 >C#

热点推荐