相关疑难解决方法(0)

恢复另一个应用程序的最小化窗口

我正在向一个应用程序添加一些代码,如果它还没有运行,它将启动另一个应用程序,或者如果是,请将它带到前面.这需要少量的interop/WinAPI代码,我从其他网站获得了示例,但似乎无法在Win7中工作.

如果窗口处于某种可见状态,则API的SetForegroundWindow方法就像处理一样(这将是主要情况,根据公司策略,如果外部应用程序正在运行,则不应该最小化).但是,如果它被最小化(例外但很重要,因为我的应用程序在这种情况下似乎什么也不做),这个方法和ShowWindow/ShowWindowAsync实际上都不会从任务栏中恢复窗口; 所有方法都只是突出显示任务栏按钮.

这是代码; 大部分工作都很好,但是对ShowWindow()的调用(我也尝试过ShowWindowAsync),无论我发送的命令是什么,都不会做我想要的事情:

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

    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMAXIMIZED = 3;
    private const int SW_RESTORE = 9;

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

...

//The app is named uniquely enough that it can't be anything else,
//and is not normally launched except by this one.
//so this should normally return zero or one instance
var processes = Process.GetProcessesByName("ExternalApp.exe");

        if (processes.Any()) //a copy is already …
Run Code Online (Sandbox Code Playgroud)

c# winapi interop windows-7

18
推荐指数
3
解决办法
3万
查看次数

无法使用pinvoke将WM_CLOSE发送到Windows资源管理器窗口

我有一个C#应用程序,它使用SendMessage pinvoke方法向应用程序外的各个窗口发送"关闭窗口"消息(WM_CLOSE/16).这很有效,除非有问题的窗口是Windows资源管理器窗口.我没有异常,但窗口没有关闭.

这是签名:

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    internal static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
Run Code Online (Sandbox Code Playgroud)

我需要将不同的消息发送到Windows资源管理器窗口吗?或者另一种方法来实现这一目标?

c# pinvoke

3
推荐指数
1
解决办法
4217
查看次数

最大化另一个运行程序的窗口

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

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

private void button1_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Maximised;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,它只显示我的应用 我想它最大化另一个exe,但如果它找不到它然后我想它退出.

.net c# .net-4.0 winforms

3
推荐指数
1
解决办法
4370
查看次数

标签 统计

c# ×3

.net ×1

.net-4.0 ×1

interop ×1

pinvoke ×1

winapi ×1

windows-7 ×1

winforms ×1