在繪畫事件中,因為我希望能夠控制點大小顏色和更多屬性。
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
public LoadingLabel()
{
InitializeComponent();
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
Thread.Sleep(1);
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 0, 0);
Thread.Sleep(1);
}
}
我首先嘗試制作一個簡單的點,一段時間后消失,然后再次顯示,但它不起作用我看到一個紅色的點(點)。
稍后當這將起作用時,我想讓 3 個點像加載影片一樣影片。
這是我嘗試過的:
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private bool animate = false;
public LoadingLabel()
{
InitializeComponent();
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (animate == false)
{
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
}
else
{
e.Graphics.FillEllipse(Brushes.Red, 5, 1, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count ;
if(count == 10 && animate == false)
{
animate = true;
}
if(count == 20 && animate)
{
animate = false;
count = 0;
}
this.Invalidate();
}
}
結果是第一次抽簽,然后是第二次抽簽,但第一次抽簽消失了:
看起來該點正在向右移動,然后又向左移動。

但我想要一個 3 點的加載效果。而不是移動點。
這適用于 3 點,但對于 3 點來說看起來太復雜了。如果我想要100分?
也許我應該在繪畫事件中使用回圈?
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private int numofpoints = 0;
public LoadingLabel()
{
InitializeComponent();
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if(numofpoints == 0)
{
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
}
if(numofpoints == 1)
{
e.Graphics.FillEllipse(Brushes.Red, 5, 1, 20, 20);
}
if(numofpoints == 2)
{
e.Graphics.FillEllipse(Brushes.Red, 10, 1, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count ;
if(count == 10)
{
numofpoints = 0;
}
if(count == 20)
{
numofpoints = 1;
}
if(count == 30)
{
numofpoints = 2;
count = 0;
}
this.Invalidate();
}
}
我嘗試過的另一個更新:
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private List<PointF> points = new List<PointF>();
public LoadingLabel()
{
InitializeComponent();
points.Add(new PointF(0, 0));
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int i = 0; i < points.Count; i )
{
e.Graphics.FillEllipse(Brushes.Red, points[i].X, points[i].Y, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count ;
if (count < 3)
{
points.Add(new PointF(count * 20, 0));
//points = new List<PointF>();
}
//this.Invalidate();
}
}
如果我將在滴答事件中創建實體,它將不會繪制任何東西。如果我將使用無效線,它將使點像閃爍。我想要的是創建一個加載效果影片。
現在代碼的結果仍然是 3 分,我想像在鏈接中那樣為它們設定影片。

像這樣的東西:

uj5u.com熱心網友回復:
因為,根據您發布的影像,您希望為一系列點設定影片,其中只有活動的點會改變顏色,所以您的 UserControl 可以定義允許指定點數、點的顏色和點的顏色的屬性活動點。
計時器可用于更改當前活動的點,因此繪制程式知道何時更改其中一個點的顏色。
當指定的點數發生變化時,UserControl 會自動調整大小。
此外,當第一次創建 UserControl 時,它會設定它的MinimumSize,因此 Dots 始終可見。
您可以擴展此模板,添加更多功能。
請注意 UserControl 的建構式中的這些行:
components = new Container();
dotsTimer = new Timer(components) { ... };
這指示 Timer 組件將自身添加到 Parent 容器的組件中,因此當 Parent 被釋放時,Timer 也被釋放并洗掉其事件處理程式。
設定DoubleBuffered = true;避免在繪制點時閃爍。
呼叫Start()方法來啟動影片和Stop()方法來停止它。
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl {
private int m_NumberOfDots = 5;
private Color m_DotColor = Color.Cyan;
private Color m_DotActiveColor = Color.Blue;
private float dotSize = 20.0f;
private float dotSpacing = 20.0f;
private int currentDot = 0;
private Timer dotsTimer = null;
public LoadingLabel()
{
InitializeComponent();
components = new Container();
dotsTimer = new Timer(components) { Interval = 200 };
dotsTimer.Tick = DotsTimer_Tick;
DoubleBuffered = true;
Padding = new Padding(5);
}
[DefaultValue(5)]
public int NumberOfDots {
get => m_NumberOfDots;
set {
value = Math.Max(3, Math.Min(value, 7));
if (m_NumberOfDots != value) {
m_NumberOfDots = value;
bool running = dotsTimer.Enabled;
Stop();
SetMinSize();
if (running) Start();
}
}
}
[DefaultValue(typeof(Color), "Cyan")]
public Color DotColor {
get => m_DotColor;
set {
m_DotColor = value;
Invalidate();
}
}
[DefaultValue(typeof(Color), "Blue")]
public Color DotActiveColor {
get => m_DotActiveColor;
set {
m_DotActiveColor = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int dot = 0; dot < m_NumberOfDots; dot ) {
var color = dot == currentDot ? DotActiveColor : DotColor;
var pos = Padding.Left (dotSize dotSpacing) * dot;
using (var brush = new SolidBrush(color)) {
e.Graphics.FillEllipse(brush, pos, Padding.Top, dotSize, dotSize);
}
}
base.OnPaint(e);
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
SetMinSize();
}
protected override void OnHandleDestroyed(EventArgs e) {
Stop();
base.OnHandleDestroyed(e);
}
private void DotsTimer_Tick(object sender, EventArgs e) {
currentDot = 1;
currentDot %= m_NumberOfDots;
Invalidate();
}
public void Start() => dotsTimer.Start();
public void Stop() {
dotsTimer.Stop();
currentDot = 0;
Invalidate();
}
private void SetMinSize() {
var width = Padding.Left Padding.Right
(dotSize * m_NumberOfDots) (dotSpacing * (m_NumberOfDots - 1)) 1;
var height = Padding.Top Padding.Bottom dotSize 1;
MinimumSize = new Size((int)width, (int)height);
Size = MinimumSize;
}
}
這是它的作業原理:

根據需要,這是用于選擇顏色的自定義 ComboBox 控制元件的 PasteBin 。
uj5u.com熱心網友回復:
對 Paint 方法/事件的單個呼叫應該繪制控制元件,因為它應該在那個瞬間查看。如果你想添加影片,你應該讓控制元件反復重繪自己,并使用一些內部狀態來跟蹤影片。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510003.html
標籤:C#。网表格图形用户控制
上一篇:按列寬自動拉伸ListView行
