ShakalX Ответов: 0

C# щелчок мышью по фоновому окну. "Помощь"


Привет, ребята. Я пытаюсь сделать автоматический щелчок по окну google chrome. но я хочу, чтобы это окно оставалось в фоновом режиме, и мышь продолжает щелкать по нему, и я могу делать другие вещи на ПК. Я пробовал это, но безуспешно, когда активация окна получает фокус и остается впереди, а щелчок не может делать другие вещи на ПК. Кто-нибудь может мне помочь?

Что я уже пробовал:

<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    
public partial class Form1 : Form
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

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


        public enum WMessages : int
        {
            WM_LBUTTONDOWN = 0x201, //Left mousebutton down
            WM_LBUTTONUP = 0x202, //Left mousebutton up
            WM_RBUTTONDOWN = 0x204, //Right mousebutton down
            WM_RBUTTONUP = 0x205, //Right mousebutton up
        }

        string ablak = "Google Chrome";

        public Form1()
        {
            InitializeComponent();
        }


        public int MakeLParam(int LoWord, int HiWord)
        {
            return ((HiWord << 16) | (LoWord & 0xffff));
        }

        public void ControlClickWindow(string wndName, int x, int y)
        {
            IntPtr hWnd = FindWindow(null, ablak);
            SetForegroundWindow(hWnd);

           // Cursor.Position = new Point(90, 110);

            int LParam = MakeLParam(x, y);

            SendMessage(hWnd, (int)WMessages.WM_LBUTTONDOWN, 0, LParam);
            SendMessage(hWnd, (int)WMessages.WM_LBUTTONUP, 0, LParam);

        }

        private void button1_Click(object sender, EventArgs e)
        {
           // ControlClickWindow(ablak, 90, 110);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ControlClickWindow(ablak, 90, 110);
        }
    }
}



Я попробовал это сделать. Теперь я могу переместить мышь из окна, и она продолжает щелкать, но проблема в том, что при открытии другой программы или браузера. окно, которое должно быть нажато, появляется на экране. И мышь не правильная она все равно выглядит странно. Что я могу сделать?

<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;  
           return 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)
        {
            timer1.Enabled = (true);
          
        }

        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(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);
        }
    }
}

Graeme_Grant

Почему бы не сделать ваше приложение самым верхним окном? Таким образом, когда он теряет фокус и Chrome получает фокус, Chrome остается за вашим окном.

BillWoodruff

Сделайте некоторые исследования на тему "Глобальный Мышиный крючок"

0 Ответов