本文介绍了最大化另一个正在运行的程序的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式最大化我当前在PC上运行的程序。例如,如果我在任务管理器中运行 WINWORD.exe 。如何最大化它?

How do I programmatically maximize a program that I have currently running on my pc. For example if I have WINWORD.exe running in task manager. How do I maximize it?

在我的代码中,我尝试过:

In my code I have tried:

private void button1_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Maximised;
}

很遗憾,仅显示我的申请。我希望它最大化另一个exe,但如果找不到它,那么我要退出它。

Unfortunately that only displays my application. I would like it to maximise another exe, BUT if it cannot find it then i want to it to exit.

推荐答案

使用ShowWindow

您可以使用方法。为此,您首先需要找到窗口句柄,然后使用该方法。然后以这种方式最大化窗口:

You can set windows state using ShowWindow method. To do so, you first need to find the window handle and then using the method. Then maximize the window this way:

private const int SW_MAXIMIZE = 3;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private void button1_Click(object sender, EventArgs e)
{
    var p = System.Diagnostics.Process.GetProcessesByName("WINWORD").FirstOrDefault();
    if(p!=null)
    {
        ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);
    }
}

使用WindowPattern.SetWindowVisualState

作为另一个选项(基于汉斯的评论),您可以使用设置窗口状态的方法。为此,请首先添加对 UIAutomationClient.dll UIAutomationTypes.dll 的引用,然后添加使用System.Windows.Automation; 并以这种方式最大化窗口:

Also as another option (based on Hans's comment), you can use SetWindowVisualState method to set state of a window. To so so, first add a reference to UIAutomationClient.dll and UIAutomationTypes.dll then add using System.Windows.Automation; and maximize the window this way:

var p = System.Diagnostics.Process.GetProcessesByName("WINWORD").FirstOrDefault();
if (p != null)
{
    var element = AutomationElement.FromHandle(p.MainWindowHandle);
    if (element != null)
    {
        var pattern = element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
        if (pattern != null)
            pattern.SetWindowVisualState(WindowVisualState.Maximized);
    }
}

这篇关于最大化另一个正在运行的程序的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:16