• 利用API函数向外部应用程序发送消息


    //为了使用Win32 API,需要先引入下面这个命名空间
    using System.Runtime.InteropServices;

    namespace k8
    {
        public partial class _3k8Frm : Form
        {
            #region Dll Import 添加对API的引用

                //获取主窗口句柄的API函数
                [DllImport("User32.dll", EntryPoint = "FindWindow")]
                private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
               
                //获取子窗口句柄的API函数
                [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
                private static extern 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);

                const int WM_GETTEXT = 0x000D;
                const int WM_SETTEXT = 0x000C;
                const int WM_CLICK = 0x00F5;

            #endregion

            #region 声明字段 用Spy++查到的
                //下面的这些参数都可以用Spy++查到
                string lpszParentClass = "TForm1"; //整个窗口的类名
                string lpszParentWindow = "Form1"; //窗口标题
                string lpszClass = "TEdit"; //需要查找的子窗口的类名,也就是输入框
                string lpszClass_Submit = "TBitBtn"; //需要查找的Button的类名
                string lpszName_Submit = "确定"; //需要查找的Button的标题
                string text = "";

                string lpszClass1 = "TPanel";//多了一个类,输入框和按钮都放在这个类上的!

                IntPtr ParenthWnd = new IntPtr(0);
                IntPtr ParenthWnd_sub = new IntPtr(0);
                IntPtr EdithWnd = new IntPtr(0);

                //下面的参数设定登陆用户名及密码
                string UserID_3k8 = "4898";
                string Pwd_3k8="2";

            #endregion

            #region 构造函数
           
            public _3k8Frm()
            {
                InitializeComponent();
            }
            #endregion
            
            #region SearchWindow 核心部分,查找窗体并对它进行登陆操作

            private int SearchWindow()
            {
                int retval = 0; //增加一个返回值用来判断操作是否成功 

                //查到窗体,得到整个窗体
                ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
                ParenthWnd_sub = FindWindowEx(ParenthWnd, EdithWnd, lpszClass1, "");


                 //判断这个窗体是否有效
                if (!ParenthWnd_sub.Equals(IntPtr.Zero))
                {
                    //得到User Name这个子窗体,并设置其内容
                    EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass, "");
                    if (!EdithWnd.Equals(IntPtr.Zero))
                    {
                        text = Pwd_3k8.Trim(); //this.tbUserName.Text.Trim();
                        //调用SendMessage方法设置其内容
                        SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
                        retval++;
                    }

                    //得到Password这个子窗体,并设置其内容
                    EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass, "");
                    if (!EdithWnd.Equals(IntPtr.Zero))
                    {
                        text = UserID_3k8; //this.tbPassword.Text.Trim();
                        SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
                        retval++;
                    }

                     //得到Button这个子窗体,并触发它的Click事件
                    EdithWnd = (IntPtr)0;
                    EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass_Submit, lpszName_Submit);
                    if (!EdithWnd.Equals(IntPtr.Zero))
                    {
                        SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
                        retval++;
                    }

                    return retval;
            }

            #endregion

        }

  • 相关阅读:
    用户模板和用户场景
    人月神话阅读笔记02
    人月神话阅读笔记01
    软件工程周总结07
    NABCD
    软件工程周总结06
    软件工程周总结05
    tomcat端口被占用
    SQLyog出现2003错
    一维最大子数组和(续)
  • 原文地址:https://www.cnblogs.com/wenrenhua08/p/3993644.html
Copyright © 2020-2023  润新知