前言
最近玩貪玩藍月,老是讓我呆呆站着領在線禮包?我可能每過幾分鐘就領一下禮包嗎?害,要是有人能幫我一會點一下鼠標就好了… …於是,奇怪的知識又增加了… …
頁面設計
注:因爲控件可能有點多,代碼中也沒有辦法標明是哪裏,所以又回頭把所有控件的名字都標在上面,肝完保存才發現有幾個第一個字母只顯示了一半,這裏補充一下,顯示一半的文本框第一個字母是t,單選按鈕第一個字母是r,按鈕第一個字母是b,因爲每個都標上去要耗費很久時間,所以就不再重新制作一遍了,希望理解。(其中button1、button2這些命名格式是反面教材!!!)
界面很簡單,一般來說,模擬鼠標操作一般都是給定像素位置,所以起始要先設置一下自己屏幕的分辨率,分辨率不符點擊的效果就會出現誤差。當然,憑肉眼看要點擊的目標點的位置貌似有點扯,但總不能就這點兒需求還需要第三方軟件輔助吧?所以右上角就是簡單的獲取當前鼠標位置,大概如下圖:
然後就是鼠標事件的執行,包括朝指定位置移動阿,朝指定方向移動啊,之類的,還有鼠標的單擊,雙擊,右鍵,以及滾輪的滑動。然後就是整個程序的“開始”和“停止”了,接下來就看一看代碼吧。
代碼實現部分
引入模擬鼠事件
具體每個操作對應的行爲都在註釋裏標明瞭。
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int mouse_event(int mouseevent, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
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; //標示是否採用絕對座標
const int MOUSEEVENTF_WHEEL = 0x800; //模擬鼠標滑輪移動
更改分辨率事件
這裏默認的是1366786,因爲博主的是1366768(分辨率很低了,害),然後就是輸入格式正確的話,就對power_X,power_Y分別重新賦值,因爲下面移動以及點擊都需要用到屏幕的分辨率。
public int power_X = 1366; //分辨率X的值
public int power_Y = 768; //分辨率Y的值
private void button4_Click(object sender, EventArgs e)
{
try
{
power_X = int.Parse(power_Xtxt.Text);
power_Y = int.Parse(power_Ytxt.Text);
MessageBox.Show("分辨率更改成功,若不爲默認分辨率,則每次啓動均需手動修改,謝謝", "操作成功", MessageBoxButtons.OK);
}
catch (Exception eee)
{
MessageBox.Show("分辨率未成功修改,請檢查輸入格式是否正確\n詳細錯誤信息爲:"+eee.Message, "錯誤提示", MessageBoxButtons.OK);
}
}
獲取鼠標當前位置
有時需要查看目標點的位置,就可以需要使用此功能,下列代碼依次爲“開始顯示”,“停止顯示”以及“timer顯示事件”。
private void button5_Click(object sender, EventArgs e)
{
timerMousemovement.Enabled = true; //開始記錄鼠標位置
}
private void button6_Click(object sender, EventArgs e)
{
timerMousemovement.Enabled = false; //停止記錄鼠標位置
nowX.Text = "已結束";
nowY.Text = "已結束";
}
private void timerMousemovement_Tick(object sender, EventArgs e)
{
nowX.Text = Cursor.Position.X.ToString(); //記錄鼠標指針的詳細座標
nowY.Text = Cursor.Position.Y.ToString();
}
設定按鈕事件
此功能僅爲重新賦予定時器間隔。
private void button3_Click(object sender, EventArgs e)
{
string time = ((double.Parse(Secondtxt.Text) + double.Parse(Minutetxt.Text) * 60) * 1000).ToString(); //設定timer間隔時間
timer1.Interval = int.Parse(time);
}
執行定時器事件
此處的定時器分別調用點擊方法和移動方法,根據“先執行移動”和“先執行點擊”按鈕的選中狀態來建立點擊和移動的先後順序,註釋中也有說明。
private void timer1_Tick(object sender, EventArgs e)
{
if (radiokickfirst.Checked == true)
{
buttendown(); //鼠標點擊操作
}
mouse_move(); //鼠標移動操作
if (radiomovefirst.Checked == true)
{
buttendown();
}
//如果選擇了鼠標點擊優先,就執行前兩個,點擊事件在前,如果選擇移動優先,就執行後兩個,移動事件在前,達到相應的邏輯關係
}
鼠標點擊方法
根據單選按鈕的選中狀態執行相關語句,由上到下依次爲:左鍵單擊、左鍵雙擊、右鍵單擊和滾輪操作,其中前三個都是調用語句即可,滾輪模擬的具體用法註釋中也有解釋。
private void buttendown()
{
if (radiolifedown.Checked == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (radiolifedoubledown.Checked == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
if (radiorightdown.Checked == true)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
}
if (radioRoller.Checked == true)
{
try
{
int count = int.Parse(textRoller.Text);
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, count, 0); //控制鼠標滑輪滾動,count代表滾動的值,負數代表向下,正數代表向上,如-100代表向下滾動100的y座標
}
catch (Exception eee)
{
timer1.Enabled = false;
MessageBox.Show("引發此異常的原因是您輸入的滾輪數值爲非法格式\n您可以單擊“確定”並檢查輸入信息後重試\n詳細錯誤信息爲:" + eee.Message, "錯誤提示", MessageBoxButtons.OK);
}
}
}
鼠標移動方法
此處即需要使用開頭定義的屏幕分辨率了,從上到下依次爲向指定座標移動和向指定方向移動,其中方向移動從上到下依次爲向上、下、左、右移動。
private void mouse_move()
{
try
{
if (radioButton1.Checked == true)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, (int.Parse(textBoxmoveX.Text)) * 65536 / power_X, (int.Parse(textBoxmoveY.Text)) * 65536 / power_Y, 0, 0);
}
if (radioButton2.Checked == true)
{
if (checkBoxUP.Checked == true)
{
int nowX = Cursor.Position.X; //獲取當前座標的X,Y值
int nowY = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, (nowX) * 65536 / power_X, (nowY - int.Parse(textBoxUP.Text)) * 65536 / power_Y, 0, 0); //根據輸入的power_X,power_Y的分辨率值詳細移動
}
if (checkBoxdown.Checked == true)
{
int nowX = Cursor.Position.X;
int nowY = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, (nowX) * 65536 / power_X, (nowY + int.Parse(textBoxdown.Text)) * 65536 / power_Y, 0, 0);
}
if (checkBoxlife.Checked == true)
{
int nowX = Cursor.Position.X;
int nowY = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, (nowX - int.Parse(textBoxlife.Text)) * 65536 / power_X, (nowY) * 65536 / power_Y, 0, 0);
}
if (checkBoxright.Checked == true)
{
int nowX = Cursor.Position.X;
int nowY = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, (nowX + int.Parse(textBoxrigth.Text)) * 65536 / power_X, (nowY) * 65536 / power_Y, 0, 0);
}
}
}
catch (Exception eee)
{
timer1.Enabled = false;
MessageBox.Show("引發此異常的原因可能是您輸入非法格式,或邏輯選擇錯誤\n您可以單擊“確定”並檢查輸入信息後重試\n詳細錯誤信息爲:" + eee.Message, "錯誤提示", MessageBoxButtons.OK);
}
}
開始&結束
開始和結束按鈕就是簡單的啓動定時器和關閉定時器。
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true; //定時器啓動
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false; //定時器關閉
}
其他邏輯關係
即如果沒有勾選移動鼠標時,移動鼠標選項不啓用,沒有勾選方向移動時,運動方向不啓用,諸如此類,是爲了避免程序出錯。
private void checkBoxmove_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxmove.Checked == true)
{
groupmove.Enabled = true;
}
else
{
groupmove.Enabled = false;
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
textBoxmoveX.Enabled = true;
textBoxmoveY.Enabled = true;
}
else
{
textBoxmoveX.Enabled = false;
textBoxmoveY.Enabled = false;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked == true)
{
groupBoxmovewhere.Enabled = true;
}
else
{
groupBoxmovewhere.Enabled = false;
}
}
private void radioRoller_CheckedChanged(object sender, EventArgs e)
{
if (radioRoller.Checked == true)
{
groupBox5.Enabled = true;
}
else
{
groupBox5.Enabled = false;
}
}
結語
模擬鼠標操作的方法有很多種,組合也有很多,這個小例子只是課堂作業基本的小玩意,希望你能喜歡。如果你覺得對你有所幫助就請給我點個贊,關注更好,嘿嘿,謝謝。如有描述錯誤或不足之處,還望指正。