c# – 如何在不将Dock设置为Fill的情况下将Label置于Panel内部
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在不将Dock设置为Fill的情况下将Label置于Panel内部,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2834字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何在不将Dock设置为Fill的情况下将Label置于Panel内部](/upload/InfoBanner/zyjiaocheng/806/e18029a3390e468ea78fe4c3404600f6.jpg)
我试图创建一个带有边框的自定义面板,其颜色可以更改,以便在某些条件下“突出显示”面板.
小组还需要通过案文传达某些信息.为此,我在Panel中添加了一个Label.我已尝试使用规定的方法对Label进行居中,但出于某种原因,它始终将其放在Panel的左上角.我无法将Label的Dock设置为Fill,因为它掩盖了已创建的自定义边框.所以我需要做到这一点,以便Label适合边框.
Label的Anchor设置为None,其位置为
new Point((ClientSize.Width - Size.Width)/2, (ClientSize.Height - Size.Height)/2);
自定义Panel的代码是:
public class CustomPanel : Panel
{
public CustomPanel(int borderThickness, Color borderColor) : base()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw, true);
BackColor = SystemColors.ActiveCaption;
BorderStyle = BorderStyle.FixedSingle;
Size = new Size(45, 45);
Margin = new Padding(0);
BorderThickness = borderThickness;
BorderColor = borderColor;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderStyle == BorderStyle.FixedSingle)
{
int halfThickness = BorderThickness / 2;
using (Pen p = new Pen(BorderColor, BorderThickness))
{
e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
halfThickness,
ClientSize.Width - BorderThickness, ClientSize.Height - BorderThickness));
}
}
}
public int BorderThickness { get; set; }
public Color BorderColor { get; set; }
}
表格代码是:
private void NewPanelTest_Load(object sender, EventArgs e)
{
CustomPanel cp = new CustomPanel(3, Color.Black);
// Create new Label
Label info = new Label()
{
Size = new Size(30, 30),
Text = "Info",
Anchor = AnchorStyles.None,
TextAlign = ContentAlignment.MiddleCenter,
Enabled = false,
Font = new Font("Microsoft Sans Serif", 6),
ForeColor = Color.White,
Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
};
cp.Controls.Add(info);
this.Controls.Add(cp);
}
编辑:我看过类似的问题并尝试更改Label的属性,但没有结果.
// Create new Label
Label info = new Label()
{
// Same code as before
// Different code
Left = (this.ClientSize.Width - Size.Width) / 2,
Top = (this.ClientSize.Height - Size.Height) / 2,
//Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
};
我也试过改变Panel的Padding,也没有结果.
Padding = new Padding(5);
编辑:尝试以编程方式将Label放置在Panel的中心(产生X = 0,Y = 0的结果)
// Create new Label
Label info = new Label()
{
// Same code as before (excluding "Left", "Top", and "Location")
};
int X = (info.ClientSize.Width - info.Width) / 2;
int Y = (info.ClientSize.Height - info.Height) / 2;
info.Location = new Point(X, Y);
MessageBox.Show(info.Location.ToString());
cp.Controls.Add(info);
解决方法:
我们可以通过简单的步骤实现这一目标
>将标签锚设置为左右
>将标签AutoSize设置为false;
>将Label TextAlign设置为MiddleCenter;
现在将标签置于面板中间.
int x = (panel1.Size.Width - label1.Size.Width) / 2;
label1.Location = new Point(x, label1.Location.Y);
内容总结
以上是互联网集市为您收集整理的c# – 如何在不将Dock设置为Fill的情况下将Label置于Panel内部全部内容,希望文章能够帮你解决c# – 如何在不将Dock设置为Fill的情况下将Label置于Panel内部所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。