我如何集中一个外国的窗口? [英] How do I focus a foreign window?

查看:104
本文介绍了我如何集中一个外国的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能只有本身一个实例同时打开的应用程序。为了实施这个,我用这个code:

 的System.Diagnostics.Process [] myProcesses = System.Diagnostics.Process.GetProcesses();
        的System.Diagnostics.Process我= System.Diagnostics.Process.GetCurrentProcess();
        的foreach(在myProcesses的System.Diagnostics.Process P)
        {
            如果(p.ProcessName == me.ProcessName)
                如果(p.Id!= me.Id)
                {
                    //如果已经运行,取消该副本。
                    返回;
                }
        }
        //启动应用程序。
        // ...
 

它工作正常。我也喜欢它能够集中精力的已经运行的副本形式。也就是说,返回之前,我想要把这个应用程序的另一个实例推向前台。

我该怎么办呢?

回复:SetForeGroundWindow:

SetForeGroundWindow工作,到一个点:

  [System.Runtime.InteropServices.DllImport(user32.dll中)
    公共静态的extern BOOL SetForegroundWindow(IntPtr的的HWND);

    // ...
                如果(p.Id!= me.Id)
                {
                    //如果已经运行,聚焦,然后终止该副本。
                    SetForegroundWindow(p.MainWindowHandle);
                    返回;
                }
    // ...
 

这确实带来了窗口切换到前台,如果它不是最小化。真棒。 如果窗口最小化,但是,它仍然最小化。

这需要取消最小化。

通过SwitchToThisWindow解决方案(作品!):

  [System.Runtime.InteropServices.DllImport(user32.dll中)
    公共静态外部无效SwitchToThisWindow(IntPtr的的HWND,布尔fAltTab);

    [STAThread]
    静态无效的主要()
    {
        的System.Diagnostics.Process我= System.Diagnostics.Process.GetCurrentProcess();
        的System.Diagnostics.Process [] myProcesses = System.Diagnostics.Process.GetProcessesByName(me.ProcessName);
        的foreach(在myProcesses的System.Diagnostics.Process P)
        {
            如果(p.Id!= me.Id)
            {
                SwitchToThisWindow(p.MainWindowHandle,真正的);
                返回;
            }
        }
        //现在继续前进,开始我们的应用程序;-)
 

解决方案

我有同样的问题, SwitchToThisWindow ()工作最适合我。唯一的限制是,你必须安装XP ​​SP1。我打了SetForegroundWindow,ShowWindow函数,他们都出了问题拉窗进入视野。

I have an application which may only have one instance of itself open at a time. To enforce this, I use this code:

        System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
        System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess();
        foreach (System.Diagnostics.Process p in myProcesses)
        {
            if (p.ProcessName == me.ProcessName)
                if (p.Id != me.Id)
                {
                    //if already running, abort this copy.
                    return;
                }
        }
        //launch the application.
        //...

It works fine. I would also like it to be able to focus the form of the already-running copy. That is, before returning, I want to bring the other instance of this application into the foreground.

How do I do that?

Re: SetForeGroundWindow:

SetForeGroundWindow works, to a point:

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

    //...
                if (p.Id != me.Id)
                {
                    //if already running, focus it, and then abort this copy.
                    SetForegroundWindow(p.MainWindowHandle);
                    return;
                }
    //...

This does bring the window to the foreground if it is not minimized. Awesome. If the window IS minimized, however, it remains minimized.

It needs to un-minimize.

Solution via SwitchToThisWindow (Works!):

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

    [STAThread]
    static void Main()
    {
        System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess();
        System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(me.ProcessName);
        foreach (System.Diagnostics.Process p in myProcesses)
        {
            if (p.Id != me.Id)
            {
                SwitchToThisWindow(p.MainWindowHandle, true);
                return;
            }
        }
        //now go ahead and start our application ;-)

解决方案

I had the same problem and SwitchToThisWindow() worked the best for me. The only limitation is that you must have XP sp1 installed. I played with SetForegroundWindow, ShowWindow, and they both had problems pulling the window into view.

这篇关于我如何集中一个外国的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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