让窗口总是停留在已经停留在顶部的另一个窗口的顶部? [英] Make window always stay on top of ANOTHER window that already stays on top?

查看:233
本文介绍了让窗口总是停留在已经停留在顶部的另一个窗口的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使一个窗口始终停留在一个已经永远停留在顶部的另一个窗口的顶部?不,它必须留在所有其他窗口的顶部,我只需要保持在一个特定的窗口的顶部。

How can I make a window always stay on top of another window that already always stays on top? Not that it has to stay on top of all other windows, I just need it to stay on top of a particular window.

推荐答案

由于SLaks的答案,有的上的评论,我能想出如何把我的表之间的父子关系。我之前的其他形式无法使用 Form.Show(所有者),因为我想留在前面的形式显示。我使用反射来检查后面 Form.Show(所有者)的code和发现,在幕后,这一切都解决到的SetWindowLong

Thanks to SLaks's answer and some of the comments on it, I was able to figure out how set a child-parent relationship between my forms. I couldn't use Form.Show(owner), because the form that I wanted to stay in front was shown before the other form. I used Reflector to examine the code behind Form.Show(owner) and discovered that behind the scenes, it all resolves down to SetWindowLong in the Windows API.

LONG SetWindowLong(      
    HWND hWnd,
    int nIndex,
    LONG dwNewLong
);

Form.Show(所有者)要求与参数nIndex -8 。 MSDN联机文档不会告诉你这件事,但根据Winuser.h中可用于常量参数nIndex 中的一个 GWL_HWNDPARENT ,其中有 -8 。有一次,我连接这些点,这个问题很容易解决。

Form.Show(owner) calls SetWindowLong with an nIndex of -8. The MSDN online documentation won't tell you about it, but according to Winuser.h one of the constants available for nIndex is GWL_HWNDPARENT, which has a value of -8. Once I connected these dots, the problem was quite easy to solve.

这下面是如何设置窗口的父母,哪怕是已经显示出:

This following is how to set the window's parent, even if it is already shown:

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

这篇关于让窗口总是停留在已经停留在顶部的另一个窗口的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆