如何将另一个Windows窗口隐藏的HTA窗口带到前面?

时间:2018-03-27 13:57:21

标签: c# windows winapi hta

我在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);

1 个答案:

答案 0 :(得分:0)

解决方案非常简单

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