Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys. I need help. I'm doing an auto click with left mouse, but I need it to click only on a specific window and it can be minimized and continue to click while I can do other things on the pc. How to navigate the web etc. This code works the wrong way. It clicks and I can move the mouse in the meantime. But, I can not use the left mouse button and move windows or even minimize the window in which the mouse was programmed to click because it stops clicking. can you help me? Thank you.

What I have tried:

C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AUTOCLIQUEMOUSEHK
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public enum WMessages : int
        {

            WM_LBUTTONDOWN = 0x201,
            WM_LBUTTONUP = 0x202,
            WM_LBUTTONDBLCLK= 0x203,
            WM_RBUTTONDOWN= 0x204,
            WM_RBUTTONUP = 0x205,
            WM_RBUTTONDBLCLK = 0x206,


        }

        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string ClassName, IntPtr WindowTitle);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern int SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int GetWindowtext(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public IntPtr findme()

        {
            return this.Handle;  
            //FindWindow("Google Chrome", "Chrome");
        }

        private int MAKELPARAM(int p, int p_2)
        {
            return ((p_2 << 16) | (p & 0xFFFF));
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
          /**  IntPtr myHandle = findme();
            Point pt = new Point(79, 312); // coordes
            IntPtr handle = WindowFromPoint(pt.X, pt.Y);
            SetForegroundWindow(handle);
            SendMessage(handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
            SendMessage(handle, (int)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(pt.X, pt.Y));
    **/

            // PostMessage(handle, (uint)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
            // PostMessage(handle, (uint)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(pt.X, pt.Y));

           // SetForegroundWindow(myHandle);
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            this.Text = "X:" + Cursor.Position.X + "Y:" + Cursor.Position.Y;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            IntPtr myHandle = findme();
            Point pt = new Point(312, 324); // coordes
            IntPtr handle = WindowFromPoint(pt.X, pt.Y);
            //SetForegroundWindow(myHandle);
           // SendMessage(handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
           // SendMessage(handle, (int)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(pt.X, pt.Y));


             PostMessage(handle, (uint)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X, pt.Y));
             PostMessage(handle, (uint)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(pt.X, pt.Y));

            SetForegroundWindow(myHandle);
        }
    }
}
Posted
Updated 19-Aug-17 12:43pm
v4
Comments
Dave Kreskowiak 15-Aug-17 14:38pm    
Ummm...by definition, a minimized window no longer has the input focus. You cannot "click on" a minimized window, so there's no possible way this code is going to work.

Or did I misunderstand something? Your description is a bit vague.
ShakalX 15-Aug-17 15:10pm    
Yes, that would be it. Click on minimized window. So let's say I wanted to use it without minimizing it but in the background and continuing to click only on that window and the specific coordinates while browsing other applications?
Dave Kreskowiak 15-Aug-17 15:22pm    
Still a problem as that window still does not have the input focus. Your foreground application does.

So, you really cannot "click on" one window while continuing to use the real mouse on another one.
ShakalX 15-Aug-17 15:55pm    
Weird. Because in delphi it has and works perfect mouse click on minimized window and I can continue using other applications in pc = /
Dave Kreskowiak 15-Aug-17 16:04pm    
Interesting, but I have no idea how it's doing it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900