C# 快速手機“充電“動態圖(WinForm)

2024年2月6日 22点热度 0人点赞

變量與時間控件:

 private Color emitterColor = Color.FromArgb(255, 0, 0); // 發射器顏色
 private Color ringColor = Color.FromArgb(0, 0, 255); // 圓環顏色
 private Color bubbleColor = Color.FromArgb(255, 0, 255); // 氣泡顏色
 private int batteryPercentage = 0; // 電量百分比
 // 在類的成員變量中定義一個用於保存旋轉角度的變量
 private double rotationAngle = 0;
private void timer1_Tick(object sender, EventArgs e)
{          
    // 電量百分比隨時間變化
    rotationAngle = (rotationAngle   5) % 360; // 每次更新角度增加5度,控制旋轉速度
    this.batteryPercentage = (this.batteryPercentage   1) % 101;
    this.Invalidate(); // 重新繪制窗體
}

繪制手機動態圖:

 protected override void OnPaint(PaintEventArgs e)
 {
     base.OnPaint(e);
     Graphics g = e.Graphics;
     // 畫出發射器
     int emitterX = this.ClientSize.Width / 2;
     int emitterY = this.ClientSize.Height - 50;
     Point[] emitterPoints = {
     new Point(emitterX - 60, emitterY),
     new Point(emitterX, emitterY - 50),
     new Point(emitterX   60, emitterY)
 };
     g.FillClosedCurve(new SolidBrush(emitterColor), emitterPoints);
     // 畫出圓環
     int ringRadius = 200;
     int ringCenterX = this.ClientSize.Width / 2;
     int ringCenterY = this.ClientSize.Height / 2;
     for (int i = 0; i < 360; i  = 30)
     {
         double angle1 = (i   rotationAngle) * Math.PI / 180;
         double angle2 = (i   30   rotationAngle) * Math.PI / 180;
         Point[] ringPoints = {
     new Point((int)(ringCenterX   ringRadius * Math.Cos(angle1)), (int)(ringCenterY   ringRadius * Math.Sin(angle1))),
     new Point(ringCenterX, ringCenterY),
     new Point((int)(ringCenterX   ringRadius * Math.Cos(angle2)), (int)(ringCenterY   ringRadius * Math.Sin(angle2)))
 };
         g.FillClosedCurve(new SolidBrush(Color.FromArgb(150, ringColor)), ringPoints);
     }          
     // 畫出氣泡
     Random random = new Random();
     int numBubbles = 18;
     for (int i = 0; i < numBubbles; i  )
     {
         int bubbleX = random.Next(emitterX - 30, emitterX   30);
         int bubbleY = emitterY - 50;
         int bubbleRadius = 15;
         int bubbleSpeed = 2;
         int bubbleDistanceToRing = 100;
         // 計算氣泡的半徑和位置
         double bubbleRadiusMultiplier = 1 - (double)i / numBubbles; // 根據氣泡序號計算半徑變化
         bubbleRadius = (int)(bubbleRadius * bubbleRadiusMultiplier);
         bubbleY -= i * 30; // 根據氣泡序號計算位置
         // 繪制氣泡
         g.FillEllipse(new SolidBrush(bubbleColor), bubbleX - bubbleRadius, bubbleY - bubbleRadius, bubbleRadius * 2, bubbleRadius * 2);
         // 如果氣泡靠近圓環,添加黏性變化效果
         int distanceToCenter = (int)Math.Sqrt(Math.Pow(bubbleX - ringCenterX, 2)   Math.Pow(bubbleY - ringCenterY, 2));
         if (distanceToCenter < bubbleDistanceToRing)
         {
             int numPoints = 10;
             Point[] stickyPoints = new Point[numPoints];
             for (int j = 0; j < numPoints; j  )
             {
                 int x = bubbleX   (int)(j * (ringCenterX - bubbleX) / numPoints);
                 int y = bubbleY   (int)(j * (ringCenterY - bubbleY) / numPoints);
                 stickyPoints[j] = new Point(x, y);
             }
             g.DrawCurve(new Pen(bubbleColor, 2), stickyPoints);
         }
     }
     // 畫出閃電圖案
     int lightningX = this.ClientSize.Width / 2 - 30;
     int lightningY = this.ClientSize.Height / 2 - 20;
     Point[] lightningPoints = {
     new Point(lightningX, lightningY),
     new Point(lightningX   20, lightningY - 40),
     new Point(lightningX   10, lightningY - 40),
     new Point(lightningX   30, lightningY - 80),
     new Point(lightningX   20, lightningY - 80),
     //new Point(lightningX   30, lightningY - 100),
 };
     g.DrawLines(new Pen(Color.Red, 18), lightningPoints);
     // 顯示正在充電的字體百分比
     string chargingText = "正在充電";
     Font font = new Font("Arial", 22);
     SizeF textSize = g.MeasureString(chargingText, font);
     g.DrawString(chargingText, font, new SolidBrush(Color.White), (this.ClientSize.Width - textSize.Width) / 2, lightningY   30);
     string percentageText = batteryPercentage   "%";
     Font percentageFont = new Font("Arial", 50, FontStyle.Bold);
     SizeF percentageSize = g.MeasureString(percentageText, percentageFont);
     g.DrawString(percentageText, percentageFont, new SolidBrush(Color.Yellow), (this.ClientSize.Width - percentageSize.Width) / 2, lightningY   60);
 }

#冬日生活打卡季#