If there is a way to direct an ascii output to Windows notepad. Scenario: an application starts notepad instance of Windows Notepad or finds one. Than the application pushes some text messages to the notepad so that the message appends to the opened notepad text.
Asked
Active
Viewed 1,070 times
0
-
Specifically notepad or just an external program? – Roman Dec 05 '14 at 15:17
-
You would to have to mess about with window handles and other Win32Api stuff. Whilst you can, it would be much easier to use a standard windows forms or WPF control. – Ben Robinson Dec 05 '14 at 15:18
-
Is it an option to open a new window with a `TextBox` control and output the text there? It's quite similar to your request and much more simpler. The problem with the Notepad option is that anybody can easily interfere with the output. With my popup option, you could set it read-only as long as you need. – Andrew Dec 05 '14 at 15:24
2 Answers
3
Raymond Chen has a blog and made a post describing something similar, maybe you can adapt it to what you want? Link
Edit: Relevant code from the blog
using System;
using System.Diagnostics;
using System.Windows.Automation;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
// Slurp stdin into a string.
var everything = Console.In.ReadToEnd();
// Fire up a brand new Notepad.
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = @"C:\Windows\System32\notepad.exe";
process.Start();
process.WaitForInputIdle();
// Find the Notepad edit control.
var edit = AutomationElement.FromHandle(process.MainWindowHandle)
.FindFirst(TreeScope.Subtree,
new PropertyCondition(
AutomationElement.ControlTypeProperty,
ControlType.Document));
// Shove the text into that window.
var nativeHandle = new IntPtr((int)edit.GetCurrentPropertyValue(
AutomationElement.NativeWindowHandleProperty));
SendMessage(nativeHandle, WM_SETTEXT, IntPtr.Zero, everything);
}
[DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Unicode)]
static extern IntPtr SendMessage(
IntPtr windowHandle, int message, IntPtr wParam, string text);
const int WM_SETTEXT = 0x000C;
}

ptsoccer
- 158
- 6
-
-
Found the replay valuable, but it is about putting text into a window, but not appends. hope this link will help me to solve the "append" requirement, but I really would appreciate complete answer. Thanks! http://stackoverflow.com/questions/10052804/append-text-to-memo-using-win-api – Yaugen Vlasau Dec 05 '14 at 15:43
-
I'm having trouble understanding what you're looking for. Are you saying you want the solution to append to whatever text is currently in notepad? If so you can get the current text of the notepad using SendMessage with the WM_GETTEXT message, then set it with the appended values with WM_SETTEXT – ptsoccer Dec 05 '14 at 15:47
0
You can send the text to the Process
with SendKeys.SendWait
:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SendKeysToProcess
{
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args)
{
// Start Notepad process
Process notepad = new Process();
notepad.StartInfo.FileName = @"C:\Windows\Notepad.exe";
notepad.Start();
// Wait for notepad to start
notepad.WaitForInputIdle();
IntPtr p = notepad.MainWindowHandle;
ShowWindow(p, 1);
// Write some text to the Notepad window
SendKeys.SendWait("Hello World!");
}
}
}

David
- 10,458
- 1
- 28
- 40

Omer Eldan
- 1,757
- 1
- 11
- 10
-
Couldn't this end up dumping text into the wrong position if the cursor gets misplaced? Could that be fixed by sending a `ctrl+end` keystroke before each message? – David Dec 05 '14 at 15:28
-
Not only the wrong position, but it could send the keystrokes to the wrong window entirely if a different window gets focus. It's just generating keystrokes, and not actually sending data to the Notepad window specifically. – nobody Dec 05 '14 at 15:32
-
it sends keys to an in-focus windows. http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx – Yaugen Vlasau Dec 05 '14 at 15:38