|
| 1 | +// Copyright (c) Jeremy W. Kuhne. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +// The GDI+ variant works, but is far from optimized. Creating and disposing a Graphics object every second |
| 5 | +// is clearly not optimal. Keeping things aligned with the sample to show the direct mapping. |
| 6 | +// |
| 7 | +#define GDIPLUS |
| 8 | + |
| 9 | +#if GDIPLUS |
| 10 | +using Windows.Win32.Graphics.GdiPlus; |
| 11 | +using GdiPlusPen = Windows.Win32.Graphics.GdiPlus.Pen; |
| 12 | +using GdiPlusBrush = Windows.Win32.Graphics.GdiPlus.Brush; |
| 13 | +#endif |
| 14 | + |
| 15 | +using Windows; |
| 16 | +using System.Drawing; |
| 17 | +using Windows.Win32.Foundation; |
| 18 | +using Windows.Win32; |
| 19 | +using Point = System.Drawing.Point; |
| 20 | + |
| 21 | +namespace Clock; |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// Sample from Programming Windows, 5th Edition. |
| 25 | +/// Original (c) Charles Petzold, 1998 |
| 26 | +/// Figure 8-5, Pages 346-350. |
| 27 | +/// </summary> |
| 28 | +internal static class Program |
| 29 | +{ |
| 30 | + [STAThread] |
| 31 | + private static void Main() => Application.Run(new Clock("Analog Clock")); |
| 32 | +} |
| 33 | + |
| 34 | +internal class Clock : MainWindow |
| 35 | +{ |
| 36 | +#if GDIPLUS |
| 37 | + private readonly GdiPlusPen _blackPen = new(Color.Black); |
| 38 | + private static readonly GdiPlusBrush s_blackBrush = new SolidBrush(Color.Black); |
| 39 | + private readonly GdiPlusBrush _whiteBrush = new SolidBrush(Color.White); |
| 40 | +#endif |
| 41 | + |
| 42 | + public Clock(string title) : base(title) { } |
| 43 | + |
| 44 | + private void SetIsotropic(DeviceContext hdc) |
| 45 | + { |
| 46 | + hdc.SetMappingMode(MappingMode.Isotropic); |
| 47 | + hdc.SetWindowExtents(new Size(1000, 1000)); |
| 48 | + hdc.SetViewportExtents(new Size(_clientSize.Width / 2, -_clientSize.Height / 2)); |
| 49 | + hdc.SetViewportOrigin(new Point(_clientSize.Width / 2, _clientSize.Height / 2)); |
| 50 | + } |
| 51 | + |
| 52 | + private static void RotatePoint(Point[] pt, int iNum, int iAngle) |
| 53 | + { |
| 54 | + for (int i = 0; i < iNum; i++) |
| 55 | + { |
| 56 | + pt[i] = new Point |
| 57 | + ( |
| 58 | + (int)(pt[i].X * Math.Cos(TWOPI * iAngle / 360) + pt[i].Y * Math.Sin(TWOPI * iAngle / 360)), |
| 59 | + (int)(pt[i].Y * Math.Cos(TWOPI * iAngle / 360) - pt[i].X * Math.Sin(TWOPI * iAngle / 360)) |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void DrawClock(DeviceContext dc) |
| 65 | + { |
| 66 | + int iAngle; |
| 67 | + Point[] pt = new Point[3]; |
| 68 | + |
| 69 | +#if GDIPLUS |
| 70 | + using var graphics = new Graphics(dc); |
| 71 | + graphics.SetSmoothingMode(SmoothingMode.SmoothingModeHighQuality); |
| 72 | +#else |
| 73 | + dc.SelectObject(StockBrush.Black); |
| 74 | +#endif |
| 75 | + for (iAngle = 0; iAngle < 360; iAngle += 6) |
| 76 | + { |
| 77 | + pt[0].X = 0; |
| 78 | + pt[0].Y = 900; |
| 79 | + RotatePoint(pt, 1, iAngle); |
| 80 | + pt[2].X = pt[2].Y = iAngle % 5 != 0 ? 33 : 100; |
| 81 | + pt[0].X -= pt[2].X / 2; |
| 82 | + pt[0].Y -= pt[2].Y / 2; |
| 83 | + pt[1].X = pt[0].X + pt[2].X; |
| 84 | + pt[1].Y = pt[0].Y + pt[2].Y; |
| 85 | +#if GDIPLUS |
| 86 | + graphics.FillEllipse(s_blackBrush, pt[0].X, pt[0].Y, pt[1].X - pt[0].X, pt[1].Y - pt[0].Y); |
| 87 | +#else |
| 88 | + dc.Ellipse(Rectangle.FromLTRB(pt[0].X, pt[0].Y, pt[1].X, pt[1].Y)); |
| 89 | +#endif |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void DrawHands(DeviceContext dc, SYSTEMTIME time, bool erase = false, bool drawHourAndMinuteHands = true) |
| 94 | + { |
| 95 | + int[] handAngles = |
| 96 | + [ |
| 97 | + (time.wHour * 30) % 360 + time.wMinute / 2, |
| 98 | + time.wMinute * (360 / 60), |
| 99 | + time.wSecond * (360 / 60) |
| 100 | + ]; |
| 101 | + |
| 102 | + Point[][] handPoints = |
| 103 | + [ |
| 104 | + [new(0, -150), new(100, 0), new(0, 600), new(-100, 0), new(0, -150)], |
| 105 | + [new(0, -200), new(50, 0), new(0, 800), new(-50, 0), new(0, -200)], |
| 106 | + [new(0, 0), new(0, 0), new(0, 0), new(0, 0), new(0, 800)] |
| 107 | + ]; |
| 108 | + |
| 109 | +#if GDIPLUS |
| 110 | + using var graphics = new Graphics(dc); |
| 111 | + if (erase) |
| 112 | + { |
| 113 | + graphics.FillEllipse(_whiteBrush, -830, -830, 1660, 1660); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + graphics.SetSmoothingMode(SmoothingMode.SmoothingModeHighQuality); |
| 118 | +#else |
| 119 | + dc.SelectObject(erase ? StockPen.White : StockPen.Black); |
| 120 | +#endif |
| 121 | + |
| 122 | + for (int i = drawHourAndMinuteHands ? 0 : 2; i < 3; i++) |
| 123 | + { |
| 124 | + RotatePoint(handPoints[i], 5, handAngles[i]); |
| 125 | + |
| 126 | +#if GDIPLUS |
| 127 | + graphics.DrawLines(_blackPen, handPoints[i]); |
| 128 | +#else |
| 129 | + dc.Polyline(handPoints[i]); |
| 130 | +#endif |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private const int ID_TIMER = 1; |
| 135 | + private const double TWOPI = Math.PI * 2; |
| 136 | + private Size _clientSize; |
| 137 | + private SYSTEMTIME _previousTime; |
| 138 | + |
| 139 | + protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam) |
| 140 | + { |
| 141 | + switch (message) |
| 142 | + { |
| 143 | + case MessageType.Create: |
| 144 | + window.SetTimer(ID_TIMER, 1000); |
| 145 | + Interop.GetLocalTime(out _previousTime); |
| 146 | + return (LRESULT)0; |
| 147 | + case MessageType.Size: |
| 148 | + _clientSize = new Message.Size(wParam, lParam).NewSize; |
| 149 | + return (LRESULT)0; |
| 150 | + case MessageType.Timer: |
| 151 | + Interop.GetLocalTime(out SYSTEMTIME time); |
| 152 | + bool drawAllHands = time.wHour != _previousTime.wHour || time.wMinute != _previousTime.wMinute; |
| 153 | + using (DeviceContext dc = window.GetDeviceContext()) |
| 154 | + { |
| 155 | + SetIsotropic(dc); |
| 156 | + DrawHands(dc, _previousTime, erase: true, drawAllHands); |
| 157 | + DrawHands(dc, time); |
| 158 | + } |
| 159 | + _previousTime = time; |
| 160 | + return (LRESULT)0; |
| 161 | + case MessageType.Paint: |
| 162 | + using (DeviceContext dc = window.BeginPaint()) |
| 163 | + { |
| 164 | + SetIsotropic(dc); |
| 165 | + DrawClock(dc); |
| 166 | + DrawHands(dc, _previousTime); |
| 167 | + } |
| 168 | + return (LRESULT)0; |
| 169 | + } |
| 170 | + |
| 171 | + return base.WindowProcedure(window, message, wParam, lParam); |
| 172 | + } |
| 173 | +} |
0 commit comments