内容对齐枚举值的说明
本文关键字:说明 枚举 对齐 | 更新日期: 2023-09-27 18:31:29
System.Drawing.ContentAlign 枚举如下所示:
namespace System.Drawing
{
// Summary:
// Specifies alignment of content on the drawing surface.
[Editor("System.Drawing.Design.ContentAlignmentEditor, System.Drawing.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public enum ContentAlignment
{
TopLeft = 1,
TopCenter = 2,
TopRight = 4,
MiddleLeft = 16,
MiddleCenter = 32,
MiddleRight = 64,
BottomLeft = 256,
BottomCenter = 512,
BottomRight = 1024,
}
}
为什么值以旗帜样式定义?为什么 8 和 128 不见了?
也许是为了让ContentAlignment
枚举参与按位运算。
但是为什么它没有用
FlagsAttribute
装饰?
因为它不打算被客户端用作按位标志,所以他们没有使用 FlagsAttribute 装饰枚举。
您可以参考 .net 框架源代码,了解它们如何很好地将ContentAlignment
与按位运算结合使用。
ControlPaint.TranslateAlign利用它,你可以在类的顶部看到带有按位OR的声明
private static readonly ContentAlignment anyRight = ContentAlignment.TopRight | ContentAlignment.MiddleRight | ContentAlignment.BottomRight;
private static readonly ContentAlignment anyBottom = ContentAlignment.BottomLeft | ContentAlignment.BottomCenter | ContentAlignment.BottomRight;
private static readonly ContentAlignment anyCenter = ContentAlignment.TopCenter | ContentAlignment.MiddleCenter | ContentAlignment.BottomCenter;
private static readonly ContentAlignment anyMiddle = ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight;
这应该回答"为什么以旗帜样式定义值"。
为什么 8 和 128 不见了?
我不知道。如果有人有,将不胜感激。
昨天我看到一位匿名用户的建议编辑,试图解释为什么缺少 8 和 128。它被拒绝了,因为它被编辑到现有的答案中。
我认为这只是猜测,而不是一个明确的答案,但它听起来很有道理,所以这里是(引用自 Sriram Sakthivel 的回答):
为什么 8 和 128 不见了?
我不知道。如果有人有,将不胜感激。
那么让我们来看看:
0000 00000000 0000
没有 8、128 或高于 1024 的任何值的原因如下:对于前 3 位(左上角、顶部居中和右上角),取前 4 位:
TopLeft = 1 (0001)
TopCenter = 2 (0010)
TopRight = 4 (0100)
然后是第二个 3(中间左、中间和中间右)
MiddleLeft = 16 (0001[0000])
MiddleCenter = 32 (0010[0000])
MiddleRight = 64 (0100[0000])
然后最后 3 个(左下、下中心和右下
)BottomLeft = 256 (0001[00000000])
BottomCenter = 512 (0010[00000000])
BottomRight = 1024 (0100[00000000])
这使得不仅垂直而且水平使用按位运算变得容易。