How to make the mouse click...

How can i make the mouse click automatic, 

example: 

if(num5 == num 6) 

//Mouse click 


Thx in advance 

Note: This is C# and windows form.


Answer this question

How to make the mouse click...

  • synesthetia

    If you are in an class that inherits from Form you might be able to do something like this:

    this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, MousePosition.X, MousePosition.Y, 0));

    I'm not sure it that will do exactly what you want but its worth a try.

    Why do you want to click the mouse button automatically anyway

    HTH

  • Shadokin

    >full application application

    Full screen application  see this sample application:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    class MyForm: Form
    {
    public MyForm()
    {
    FormBorderStyle = FormBorderStyle.None;
    WindowState = FormWindowState.Maximized;
    Button button = new Button();
    button.Text =  "Do click";
    button.Parent = this;
    button.Click += new EventHandler(OnButtonClick);
    Click += new EventHandler(OnFormClick);
    }

    void OnButtonClick(object sender, EventArgs e)
    {
    PerformClick();
    }

    void OnFormClick(object sender, EventArgs e)
    {
    MessageBox.Show("Form was clicked by mouse at: " + PointToClient(Cursor.Position));
    }

    static void Main()
    {
    Application.Run(new MyForm());
    }

    void PerformClick() 

    const int x = 30000; //see mouse_event in MSDN for details on this value 
    const int y = 30000; 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, x, y, 0, UIntPtr.Zero); 


    [DllImport("User32.dll")] 
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, 
    UIntPtr dwExtraInfo); 

    const int MOUSEEVENTF_RIGHTDOWN = 0x0008; 
    const int MOUSEEVENTF_RIGHTUP = 0x0010; 
    const int MOUSEEVENTF_MOVE = 0x0001; 
    const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    }

  • modem

    Thanks for the help.
  • umuhk

    Hi everyone...in the future, I'd ask that you all please use the <<b>code</b>><<b>/code</b>> tags in the future when posting code so it is formatted nicer.  Thanks!  :) 
  • KrishnaC

    Didnt work, 

    I want the autoclick to select thing and more.

    Here is the code:

    if(str == Testestr)
    {
    // Auto click
    }

  • YingZ

    > ... i changed that to LEFTDOWN and LEFTUP, maybe that is the problem. 

    No, I checked this, and it works correctly:

    using System; 
    using System.Windows.Forms; 
    using System.Runtime.InteropServices; 
    using System.Diagnostics;

    class MyForm: Form 

    Process notepadProcess;

    public MyForm() 

    ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Maximized;
    notepadProcess = Process.Start(startInfo);
    notepadProcess.WaitForInputIdle();

    Button button = new Button(); 
    button.Text = "click notepad"; 
    button.Parent = this; 
    button.Click += new EventHandler(OnButtonClick); 

    TopMost = true;


    protected override void OnClosed(EventArgs e)
    {
    if (!notepadProcess.HasExited)
    notepadProcess.CloseMainWindow();
    }

    void OnButtonClick(object sender, EventArgs e) 

    PerformClick(); 


    static void Main() 

    Application.Run(new MyForm()); 


    void PerformClick() 

    const int x = 30000; //see mouse_event in MSDN for details on this value 
    const int y = 30000; 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero); 


    [DllImport("User32.dll")] 
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, 
    UIntPtr dwExtraInfo); 

    const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    const int MOUSEEVENTF_LEFTUP = 0x0004;
    const int MOUSEEVENTF_MOVE = 0x0001; 
    const int MOUSEEVENTF_ABSOLUTE = 0x8000; 
    }

  • madref

    Someone told me i could use API calls, how do i make that
  • Aleksandar Petkovic

    There are two errors in your code:
    1. wParam of WM_RBUTTONx messages should be "key indicators" and lParam - "horizontal and vertical position" (see description of WM_RBUTTONUP/WM_RBUTTONDOWN in MSDN)
    2. if you want perform click onto desktop - you must specify desktop Handle - not Form (Desktop handle always zero)

    Instead of sending messages directly, you can use mouse_event function:

    void PerformClick() 

    const int x = 30000; //see mouse_event in MSDN for details on this value 
    const int y = 30000;
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, UIntPtr.Zero);
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, x, y, 0, UIntPtr.Zero);
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, x, y, 0, UIntPtr.Zero);
    }

    [DllImport("User32.dll")]
    static extern void mouse_event(int dwFlags,int dx, int dy,int dwData,
    UIntPtr dwExtraInfo);

    const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    const int MOUSEEVENTF_RIGHTUP = 0x0010;
    const int MOUSEEVENTF_MOVE = 0x0001;
    const int MOUSEEVENTF_ABSOLUTE = 0x8000;

  • aatik

    I want to click in another aplication, its not inside the aplication.
  • BenniG.

    Try to use SendMessage (with probably SetForegroundWindow()) with WM_LBUTTONDOWN and then WM_LBUTTONUP.
  • DAVID SAIANI

    I will try explain myselft better, i am making an application which make an auto click when it find a color determinated by a person who use it, soo when it find the color in any part of the screen it will make an auto click.

    When i start the application and i start another program (this program is a full screen), it doesnt happen nothing.

    Another thing, you are writting always RIGHTDOWN or RIGHTUP, and i changed that to LEFTDOWN and LEFTUP, maybe that is the problem.

    ...void PerformClick() 

    const int x = 30000; //see mouse_event in MSDN for details on this value 
    const int y = 30000; 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, x, y, 0, UIntPtr.Zero); 
    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, x, y, 0, UIntPtr.Zero); 


    [DllImport("User32.dll")] 
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, 
    UIntPtr dwExtraInfo); 

    const int MOUSEEVENTF_RIGHTDOWN = 0x0008; 
    const int MOUSEEVENTF_RIGHTUP = 0x0010; 
    const int MOUSEEVENTF_MOVE = 0x0001; 
    const int MOUSEEVENTF_ABSOLUTE = 0x8000; 

    public frmAim2()
    {

    Text = "Aimbot";

    BackColor = SystemColors.Window;

    ForeColor = SystemColors.WindowText;

    FormBorderStyle = FormBorderStyle.FixedSingle;

    MaximizeBox = false;

    ClientSize = new Size(12 * AutoScaleBaseSize.Width, 

    3 * AutoScaleBaseSize.Height);

    TopMost = true;


    Timer timer = new Timer();

    timer.Interval = 1;

    timer.Tick += new EventHandler(TimerOnTick);

    timer.Start();

    }

    void TimerOnTick(object obj, EventArgs ea)

    {

    Point pt = MousePosition;
    pt.X = SystemInformation.VirtualScreen.Width / 2;
    pt.Y = SystemInformation.VirtualScreen.Height / 2;

    IntPtr hdcScreen = CreateDC("Display", null, null, IntPtr.Zero);

    int cr = GetPixel(hdcScreen, pt.X, pt.Y);

    DeleteDC(hdcScreen);

    clr = Color.FromArgb((cr & 0x000000FF),

    (cr & 0x0000FF00) >> 8,

    (cr & 0x00FF0000) >> 16);


    if (clr != clrLast)

    {

    clrLast = clr;

    Invalidate();

    }

    }

    protected override void OnPaint(PaintEventArgs pea)

    {

    Graphics grfx = pea.Graphics;

    StringFormat strfmt = new StringFormat();

    strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;

    string str = String.Format("{0:X2}-{1:X2}-{2:X2}\n{3}-{4}-{5}",

    clr.R, clr.G, clr.B, clr.R, clr.G, clr.B);

              

    grfx.DrawString(str, Font, new SolidBrush(ForeColor), ClientRectangle, strfmt);               

    if((clr.R = 30) && (clr.G = 81) && (clr.B = 75))
    {
    PerformClick(); 
    }
    }Maybe that should help, thanks for all the help you give to me.

  • tsgomez

    I discover it doesnt click when i have a full application application, how can i make it click there too
  • SSRS

    I got this code soo far:

    public const int WM_LBUTTONDOWN = 0x0201;
    public const int WM_LBUTTONUP = 0x0202;
    public const int WM_RBUTTONDOWN = 0x0204;
    public const int WM_RBUTTONUP = 0x0205;

    [DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto)]
    public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

    public void PerformClick()
    {
    Point p = Cursor.Position;
    if (SystemInformation.MouseButtonsSwapped)
    {
    p.X = SystemInformation.VirtualScreen.Width / 2;
    p.Y = SystemInformation.VirtualScreen.Height / 2;
    SendMessage(this.Handle, WM_RBUTTONDOWN, p.X, p.Y);
    SendMessage(this.Handle, WM_RBUTTONUP, p.X, p.Y);
    }

    }

    But it doesn't do the auto click, what is the problem

  • Yosi

    Do you just want to click a button   If so then you can just do MyButton.PerformClick();

    What is it that you actually want to click on


  • How to make the mouse click...