user32 sendmessage

来源:互联网 发布:扫街销售知乎 编辑:程序博客网 时间:2024/05/22 00:50
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace restart1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }




    //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")]
        //public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        [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 = "WindowFromPoint")]
        public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        //通过索引查找相应控件句柄
        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;
        }
        public struct POINTAPI
        {
            public int x;
            public int y;
        }
        private void button1_Click(object sender, EventArgs e)
        {

            string path = "C:\\Users\\Microsoft\\Desktop\\timer\\timer\\bin\\Debug\\timer.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("Form1", 100, 25);
            Console.WriteLine("Handle to main window is" + mainWindows);

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

            System.Threading.Thread.Sleep(10000);
            SendMessage(butt, 0xF5, 0, 0);



        }
    }
}


0 0