SetForegroundWindow不适用于最小化的过程 [英] SetForegroundWindow doesn't work with minimized process

查看:403
本文介绍了SetForegroundWindow不适用于最小化的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此主题上找不到任何好的答案,因此也许有人可以帮助我.我正在制作一个小型个人程序,希望将某个应用程序放在前台.它已经可以工作,但是有一个小问题.当过程最小化时,我的代码不起作用.该过程不会像未最小化时那样显示在前台.

Couldn't find any good answer on this topic, so maybe someone can help me out. I'm making a small personal program where I want to bring a certain application to the foreground. It already works, but there is one small problem. When the process is minimized my code doesn't work. The process won't get showed on the foreground like it does when it is not minimized.

以下是代码段:

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    public Form1()
    {
       InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process[] p
            = System.Diagnostics.Process.GetProcessesByName("Client");

        if (p.Length > 0)
        {
            SetForegroundWindow(p[0].MainWindowHandle);
        }
        else
        {
            MessageBox.Show("Window Not Found!");
        }
    }
}

推荐答案

您将需要致电

You are going to need to call ShowWindow before you try to set it as the foreground window.

可能与SW_RESTORE:

 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

 if (p.Length > 0)
 {
   ShowWindow(p[0].MainWindowHandle, 9);
   SetForegroundWindow(p[0].MainWindowHandle);
 }

PInvoke.net-ShowWindow DllImport和使用C#中的功能.

PInvoke.net - ShowWindow has some examples on DllImport and using the function in C#.

这篇关于SetForegroundWindow不适用于最小化的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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