C#SendKeys.SendWait到另一个进程的对话框(记事本)对话框、进程、记事本、SendKeys

由网友(贯穿一生一个帅)分享简介:我试图在C#中的以下内容:I'm trying to do the following in C#:打开一个新的进程(记事本)输入一些文字(使用的SendKeys)关闭记事本(应付任何确认对话框)下面是我得到了Process p = new Process();p.StartInfo.Filename = "...

我试图在C#中的以下内容:

I'm trying to do the following in C#:

打开一个新的进程(记事本) 输入一些文字(使用的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,我收到了 你想保存更改为未命名对话框,我想从内部关闭 我的应用程序通过pressingN为不保存。然而

All this works just as expected, but now after I've sent ALT+f+x I get a "Do you want to save changed to Untitled" dialog and I would like to close it from within my applications by "pressing" 'n' for "Don't Save". However

SendKeys.SendWait( "n" ); 

只有当我的应用程序并没有失去焦点(ALT + F +后X)。如果那样,我回去试试使用

works only if my application has not lost focus (after ALT+f+x). If it did and I try to go back using

SetForegroundWindow( p.MainWindowHandle );

这将焦点设置到主记事本窗口,而不是确认对话框。我用从user32.dll中的 GetForegroundWindow 的方法,发现了话柄比记事本处理不同(这有点让SENCE),但 SetForegroundWindow 不与工作,即使与对话窗口句柄

this sets the focus to the main notepad window instead of the confirmation dialog. I used the GetForegroundWindow method from user32.dll and found out that the dialog handle is different than the notepad handle (which kinda makes sence) but SetForegroundWindow doesn't work with even with the dialog window handle

任何想法如何,我可以给焦点返回到对话框,这样我可以成功地使用的SendKeys

Any idea how I can give the focus back to the dialog so that I can successfully use SendKeys?

下面是完整的code

here's the full code

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

感谢您

推荐答案

您不能给你没有的东西 - SetForegroundWindow()只有当你的应用程序的工作原理目前关注的焦点。

You can't give what you don't have - SetForegroundWindow() only works if your application currently has the focus.

现代的Windows版本prevent从偷焦点,因为这是一个重大的烦恼早在Windows 9x中日子。应用

Modern Windows versions prevent applications from stealing focus, as that was a major annoyance back in the Windows 9x days.

此外,按Alt + F,X'仅仅是指退出英语版本的Windows,它不会对大多数其他语言工作。避免使用的SendKeys(),这是不可能以一种可靠的方式来使用。

Also, 'Alt+F, x' only refers to exit on English Windows versions, it won't work on most other languages. Avoid using SendKeys(), it's impossible to use in a reliable way.

阅读全文

相关推荐

最新文章