C#调用其他程序,比如控制别的程序上的按钮 分类: .NET 20120419 16:50 2216人阅读 评论(0) 收藏

        [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");
            }

        }


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/configman/p/4657592.html