关闭

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

296人阅读 评论(0) 收藏 举报

//为了使用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

    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:248301次
    • 积分:3399
    • 等级:
    • 排名:第10235名
    • 原创:21篇
    • 转载:306篇
    • 译文:3篇
    • 评论:42条