C#调用其他程序,比如控制别的程序上的按钮

来源:互联网 发布:aspose.cells java 编辑:程序博客网 时间:2024/05/23 19:35
        [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 );        private void button2_Click(object sender, EventArgs e)        {            const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册            IntPtr hwndPhoto = FindWindow(null, "UC580Demo"); //查找拍照程序的句柄【任务管理器中的应用程序名称】                        if (hwndPhoto != IntPtr.Zero)            {                IntPtr hwndThree = FindWindowEx(hwndCalc, 0, null, "快照"); //获取按钮快照的句柄                SetForegroundWindow(hwndPhoto);    //将UcDemo程序设为当前活动窗口                System.Threading.Thread.Sleep(500);   //暂停500毫秒                               SendMessage(hwndThree, BM_CLICK, 0, 0);//模拟点击拍照按钮            }            else            {                MessageBox.Show("没有启动 UcDemo");            }        }


原创粉丝点击