打开进程(记事本)后如何将焦点设置回表单? [英] How to set focus back to form after opening up a process (Notepad)?

查看:19
本文介绍了打开进程(记事本)后如何将焦点设置回表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Process.Start() 从我的程序中打开了一个记事本,但新打开的记事本覆盖了屏幕.但我确实希望我的应用程序保持专注.

I open up a notepad from my program using Process.Start() but the new opened notepad covers the screen. But I do want my application to maintain its focus.

我同样(使用相同的 Process.Start)打开 MS Excel 和 Word,但为了让焦点回到我的表单,我需要写的是:

I similarly (using the same Process.Start) open up MS Excel and Word but to get focus back to my form all I need to write is:

this.Focus();

但记事本的怪癖:我打开记事本(以及所有其他类似的进程)

But quirk with Notepad: I open notepad (and all other processes like this)

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.StartInfo.FileName = @"abc.log";
process.Start();

现在记事本成为焦点.

我试过这些:

  1. this.Activate()this.Focus(),不用说了

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]  
public static extern IntPtr SetFocus(HandleRef hWnd);

{
   IntPtr hWnd = myProcess.Handle;
   SetFocus(new HandleRef(null, hWnd));
}

  • [DllImport("User32")]
    private static extern int SetForegroundWindow(IntPtr hwnd);
    
    [DllImportAttribute("User32.DLL")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    private const int SW_SHOW = 5;
    private const int SW_MINIMIZE = 6;
    private const int SW_RESTORE = 9;
    
    {
        ShowWindow(Process.GetCurrentProcess().MainWindowHandle, SW_RESTORE);
        SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle);
    }
    

  • 另一个更长的解决方案来自这里.

    所有这些仍然保持在记事本上.为什么仅仅将焦点放在一个窗口上如此困难,那也是应用程序自己的窗口?

    All which still keeps the focus on notepad. Why is it so difficult to merely get focus to a window, that too application's own window?

    充其量我可以打开最小化的记事本,但在尝试上述所有代码后它仍然不会将焦点放在表单上.记事本打开时已最小化,但焦点仍会在记事本上(有时我们在 windows xp 中会看到这种情况)并且表单将失去焦点.

    At best I can open the notepad minimized, but it still wouldn't give the focus to the form after trying all the above codes. Notepad opens minimized, but focus will be still on notepad (something that sometimes we see in windows xp) and form will be out of focused.

    推荐答案

    我几乎尝试了互联网上的所有内容(非常确定 :)).充其量我可以将我的表格放在所有其他表格之上,但没有焦点(通过@Hans Passant 的方法).通过大量的代码块,我不知何故觉得这不会很容易.所以我总是将 SetForegroundWindow() 与其他代码块一起使用.从未想过仅仅 SetForegroundWindow() 就可以解决问题.

    I tried almost everything on internet (so sure about it :)). At best I could get my form on top of all other forms, but without focus (going by @Hans Passant's method). Going by heavy blocks of codes all over, I somehow felt this aint gonna be easy. So I always used SetForegroundWindow() with chunks of other code. Never thought merely SetForegroundWindow() would do the trick.

    这奏效了.

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    
    private void button1_Click(object sender, EventArgs e)
    { 
        Process process = new Process();
        process.StartInfo.FileName = @...abc.log";
        process.Start();
    
        process.WaitForInputIdle(); //this is the key!!
    
        SetForegroundWindow(this.Handle);
    }
    

    有时这种方法会产生对父表单的关注(在我想要的表单是其父表单的模态子表单的情况下);在这种情况下,只需将 this.Focus() 添加到最后一行..

    At times this method yields in a focus on the parent form (in cases where my desired form is a modal child form of its parent form); in such cases, just add this.Focus() to the last line..

    即使这样也有效:

    Microsoft.VisualBasic.Interaction.Shell(@"notepad.exe D:abc.log", 
                                            Microsoft.VisualBasic.AppWinStyle.NormalNoFocus);
    

    此处

    这篇关于打开进程(记事本)后如何将焦点设置回表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆