2014-12-05 41 views
0

如果有辦法將ascii輸出指向Windows記事本。 場景: 應用程序啓動Windows記事本的記事本實例或找到一個。比應用程序推送一些文本消息到記事本,以便消息附加到打開的記事本文本。使用Windows記事本作爲輸出控制檯

+0

具體記事本或只是一個外部程序? – R0MANARMY 2014-12-05 15:17:54

+0

你將不得不與窗口句柄和其他Win32Api的東西。雖然可以,但使用標準的Windows窗體或WPF控件要容易得多。 – 2014-12-05 15:18:12

+0

是否可以使用「TextBox」控件打開一個新窗口並在其中輸出文本?這與您的要求非常相似,而且要簡單得多。記事本選項的問題是任何人都可以輕易地干擾輸出。使用彈出選項,只要您需要,您可以將其設置爲只讀。 – Andrew 2014-12-05 15:24:26

回答

2

Raymond Chen有一個博客,並發表了一篇文章描述了類似的東西,也許你可以根據自己的需要調整它。 Link

編輯:從博客

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; 
} 
+0

看起來像個聰明的主意! – 2014-12-05 15:18:37

+0

發現重播很有價值,但它是關於將文本放入窗口中,但不會追加。希望這個鏈接能幫助我解決「追加」要求,但我真的很感謝完整的答案。謝謝! http://stackoverflow.com/questions/10052804/append-text-to-memo-using-win-api – 2014-12-05 15:43:58

+0

我無法理解你在找什麼。你是說你想要解決方案附加到目前在記事本中的任何文本?如果是這樣,你可以使用帶有WM_GETTEXT消息的SendMessage獲取記事本的當前文本,然後使用附加值WM_SETTEXT – ptsoccer 2014-12-05 15:47:04

0

相關的代碼可以將文本發送到ProcessSendKeys.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!"); 
     } 
    } 
} 
+0

設置它的附加值。如果光標錯位,最終是否會將文本轉儲到錯誤的位置?這可以通過在每封郵件前發送一個「Ctrl + End」按鍵來解決嗎? – David 2014-12-05 15:28:48

+0

不僅錯誤的位置,但它可以發送擊鍵到錯誤的窗口完全如果一個不同的窗口獲得焦點。它只是產生擊鍵,而不是專門將數據發送到記事本窗口。 – 2014-12-05 15:32:02

+0

它將密鑰發送到對焦窗口。 http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx – 2014-12-05 15:38:09