如何使窗口始终位于另一个始终位于顶部的窗口之上?并不是说它必须保持在所有其他窗口的顶部,我只需要它保持在特定窗口的顶部.
Zac*_*son 16
感谢SLaks的回答和一些评论,我能够弄清楚如何设置我的表单之间的子父关系.我无法使用Form.Show(owner),因为我想要留在前面的形式是在另一种形式之前显示的.我使用Reflector来检查背后的代码并发Form.Show(owner)现在幕后,它都解析为Windows API中的SetWindowLong.
LONG SetWindowLong(
HWND hWnd,
int nIndex,
LONG dwNewLong
);
Run Code Online (Sandbox Code Playgroud)
Form.Show(owner)与调用SetWindowLong函数nIndex的-8.MSDN在线文档不会告诉你它,但根据Winuser.h,可用的一个常量nIndex是GWL_HWNDPARENT,其值为-8.一旦我连接这些点,问题就很容易解决.
以下是如何设置窗口的父窗口,即使它已经显示:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern int SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong);
public static void SetOwner(IWin32Window child, IWin32Window owner)
{
NativeMethods.SetWindowLong(
new HandleRef(child, child.Handle),
-8, // GWL_HWNDPARENT
new HandleRef(owner, owner.Handle));
}
Run Code Online (Sandbox Code Playgroud)