简体   繁体   中英

How to postmessage to an application with C# on Winforms?

I am trying to make an application for routine projects;

It is more like autogui,macro but when macro is running i should be able to do another things so i need two mouse and two keyboards at the sametime running one of for my use otherone should run in the application

I can successfully post message to notepad with enter it to textbox but other applications not work like calculator,notepad++,discord,.. so on

I need to send keyboard,mouse to current form application

In both methods

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

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

First method

    [DllImport("User32.Dll", EntryPoint = "PostMessageA", SetLastError = true)]
    public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    public void control()
    {
        Process[] processes = Process.GetProcessesByName(textBox1.Text);

        foreach (Process p in processes)
        {
            textBox2.Text += p.ProcessName + System.Environment.NewLine;
            IntPtr windowHandle = p.MainWindowHandle;
            const int WM_KEYDOWN = 0x0100;
            const int VM_KEYUP = 0x0101;
            IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "edit", null);
            PostMessage(editx, WM_KEYDOWN, (IntPtr)66, IntPtr.Zero);
            PostMessage(editx, VM_KEYUP, (IntPtr)66, IntPtr.Zero);
        }

    }

Other tried method

    [DllImport("user32.dll")]
    private static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, Int32 lParam);

    public void control()
    {
            IntPtr handle = this.Handle;
            IntPtr handle2 = Handle;
            const int WM_KEYDOWN = 0x0100;
            const int VM_KEYUP = 0x0101;
            const int WM_MOUSEMOVE = 0x0200;
            const int WM_LBUTTONDOWN = 0x0201;
            const int WM_LBUTTONUP = 0x0202;
            IntPtr editx = FindWindowEx(handle , IntPtr.Zero, "edit", null);
            IntPtr editx2 = FindWindowEx(handle2, IntPtr.Zero, "edit", null);

            PostMessage(editx, WM_KEYDOWN, (Int32)Keys.S, 1);
            PostMessage(editx, VM_KEYUP, (Int32)Keys.S, 1);
            PostMessage(editx, WM_LBUTTONDOWN, 1, (280 << 16) | 280);
            PostMessage(editx, WM_LBUTTONUP, 0, (280 << 16) | 280);
            PostMessage(editx2, WM_KEYDOWN, (Int32)Keys.S, 1);
            PostMessage(editx2, VM_KEYUP, (Int32)Keys.S, 1);
            PostMessage(editx2, WM_LBUTTONDOWN, 1, (280 << 16) | 280);
            PostMessage(editx2, WM_LBUTTONUP, 0, (280 << 16) | 280);
    }

It can be about:

IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "edit", null);

What can i write instead edit and how can i find it ?

I tried to trace what i giving to GetProcessesByName("") with textBox2.Text , i am sure about process names are true

Even to make sure about used method i send to all related names with for

So what is wrong about this method ?

Why it is not working on other applications ?

You're just sending to the wrong windows. In the case of notepad.exe the window to send to is the main window so that works nicely.

In the case of Notepad++ there are a bunch of sub-windows (for the button-bars etc), you can't send to the main Notepad++ window, it doesn't know what to do with the WM_KEYDOWN messages.

This works for Notepad++:

IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "Scintilla", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM