Anonymous
В моих аналоговых часах C# Winforms Как исправить линию секунды, сделав первое время назад, а затем продолжить вперед пр
Сообщение
Anonymous » 11 июн 2025, 01:25
Это происходит, когда я меняю состояние флажкового ящика на режим прыжков, затем линии секунды впервые сделают один шаг назад, затем продолжаю прыгать каждую секунду вперед, как и должно быть. Затем, если я переключаюсь больше раз между режимом прыжка и плавным режимом, когда я переключаюсь в режим прыжка, иногда существует небольшой обратный шаг, то он продолжает вперед. Таким образом, один большой один раз назад шаг после запуска приложения и перехода на режим прыжка, а иногда и небольшой, но также один обратный прыжок при снова переключающемся между состояниями.
Код: Выделить всё
private void checkBoxStep_CheckedChanged(object sender, EventArgs e)
{
checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode";
if (!checkBoxStep.Checked)
{
// Switching to Smooth Mode → record current angles
SetSmoothStartFromCurrentAngles();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
if (checkBoxStep.Checked)
{
SetAnglesFromSystemTime();
}
else
{
TimeSpan elapsed = DateTime.Now - smoothStartTime;
angleSeconds = (smoothStartAngleSec + 6.0 * elapsed.TotalSeconds) % 360.0;
if (angleSeconds < 0) angleSeconds += 360.0;
angleMinutes = (smoothStartAngleMin + 0.1 * elapsed.TotalSeconds) % 360.0;
if (angleMinutes < 0) angleMinutes += 360.0;
angleHours = (smoothStartAngleHour + (1.0 / 120) * elapsed.TotalSeconds) % 360.0;
if (angleHours < 0) angleHours += 360.0;
}
this.Invalidate();
}
private void SetAnglesFromSystemTime()
{
DateTime now = DateTime.Now;
angleSeconds = now.Second * 6.0;
angleMinutes = (now.Minute + now.Second / 60.0) * 6.0;
angleHours = ((now.Hour % 12) + now.Minute / 60.0) * 30.0;
}
private void SetSmoothStartFromCurrentAngles()
{
smoothStartTime = DateTime.Now;
smoothStartAngleSec = angleSeconds;
smoothStartAngleMin = angleMinutes;
smoothStartAngleHour = angleHours;
}
< /code>
и заполненный код для получения дополнительной прояснения. < /p>
Использование System;
с использованием system.drawing;
с использованием system.windows.forms; < /p>
form
privatial class form1: form
privatial class 1: {br /> privatial class1: {br /> privatial class
{br /> privatial class
{br /> privatial class
{br /> privatial class
{br /> privatial class
{br /> privatial class
/> Private Double Angleseconds = 0;
Private Double Angleminutes = 0;
Private Double AngleHours = 0; < /p>
// For smooth mode
private double smoothStartAngleSec = 0, smoothStartAngleMin = 0, smoothStartAngleHour = 0;
private DateTime smoothStartTime;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.ResizeRedraw = true;
checkBoxShowTicks.Checked = true;
checkBoxShowTicks.CheckedChanged += (s, e) =>
{
checkBoxShowTicks.Text = checkBoxShowTicks.Checked ? "Show Small Ticks" : "Hide Small Ticks";
this.Invalidate();
};
// Make sure you have your Step/Smooth mode checkbox
checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode";
checkBoxStep.CheckedChanged += checkBoxStep_CheckedChanged;
timer = new Timer();
timer.Interval = 1000 / 60; // 60 FPS
timer.Tick += Timer_Tick;
timer.Start();
SetAnglesFromSystemTime();
SetSmoothStartFromCurrentAngles();
}
private void checkBoxStep_CheckedChanged(object sender, EventArgs e)
{
checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode";
if (!checkBoxStep.Checked)
{
// Switching to Smooth Mode → record current angles
SetSmoothStartFromCurrentAngles();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
if (checkBoxStep.Checked)
{
SetAnglesFromSystemTime();
}
else
{
TimeSpan elapsed = DateTime.Now - smoothStartTime;
angleSeconds = (smoothStartAngleSec + 6.0 * elapsed.TotalSeconds) % 360.0;
if (angleSeconds < 0) angleSeconds += 360.0;
angleMinutes = (smoothStartAngleMin + 0.1 * elapsed.TotalSeconds) % 360.0;
if (angleMinutes < 0) angleMinutes += 360.0;
angleHours = (smoothStartAngleHour + (1.0 / 120) * elapsed.TotalSeconds) % 360.0;
if (angleHours < 0) angleHours += 360.0;
}
this.Invalidate();
}
private void SetAnglesFromSystemTime()
{
DateTime now = DateTime.Now;
angleSeconds = now.Second * 6.0;
angleMinutes = (now.Minute + now.Second / 60.0) * 6.0;
angleHours = ((now.Hour % 12) + now.Minute / 60.0) * 30.0;
}
private void SetSmoothStartFromCurrentAngles()
{
smoothStartTime = DateTime.Now;
smoothStartAngleSec = angleSeconds;
smoothStartAngleMin = angleMinutes;
smoothStartAngleHour = angleHours;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int w = this.ClientSize.Width;
int h = this.ClientSize.Height;
int size = Math.Min(w, h) * 3 / 4;
float radius = size / 2f;
float cx = w / 2f;
float cy = h / 2f;
// Draw outer circle
using (Pen pen = new Pen(Color.DeepSkyBlue, 3f))
{
e.Graphics.DrawEllipse(pen, cx - radius, cy - radius, 2 * radius, 2 * radius);
}
// Draw small green ticks if enabled
if (checkBoxShowTicks.Checked)
DrawSmallTicks(e.Graphics, cx, cy, radius);
// Draw hour numbers
DrawHourNumbers(e.Graphics, cx, cy, radius);
// Draw hands (hour, minute, second)
DrawHand(e.Graphics, cx, cy, radius * 0.5f, angleHours, Color.Black, 6f); // Hour hand
DrawHand(e.Graphics, cx, cy, radius * 0.7f, angleMinutes, Color.DimGray, 4f); // Minute hand
// Second hand: almost reaches green ticks, so a bit shorter than radius
DrawHand(e.Graphics, cx, cy, radius * 0.95f, angleSeconds, Color.Red, 2f); // Second hand
}
// Draw 60 green ticks, one for each minute/second
private void DrawSmallTicks(Graphics g, float cx, float cy, float radius)
{
float tickOuter = radius - 3;
float tickInner;
for (int i = 0; i < 60; ++i)
{
// Make hour ticks (multiples of 5) longer, but still green
if (i % 5 == 0)
tickInner = tickOuter - 15;
else
tickInner = tickOuter - 8;
double angle = (i * 6.0 - 90) * Math.PI / 180.0;
float x1 = cx + (float)(tickInner * Math.Cos(angle));
float y1 = cy + (float)(tickInner * Math.Sin(angle));
float x2 = cx + (float)(tickOuter * Math.Cos(angle));
float y2 = cy + (float)(tickOuter * Math.Sin(angle));
using (Pen pen = new Pen(Color.LimeGreen, i % 5 == 0 ? 3f : 2f))
{
g.DrawLine(pen, x1, y1, x2, y2);
}
}
}
private void DrawHand(Graphics g, float cx, float cy, float length, double angleDeg, Color color, float thickness)
{
double radians = (angleDeg - 90) * Math.PI / 180.0; // -90 so 0 deg is at 12 o'clock
float x2 = cx + (float)(length * Math.Cos(radians));
float y2 = cy + (float)(length * Math.Sin(radians));
using (Pen pen = new Pen(color, thickness)
{
StartCap = System.Drawing.Drawing2D.LineCap.Round,
EndCap = System.Drawing.Drawing2D.LineCap.Round
})
{
g.DrawLine(pen, cx, cy, x2, y2);
}
}
private void DrawHourNumbers(Graphics g, float cx, float cy, float radius)
{
Font font = new Font("Arial", 18, FontStyle.Bold);
for (int h = 1; h
Подробнее здесь: [url]https://stackoverflow.com/questions/79661187/in-my-c-sharp-winforms-analog-clock-how-to-fix-seconds-line-taking-first-time-ba[/url]
1749594325
Anonymous
Это происходит, когда я меняю состояние флажкового ящика на режим прыжков, затем линии секунды впервые сделают один шаг назад, затем продолжаю прыгать каждую секунду вперед, как и должно быть. Затем, если я переключаюсь больше раз между режимом прыжка и плавным режимом, когда я переключаюсь в режим прыжка, иногда существует небольшой обратный шаг, то он продолжает вперед. Таким образом, один большой один раз назад шаг после запуска приложения и перехода на режим прыжка, а иногда и небольшой, но также один обратный прыжок при снова переключающемся между состояниями.[code]private void checkBoxStep_CheckedChanged(object sender, EventArgs e) { checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode"; if (!checkBoxStep.Checked) { // Switching to Smooth Mode → record current angles SetSmoothStartFromCurrentAngles(); } } private void Timer_Tick(object sender, EventArgs e) { if (checkBoxStep.Checked) { SetAnglesFromSystemTime(); } else { TimeSpan elapsed = DateTime.Now - smoothStartTime; angleSeconds = (smoothStartAngleSec + 6.0 * elapsed.TotalSeconds) % 360.0; if (angleSeconds < 0) angleSeconds += 360.0; angleMinutes = (smoothStartAngleMin + 0.1 * elapsed.TotalSeconds) % 360.0; if (angleMinutes < 0) angleMinutes += 360.0; angleHours = (smoothStartAngleHour + (1.0 / 120) * elapsed.TotalSeconds) % 360.0; if (angleHours < 0) angleHours += 360.0; } this.Invalidate(); } private void SetAnglesFromSystemTime() { DateTime now = DateTime.Now; angleSeconds = now.Second * 6.0; angleMinutes = (now.Minute + now.Second / 60.0) * 6.0; angleHours = ((now.Hour % 12) + now.Minute / 60.0) * 30.0; } private void SetSmoothStartFromCurrentAngles() { smoothStartTime = DateTime.Now; smoothStartAngleSec = angleSeconds; smoothStartAngleMin = angleMinutes; smoothStartAngleHour = angleHours; } < /code> и заполненный код для получения дополнительной прояснения. < /p> Использование System; с использованием system.drawing; с использованием system.windows.forms; < /p> form privatial class form1: form privatial class 1: {br /> privatial class1: {br /> privatial class {br /> privatial class {br /> privatial class {br /> privatial class {br /> privatial class {br /> privatial class /> Private Double Angleseconds = 0; Private Double Angleminutes = 0; Private Double AngleHours = 0; < /p> // For smooth mode private double smoothStartAngleSec = 0, smoothStartAngleMin = 0, smoothStartAngleHour = 0; private DateTime smoothStartTime; public Form1() { InitializeComponent(); this.DoubleBuffered = true; this.ResizeRedraw = true; checkBoxShowTicks.Checked = true; checkBoxShowTicks.CheckedChanged += (s, e) => { checkBoxShowTicks.Text = checkBoxShowTicks.Checked ? "Show Small Ticks" : "Hide Small Ticks"; this.Invalidate(); }; // Make sure you have your Step/Smooth mode checkbox checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode"; checkBoxStep.CheckedChanged += checkBoxStep_CheckedChanged; timer = new Timer(); timer.Interval = 1000 / 60; // 60 FPS timer.Tick += Timer_Tick; timer.Start(); SetAnglesFromSystemTime(); SetSmoothStartFromCurrentAngles(); } private void checkBoxStep_CheckedChanged(object sender, EventArgs e) { checkBoxStep.Text = checkBoxStep.Checked ? "Jump Mode" : "Smooth Mode"; if (!checkBoxStep.Checked) { // Switching to Smooth Mode → record current angles SetSmoothStartFromCurrentAngles(); } } private void Timer_Tick(object sender, EventArgs e) { if (checkBoxStep.Checked) { SetAnglesFromSystemTime(); } else { TimeSpan elapsed = DateTime.Now - smoothStartTime; angleSeconds = (smoothStartAngleSec + 6.0 * elapsed.TotalSeconds) % 360.0; if (angleSeconds < 0) angleSeconds += 360.0; angleMinutes = (smoothStartAngleMin + 0.1 * elapsed.TotalSeconds) % 360.0; if (angleMinutes < 0) angleMinutes += 360.0; angleHours = (smoothStartAngleHour + (1.0 / 120) * elapsed.TotalSeconds) % 360.0; if (angleHours < 0) angleHours += 360.0; } this.Invalidate(); } private void SetAnglesFromSystemTime() { DateTime now = DateTime.Now; angleSeconds = now.Second * 6.0; angleMinutes = (now.Minute + now.Second / 60.0) * 6.0; angleHours = ((now.Hour % 12) + now.Minute / 60.0) * 30.0; } private void SetSmoothStartFromCurrentAngles() { smoothStartTime = DateTime.Now; smoothStartAngleSec = angleSeconds; smoothStartAngleMin = angleMinutes; smoothStartAngleHour = angleHours; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; int w = this.ClientSize.Width; int h = this.ClientSize.Height; int size = Math.Min(w, h) * 3 / 4; float radius = size / 2f; float cx = w / 2f; float cy = h / 2f; // Draw outer circle using (Pen pen = new Pen(Color.DeepSkyBlue, 3f)) { e.Graphics.DrawEllipse(pen, cx - radius, cy - radius, 2 * radius, 2 * radius); } // Draw small green ticks if enabled if (checkBoxShowTicks.Checked) DrawSmallTicks(e.Graphics, cx, cy, radius); // Draw hour numbers DrawHourNumbers(e.Graphics, cx, cy, radius); // Draw hands (hour, minute, second) DrawHand(e.Graphics, cx, cy, radius * 0.5f, angleHours, Color.Black, 6f); // Hour hand DrawHand(e.Graphics, cx, cy, radius * 0.7f, angleMinutes, Color.DimGray, 4f); // Minute hand // Second hand: almost reaches green ticks, so a bit shorter than radius DrawHand(e.Graphics, cx, cy, radius * 0.95f, angleSeconds, Color.Red, 2f); // Second hand } // Draw 60 green ticks, one for each minute/second private void DrawSmallTicks(Graphics g, float cx, float cy, float radius) { float tickOuter = radius - 3; float tickInner; for (int i = 0; i < 60; ++i) { // Make hour ticks (multiples of 5) longer, but still green if (i % 5 == 0) tickInner = tickOuter - 15; else tickInner = tickOuter - 8; double angle = (i * 6.0 - 90) * Math.PI / 180.0; float x1 = cx + (float)(tickInner * Math.Cos(angle)); float y1 = cy + (float)(tickInner * Math.Sin(angle)); float x2 = cx + (float)(tickOuter * Math.Cos(angle)); float y2 = cy + (float)(tickOuter * Math.Sin(angle)); using (Pen pen = new Pen(Color.LimeGreen, i % 5 == 0 ? 3f : 2f)) { g.DrawLine(pen, x1, y1, x2, y2); } } } private void DrawHand(Graphics g, float cx, float cy, float length, double angleDeg, Color color, float thickness) { double radians = (angleDeg - 90) * Math.PI / 180.0; // -90 so 0 deg is at 12 o'clock float x2 = cx + (float)(length * Math.Cos(radians)); float y2 = cy + (float)(length * Math.Sin(radians)); using (Pen pen = new Pen(color, thickness) { StartCap = System.Drawing.Drawing2D.LineCap.Round, EndCap = System.Drawing.Drawing2D.LineCap.Round }) { g.DrawLine(pen, cx, cy, x2, y2); } } private void DrawHourNumbers(Graphics g, float cx, float cy, float radius) { Font font = new Font("Arial", 18, FontStyle.Bold); for (int h = 1; h Подробнее здесь: [url]https://stackoverflow.com/questions/79661187/in-my-c-sharp-winforms-analog-clock-how-to-fix-seconds-line-taking-first-time-ba[/url]