如何将焦点设置为已处于运行状态的应用程序? [英] How can i set the focus to application which is already in running state?

查看:52
本文介绍了如何将焦点设置为已处于运行状态的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了C#Windows应用程序&创建了它的exe文件.

I have developed a C# Windows application & created a exe of it.

我想要的是,每当我尝试运行该应用程序时,如果它已经处于运行状态,则激活该应用程序实例,否则启动新的应用程序.

What I want is that when ever I try to run the application, if it is already in running state, then activate that application instance, else start new application.

这意味着我不想多次打开同一应用程序

That means I don't want to open same application more than one time

推荐答案

使用以下代码将焦点设置为当前应用程序:

Use the following code to set focus to the current application:

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

        [DllImport("user32.dll")]
        internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        ... 
        Process currentProcess = Process.GetCurrentProcess();
        IntPtr hWnd = currentProcess.MainWindowHandle;
        if (hWnd != IntPtr.Zero)
        {
            SetForegroundWindow(hWnd);
            ShowWindow(hWnd, User32.SW_MAXIMIZE);
        }

这篇关于如何将焦点设置为已处于运行状态的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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