简体   繁体   English

如何在 Winforms 上使用 C# 向应用程序发送消息?

[英]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它更像是 autogui,macro 但是当宏运行时我应该能够做其他事情所以我需要两个鼠标和两个键盘同时运行一个供我使用其他人应该在应用程序中运行

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我试图用 textBox2.Text 追踪我给 GetProcessesByName("") 的内容,我确信进程名称是正确的

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.在 notepad.exe 的情况下,要发送到的窗口是主窗口,因此效果很好。

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.在 Notepad++ 的情况下,有一堆子窗口(用于按钮栏等),您无法发送到 Notepad++ 主窗口,它不知道如何处理 WM_KEYDOWN 消息。

This works for Notepad++:这适用于记事本++:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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