0

I've currently got another process running inside a form, and I want to be able to simulate mouse clicks in that process without affecting my real mouse and without the need to have the form window active. I've only been working with c# for a few days so I'm not sure if this is even possible.

Currently I'm listening for clicks on the form itself, which works since I can see the console log "Form clicked", however I don't see any clicks occurring inside the process.

Here's the code I have so far:

    public partial class Form1 : Form
    {
        private const int WM_LBUTTONDOWN = 0x0201;
        private const int WM_LBUTTONUP = 0x0202;
        public Form1()
        {
            InitializeComponent();
            startProcess();
        }
        private void startProcess()
        {
            Process p = Process.Start("notepad.exe");
            p.WaitForInputIdle();
            SetParent(p.MainWindowHandle, Handle);
        }

        private void formClick(object sender, EventArgs e)
        {
            Console.WriteLine("Form clicked");
            Thread.Sleep(500);
            PostMessage(FindWindow(null, "Form1"), WM_LBUTTONDOWN, new IntPtr(0x1), CreateLParam(MousePosition.X + 50, MousePosition.Y + 50));
            PostMessage(FindWindow(null, "Form1"), WM_LBUTTONUP, new IntPtr(0x1), CreateLParam(MousePosition.X + 50, MousePosition.Y + 50));
        }

        private static IntPtr CreateLParam(int LoWord, int HiWord)
        {
            return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
        }

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]

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

        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        public static extern bool PostMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
    }
d0zza
  • 1
  • 3
    It's an XY problem. Click on a hidden application is not what you want to achieve. Tell us what you really want to do. – Louis Go Apr 16 '20 at 08:25
  • It is what I want to achieve, I want to be able to simulate inputs to this process while I'm doing something else, kind of like a bot. – d0zza Apr 16 '20 at 08:26
  • Does this answer your question? [Simulate click into a hidden window](https://stackoverflow.com/questions/10279812/simulate-click-into-a-hidden-window) – Louis Go Apr 16 '20 at 08:30

0 Answers0