gpt4 book ai didi

c# - 如何从 .NET 在记事本中打开文本?

转载 作者:IT王子 更新时间:2023-10-29 04:31:48 26 4
gpt4 key购买 nike

当我点击 Windows Forms 上的按钮时窗体,我想打开一个记事本窗口,其中包含来自窗体上 TextBox 控件的文本。

我该怎么做?

最佳答案

你不需要用这个字符串创建文件。您可以使用 P/Invoke解决您的问题。

NotepadHelper类的使用:

NotepadHelper.ShowMessage("My message...", "My Title");

NotepadHelper类代码:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Notepad
{
public static class NotepadHelper
{
[DllImport("user32.dll", EntryPoint = "SetWindowText")]
private static extern int SetWindowText(IntPtr hWnd, string text);

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

[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

public static void ShowMessage(string message = null, string title = null)
{
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
if (notepad != null)
{
notepad.WaitForInputIdle();

if (!string.IsNullOrEmpty(title))
SetWindowText(notepad.MainWindowHandle, title);

if (!string.IsNullOrEmpty(message))
{
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, message);
}
}
}
}
}

引用资料(pinvoke.net 和 msdn.microsoft.com):

设置窗口文本:pinvoke | msdn

FindWindowEx: pinvoke | msdn

发送消息:pinvoke | msdn

关于c# - 如何从 .NET 在记事本中打开文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7613576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com