我目前正試圖覆寫我正在構建的一個自定義控制元件的OnPaint()方法。
以下是我為重新創建圖1而撰寫的代碼:
private GraphicsPath GetFigurePath(RectangleF rect)
{
GraphicsPath path = new GraphicsPath()。
Point TopLeft = new Point((int)rect.X, (int)rect.Y) 。
Point TopRight = new Point((int)rect.X (int)rect.Width, (int)rect.Height) 。
Point BottomLeft = new Point((int)rect.X, (int)rect.Y (int)rect.Height) 。
Point BottomRight = new Point((int)rect. X (int)rect.Width, (int)rect.Y (int)rect.Height) 。
Point MidPoint = new Point((TopLeft.X BottomLeft.X) / 2, (TopLeft.Y BottomLeft.Y) / 2) 。
Point Fulcrum = new Point((int)MidPoint.X (int)rect.Width, MidPoint.Y) 。
path.StartFigure()。
//the rectangle; path.StartFigure(;)
path.AddLine(TopLeft, TopRight);
path.AddLine(TopRight, BottomRight);
path.AddLine(BottomRight, BottomLeft);
path.AddLine(BottomLeft, TopLeft)。
//尖銳的一端。
path.AddLine(TopRight, Fulcrum);
path.AddLine(Fulcrum, BottomRight);
path.CloseFigure()。
return path。
}
protected override void OnPaint(PaintEventArgs e)。
{
if (HasBorderStyle) {
base.OnPaint(e)。
this.FlatStyle = FlatStyle.Flat;
this.Size = new Size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.BackColor = Color.Silver;
this.ForeColor = Color.White;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
RectangleF rectSurface = new RectangleF(0, 0, this. DEFAULT_WIDTH, this.DEFAULT_HEIGHT)。)
RectangleF rectBorder = new RectangleF(1, 1, this. DEFAULT_WIDTH - 0.8f, this.DEFAULT_HEIGHT - 1)。)
使用(GraphicsPath pathSurface = GetFigurePath(rectSurface))。
using (GraphicsPath pathBorder = GetFigurePath(rectBorder))
使用 (Pen penSurface = new Pen(this.Parent.BackColor, 2)
使用(Pen penBorder = new Pen(borderColour, borderSize)) {
penBorder.Alignment = PenAlignment.Inset;
this.Region = new Region(pathSurface);
e.Graphics.DrawPath(penSurface, pathSurface)。
e.Graphics.DrawPath(penBorder, pathBorder)。
}
}
}
誰能告訴我,我錯過了什么或者我做錯了什么?
uj5u.com熱心網友回復:
的幾個指標和一個例子。而在運行時:
還請看:
如何避免帶圓角的可縮放UserControl的彩色邊框的視覺偽影?
如何繪制圓角矩形作為圓角表單的邊界?
用PathGradientBrush創建的陰影顯示出不希望出現的荊棘結果
自定義控制元件:
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
[ToolboxItem(true)]
[DesignerCategory("code")]
public class NavigationShape : Control
{
private Color m_ArrowColor = Color.SteelBlue;
private Color m_BorderColor = Color.Orange;
private float m_BorderSize = 1.5f;
private bool m_BorderVisible = true;
public NavigationShape() /span> {
SetStyle(ControlStyles.OptimizedDoubleBuffer | 風格)
ControlStyles.AllPaintingInWmPaint | 繪畫風格
ControlStyles.ResizeRedraw, true)。)
MinimumSize = new Size(40, 20)。
}
protected override void OnResize(EventArgs e)?
{
base.OnResize(e)。
Region = new Region(GetRegionPath())。
}
private GraphicsPath GetRegionPath()
{
// 箭頭形狀開始于3/4或容器的當前寬度。
float arrowSection = ClientSize.Width * .75f;
PointF[] arrowPoints = new PointF[] {
new PointF(0, 0)。
new PointF (arrowSection, 0) 。
new PointF(ClientSize.Width, ClientSize.Height / 2.0f) 。
new PointF(arrowSection, ClientSize.Height)。
new PointF (0, ClientSize.Height) 。
new PointF (0, 0)
};
var path = new GraphicsPath()。
path.AddLines(arrowPoints);
path.CloseFigure()。
return path。
}
protected override void OnPaint(PaintEventArgs e)。
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias。
float border = m_BorderVisible ? m_BorderSize : .5f;
using (var path = GetRegionPath() ) {
var rect = path.GetBounds();
float scaleX = 1.0f - ((border * 2.5f) / rect.Width) 。
float scaleY = 1.0f - ((border * 2.0f) / rect.Height) 。
使用(var mx = new Matrix(scaleX, 0, 0, scaleY, border, border)
using (var brush = new SolidBrush(m_ArrowColor)) {
path.Transform(mx);
e.Graphics.FillPath(brush, path)。
if (m_BorderVisible) {
using (Pen pen = new Pen(m_BorderColor, m_BorderSize)) {
e.Graphics.DrawPath(pen, path)。
}
}
}
}
base.OnPaint(e)。
}
[DefaultValue(typeof(Color), "SteelBlue"/span>)]
[Description("形狀的顏色")]
public Color ArrowColor {
get => m_ArrowColor;
set {
if (m_ArrowColor != value) {
m_ArrowColor = value;
Invalidate();
}
}
}
[DefaultValue(true)] 。
[Description("Show or hide the Border")]
public bool BorderVisible {
get => m_BorderVisible;
set {
m_BorderVisible = value;
Invalidate()。
}
}
[DefaultValue(typeof(Color), "Orange"/span>)]
[Description("邊界的顏色")]
public Color BorderColor {
get => m_BorderColor;
set {
if (m_BorderColor != value) {
m_BorderColor = value;
Invalidate();
}
}
}
[DefaultValue(1.5f)] 。
[Description("邊界的大小[1.0, 6.0]")]
public float BorderSize {
get => m_BorderSize;
set {
if (m_BorderSize != value) {
m_BorderSize = Math.Max(Math.Min(value, 6.0f), 1.0f) 。
Invalidate()。
}
}
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public BorderStyle borderStyle{ get; set; } // Implement if needed; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/309689.html
標籤:




