5月22日
用Mouse_event()來控制鼠標(biāo)操作
在自動化測試的開發(fā)中,有一些控件的ID是很難找到的,所以有些時候,我們直接設(shè)置鼠標(biāo)的位置,然后是用click事件,會收到很好的效果。在Windows API中有個mouse_event函數(shù)為我們準(zhǔn)備好了這一切。
這個函數(shù)在user32.dll這個庫文件里面。我們可以在C:/WINDOWS/system32(XP系統(tǒng))這個目錄下找到這個文件,他是系統(tǒng)自帶的。 我們以C#直接調(diào)用這個文件中的API為例子來說下怎么進(jìn)行鼠標(biāo)操作,首先在我們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,下表中標(biāo)志之一或它們的組合 dx,dy Long,根據(jù)MOUSEEVENTF_ABSOLUTE標(biāo)志,指定x,y方向的絕對位置或相對位置 cButtons Long,沒有使用 dwExtraInfo Long,沒有使用
dwFlags常數(shù) 意義
const int MOUSEEVENTF_MOVE = 0x0001; 移動鼠標(biāo) const int MOUSEEVENTF_LEFTDOWN = 0x0002; 模擬鼠標(biāo)左鍵按下 const int MOUSEEVENTF_LEFTUP = 0x0004; 模擬鼠標(biāo)左鍵抬起 const int MOUSEEVENTF_RIGHTDOWN = 0x0008; 模擬鼠標(biāo)右鍵按下 const int MOUSEEVENTF_RIGHTUP = 0x0010; 模擬鼠標(biāo)右鍵抬起 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; 模擬鼠標(biāo)中鍵按下 const int MOUSEEVENTF_MIDDLEUP = 0x0040; 模擬鼠標(biāo)中鍵抬起 const int MOUSEEVENTF_ABSOLUTE = 0x8000; 標(biāo)示是否采用絕對坐標(biāo)
程序中我們直接調(diào)用mouse_event函數(shù)就可以了 mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);
1、這里是鼠標(biāo)左鍵按下和松開兩個事件的組合即一次單擊: mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
2、模擬鼠標(biāo)右鍵單擊事件: mouse_event (MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 )
3、兩次連續(xù)的鼠標(biāo)左鍵單擊事件 構(gòu)成一次鼠標(biāo)雙擊事件: mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ) mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
4、使用絕對坐標(biāo) MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0
需要說明的是,如果沒有使用MOUSEEVENTF_ABSOLUTE,函數(shù)默認(rèn)的是相對于鼠標(biāo)當(dāng)前位置的點,如果dx,和dy,用0,0表示,這函數(shù)認(rèn)為是當(dāng)前鼠標(biāo)所在的點。5、直接設(shè)定絕對坐標(biāo)并單擊 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分別是你要點擊的點的橫坐標(biāo)和縱坐標(biāo)
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; } } }
|