2012-07-03 4 views
0

인터넷에서이 스 니펫을 보았지만 나에게서 효과가 없습니다. 새로운 메모장 응용 프로그램을 열고 "asdf"를 추가한다고 가정합니다.메모장을 열고 텍스트가 작동하지 않음을 나타냅니다.

코드에 문제가 있습니까? 코드가 당신을 위해 트릭을 할 것입니다 다음

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

void Test() 
{ 
     const int WM_SETTEXT = 0x000C; 

    ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe"); 
    startInfo.UseShellExecute = false; 
    Process notepad = System.Diagnostics.Process.Start(startInfo); 
    SendMessage(notepad.MainWindowHandle, WM_SETTEXT, 0, "asdf"); 
} 
+2

"나를 위해 작동하지 않습니다."- 글쎄, 그게 어때? 'notepad.exe' 프로세스가 시작됩니까? 메모장 응용 프로그램이 표시됩니까? 이 코드에서 오류가 있습니까? 당신은 "인터넷에서이 발췌문을 보았다"고 말하지만 그것이 무엇을하는지 이해합니까? 예를 들어, 그'const'의 목적은 무엇입니까? – David

+0

어떤 부분이 작동하지 않습니까? 메모장이 열려 있습니까? – Blorgbeard

+0

새로운 notepadd를 열고 텍스트를 추가하려고했습니다. 위의 코드에서 "asdf" – yonan2236

답변

0

,

[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 button1_Click(object sender, EventArgs e) 
    { 
     Process [] notepads=Process.GetProcessesByName("notepad"); 
     if(notepads.Length==0)return;    
     if (notepads[0] != null) 
     { 
      IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null); 
      SendMessage(child, 0x000C, 0, textBox1.Text); 
     } 
    } 
0

이 시도 :

[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); 
     void Test() 
     { 
      ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe"); 
      startInfo.UseShellExecute = false; 
      Process notepad = System.Diagnostics.Process.Start(startInfo); 
      //Wait Until Notpad Opened 
      Thread.Sleep(100); 
      Process[] notepads = Process.GetProcessesByName("notepad"); 
      IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null); 
      SendMessage(child, 0x000c, 0, "test"); 
     } 
2

프로세스가 notepad.WaitForInputIdle()를 호출 입력을 받아 들일 준비가되어 있는지 확인하십시오. 그리고 메모장 프로세스가 아닌 방금 생성 된 프로세스 인 MainWindowHandle을 사용하는 것이 중요합니다.

[DllImport("user32.dll")] 
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 ExportToNotepad(string text) 
{ 
    ProcessStartInfo startInfo = new ProcessStartInfo("notepad"); 
    startInfo.UseShellExecute = false; 

    Process notepad = Process.Start(startInfo); 
    notepad.WaitForInputIdle(); 

    IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), null, null); 
    SendMessage(child, 0x000c, 0, text); 
} 
관련 문제