1. 程式人生 > >c# 模擬滑鼠點選

c# 模擬滑鼠點選

            const int MOUSEEVENTF_MOVE = 0x0001;      //移動滑鼠 
            const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模擬滑鼠左鍵按下 
            const int MOUSEEVENTF_LEFTUP = 0x0004; //模擬滑鼠左鍵擡起 
            const int MOUSEEVENTF_RIGHTDOWN = 0x0008;// 模擬滑鼠右鍵按下 
            const int MOUSEEVENTF_RIGHTUP = 0x0010;// 模擬滑鼠右鍵擡起 
            const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;// 模擬滑鼠中鍵按下 
            const int MOUSEEVENTF_MIDDLEUP = 0x0040;// 模擬滑鼠中鍵擡起 
            const int MOUSEEVENTF_ABSOLUTE = 0x8000;// 標示是否採用絕對座標 


            //HtmlElement el = webBrowser1.Document.GetElementById("snext");
            //HtmlElement el = webBrowser1.Document.GetElementById("su");
            HtmlElement el = GetMyEl("http://download.cnet.com/Teacher-s-Attach/3000-20415_4-12476080.html");
            if (el == null) return;
            el.ScrollIntoView(true);
            //el.ScrollTop = 0;
            
            Rectangle r = el.OffsetRectangle;


            int x=0;
            int y=0;
            if (r.Top > webBrowser1.Bottom)
                y = this.Location.Y + webBrowser1.Location.Y + webBrowser1.Bottom;
            else
                y = this.Location.Y + webBrowser1.Location.Y + r.Bottom;
            
            x=this.Location.X+webBrowser1.Location.X+r.Left;
            this.Text = "form location:" + this.Location.X + " " + this.Location.Y + " Webbrowser:" + webBrowser1.Left.ToString() + "   " +
                webBrowser1.Top.ToString() + "bottom" + webBrowser1.Bottom.ToString()
                + "   Html:" + r.Top.ToString() + "x+y" + x.ToString() + ":" + y.ToString(); ;
            
            //程式中我們直接呼叫mouse_event函式就可以了 
            //mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,500, 100, 0, 0);


            SetCursorPos( 25+x,  y-15);


          
            //PostMessage(hButton, WM_LBUTTONDOWN, MK_LBUTTON, NULL);
            //模擬滑鼠右鍵單擊事件: 
            mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
            //這裡是滑鼠左鍵按下和鬆開兩個事件的組合即一次單擊: 
            //mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);