gpt4 book ai didi

c# - 基础表格消失但仅当对话框显示时

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:23 26 4
gpt4 key购买 nike

这与通过互操作显示 C# 表单的 VB6 应用有关。

C# 窗体中的一个事件导致显示一个 VB6 应用程序窗体。

通常,当此 VB6 窗体被隐藏时 (Form.Hide),底层 C# 窗体会被带到前面。

但是,如果在 VB6 窗体的生命周期内导致显示 MsgBox,则当隐藏 VB6 窗体时,底层 C# 窗体将不会位于最前面。

为什么会这样?这就像 MsgBox 正在改变表单的 Z 顺序。

最佳答案

“如何在隐藏 VB6 后显示 C# 窗体?我必须使用窗口句柄吗?”

假设您同意孤立的 msgbox 保持打开状态。当 VB6 窗体处于隐藏状态时,您需要触发事件以获取窗口句柄:

public static int FindWindow(string windowName, bool wait)
{
int hWnd = FindWindow(null, windowName);
while (wait && hWnd == 0)
{
System.Threading.Thread.Sleep(500);
hWnd = FindWindow(null, windowName);
}

return hWnd;
}

然后将 C# 窗口置于顶部:

[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

// When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();

/// <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(HandleRef hWnd);

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

private static void ForceForegroundWindow(IntPtr hWnd)
{
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
uint appThread = GetCurrentThreadId();
const uint SW_SHOW = 5;

if (foreThread != appThread)
{
AttachThreadInput(foreThread, appThread, true);
BringWindowToTop(hWnd);
ShowWindow(hWnd, SW_SHOW);
AttachThreadInput(foreThread, appThread, false);
}
else
{
BringWindowToTop(hWnd);
ShowWindow(hWnd, SW_SHOW);
}
}

引用:SetForegroundWindow Win32-API not always works on Windows-7

关于c# - 基础表格消失但仅当对话框显示时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12544436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com