【发布时间】:2015-01-04 11:11:23
【问题描述】:
我创建了一个可以根据实时正常工作的模拟时钟。
我不希望您为我或任何其他人解决它 - 我的代码已经在运行,但它在严重闪烁,并且我的知识有限,我无法确定我需要更改什么才能停止闪烁。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClockAndStuff
{
public partial class TheName : Form
{
public TheName()
{
InitializeComponent();
timer1.Start();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics surfaceBckg;
Graphics surfaceMin;
Graphics surfaceH;
Graphics surfaceSec;
Pen penMin = new Pen(Color.Pink,2);
Pen penH = new Pen(Color.Red,3);
Pen penSec = new Pen(Color.DeepPink,1);
Pen black = new Pen(Color.Black, 4F);
surfaceBckg = panel1.CreateGraphics();
surfaceBckg.TranslateTransform(250, 250);
surfaceBckg.DrawEllipse(black,-220,-220,440,440);
surfaceMin = panel1.CreateGraphics();
surfaceMin.TranslateTransform(250, 250);
surfaceMin.RotateTransform(6 * DateTime.Now.Minute);
surfaceH = panel1.CreateGraphics();
surfaceH.TranslateTransform(250, 250);
surfaceH.RotateTransform(30 * DateTime.Now.Hour);
surfaceSec = panel1.CreateGraphics();
surfaceSec.TranslateTransform(250, 250);
surfaceSec.RotateTransform(6 * DateTime.Now.Second);
surfaceMin.DrawLine(penMin, 0, 0, 0, -200);
surfaceH.DrawLine(penH, 0, 0, 0, -120);
surfaceSec.DrawLine(penSec, 0, 0, 1, -180);
}
private void timer1_Tick(object sender, EventArgs e)
{
panel1.Refresh();
}
}
}
根据我的任务是正确的,但我不禁认为必须有更好的方法来做到这一点。
我对 C# 还不是很了解,所以如果你能给我代码示例作为答案,那么我将能够理解它的可能性更大......
【问题讨论】:
-
timer_tick 多久被调用一次?
-
@Grundy 我已经看到了所有这些链接,但我根本无法确定如何使用它们来解决我的问题,这就是我再次询问 with 的原因我的问题的细节
-
如果您无法弄清楚副本的解决方案与您的解决方案之间的区别:删除 all 对
.CreateGraphics()的调用并改用e.Graphics。阅读副本以获得解释。 -
你没有遵循重复的解决方案并且你没有指出什么你不明白。我的评论包括一个非常简单的一句话解决方案。如果您仍然不知道该怎么做,我不知道如何更好地解释它。