- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不喜欢 Label 控件的 AutoSize 属性。我有一个自定义标签,除其他外,它还绘制了花哨的圆角边框。我在我的构造函数中放置了一个 AutoSize = false
,但是,当我将它置于设计模式时,该属性始终为 True。
我已经成功覆盖了其他属性,但这个属性很高兴地无视我。如果这是“MS 设计”,有人知道吗?
如果有人感兴趣,这是我的标签的完整源代码。
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Dentactil.UI.WinControls
{
[DefaultProperty("TextString")]
[DefaultEvent("TextClick")]
public partial class RoundedLabel : UserControl
{
private static readonly Color DEFAULT_BORDER_COLOR = Color.FromArgb( 132, 100, 161 );
private const float DEFAULT_BORDER_WIDTH = 2.0F;
private const int DEFAULT_ROUNDED_WIDTH = 16;
private const int DEFAULT_ROUNDED_HEIGHT = 12;
private Color mBorderColor = DEFAULT_BORDER_COLOR;
private float mBorderWidth = DEFAULT_BORDER_WIDTH;
private int mRoundedWidth = DEFAULT_ROUNDED_WIDTH;
private int mRoundedHeight = DEFAULT_ROUNDED_HEIGHT;
public event EventHandler TextClick;
private Padding mPadding = new Padding(8);
public RoundedLabel()
{
InitializeComponent();
}
public Cursor TextCursor
{
get { return lblText.Cursor; }
set { lblText.Cursor = value; }
}
public Padding TextPadding
{
get { return mPadding; }
set
{
mPadding = value;
UpdateInternalBounds();
}
}
public ContentAlignment TextAlign
{
get { return lblText.TextAlign; }
set { lblText.TextAlign = value; }
}
public string TextString
{
get { return lblText.Text; }
set { lblText.Text = value; }
}
public override Font Font
{
get { return base.Font; }
set
{
base.Font = value;
lblText.Font = value;
}
}
public override Color ForeColor
{
get { return base.ForeColor; }
set
{
base.ForeColor = value;
lblText.ForeColor = value;
}
}
public Color BorderColor
{
get { return mBorderColor; }
set
{
mBorderColor = value;
Invalidate();
}
}
[DefaultValue(DEFAULT_BORDER_WIDTH)]
public float BorderWidth
{
get { return mBorderWidth; }
set
{
mBorderWidth = value;
Invalidate();
}
}
[DefaultValue(DEFAULT_ROUNDED_WIDTH)]
public int RoundedWidth
{
get { return mRoundedWidth; }
set
{
mRoundedWidth = value;
Invalidate();
}
}
[DefaultValue(DEFAULT_ROUNDED_HEIGHT)]
public int RoundedHeight
{
get { return mRoundedHeight; }
set
{
mRoundedHeight = value;
Invalidate();
}
}
private void UpdateInternalBounds()
{
lblText.Left = mPadding.Left;
lblText.Top = mPadding.Top;
int width = Width - mPadding.Right - mPadding.Left;
lblText.Width = width > 0 ? width : 0;
int heigth = Height - mPadding.Bottom - mPadding.Top;
lblText.Height = heigth > 0 ? heigth : 0;
}
protected override void OnLoad(EventArgs e)
{
UpdateInternalBounds();
base.OnLoad(e);
}
protected override void OnPaint(PaintEventArgs e)
{
SmoothingMode smoothingMode = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int roundedWidth = RoundedWidth > (Width - 1)/2 ? (Width - 1)/2 : RoundedWidth;
int roundedHeight = RoundedHeight > (Height - 1)/2 ? (Height - 1)/2 : RoundedHeight;
GraphicsPath path = new GraphicsPath();
path.AddLine(0, roundedHeight, 0, Height - 1 - roundedHeight);
path.AddArc(new RectangleF(0, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 180, -90);
path.AddLine(roundedWidth, Height - 1, Width - 1 - 2*roundedWidth, Height - 1);
path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 90, -90);
path.AddLine(Width - 1, Height - 1 - roundedHeight, Width - 1, roundedHeight);
path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, 0, 2*roundedWidth, 2*roundedHeight), 0, -90);
path.AddLine(Width - 1 - roundedWidth, 0, roundedWidth, 0);
path.AddArc(new RectangleF(0, 0, 2*roundedWidth, 2*roundedHeight), -90, -90);
e.Graphics.DrawPath(new Pen(new SolidBrush(BorderColor), BorderWidth), path);
e.Graphics.SmoothingMode = smoothingMode;
base.OnPaint(e);
}
protected override void OnResize(EventArgs e)
{
UpdateInternalBounds();
base.OnResize(e);
}
private void lblText_Click(object sender, EventArgs e)
{
if (TextClick != null)
{
TextClick(this, e);
}
}
}
}
(Stack Overflow 的标记和 Underscore 存在一些问题,但很容易理解代码)。
我实际上已经在一段时间前删除了该覆盖,当时我发现它不起作用。我现在再次添加它并进行测试。基本上我想用一些名为 IWillNotAutoSizeLabel 的新标签替换 Label ;)
我基本上讨厌“默认打开”的自动调整大小属性。
最佳答案
在窗体本身的构造函数中设置控件的某些属性时,我看到了类似的行为。他们似乎恢复到他们的设计时默认值。
我注意到您已经覆盖了 OnLoad 方法。您是否尝试过在那里设置 AutoSize = false ?或者您主要关心的是提供 false 的默认值吗?
关于C#.NET Winforms : Is it possible to override Label. 自动调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200/
我有一个包含需要排序的不同项目的列表。但还有一个额外的问题:某些元素只允许出现在列表中的特定位置。 示例(请查看 http://jsfiddle.net/pYL32/2/ ):有一个包含元素 foo、
关于https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained据解释,guava(以及后来的 java 8
我有一个名为 say CalculationOutcome 的类(class)和 FileHashOutcome .他们的构造函数有 (ActualResult, Throwable)参数,并在 Co
我正在使用pycharm,我的代码在分屏上。当我运行调试时,会弹出调试/运行窗口,它非常分散注意力并且限制了我在调试时可以查看的代码量......但我想保持它,因为我来回走动;另外,我想要调试变量的完
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: javascript object, access variable property name? 我确信这是可以完
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {
在将实体存储在 redis 中作为序列化二进制 blob 的应用程序中工作。我有多个客户端处理同一个数据集,我希望使用乐观并发。 我的要求是: 在一次往返中读取特定键的序列化实体 将修改后的实体写回r
这个问题是指 C/x86 上使用的 IEEE 标准浮点数。 是否可以将任何数字(即不包括 NaN 之类的特殊值)浮点数或 double 数表示为十进制字符串,以便将该字符串转换回浮点数/ double
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
如何在 Scene Kit 中使用 SCNCamera 获得像鱼眼镜头那样的失真? 类似于这种图像的“鞠躬”: //正如 Rickster 指出的那样,这种失真被称为“桶形失真”。 从文档中,这是让我
我想问是否有一种方法可以多次评估 javascript 术语,而不需要一遍又一遍地解析一个术语。 说,您想要评估 var1/var2+Math.sqrt(var3) 每秒 20 次。 使用时这可能会出
我想知道在技术上是否可以在 java applet 中创建代理。 那么是否可以通过这个 java applet 代理路由所有进一步的浏览器请求? 例如,如果用户要浏览 google.com,默认行为是
我有以下代码,我想返回一个 bool 值或一个元组。 (函数 isvariable 和 dont_care 都返回 bool 值,仅供引用) let match_element (a, b) = if
这个问题困扰我很久了。我想要一个二叉树(或类似的嵌套结构)上的迭代器,它高效、简单且Pythonic。例如,对于这样的用法: for value in values(root): do_som
目前我有以下 MySQL 查询: SELECT COUNT(*) AS `count`, `v`.`value` FROM `client_entity_int` AS `v` INN
我正在使用 Angular 开发应用程序,客户端是 100% JS。我即将替换使用 ExtJS 制作的旧应用程序,但我不会更改服务器端。只有客户端从头开始重新编码。 我想在任何地方和任何机器上处理这个
有没有办法在运行时检索实例的声明类?例如: public class Caller { private JFrame frame = new JFrame("Test"); priva
我目前正在请求 MySQL 数据库使用 PDO 计算一些计数和总和。这个过程可能需要一段时间,如果用户突然想浏览另一个页面,他可能会停留在浏览器前面。 我试图弄清楚是否可以使用 PDO 启动 MySQ
想知道它是不是这样工作的: $result .= mysqli_query($query1); $result .= mysqli_query($query2); $result 会是查询 1 和 2
所以我有这样的挑战: body 背景上的图像,背景大小,覆盖以适合整个屏幕。在背景图像上是一些元素(建筑物)。所以我想将鼠标悬停在建筑物上,他们会更改颜色或添加阴影等。问题在于屏幕调整大小,当我调整屏
我是一名优秀的程序员,十分优秀!