簡體   English   中英

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

[英]How to postmessage to an application with C# on Winforms?

我正在嘗試為常規項目申請;

它更像是 autogui,macro 但是當宏運行時我應該能夠做其他事情所以我需要兩個鼠標和兩個鍵盤同時運行一個供我使用其他人應該在應用程序中運行

我可以成功地將消息發送到記事本並將其輸入到文本框,但其他應用程序無法正常工作,如計算器、記事本++、不和諧等

我需要將鍵盤、鼠標發送到當前表單應用程序

在這兩種方法中

    [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);

第一種方法

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

    }

其他嘗試過的方法

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

它可以是:

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

我可以寫什么來編輯,我怎么能找到它?

我試圖用 textBox2.Text 追蹤我給 GetProcessesByName("") 的內容,我確信進程名稱是正確的

甚至為了確保使用的方法我發送給所有相關的名稱

那么這種方法有什么問題呢?

為什么它不適用於其他應用程序?

你只是發送到錯誤的窗口。 在 notepad.exe 的情況下,要發送到的窗口是主窗口,因此效果很好。

在 Notepad++ 的情況下,有一堆子窗口(用於按鈕欄等),您無法發送到 Notepad++ 主窗口,它不知道如何處理 WM_KEYDOWN 消息。

這適用於記事本++:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM