小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C# .NET中如何使用GetCursorPos函數(shù)

 corefashion 2015-01-02
5月22日

用Mouse_event()來控制鼠標操作

在自動化測試的開發(fā)中,有一些控件的ID是很難找到的,所以有些時候,我們直接設(shè)置鼠標的位置,然后是用click事件,會收到很好的效果。在Windows API中有個mouse_event函數(shù)為我們準備好了這一切。

這個函數(shù)在user32.dll這個庫文件里面。我們可以在C:/WINDOWS/system32(XP系統(tǒng))這個目錄下找到這個文件,他是系統(tǒng)自帶的。 我們以C#直接調(diào)用這個文件中的API為例子來說下怎么進行鼠標操作,首先在我們C#中聲明引用,如果是一個基于From的程序,這個聲明的位置寫在你的From class就可以了 
[System.Runtime.InteropServices.DllImport("user32")] 
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

參數(shù) 意義 
dwFlags Long,下表中標志之一或它們的組合 
dx,dy Long,根據(jù)MOUSEEVENTF_ABSOLUTE標志,指定x,y方向的絕對位置或相對位置 
cButtons Long,沒有使用 
dwExtraInfo Long,沒有使用

dwFlags常數(shù) 意義

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; 標示是否采用絕對坐標

 

程序中我們直接調(diào)用mouse_event函數(shù)就可以了 
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);

1、這里是鼠標左鍵按下和松開兩個事件的組合即一次單擊: 
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )

2、模擬鼠標右鍵單擊事件: 
mouse_event (MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 )

3、兩次連續(xù)的鼠標左鍵單擊事件 構(gòu)成一次鼠標雙擊事件: 
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )

4、使用絕對坐標 
MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0

需要說明的是,如果沒有使用MOUSEEVENTF_ABSOLUTE,函數(shù)默認的是相對于鼠標當(dāng)前位置的點,如果dx,和dy,用0,0表示,這函數(shù)認為是當(dāng)前鼠標所在的點。5、直接設(shè)定絕對坐標并單擊
mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0); 
mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0); 
其中X,Y分別是你要點擊的點的橫坐標和縱坐標

C# .NET中如何使用GetCursorPos函數(shù) 例程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CursorPosition
{
    public partial class frmMain : Form
    {
        // We need to use unmanaged code
        [DllImport("user32.dll")]
        // GetCursorPos() makes everything possible
        static extern bool GetCursorPos(ref Point lpPoint);
        // Variable we will need to count the traveled pixels
        static protected long totalPixels = 0;
        static protected int currX;
        static protected int currY;
        static protected int diffX;
        static protected int diffY;

        public frmMain()
        {
            InitializeComponent();
        }

        private void tmrDef_Tick(object sender, EventArgs e)
        {
            // New point that will be updated by the function with the current coordinates
            Point defPnt = new Point();
            // Call the function and pass the Point, defPnt
            GetCursorPos(ref defPnt);
            // Now after calling the function, defPnt contains the coordinates which we can read
            lblCoordX.Text = "X = " + defPnt.X.ToString();
            lblCoordY.Text = "Y = " + defPnt.Y.ToString();
            // If the cursor has moved at all
            if (diffX != defPnt.X | diffY != defPnt.Y)
            {
                // Calculate the distance of movement (in both vertical and horizontal movement)
                diffX = (defPnt.X - currX);
                diffY = (defPnt.Y - currY);
                // The difference will be negative if the cursor was moved left or up
                // and if it is so, make the number positive
                if (diffX < 0)
                {
                    diffX *= -1;
                }
                if (diffY < 0)
                {
                    diffY *= -1;
                }
                // Add to the "pixels traveled" counter
                totalPixels += diffX + diffY;
                // And display inside a label
                lblTravel.Text = "You have traveled " + totalPixels + " pixels";
            }
            // We need this to see the difference of pixels between two mouse movements
            currX = defPnt.X;
            currY = defPnt.Y;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            totalPixels = 0;
        }
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多