2017-01-13 33 views
-1

我知道這個問題已經得到解答。這是我的問題。我有一個帶有按鈕和文本框的Windows窗體。用戶在文本框中輸入信息,當用戶單擊按鈕時,將啓動記事本實例,然後將文本框的文本加載到記事本中。複製文本到C#中的記事本實例

這裏是我的代碼(這是我從一個問題得到了這個網站)

[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); 

     private void btnCopyToNotepad_Click(object sender, EventArgs e) 
     { 
      StartNotepad(); 

      Process[] notepads = Process.GetProcessesByName("notepad"); 
      if (notepads.Length == 0) return; 
      if (notepads[0] != null) 
      { 
       Clipboard.SetText(textBox1.Text); 
       SendMessage(FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, textBox1.Text); 
      } 
     } 
     private static void StartNotepad() 
     { 
      Process.Start("notepad.exe"); 
     } 

當我運行此代碼,調試通過步進,它運行良好,邏輯做什麼它打算做(複製文本到記事本的實例)。當我在發行版中運行它時,沒有任何內容被複制到記事本實例中。任何想法爲什麼發生這種情況?不,我沒有運行記事本的多個實例..

+0

你知道什麼是不工作或失敗? 它發現記事本過程? 放一些日誌語句,看看發生了什麼。 你應該可以調試你的代碼 – dgorti

+1

這相反會引出你爲什麼不寫出一個文本文件並在記事本中打開它的問題? – spender

+0

我認爲這是一個合法的問題,問題在於SendMessage發射得太快。我試圖把一個Task.Delay,但它沒有幫助。 –

回答

2

你需要等到進程啓動,然後發送文本:

[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); 

    private void btnCopyToNotepad_Click(object sender, EventArgs e) 
    { 
     StartNotepad(); 

     Process[] notepads = null; 
     while (notepads == null || notepads.Length == 0) 
     { 
      notepads = Process.GetProcessesByName("notepad"); 
      Thread.Sleep(500); 
     } 

     if (notepads.Length == 0) return; 
     if (notepads[0] != null) 
     { 
      Clipboard.SetText(textBox1.Text); 
      SendMessage(FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, textBox1.Text); 
     } 
    } 
    private static void StartNotepad() 
    { 
     Process.Start("notepad.exe"); 
    } 
+1

這對我的測試有效。 –

+1

說我愛你會很奇怪,所以謝謝。這是我可以在整個網站上找到的最佳答案。也謝謝你們的幫助。我沒有預料到從代碼執行到記事本實例的毫秒時間。希望你們享受你的週末。 – Mattaceten

+0

只是爲了紀錄(對於未來的觀衆),還有其他方法可以做得更簡單,比如 'StreamWriter file2 = new StreamWriter(@「c:\ file.txt」,true);' obv這個不使用「記事本實例」只是創建一個txt文件或附加到它,如果它已經存在。 –

0

東西可重複使用的可能?順便說一句,這每次打開一個記事本的新實例。

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

static class Notepad 
{ 
    #region Imports 
    [DllImport("user32.dll")] 
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 

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

    //this is a constant indicating the window that we want to send a text message 
    const int WM_SETTEXT = 0X000C; 
    #endregion 


    public static void SendText(string text) 
    { 
     Process notepad = Process.Start(@"notepad.exe"); 
     System.Threading.Thread.Sleep(50); 
     IntPtr notepadTextbox = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null); 
     SendMessage(notepadTextbox, WM_SETTEXT, 0, text); 
    } 
} 
相關問題