【问题标题】:"Z-Index" in winformswinforms中的“Z-Index”
【发布时间】:2011-02-08 03:26:18
【问题描述】:

在 CSS 中,我们有一个叫 z-index 的属性,Winfrom 中为 Panel 控件设置“Z-Index”是什么意思?

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    WinForms 具有 z 顺序,但您不能将其作为数字访问。相反,每个控件都有一个 BringToFront 方法和一个 SendToBack 方法,它们分别将控件移动到 z 顺序的顶部或底部。

    虽然您很少遇到 BringToFront 或 SendToBack 不能提供您需要的东西的情况,但不确定为什么会以这种方式公开。

    更新:我错了,您可以通过控件容器的Controls 集合上的方法直接访问z-order。这是一个包装它的简单方法:

    public void SetControlZOrder(Control ctrl, int z)
    {
        ctrl.Parent.Controls.SetChildIndex(ctrl, z);
    }
    

    我猜他们将其封装在 BringToFrontSendToBack 中只是为了让一切简单易用。我鼓掌。

    更新 2: 我在这里将您的 cmets 解释为不同的答案,这意味着您希望能够在面板内部且大于面板的控件(因此它的一部分被隐藏)并使其控件位于面板前面并且比它大(以便您看到整个控件)。

    您可以通过从面板中移除控件、将其位置移动原始面板的位置并将其添加到表单的控件中来做到这一点:

    panel1.Controls.Remove(button1);
    button1.Left += panel1.Left;
    button1.Top += panel1.Top; 
    this.Controls.Add(button1);
    

    左移和上移是必要的,因为按钮的位置最初是相对于面板的,现在将相对于窗体。移位使其保持在原始虚拟位置,因此它看起来像是从面板中出来的。

    然后您必须将其放回面板中,这与上面的代码正好相反。

    【讨论】:

    • @sv88erik:我删除了我的答案,因为这个更好,但这里是你写的评论:"the problem is that what then is that the controls inside the panel does not show more than the size of the panel. I make it so that when you click on the controls inside the panel will be larger, and then it goes without for the size of the panel .... "。我确定您想做的事情是可能的,但我不确定正确的方法。一种选择可能是使所有控件成为表单的子项(theControl.Parent = theForm)。然后它们就会相互重叠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2012-01-11
    相关资源
    最近更新 更多