• 利用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

        }

  • 相关阅读:
    shell脚本--php执行普通shell命令
    shell脚本--eval执行shell命令
    shell脚本--CGI获取请求数据(GET / POST)
    shell脚本--编写CGI代码(shell结合html)以及环境变量
    shell脚本--初识CGI
    数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
    写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。
    输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
    字符串分隔 ->连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
  • 原文地址:https://www.cnblogs.com/wenrenhua08/p/3993644.html
Copyright © 2020-2023  润新知