|
| 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 | +using System.Drawing; |
| 5 | +using System.Numerics; |
| 6 | +using Windows; |
| 7 | +using Windows.Win32.Foundation; |
| 8 | +using Windows.Win32.Graphics.Direct2D; |
| 9 | + |
| 10 | +namespace Direct2dDemo; |
| 11 | + |
| 12 | +internal class Program |
| 13 | +{ |
| 14 | + [STAThread] |
| 15 | + private static void Main() => Application.Run(new Direct2dDemo()); |
| 16 | + |
| 17 | + private class Direct2dDemo : MainWindow |
| 18 | + { |
| 19 | + private SolidColorBrush? _lightSlateGrayBrush; |
| 20 | + private SolidColorBrush? _cornflowerBlueBrush; |
| 21 | + |
| 22 | + public Direct2dDemo() : base(title: "Simple Direct2D Application", features: Features.EnableDirect2d) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + protected override void RenderTargetCreated(HwndRenderTarget renderTarget) |
| 27 | + { |
| 28 | + _lightSlateGrayBrush?.Dispose(); |
| 29 | + _cornflowerBlueBrush?.Dispose(); |
| 30 | + _lightSlateGrayBrush = renderTarget.CreateSolidColorBrush(Color.LightSlateGray); |
| 31 | + _cornflowerBlueBrush = renderTarget.CreateSolidColorBrush(Color.CornflowerBlue); |
| 32 | + base.RenderTargetCreated(renderTarget); |
| 33 | + } |
| 34 | + |
| 35 | + protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam) |
| 36 | + { |
| 37 | + switch (message) |
| 38 | + { |
| 39 | + case MessageType.Paint: |
| 40 | + if (IsDirect2dEnabled(out var renderTarget)) |
| 41 | + { |
| 42 | + renderTarget.SetTransform(Matrix3x2.Identity); |
| 43 | + renderTarget.Clear(Color.White); |
| 44 | + |
| 45 | + SizeF size = renderTarget.Size(); |
| 46 | + |
| 47 | + for (int x = 0; x < size.Width; x += 10) |
| 48 | + { |
| 49 | + renderTarget.DrawLine( |
| 50 | + new(x, 0), new(x, size.Height), |
| 51 | + _lightSlateGrayBrush!, |
| 52 | + 0.5f); |
| 53 | + } |
| 54 | + |
| 55 | + for (int y = 0; y < size.Height; y += 10) |
| 56 | + { |
| 57 | + renderTarget.DrawLine( |
| 58 | + new(0, y), new(size.Width, y), |
| 59 | + _lightSlateGrayBrush!, |
| 60 | + 0.5f); |
| 61 | + } |
| 62 | + |
| 63 | + RectangleF rectangle1 = RectangleF.FromLTRB( |
| 64 | + size.Width / 2 - 50, |
| 65 | + size.Height / 2 - 50, |
| 66 | + size.Width / 2 + 50, |
| 67 | + size.Height / 2 + 50); |
| 68 | + |
| 69 | + RectangleF rectangle2 = RectangleF.FromLTRB( |
| 70 | + size.Width / 2 - 100, |
| 71 | + size.Height / 2 - 100, |
| 72 | + size.Width / 2 + 100, |
| 73 | + size.Height / 2 + 100); |
| 74 | + |
| 75 | + renderTarget.FillRectangle(rectangle1, _lightSlateGrayBrush!); |
| 76 | + renderTarget.DrawRectangle(rectangle2, _cornflowerBlueBrush!); |
| 77 | + } |
| 78 | + return (LRESULT)0; |
| 79 | + } |
| 80 | + |
| 81 | + return base.WindowProcedure(window, message, wParam, lParam); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments