C#SendKeys.SendWait到另一个进程的对话框(notepad.exe)

C#SendKeys.SendWait到另一个进程的对话框(notepad.exe),c#,.net,sendkeys,sharpdevelop,C#,.net,Sendkeys,Sharpdevelop,我正在尝试用C#做以下工作: 打开一个新进程(notepad.exe) 输入一些文本(使用SendKeys) 关闭记事本(处理任何确认对话框) 这是我得到的 Process p = new Process(); p.StartInfo.Filename = "notepad.exe"; p.Start(); // use the user32.dll SetForegroundWindow method SetForegroundWindow( p.MainWindowHandle );

我正在尝试用C#做以下工作:

  • 打开一个新进程(notepad.exe)
  • 输入一些文本(使用SendKeys)
  • 关闭记事本(处理任何确认对话框)
这是我得到的

Process p = new Process();
p.StartInfo.Filename = "notepad.exe";
p.Start();

// use the user32.dll SetForegroundWindow method
SetForegroundWindow( p.MainWindowHandle ); // make sure notepad has focus

SendKeys.SendWait( "some text" );

SendKeys.SendWait( "%f" ); // send ALT+f
SendKeys.SendWait( "x" ); // send x = exit

// a confirmation dialog appears
所有这些都像预期的那样工作,但现在在我发送ALT+f+x之后,我得到了一个 “是否要保存更改为无标题”对话框,我想从内部关闭它 我的应用程序是通过“按”n“表示“不保存”。然而

仅当我的应用程序没有失去焦点(在ALT+f+x之后)时才有效。如果是的话,我试着用

SetForegroundWindow( p.MainWindowHandle );
这会将焦点设置为记事本主窗口,而不是确认对话框。我使用了user32.dll中的
getforegroughindow
方法,发现对话框句柄与记事本句柄不同(这有点道理),但
setforegroughindow
即使与对话框窗口句柄也不起作用

您知道如何将焦点返回到对话框,以便成功使用
SendKeys

这是完整的代码

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);        

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();             

[DllImport("User32.DLL")] 
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

public const int SW_RESTORE = 9;        

    ...

Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.Start();
Thread.Sleep( 1000 );

SendKeys.SendWait( "some text" );
SendKeys.SendWait( "%f" ); // send ALT+F
SendKeys.SendWait( "x" ); // send x = exit

IntPtr dialogHandle = GetForegroundWindow();
System.Diagnostics.Trace.WriteLine( "notepad handle: " + p.MainWindowHandle );
System.Diagnostics.Trace.WriteLine( "dialog handle: " + dialogHandle );

Thread.Sleep( 5000 ); // switch to a different application to lose focus

SetForegroundWindow( p.MainWindowHandle );
ShowWindow( dialogHandle, SW_RESTORE );

Thread.Sleep( 1000 );
SendKeys.SendWait( "n" );

谢谢

您不能提供您所没有的-
setforegroundindow()
只有在您的应用程序当前有焦点的情况下才有效

现代Windows版本防止应用程序窃取焦点,因为这在Windows9X时代是一个主要的麻烦


另外,“Alt+F,x”仅在英文Windows版本中表示退出,在大多数其他语言中不起作用。避免使用
SendKeys()
,它不可能以可靠的方式使用。

谢谢,这很有意义。我只剩下
SendKeys
,因为我的应用程序需要在另一个第三方应用程序中自动执行用户输入。p、 我在这里使用ALT+Fx只是为了演示
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);        

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();             

[DllImport("User32.DLL")] 
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

public const int SW_RESTORE = 9;        

    ...

Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.Start();
Thread.Sleep( 1000 );

SendKeys.SendWait( "some text" );
SendKeys.SendWait( "%f" ); // send ALT+F
SendKeys.SendWait( "x" ); // send x = exit

IntPtr dialogHandle = GetForegroundWindow();
System.Diagnostics.Trace.WriteLine( "notepad handle: " + p.MainWindowHandle );
System.Diagnostics.Trace.WriteLine( "dialog handle: " + dialogHandle );

Thread.Sleep( 5000 ); // switch to a different application to lose focus

SetForegroundWindow( p.MainWindowHandle );
ShowWindow( dialogHandle, SW_RESTORE );

Thread.Sleep( 1000 );
SendKeys.SendWait( "n" );