【问题标题】:Notepad in windows form application [closed]Windows窗体应用程序中的记事本[关闭]
【发布时间】:2015-07-21 12:57:58
【问题描述】:

我的字符串变量中有一些字符串。当我单击 Winform 应用程序中的打开按钮时,它会打开带有该字符串的记事本。记事本应临时创建。如果我关闭记事本,它应该被永久删除。

例如,当我单击“打开”按钮时,会生成字符串“结果”值,记事本应显示结果字符串值。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这应该演示如何打开记事本并文本放入其中。

    这是一个启动记事本进程然后向其中添加文本的简单示例。 将打开一个新的 Notepad.exe 进程,然后将文本“Sending a message, a message from me to you”添加到记事本的文本区域。

    SendMessage 的常量 0x000c 记录在这里 http://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx 。 常量表示SETTEXT,这实际上意味着如果您使用此常量发送多条消息,记事本中的文本将被替换。

    来源:http://www.peterhenell.se/msg/C---AddSend-text-to-notepad-process-using-user32dll-SendMessage-from-your-application

    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    
    namespace SendMessageTest
    {
        class Program
        {
            [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);
    
    
    
            static void Main(string[] args)
            {
                DoSendMessage("Sending a message, a message from me to you");
            }
    
    
    
            private static void DoSendMessage(string message)
            {
                Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
                notepad.WaitForInputIdle();
    
                if (notepad != null)
                {
                    IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
                    SendMessage(child, 0x000C, 0, message);
                }
            }
        }
    }
    

    【讨论】:

    • 我已经编辑了您的答案,包括从您获得此程序的博客中直接引用。如果这违背了您的意图,请随时恢复。我觉得它需要更多的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多