Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created June 25, 2022 06:50
Show Gist options
  • Save dj1711572002/d7374cb1a7c4f1c161c6c32aec3dfb03 to your computer and use it in GitHub Desktop.
Save dj1711572002/d7374cb1a7c4f1c161c6c32aec3dfb03 to your computer and use it in GitHub Desktop.
C# mouse3 mouse.cs AutoClick test Program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
namespace Mouse {
public partial class Form1 : Form {
/// <summary>
/// マウス操作のためのAPI
/// </summary>
/// <param name="dwFlags"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <param name="cButtons"></param>
/// <param name="dwExtraInfo"></param>
[DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x2;
private const int MOUSEEVENTF_LEFTUP = 0x4;
private const int MOUSEEVENTF_RIGHTDOWN = 0x8;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
private const int MOUSEEVENTF_MIDDLEUP = 0x40;
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
bool flgClick = false;
bool startflag = false;
int count = 0;
int clickN, iterN;
int intervalT;
public Form1() {
InitializeComponent();
//combBox1 時間間隔
comboBox1.Items.Add("1000");
comboBox1.Items.Add("1500");
comboBox1.Items.Add("2000");
comboBox1.Items.Add("3000");
comboBox1.SelectedIndex = 0;
//comboBox2 繰り返し回数
comboBox2.Items.Add("1");
comboBox2.Items.Add("5");
comboBox2.Items.Add("10");
comboBox2.Items.Add("20");
comboBox2.SelectedIndex = 2;
//
Timer timer1 = new Timer();
this.timer1.Interval =100;
this.timer1.Enabled = true;
timer1.Start();
Timer timer2 = new Timer();
this.timer2.Interval =500;
this.timer2.Enabled = true;
//timer2.Start();
}
private void button1_Click(object sender, EventArgs e)
{
int iCurX, iCurY;
int iLen;
int iTarX, iTarY;
double dIx, dIy;
int i, j;
startflag = true;
iterN = 0;
if (this.textBox1.Text == "") return;
else
{
}
string[] sPos = this.textBox1.Text.Split(',');
if (sPos.Length != 2) return;
iTarX = Convert.ToInt32(sPos[0]);
iTarY = Convert.ToInt32(sPos[1]);
// マウス現在位置取得
iCurX = System.Windows.Forms.Cursor.Position.X;
iCurY = System.Windows.Forms.Cursor.Position.Y;
//combBOx1 のインターバルタイムを読む
intervalT = int.Parse(comboBox1.Text);
//comboBox2のクリック回数を読む
clickN = int.Parse(comboBox2.Text);
/*
// 目的位置までの移動量
if(Math.Abs(iTarX - iCurX) > Math.Abs(iTarY - iCurY)){
iLen = iTarX - iCurX;
dIx = iLen / Math.Abs(iLen);
dIy = (double)(iTarY - iCurY) / (double)Math.Abs(iTarX - iCurX);
} else {
iLen = iTarY - iCurY;
dIx = (double)(iTarX - iCurX) / (double)Math.Abs(iTarY - iCurY);
dIy = iLen / Math.Abs(iLen);
}
// マウス移動
for(int iLop = 0; iLop < Math.Abs(iLen); iLop++){
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(iCurX + (int)((double)iLop * dIx), iCurY + (int)((double)iLop * dIy));
System.Threading.Thread.Sleep(1);
}
*/
// 最終的に目的位置に設定
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(iTarX, iTarY);
/*
//繰り返しクリック
iterN = int.Parse(comboBox2.SelectedItem.ToString());
clickN = 0;
while(clickN<=iterN*2+1) {
// 最終的に目的位置に設定
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(iTarX, iTarY);
// マウスクリック1回目
// System.Threading.Thread.Sleep(300);
if (clickN % 2 == 0)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
else
{
// System.Threading.Thread.Sleep(300);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
DateTime now = DateTime.Now;
clickN++;
label2.Text = clickN.ToString();
now = DateTime.Now;
Debug.Print("clickN="+clickN.ToString()+",iterN="+ iterN.ToString()+"sec="+now.Second+"msec=" +now.Millisecond);
//System.Threading.Thread.Sleep(1000);
}
*/
}
private void timer1_Tick(object sender, EventArgs e) {
String zahyou;
// マウス位置取得
zahyou = Cursor.Position.X.ToString() + "," + Cursor.Position.Y.ToString();
this.toolStripStatusLabel1.Text = zahyou; //Cursor.Position.X.ToString() + "," + Cursor.Position.Y.ToString();
//Debug.Print("timer1_Tick");
if (GetAsyncKeyState(Keys.LButton) != 0 && checkBox1.Checked==false)
{
if (flgClick == false && checkBox1.Checked==false)
{
textBox1.Text = zahyou;
flgClick = true;
checkBox1.Checked = true;
}
}
else if (GetAsyncKeyState(Keys.RButton) != 0)//右ボタンクリックでSTOP
{
startflag = false;
label2.Text = "RightButtonClick=>STOP";
}
else
{
flgClick = false;
if (startflag == true)
{
DoClick();
System.Threading.Thread.Sleep(intervalT);
iterN++;
label2.Text = iterN.ToString();
DateTime now = DateTime.Now;
Debug.Print("Timer1_Click:" + "iterN=" + iterN.ToString() + "sec=" + now.Second + "msec=" + now.Millisecond);
if (iterN >= clickN)
{
startflag = false;
}
}
/*
count++;
Debug.Print("startflag=" + startflag + ":count*100=" + (count * 100).ToString() + "intervalT=" + intervalT.ToString() + "iterN=" + iterN.ToString());
// Click 開始 
if (startflag == true && iterN<=clickN && count*100>=intervalT)
{
iterN++;
DoClick();
//System.Threading.Thread.Sleep(intervalT);
count =0;
DateTime now = DateTime.Now;
Debug.Print("Timer1_Click:" + "iterN=" + iterN.ToString() + "sec=" + now.Second + "msec=" + now.Millisecond);
}
*/
}
}
//private void button2_Click(object sender, EventArgs e)
// {
//this.button1.PerformClick(); //--- ボタン1を押したことにする
//System.Threading.Thread.Sleep(1000);
// startflag = false;
// }
private void Form1_Load(object sender, EventArgs e)
{
}
private void DoClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
// System.Threading.Thread.Sleep(300);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment