“Z-索引"在winforms中 [英] "Z-Index" in winforms

查看:29
本文介绍了“Z-索引"在winforms中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CSS 中,我们有一个名为 z-index 的属性,Winfrom 中为 Panel 控件设置的与Z-Index"相同的是什么?

In CSS, we have a Property called z-index, what is the same in Winfrom set for a Panel control to the "Z-Index?

推荐答案

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

WinForms has a z-order, but you can't access it as a number. Instead, every control has a BringToFront method and a SendToBack method, which move the control to the top of the z-order or to the bottom, respectively.

不确定为什么会以这种方式公开,尽管您很少遇到BringToFront 或SendToBack 不能提供您需要的内容的情况.

Not sure exactly why it was exposed this way, although you rarely encounter situations where either BringToFront or SendToBack don't provide what you need.

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

Update: I'm wrong, you can access the z-order directly via a method on the control's container's Controls collection. Here's a simple method that wraps it:

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

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

I'm guessing they encapsulated this in BringToFront and SendToBack just to keep everything simple and easy to use. I applaud.

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

Update 2: I interpreted your comments to a different answer here to mean that you want to be able to take a control that is inside a panel and larger than the panel (so that part of it is hidden) and make it so that the control is in front of the panel and larger than it (so that you see the whole control).

您可以通过从面板中移除控件,将其位置移动原始面板的位置,然后将其添加到表单的控件中来实现:

You can do this by removing the control from the panel, shifting its position by the original panel's position, and adding it to the form's controls:

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

左移和上移是必要的,因为按钮的位置最初是相对于面板的,现在将是相对于表单的.移位使其保持在原始虚拟位置,因此它似乎是从面板中出来的.

The Left and Top shifts are necessary because the button's position was originally relative to the panel, and will now be relative to the form. The shifts keep it in the original virtual position, so it appears to come out of the panel.

然后您必须处理将其放回面板的问题,这与上述代码正好相反.

You would then have to deal with putting it back in the panel, which is just a reverse of the above code.

这篇关于“Z-索引"在winforms中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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