Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将焦点设置为已处于运行状态的应用程序?_C# - Fatal编程技术网

C# 如何将焦点设置为已处于运行状态的应用程序?

C# 如何将焦点设置为已处于运行状态的应用程序?,c#,C#,我开发了一个C#Windows应用程序并创建了一个exe [DllImport("user32.dll")] internal static extern IntPtr SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

我开发了一个C#Windows应用程序并创建了一个exe

        [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);
        }
我想要的是,当我尝试运行应用程序时,如果它已经处于运行状态,则激活该应用程序实例,否则启动新的应用程序

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

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

使用以下代码部分检查exe的多个实例,以及它是否在表单加载时返回true。要在应用程序中运行此功能,请使用System.Diagnostics
命名空间

private bool CheckMultipleInstanceofApp()
        {
            bool check = false;
            Process[] prc = null;
            string ModName, ProcName;
            ModName = Process.GetCurrentProcess().MainModule.ModuleName;
            ProcName = System.IO.Path.GetFileNameWithoutExtension(ModName);
            prc = Process.GetProcessesByName(ProcName);
            if (prc.Length > 1)
            {
                MessageBox.Show("There is an Instance of this Application running");
                check = true;
                System.Environment.Exit(0);
            }
            return check;
        }
        [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);
        }

使用互斥来启动应用程序的单个实例。
此外,您还可以使用Process类查找应用程序并对其设置焦点。在这里

您可以从user32.dll中的PInvoke setforegroughindow()和SetFocus()执行此操作

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

// SetFocus will just focus the keyboard on your application, but not bring your process to front.
// You don't need it here, SetForegroundWindow does the same.
// Just for documentation.
[DllImport("user32.dll")]
static extern IntPtr SetFocus(HandleRef hWnd);
        [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);
        }
作为参数,您将传递要置于前端和焦点的进程的窗口句柄

SetForegroundWindow(myProcess.MainWindowHandle);

SetFocus(new HandleRef(null, myProcess.Handle)); // not needed
        [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);
        }

另请参见。

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

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

请检查以下内容,您可能会得到您想要的纯和干净:.NET版本没有DLL地狱。当我添加user32.dll时,应用程序终止当我尝试在代码中添加user32的dll时,visual studio关闭它只是关闭,没有任何消息/错误报告对话框?您是否尝试过以管理员权限启动visual studio?+1用于订单或命令(首先是SetForeground..,然后是ShowWindow),否则无法正常工作!如果您没有User32枚举,则可在以下位置找到值:复制/粘贴的枚举可在以下位置找到:。此外,不需要显示窗口。使用System.Diagnostics;使用System.Runtime.InteropServices;