Создать электронные и стрелочные часы через Console.Application и выбирать их через меню - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте ! Задача такая: Надо создать электронные и стрелочные часы через Console.Application и выбирать их через меню. Тоесть в методе нарисовать, потом сгенерировать через массив, считать время и вывести на экран. Ну это как я понимаю. Цифры можно нарисовать любыми символами. Помогите реализовать. Спасибо заранее

Решение задачи: «Создать электронные и стрелочные часы через Console.Application и выбирать их через меню»

textual
Листинг программы
  1. using System;
  2. using System.Timers;
  3. using System.Drawing;
  4. using HWND = System.IntPtr;
  5. using System.Runtime.InteropServices;
  6.  
  7. //Автор: Петррр
  8.  
  9. [StructLayout(LayoutKind.Sequential)]
  10. public struct Rect
  11. {
  12.     public int Left;        
  13.     public int Top;        
  14.     public int Right;      
  15.     public int Bottom;      
  16. }
  17.  
  18. class AnalogClock
  19. {
  20.     static Graphics graphics;
  21.     static Rect rect;
  22.     static HWND hWnd;
  23.     static Font font;
  24.     static SolidBrush brush;
  25.  
  26.     [DllImport("user32.dll")]
  27.     private static extern bool GetWindowRect(IntPtr hWnd, ref Rect rect);
  28.  
  29.     public static void Main()
  30.     {
  31.         font = new Font("Calibri", 9F, FontStyle.Regular);
  32.         Timer timer = new Timer();
  33.         timer.Interval = 1000;
  34.         timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
  35.         brush = new SolidBrush(Color.Black);
  36.         timer.Start();
  37.         Console.Title = "Аналоговые часы";
  38.         Console.ReadKey(true);
  39.     }
  40.    
  41.     static void timer_Elapsed(object sender, ElapsedEventArgs e)
  42.     {
  43.         /*
  44.          * Рисуем цифер блат.
  45.          */
  46.        
  47.         hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
  48.         graphics = Graphics.FromHwnd(hWnd);
  49.         rect = new Rect();
  50.         graphics.Clear(Color.White);
  51.         GetWindowRect(hWnd, ref rect);
  52.         int height = rect.Bottom - rect.Top;
  53.         int width = rect.Right - rect.Left;
  54.         int xCenter = width >> 1;
  55.         int yCenter = (height >> 1) - 25;
  56.         int r = 120;
  57.         int d = 120 << 1;
  58.         graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(xCenter - 5, yCenter - 5, 10, 10));
  59.         graphics.DrawEllipse(new Pen(Color.Black), new Rectangle(xCenter - r, yCenter - r, d, d));
  60.  
  61.         for (int i = 1; i < 61; i++)
  62.         {
  63.             if (i % 5 == 0)
  64.             {
  65.                 // x = r*cos(a) + u;
  66.                 // y = r*sin(a) + v
  67.                 Point pV = new Point((int)((r - 15) * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
  68.                                      (int)((r - 15) * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
  69.                 Point pL = new Point((int)(r * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
  70.                                      (int)(r * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
  71.                 graphics.DrawLine(new Pen(Color.Blue, 2), pL, pV);                
  72.             }
  73.             else
  74.             {
  75.                 Point pV = new Point((int)((r - 5) * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
  76.                                      (int)((r - 5) * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
  77.                 Point pL = new Point((int)(r * Math.Cos((i * 6) * Math.PI / 180) + xCenter),
  78.                                      (int)(r * Math.Sin((i * 6) * Math.PI / 180) + yCenter));
  79.                 graphics.DrawLine(new Pen(Color.Green, 2), pL, pV);
  80.             }
  81.         }
  82.         graphics.DrawString("12", font, brush, new Point(xCenter - ((int)graphics.MeasureString("12", font).Width >> 1), yCenter - r - 15));
  83.         graphics.DrawString("6", font, brush, new Point(xCenter - ((int)graphics.MeasureString("6", font).Width >> 1), yCenter + r + 15));
  84.         graphics.DrawString("3", font, brush, new Point(xCenter + r + 15, yCenter - ((int)graphics.MeasureString("3", font).Height >> 1)));
  85.         graphics.DrawString("9", font, brush, new Point(xCenter - r - 15, yCenter - ((int)graphics.MeasureString("9", font).Height >> 1)));
  86.  
  87.         Point pNumberPos = new Point((int)((r + 15) * Math.Cos(-60 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(-60 * Math.PI / 180) + yCenter));
  88.         graphics.DrawString("1", font, brush, pNumberPos);
  89.  
  90.         pNumberPos = new Point((int)((r + 15) * Math.Cos(-30 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(-30 * Math.PI / 180) + yCenter));
  91.         graphics.DrawString("2", font, brush, pNumberPos);
  92.  
  93.         pNumberPos = new Point((int)((r + 15) * Math.Cos(30 * Math.PI / 180) + xCenter), (int)((r + 15)  * Math.Sin(30 * Math.PI / 180) + yCenter));
  94.         graphics.DrawString("4", font, brush, pNumberPos);
  95.  
  96.         pNumberPos = new Point((int)((r + 15) * Math.Cos(60 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(60 * Math.PI / 180) + yCenter));
  97.         graphics.DrawString("5", font, brush, pNumberPos);
  98.  
  99.         pNumberPos = new Point((int)((r + 15) * Math.Cos(120 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(120 * Math.PI / 180) + yCenter));        
  100.         graphics.DrawString("7", font, brush, pNumberPos);
  101.  
  102.         pNumberPos = new Point((int)((r + 15) * Math.Cos(150 * Math.PI / 180) + xCenter), (int)((r + 15) * Math.Sin(150 * Math.PI / 180) + yCenter));
  103.         graphics.DrawString("8", font, brush, pNumberPos);
  104.  
  105.         pNumberPos = new Point((int)((r + 19) * Math.Cos(210 * Math.PI / 180) + xCenter), (int)((r + 19) * Math.Sin(210 * Math.PI / 180) + yCenter));
  106.         graphics.DrawString("10", font, brush, pNumberPos);
  107.  
  108.         pNumberPos = new Point((int)((r + 19) * Math.Cos(240 * Math.PI / 180) + xCenter), (int)((r + 19) * Math.Sin(240 * Math.PI / 180) + yCenter));
  109.         graphics.DrawString("11", font, brush, pNumberPos);
  110.         /*
  111.          * Рисуем стрелки
  112.          */
  113.         int second = DateTime.Now.Second;
  114.         int minute = DateTime.Now.Minute;
  115.         int hour = DateTime.Now.Hour;
  116.  
  117.         int secondAngle = second * 6 - 90;
  118.         int minuteAngle = minute * 6 - 90;
  119.         int hourAngle = hour * 30 - 90;
  120.  
  121.         /* Стрелка секундная */
  122.         graphics.DrawLine(new Pen(Color.Green, 1), new Point(xCenter, yCenter),
  123.             new Point((int)((r - 25) * Math.Cos(secondAngle * Math.PI / 180) + xCenter),
  124.                                      (int)((r - 25) * Math.Sin(secondAngle * Math.PI / 180) + yCenter)));
  125.         /* Минутная стрелка */
  126.         graphics.DrawLine(new Pen(Color.Blue, 2), new Point(xCenter, yCenter),
  127.             new Point((int)((r - 30) * Math.Cos(minuteAngle * Math.PI / 180) + xCenter),
  128.                                      (int)((r - 30) * Math.Sin(minuteAngle * Math.PI / 180) + yCenter)));
  129.         /* Часовая стрелка */
  130.         graphics.DrawLine(new Pen(Color.Red, 3), new Point(xCenter, yCenter),
  131.             new Point((int)((r - 25) * Math.Cos(hourAngle * Math.PI / 180) + xCenter),
  132.                                      (int)((r - 25) * Math.Sin(hourAngle * Math.PI / 180) + yCenter)));
  133.     }
  134. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.75 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы