我创建了一个自定义类,它使用了一个继承的面板,一个图片框和两个标签,用作"图标"在表单上供用户在整个应用程序中单击和导航。我这样做是因为它提供了更好的图形印象,并且比单独一个按钮更具可定制性。
我已经包含了一个类来从以下codeproject中绘制圆角矩形(它们在面板上绘制以使面板看起来像圆角): http://www.codeproject.com/Articles/5649/Extended-Graphics-An-implementation-of-Rounded-Rec
关于绘画,我已连接我的MouseEnter和MouseLeave事件以切换布尔值,然后调用me.invalidate()以根据事件设置的布尔值重绘面板背景。我重绘的代码如下:
If mouseoverBool Then 'class variable mouseoverBool set by mouseenter/mouseleave to true/false
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
Dim brush As New LinearGradientBrush(New Point(Me.Width / 2, 0), New Point(Me.Width / 2, Me.Height), Color.LightSteelBlue, Color.LightBlue)
g.FillRoundedRectangle(brush, 0, 0, Me.Width - 1, Me.Height, 10)
g.FillRoundedRectangle(New SolidBrush(Color.FromArgb(100, 118, 173, 218)), 0, 0, Me.Width, Me.Height, 10)
g.DrawRoundedRectangle(New Pen(ControlPaint.Light(SystemColors.InactiveBorder, 0.0F)), 0, 0, Me.Width - 1, Me.Height - 1, 10)
Else
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
Dim brush As New LinearGradientBrush(New Point(Me.Width / 2, 0), New Point(Me.Width / 2, Me.Height), SystemColors.GradientInactiveCaption, ControlPaint.Dark(SystemColors.GradientActiveCaption, 0.5F))
g.FillRoundedRectangle(brush, 0, 0, Me.Width - 1, Me.Height, 10)
g.FillRoundedRectangle(New SolidBrush(Color.FromArgb(100, 118, 173, 218)), 0, 0, Me.Width, Me.Height, 10)
g.DrawRoundedRectangle(New Pen(ControlPaint.Light(SystemColors.InactiveBorder, 0.0F)), 0, 0, Me.Width - 1, Me.Height - 1, 10)
End If
或者在C#中:
if (mouseoverBool) {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(new Point(this.Width / 2, 0), new Point(this.Width / 2, this.Height), Color.LightSteelBlue, Color.LightBlue);
g.FillRoundedRectangle(brush, 0, 0, this.Width - 1, this.Height, 10);
g.FillRoundedRectangle(new SolidBrush(Color.FromArgb(100, 118, 173, 218)), 0, 0, this.Width, this.Height, 10);
g.DrawRoundedRectangle(new Pen(ControlPaint.Light(SystemColors.InactiveBorder, 0f)), 0, 0, this.Width - 1, this.Height - 1, 10);
} else {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(new Point(this.Width / 2, 0), new Point(this.Width / 2, this.Height), SystemColors.GradientInactiveCaption, ControlPaint.Dark(SystemColors.GradientActiveCaption, 0.5f));
g.FillRoundedRectangle(brush, 0, 0, this.Width - 1, this.Height, 10);
g.FillRoundedRectangle(new SolidBrush(Color.FromArgb(100, 118, 173, 218)), 0, 0, this.Width, this.Height, 10);
g.DrawRoundedRectangle(new Pen(ControlPaint.Light(SystemColors.InactiveBorder, 0f)), 0, 0, this.Width - 1, this.Height - 1, 10);
}
出现的问题是面板内的标签和图片框(所有控件设置为透明背景)在第一次自定义" icon"面板绘制背景。小组描绘,然后半秒钟后,标签和图片框上的背景最终更新。我假设这是因为调用.invalidate()强制自定义面板重绘,然后随后是所有控件。我正在追求一个无缝的'油漆整个控件一次涂料,包括标签和图片框。我该如何尝试实现这一目标?
答案 0 :(得分:0)
我在自定义面板中使用了2个标签和一个图片框来显示文字和图片。正如评论中指出的那样,您可以轻松地自己绘制这些项目(这不仅更快,而且更有意义,因为您必须通过首先使用控件来处理所有控件mouseEnter / mouseLeave事件),这会停止闪烁来自发生。
由于我已经创建了用于保存图像和标签文本的属性(与之前的控件一起使用),因此很容易将其转换为自定义面板的绘制事件。
e.Graphics.DrawString(Me.txtHeaderDescription, New Font("Calibri", 12, FontStyle.Italic), Brushes.White, New Point(105, 15))
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Near
Dim Rectf As RectangleF = New Rectangle(105, 35, 200, 75)
e.Graphics.DrawString(Me.txtDescription, New Font("Calibri", 8, FontStyle.Regular), Brushes.White, Rectf, sf)
e.Graphics.DrawImage(Me.MyImage, 5, 3, 96, 96)