【问题标题】:How to bring a HTA window hidden by another Windows window to the front?如何将被另一个 Windows 窗口隐藏的 HTA 窗口带到前面?
【发布时间】:2018-03-27 13:57:21
【问题描述】:

我在 COM 代理进程中通过 Process.Start() 启动 HTA 文件。

由于某种原因,该进程从一个最大化的窗口开始,该窗口立即被创建它的应用程序窗口隐藏。

我怎样才能把这个 HTA 窗口放在前面?我已经尝试过 SetWindowPos 等但没有成功。

这适用于记事本,但不适用于 HTA

这适用于启动记事本

private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

....
Process p = Process.Start("notepad.exe");
Thread.Sleep(500); // Allow the process to open it's window

SetWindowPos(p.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);

【问题讨论】:

  • 实际上是隐藏的窗口还是就在后面...
  • 它在后面,因为它是 HTA,所以窗口在任务栏的右键菜单中只有关闭按钮,而不是最小化、最大化等。
  • 并且启动它的应用程序没有设置为始终位于顶部?
  • 没有。问题是 HTA 是在 COM 代理中启动的。
  • 如果我点击任务栏,应用程序会转到前台。如果我按 ALT+TAB,应用程序会转到前台。所以我想我应该能够以某种方式模仿那些调用,但还没有成功。

标签: c# windows winapi hta


【解决方案1】:

解决方案非常简单

                var thread = new Thread(() => {
                    try
                    {
                        Thread.Sleep(500);

                        var me = Process.GetCurrentProcess();
                        var myProcesses = Process.GetProcessesByName(me.ProcessName);
                        foreach (var p in myProcesses)
                        {
                            if (p.Id != me.Id)
                            {
                                SwitchToThisWindow(p.MainWindowHandle, true);
                            }
                        }

                    }
                    catch (Exception e)
                    {
                        Log.Exception(e);
                    }});

                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();

....

    [DllImport("user32.dll", SetLastError = true)]
    static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多