C#SetForegroundWindow无法正常工作 [英] C# SetForegroundWindow not working

查看:799
本文介绍了C#SetForegroundWindow无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用此问题将我的C#Windows应用程序限制为一次只能运行一个实例,

I have restricted my C# windows application to only allow one instance to be running at a time, using this question How to force C# .net app to run only one instance in Windows?

它运行良好,并且不允许同时运行一个以上的应用程序实例。

It works well and does not allow more than one instance of the application to run at the same time.

问题是如果用户尝试执行以下操作:打开应用程序的第二个实例,我希望当前处于活动状态的实例出现在前面。

The problem is that if the user attempts to open a second instance of the application, I want the currently active one to come to the front.

我工作的问题似乎可以解决此问题,但是它没有用为了我。

The question I worked from seems to address this, but it is not working for me.

我认为这是因为我的应用程序不符合允许该方法的标准: SetForegroundWindow
去工作。

I think it is because my application is not meeting the criteria to allow the method :SetForegroundWindow to work.

我的问题是,如何实现这一目标。我的代码如下:

My question is, how can I achieve this. My code is below:

using System    ;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace RTRFIDListener_Client
{
    static class Program
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew = true;

            using (Mutex mutex = new Mutex(true, "RTRFIDListener_Client", out createdNew))
            {
                if (createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new frm_Main());
                }
                else
                {
                    Process current = Process.GetCurrentProcess();
                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }
                }
            }
        }
    }
}


推荐答案

旋转您自己的单实例应用程序通常是一个错误,.NET Framework已经对其提供了强大的支持,并且功能强大,很难利用。并具有您要寻找的功能,一个StartupNextInstance事件,该事件在用户再次启动您的应用程序时触发。添加对Microsoft.VisualBasic的引用,并使您的Program.cs文件如下所示:

Spinning your own single-instance app is generally a mistake, the .NET Framework already supports it strongly and it is rock-solid, very hard to exploit. And has the feature you are looking for, a StartupNextInstance event that fires when the user starts your app again. Add a reference to Microsoft.VisualBasic and make your Program.cs file look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WhatEverYouUse {
    class Program : WindowsFormsApplicationBase {
        [STAThread]
        static void Main(string[] args) {
            Application.SetCompatibleTextRenderingDefault(false);
            new Program().Start(args);
        }
        void Start(string[] args) {
            this.EnableVisualStyles = true;
            this.IsSingleInstance = true;
            this.MainForm = new Form1();
            this.Run(args);
        }
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
            eventArgs.BringToForeground = true;
            base.OnStartupNextInstance(eventArgs);
        }
    }
}

如果您有任何用途用于启动第二个实例的命令行参数,例如当您使用文件关联,然后在事件处理程序中使用 eventArgs.CommandLine 时,通常。

If you have any use for the command line arguments that were used to start the 2nd instance, typical when you use a file association for example, then use eventArgs.CommandLine in your event handler.

这篇关于C#SetForegroundWindow无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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