- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在某个地方找到了一些自定义分组框的代码,用圆角、渐变背景和阴影绘制。我觉得它看起来很不错,所以我调整它以使用标签控件做同样的事情。
奇怪的是,当我将其中一个控件放到窗体设计器上时,默认属性已设置并且绘制得很好 - 我在窗体上放置了多个副本,它们都很好。
但是,当我尝试在运行时添加多个控件时,实际上只有第一个版本被正确绘制。其他的最终只是空白的白框。
我真的不知道该怎么办。
标签的代码如下(抱歉 - 太长了!)
public class LabelEx : Label
{
private System.Drawing.Color _BorderColor = Color.FromArgb(141, 178, 227);
private float _BorderWidth = 1;
private System.Drawing.Color _BackgroundColor = Color.White;
private System.Drawing.Color _BackgroundColorGradient = Color.FromArgb(227, 235, 246);
private ControlGradientMode _BackgroundGradientMode = ControlGradientMode.Vertical;
private int _CornerRadius = 5;
private RoundedControlCorners _Corners = RoundedControlCorners.All;
private int _DropShadowThickness = 3;
private bool _DropShadowVisible = true;
private System.Drawing.Color _ShadowColor = Color.FromArgb(50, Color.Black);
private ControlStyles _LabelStyle = ControlStyles.Extended;
public enum ControlStyles
{
Standard,
Extended
}
public LabelEx()
{
//InitializeComponent();
}
/// <summary>Gets or sets the radius of the corners of the control.</summary>
//[Category("Appearance"), Description("This feature will round the corners of the control.")]
public int CornerRadius
{
get { return _CornerRadius; }
set
{
if (value > 35)
{
_CornerRadius = 35;
}
else
{
if (value < 1)
{
_CornerRadius = 1;
}
else
{
_CornerRadius = value;
}
}
Invalidate();
}
}
/// <summary>Turns on or off the control shadowing.</summary>
//[Category("Appearance"), Description("This feature will turn on control shadowing.")]
public bool DropShadowVisible
{
get { return _DropShadowVisible; }
set
{
_DropShadowVisible = value;
Invalidate();
}
}
/// <summary>Gets or sets the color of the control's border.</summary>
//[Category("Appearance"), Description("This feature will allow you to change the color of the control's border.")]
public System.Drawing.Color BorderColor
{
get { return _BorderColor; }
set
{
_BorderColor = value;
Invalidate();
}
}
/// <summary>Gets or Sets the control's border size.</summary>
//[Category("Appearance"), Description("This feature will allow you to set the control's border size.")]
public float BorderWidth
{
get { return _BorderWidth; }
set
{
if (value > 10)
{
_BorderWidth = 10;
}
else
{
if (value < 1)
{
_BorderWidth = 1;
}
else
{
_BorderWidth = value;
}
}
Invalidate();
}
}
/// <summary>Gets or sets the size of the shadow border thickness.</summary>
// [Category("Appearance"), Description("This feature will change the size of the shadow border.")]
public int DropShadowThickness
{
get { return _DropShadowThickness; }
set
{
if (value > 10)
{
_DropShadowThickness = 10;
}
else
{
if (value < 1)
{
_DropShadowThickness = 1;
}
else
{
_DropShadowThickness = value;
}
}
Invalidate();
}
}
/// <summary>Gets or sets the background color to use. This color can also be used in combination with BackgroundColorGradient for a gradient paint.</summary>
// [Category("Appearance"), Description("This feature will change the group control color. This color can also be used in combination with BackgroundColorGradient for a gradient paint.")]
public System.Drawing.Color BackgroundColor
{
get { return _BackgroundColor; }
set
{
_BackgroundColor = value;
Invalidate();
}
}
/// <summary>Specifies the second color when using the gradient mode.</summary>
// [Category("Appearance"), Description("This feature can be used in combination with BackgroundColor to create a gradient background.")]
public System.Drawing.Color BackgroundColorGradient
{
get { return _BackgroundColorGradient; }
set
{
_BackgroundColorGradient = value;
Invalidate();
}
}
/// <summary>This property secifies the type of gradient to use when painting the background.</summary>
// [Category("Appearance"), Description("This feature turns on background gradient painting.")]
public ControlGradientMode BackgroundGradientMode
{
get { return _BackgroundGradientMode; }
set
{
_BackgroundGradientMode = value;
Invalidate();
}
}
/// <summary>Specifies which corners to apply rounding for</summary>
public RoundedControlCorners Corners
{
get { return _Corners; }
set
{
_Corners = value;
Invalidate();
}
}
/// <summary>Gets or sets the Style for the GroupBox. Standard duplicates the stock groupbox. Extended allows the customization ability</summary>
// [Category("Appearance"), Description("Gets or sets the Style for the GroupBox. Standard duplicates the stock groupbox. Extended allows the customization ability")]
// [DefaultValue(ControlStyles.Extended)]
public ControlStyles LabelStyle
{
get { return _LabelStyle; }
set
{
_LabelStyle = value;
Invalidate();
}
}
/// <summary>
/// Function to paint the label as per what we want.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
//Just use a normal label if the "standard" is set.
if (_LabelStyle == ControlStyles.Standard)
{
base.OnPaint(e);
return;
}
//we must set the smoothing mode to anti alias or high quality(They are the same) in order to get the
//nice rounded corders on our control
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//first lets set the rectangles we will be drawing in. If the drop shadow is visible then we need to
//reduce the size of the main rectangle and create a second one that is offset by the shadow thickness.
Rectangle shadowRec = default(Rectangle);
Rectangle frameRec = default(Rectangle);
if (DropShadowVisible)
{
shadowRec = Rectangle.FromLTRB(DropShadowThickness, DropShadowThickness, this.ClientRectangle.Right - 1, this.ClientRectangle.Bottom - 1);
frameRec = Rectangle.FromLTRB(0, 0, this.ClientRectangle.Right - DropShadowThickness - 2, this.ClientRectangle.Bottom - DropShadowThickness - 2);
}
else
{
frameRec = Rectangle.FromLTRB(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Right - 1, this.ClientRectangle.Bottom - 1);
}
if (frameRec.Height <= 0 | frameRec.Width <= 0)
return;
//draw the drop shadow using the shadow path rectangle
if (DropShadowVisible)
{
using (GraphicsPath path = GraphicsFunctions.RoundRectangle(shadowRec, _CornerRadius, _Corners))
{
using (SolidBrush b = new SolidBrush(_ShadowColor))
{
e.Graphics.FillPath(b, path);
}
}
}
//draw the rest of the control in the main frame path
using (GraphicsPath path = GraphicsFunctions.RoundRectangle(frameRec, _CornerRadius, _Corners))
{
//paint the background inside the frame
if (BackgroundGradientMode == ControlGradientMode.None)
{
//solid background
using (SolidBrush b = new SolidBrush(BackgroundColor))
{
e.Graphics.FillPath(b, path);
}
}
else
{
//gradient
if (BackgroundGradientMode == ControlGradientMode.VerticalGlow)
{
using (LinearGradientBrush b = new LinearGradientBrush(new Point(0, frameRec.Top + (frameRec.Height / 2)), new Point(0, frameRec.Bottom - 1), BackgroundColorGradient, BackgroundColor))
{
b.WrapMode = WrapMode.TileFlipXY;
e.Graphics.FillPath(b, path);
}
}
else if (BackgroundGradientMode == ControlGradientMode.HorizontalGlow)
{
using (LinearGradientBrush b = new LinearGradientBrush(new Point(frameRec.Left + (frameRec.Width / 2), 0), new Point(frameRec.Right - 1, 0), BackgroundColorGradient, BackgroundColor))
{
b.WrapMode = WrapMode.TileFlipXY;
e.Graphics.FillPath(b, path);
}
}
else
{
using (LinearGradientBrush br = new LinearGradientBrush(frameRec, BackgroundColor, BackgroundColorGradient, (LinearGradientMode)BackgroundGradientMode))
{
e.Graphics.FillPath(br, path);
}
}
//
using (SolidBrush b = new SolidBrush(ForeColor))
{
StringFormat sf = new StringFormat();
//Rectangle textRec = default(Rectangle);
switch (this.TextAlign)
{
case ContentAlignment.BottomCenter:
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomLeft:
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomRight:
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.MiddleCenter:
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleLeft:
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleRight:
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.TopCenter:
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopLeft:
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopRight:
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Near;
break;
}
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(this.Text, this.Font, b, (RectangleF)frameRec, sf);
Invalidate();
}
Invalidate();
}
e.Graphics.ResetTransform();
//draw the border
using (Pen p = new Pen(BorderColor, BorderWidth))
{
e.Graphics.DrawPath(p, path);
}
}
}
}
它依赖于此:
[Flags()]
public enum RoundedControlCorners
{
None = 0,
NorthWest = 2,
NorthEast = 4,
SouthEast = 8,
SouthWest = 16,
All = NorthWest | NorthEast | SouthEast | SouthWest,
North = NorthWest | NorthEast,
South = SouthEast | SouthWest,
East = NorthEast | SouthEast,
West = NorthWest | SouthWest
}
public enum ControlGradientMode
{
/// <summary>Specifies no gradient mode.</summary>
None = 4,
/// <summary>Specifies a gradient from upper right to lower left.</summary>
BackwardDiagonal = LinearGradientMode.BackwardDiagonal,
/// <summary>Specifies a gradient from upper left to lower right.</summary>
ForwardDiagonal = LinearGradientMode.ForwardDiagonal,
/// <summary>Specifies a gradient from left to right.</summary>
Horizontal = LinearGradientMode.Horizontal,
/// <summary>Specifies a gradient from top to bottom.</summary>
Vertical = LinearGradientMode.Vertical,
VerticalGlow = 5,
HorizontalGlow = 6
}
public static class GraphicsFunctions
{
public static GraphicsPath RoundRectangle(Rectangle r, int radius, RoundedControlCorners corners)
{
GraphicsPath path = new GraphicsPath();
if (r.Width <= 0 | r.Height <= 0)
return path;
int d = radius * 2;
int nw = ((corners & RoundedControlCorners.NorthWest) == RoundedControlCorners.NorthWest ? d : 0);
int ne = ((corners & RoundedControlCorners.NorthEast) == RoundedControlCorners.NorthEast ? d : 0);
int se = ((corners & RoundedControlCorners.SouthEast) == RoundedControlCorners.SouthEast ? d : 0);
int sw = ((corners & RoundedControlCorners.SouthWest) == RoundedControlCorners.SouthWest ? d : 0);
//path.AddLine(r.Left + nw, r.Top, r.Right - ne, r.Top);
path.AddLine(r.Left + nw, r.Top, r.Right - ne, r.Top);
if (ne > 0)
{
path.AddArc(Rectangle.FromLTRB(r.Right - ne, r.Top, r.Right, r.Top + ne), -90, 90);
}
path.AddLine(r.Right, r.Top + ne, r.Right, r.Bottom - se);
if (se > 0)
{
path.AddArc(Rectangle.FromLTRB(r.Right - se, r.Bottom - se, r.Right, r.Bottom), 0, 90);
}
path.AddLine(r.Right - se, r.Bottom, r.Left + sw, r.Bottom);
if (sw > 0)
{
path.AddArc(Rectangle.FromLTRB(r.Left, r.Bottom - sw, r.Left + sw, r.Bottom), 90, 90);
}
path.AddLine(r.Left, r.Bottom - sw, r.Left, r.Top + nw);
if (nw > 0)
{
path.AddArc(Rectangle.FromLTRB(r.Left, r.Top, r.Left + nw, r.Top + nw), 180, 90);
}
path.CloseFigure();
return path;
}
}
我用来将其添加到表单的代码:
LabelEx oLabel1 = new LabelEx();
LabelEx oLabel2 = new LabelEx();
LabelEx oLabel3 = new LabelEx();
oLabel1.Top = 50;
oLabel2.Top = 100;
oLabel3.Top = 150;
oLabel1.Height = 25;
oLabel2.Height = 25;
oLabel1.Text = "Hello";
oLabel2.Text = "There";
scBase.Panel2.Controls.Add(oLabel1);
scBase.Panel2.Controls.Add(oLabel2);
最佳答案
protected override void OnPaint(PaintEventArgs e)
{
//...
Invalidate();
}
对于任何程序员来说,一个好的做法是始终运行任务管理器。我总是将它最小化到通知区域。鸟瞰我的机器上发生的事情,快速点击激活它。
那很快就确定了你的程序出了什么问题,它正在燃烧 100% 的核心。那是因为您在 OnPaint() 方法中调用了 Invalidate(),立即使您绘制的内容无效。这有几个副作用,其中之一是您会看到您的程序永远不会闲置并继续在处理器内核上运行代码。因为 Windows 不断地一遍又一遍地生成绘制事件。
另一方面,只有一个标签控件可以被绘制。第一个,Z 顺序中最低的。因为在完成绘制后,由于 Invalidate() 调用,下一次绘制请求再次针对同一标签。
删除 Invalidate() 调用以解决您的问题,有两个。
关于c# - 自定义控件 - 首先绘制 OK,第二个不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629288/
好的,所以我编辑了以下... 只需将以下内容放入我的 custom.css #rt-utility .rt-block {CODE HERE} 但是当我尝试改变... 与 #rt-sideslid
在表格 View 中,我有一个自定义单元格(在界面生成器中高度为 500)。在该单元格中,我有一个 Collection View ,我按 (10,10,10,10) 固定到边缘。但是在 tablev
对于我的无能,我很抱歉,但总的来说,我对 Cocoa、Swift 和面向对象编程还很陌生。我的主要来源是《Cocoa Programming for OS X》(第 5 版),以及 Apple 的充满
我正在使用 meta-tegra 为我的 NVIDIA Jetson Nano 构建自定义图像。我需要 PyTorch,但没有它的配方。我在设备上构建了 PyTorch,并将其打包到设备上的轮子中。现
在 jquery 中使用 $.POST 和 $.GET 时,有没有办法将自定义变量添加到 URL 并发送它们?我尝试了以下方法: $.ajax({type:"POST", url:"file.php?
Traefik 已经默认实现了很多中间件,可以满足大部分我们日常的需求,但是在实际工作中,用户仍然还是有自定义中间件的需求,为解决这个问题,官方推出了一个 Traefik Pilot[1] 的功
我想让我的 CustomTextInputLayout 将 Widget.MaterialComponents.TextInputLayout.OutlinedBox 作为默认样式,无需在 XML 中
我在 ~/.emacs 中有以下自定义函数: (defun xi-rgrep (term) (grep-compute-defaults) (interactive "sSearch Te
我有下表: 考虑到每个月的权重,我的目标是在 5 个月内分散 10,000 个单位。与 10,000 相邻的行是我最好的尝试(我在这上面花了几个小时)。黄色是我所追求的。 我试图用来计算的逻辑如下:计
我的表单中有一个字段,它是文件类型。当用户点击保存图标时,我想自然地将文件上传到服务器并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。 我希望该用户名是自定义用户名,而不是整个电子邮件地址。 最佳答案 发件人地址应该使用的格式是: You
我想减小 ggcorrplot 中标记的大小,并减少文本和绘图之间的空间。 library(ggcorrplot) data(mtcars) corr <- round(cor(mtcars), 1)
GTK+ noob 问题在这里: 是否可以自定义 GtkFileChooserButton 或 GtkFileChooserDialog 以删除“位置”部分(左侧)和顶部的“位置”输入框? 我实际上要
我正在尝试在主页上使用 ajax 在 magento 中使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是将分页工具栏与结果集一起添加. 这是我添加的以显示流行产
我正在尝试使用 PasswordResetForm 内置函数。 由于我想要自定义表单字段,因此我编写了自己的表单: class FpasswordForm(PasswordResetForm):
据我了解,新的 Angular 7 提供了拖放功能。我搜索了有关 DnD 的 Tree 组件,但没有找到与树相关的内容。 我在 Stackblitz 上找到的一个工作示例.对比drag'ndrop功能
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。 以下是我的表的外观示例: my.table <- data.frame(Specif
用ejs在这里显示日期 它给我结果 Tue Feb 02 2016 16:02:24 GMT+0530 (IST) 但是我需要表现为 19th January, 2016 如何在ejs中执行此操作?
我想问在 JavaFX 中使用自定义对象制作 ListView 的最佳方法,我想要一个每个项目如下所示的列表: 我搜了一下,发现大部分人都是用细胞工厂的方法来做的。有没有其他办法?例如使用客户 fxm
我是一名优秀的程序员,十分优秀!