C#一段很简单的代码,功能是打开记事本并输入文本,以前在win10上完美运行,升级到win11后就只能打开记事本但不显示文本,求问为什么?是否跟user32.dll有关?
代码如下,.net版是4.0:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace OpenNotepadAndWriteText
{
class Program
{
static void Main(string[] args)
{
string msg = "Example text.";
const int WM_SETTEXT = 0xC;
Process pro = Process.Start("notepad");
pro.WaitForInputIdle();
SendMessage(FindWindowEx(pro.MainWindowHandle, IntPtr.Zero, "Edit", null), WM_SETTEXT, 0, msg);
}
[DllImport("User32")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32")]
private static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam);
}
}