• C#捕获Windows窗体控件


      最近由于客户的需求,需要一个登陆的解决方案,但是我们需要捕获界面的控件,并赋值,然后自动登录,在网上找了一下,AutoIt这个工具不错,可以捕获的到窗体及控件,但是没有源码呀,自己动手写一个简单版的吧。

    好了,下面咱们来说道说道·······

      思路分析: 我们首先要捕获鼠标的移动,当鼠标按下时,触发一个事件,然后移动鼠标到控件,释放鼠标,捕获控件。如何捕获鼠标事件呢?我们用钩子(Hook)

      

     private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
            {
                MouseControl.MouseHookStruct MyMouseHookStruct = (MouseControl.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseControl.MouseHookStruct));
                if (nCode < 0)
                {
                    return MouseControl.CallNextHookEx(hHook, nCode, wParam, lParam);
                }
                else
                {
                    if (MouseDownEvent != null)
                    {
                        MouseButtons button = MouseButtons.None;
                        int clickCount = 0;
                        switch ((Int32)wParam)
                        {
                            
                            case WM_RBUTTONDOWN:
                                 button = MouseButtons.Right;
                                 clickCount = 1;
                                 break;
                            
                        }
                        var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
                        MouseDownEvent(this,e); // MouseClickEvent(this, e);
                       
                     }
                    if (MouseUpEvent != null)
                    {
                        MouseButtons button = MouseButtons.None;
                        int clickCount = 0;
                        switch ((Int32)wParam)
                        {
                            case WM_RBUTTONUP:
                                button = MouseButtons.Right;
                                clickCount = 1;
                                break;
                        }
                        var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
                        MouseUpEvent(this, e);
                    }
                    if (MouseMoveEvent != null)
                    {
                        MouseButtons button = MouseButtons.None;
                        int clickCount = 0;
                        switch ((Int32)wParam)
                        {
                            case WM_RBUTTONUP:
                                button = MouseButtons.None;
                                clickCount = 1;
                                break;
                        }
                        var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
                        MouseMoveEvent(this, e);
    
                    }
                    this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
                    return MouseControl.CallNextHookEx(hHook, nCode, wParam, lParam);
                }
            }
    

      在前台我们会调用钩子委托,来实现我们想要的结果

       

     1   private void mh_MouseUpEvent(object sender, MouseEventArgs e) {
     2            
     3             if (e.Button == MouseButtons.Right)
     4             {
     5                 Dictionary<string, string> dic = this.m_windowDetective.DicResult();
     6                 CursorControl.BackCursor();
     7                 if (this.ptMouse == this.m_windowDetective.MousePosition)
     8                 {
     9                     this.waitCount++;
    10                     if (this.waitCount < 10)
    11                     {
    12                         this.Text += ".";
    13                         return;
    14                     }
    15                     this.waitCount = 0;
    16                     this.Text = "窗口适配";
    17                 }
    18                 this.ptMouse = this.m_windowDetective.MousePosition;
    19                 //this.txtWorkReport.Text = this.m_windowDetective.Result().ToString();
    20                 txtTitle.Text = dic["标题"].ToString();
    21                 txtClassName.Text = dic["类别"].ToString();
    22                 txtInstance.Text = dic["INSTANCE"].ToString();
    23                 pictureBox1.ImageLocation = Environment.CurrentDirectory + "\Icon\snows.png";
    24                 
    25             }
    26         }
    27         private void mh_MouseMoveEvent(object sender, MouseEventArgs e)
    28         {
    29            if (e.Button == MouseButtons.None)
    30             {
    31                 int x = e.Location.X;
    32                 int y = e.Location.Y;
    33                 this.m_windowDetective.ChangeBorder();
    34             }
    35         }
    View Code
  • 相关阅读:
    sprint1的个人总结及《构建之法》8、9、10章读后感
    操作系统作业----实验三
    《构建之法》第六,七章读后感
    实验二 作业调度模拟程序 报告
    参考的博客园
    复利计算器6.0
    复利计算-做汉堡,结对2.
    复利计算器5.0
    0608典型用户
    0603我的感受
  • 原文地址:https://www.cnblogs.com/ypyhy/p/6265136.html
Copyright © 2020-2023  润新知