本文介绍了如何在 C#/Win32 中将文本发送到记事本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SendMessage 到记事本,以便我可以插入书面文本,而无需将记事本设为活动窗口.

I'm trying to use SendMessage to Notepad, so that I can insert written text without making Notepad the active window.

我过去曾使用 SendText 做过类​​似的事情,但这需要让记事本获得焦点.

I have done something like this in the past using SendText, but that required giving Notepad focus.

现在,首先我要检索 Windows 句柄:

Now, first I'm retrieving the Windows handle:

Process[] processes = Process.GetProcessesByName("notepad");
Console.WriteLine(processes[0].MainWindowHandle.ToString());

我已经确认它是记事本的正确句柄,与 Windows 任务管理器 中显示的相同.

I've confirmed it's the right handle for Notepad, the same shown within Windows Task Manager.

[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

从这里开始,我无法让 SendMessage 在我的所有实验中工作.我是不是走错方向了?

From here, I haven't been able to get SendMessage to work in all my experimentation. Am I going in the wrong direction?

推荐答案

    [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
    private void button1_Click(object sender, EventArgs e)
    {
        Process [] notepads=Process.GetProcessesByName("notepad");
        if(notepads.Length==0)return;
        if (notepads[0] != null)
        {
            IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, textBox1.Text);
        }
    }

WM_SETTEXT=0x000c

WM_SETTEXT=0x000c

这篇关于如何在 C#/Win32 中将文本发送到记事本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:59