Создать электронные и стрелочные часы через Console.Application и выбирать их через меню - C#
Формулировка задачи:
Здравствуйте !
Задача такая:
Надо создать электронные и стрелочные часы через Console.Application и выбирать их через меню. Тоесть в методе нарисовать, потом сгенерировать через массив, считать время и вывести на экран. Ну это как я понимаю. Цифры можно нарисовать любыми символами.
Помогите реализовать. Спасибо заранее
Решение задачи: «Создать электронные и стрелочные часы через Console.Application и выбирать их через меню»
textual
Листинг программы
using System;
using System.Timers;
using System.Drawing;
using HWND = System.IntPtr;
using System.Runtime.InteropServices;
//Автор: Петррр
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
class AnalogClock
{
static Graphics graphics;
static Rect rect;
static HWND hWnd;
static Font font;
static SolidBrush brush;
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rect rect);
public static void Main()
{
font = new Font("Calibri", 9F, FontStyle.Regular);
Timer timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
brush = new SolidBrush(Color.Black);
timer.Start();
Console.Title = "Аналоговые часы";
Console.ReadKey(true);
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
/*
* Рисуем цифер блат.
*/
hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
graphics = Graphics.FromHwnd(hWnd);
rect = new Rect();
graphics.Clear(Color.White);
GetWindowRect(hWnd, ref rect);
int height = rect.Bottom - rect.Top;
int width = rect.Right - rect.Left;
int xCenter = width >> 1;
int yCenter = (height >> 1) - 25;
int r = 120;
int d = 120 << 1;
graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(xCenter - 5, yCenter - 5, 10, 10));
graphics.DrawEllipse(new Pen(Color.Black), new Rectangle(xCenter - r, yCenter - r, d, d));
for (int i = 1; i < 61; i++)
{
if (i % 5 == 0)
{
// x = r*cos(a) + u;
// y = r*sin(a) + v
Point pV = new Point((int)((r - 15) * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
(int)((r - 15) * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
Point pL = new Point((int)(r * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
(int)(r * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
graphics.DrawLine(new Pen(Color.Blue, 2), pL, pV);
}
else
{
Point pV = new Point((int)((r - 5) * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
(int)((r - 5) * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
Point pL = new Point((int)(r * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
(int)(r * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
graphics.DrawLine(new Pen(Color.Green, 2), pL, pV);
}
}
graphics.DrawString("12", font, brush, new Point(xCenter - ((int)graphics.MeasureString("12", font).Width >> 1), yCenter - r - 15));
graphics.DrawString("6", font, brush, new Point(xCenter - ((int)graphics.MeasureString("6", font).Width >> 1), yCenter + r + 15));
graphics.DrawString("3", font, brush, new Point(xCenter + r + 15, yCenter - ((int)graphics.MeasureString("3", font).Height >> 1)));
graphics.DrawString("9", font, brush, new Point(xCenter - r - 15, yCenter - ((int)graphics.MeasureString("9", font).Height >> 1)));
Point pNumberPos = new Point((int)((r + 15) * Math.Cos(-60 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(-60 * Math.PI / 180) + yCenter));
graphics.DrawString("1", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 15) * Math.Cos(-30 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(-30 * Math.PI / 180) + yCenter));
graphics.DrawString("2", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 15) * Math.Cos(30 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(30 * Math.PI / 180) + yCenter));
graphics.DrawString("4", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 15) * Math.Cos(60 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(60 * Math.PI / 180) + yCenter));
graphics.DrawString("5", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 15) * Math.Cos(120 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(120 * Math.PI / 180) + yCenter));
graphics.DrawString("7", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 15) * Math.Cos(150 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(150 * Math.PI / 180) + yCenter));
graphics.DrawString("8", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 19) * Math.Cos(210 * Math.PI / 180) + xCenter), (int)((r + 19) * Math.Sin(210 * Math.PI / 180) + yCenter));
graphics.DrawString("10", font, brush, pNumberPos);
pNumberPos = new Point((int)((r + 19) * Math.Cos(240 * Math.PI / 180) + xCenter), (int)((r + 19) * Math.Sin(240 * Math.PI / 180) + yCenter));
graphics.DrawString("11", font, brush, pNumberPos);
/*
* Рисуем стрелки
*/
int second = DateTime.Now.Second;
int minute = DateTime.Now.Minute;
int hour = DateTime.Now.Hour;
int secondAngle = second * 6 - 90;
int minuteAngle = minute * 6 - 90;
int hourAngle = hour * 30 - 90;
/* Стрелка секундная */
graphics.DrawLine(new Pen(Color.Green, 1), new Point(xCenter, yCenter),
new Point((int)((r - 25) * Math.Cos(secondAngle * Math.PI / 180) + xCenter),
(int)((r - 25) * Math.Sin(secondAngle * Math.PI / 180) + yCenter)));
/* Минутная стрелка */
graphics.DrawLine(new Pen(Color.Blue, 2), new Point(xCenter, yCenter),
new Point((int)((r - 30) * Math.Cos(minuteAngle * Math.PI / 180) + xCenter),
(int)((r - 30) * Math.Sin(minuteAngle * Math.PI / 180) + yCenter)));
/* Часовая стрелка */
graphics.DrawLine(new Pen(Color.Red, 3), new Point(xCenter, yCenter),
new Point((int)((r - 25) * Math.Cos(hourAngle * Math.PI / 180) + xCenter),
(int)((r - 25) * Math.Sin(hourAngle * Math.PI / 180) + yCenter)));
}
}