|
| 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.Runtime.InteropServices; |
| 6 | +using Windows; |
| 7 | +using Windows.Win32; |
| 8 | +using Windows.Win32.Foundation; |
| 9 | +using Windows.Win32.Graphics.Gdi; |
| 10 | +using Windows.Win32.UI.WindowsAndMessaging; |
| 11 | + |
| 12 | +namespace RandRect; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Sample from Programming Windows, 5th Edition. |
| 16 | +/// Original (c) Charles Petzold, 1998 |
| 17 | +/// Figure 5-26, Pages 200-202. |
| 18 | +/// </summary> |
| 19 | +internal unsafe static class Program |
| 20 | +{ |
| 21 | + [STAThread] |
| 22 | + private static void Main() |
| 23 | + { |
| 24 | + const string szAppName = "RandRect"; |
| 25 | + |
| 26 | + WindowProcedure wndProc = WindowProcedure; |
| 27 | + HMODULE module; |
| 28 | + Interop.GetModuleHandleEx(0, (PCWSTR)null, &module); |
| 29 | + |
| 30 | + HWND hwnd; |
| 31 | + |
| 32 | + fixed (char* appName = szAppName) |
| 33 | + fixed (char* title = "Random Rectangles") |
| 34 | + { |
| 35 | + WNDCLASSEXW wndClass = new() |
| 36 | + { |
| 37 | + cbSize = (uint)sizeof(WNDCLASSEXW), |
| 38 | + style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW, |
| 39 | + lpfnWndProc = (WNDPROC)Marshal.GetFunctionPointerForDelegate(wndProc), |
| 40 | + hInstance = module, |
| 41 | + hIcon = Interop.LoadIcon(default, Interop.IDI_APPLICATION), |
| 42 | + hCursor = Interop.LoadCursor(default, Interop.IDC_ARROW), |
| 43 | + hbrBackground = (HBRUSH)Interop.GetStockObject(GET_STOCK_OBJECT_FLAGS.WHITE_BRUSH), |
| 44 | + lpszClassName = appName |
| 45 | + }; |
| 46 | + |
| 47 | + ATOM atom = Interop.RegisterClassEx(&wndClass); |
| 48 | + |
| 49 | + hwnd = Interop.CreateWindowEx( |
| 50 | + WINDOW_EX_STYLE.WS_EX_OVERLAPPEDWINDOW, |
| 51 | + appName, |
| 52 | + title, |
| 53 | + WINDOW_STYLE.WS_OVERLAPPEDWINDOW, |
| 54 | + Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, |
| 55 | + HWND.Null, |
| 56 | + HMENU.Null, |
| 57 | + module, |
| 58 | + null); |
| 59 | + |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + Interop.ShowWindow(hwnd, SHOW_WINDOW_CMD.SW_SHOWDEFAULT); |
| 64 | + Interop.UpdateWindow(hwnd); |
| 65 | + |
| 66 | + while (true) |
| 67 | + { |
| 68 | + if (Interop.PeekMessage(out MSG message, HWND.Null, 0, uint.MaxValue, PEEK_MESSAGE_REMOVE_TYPE.PM_REMOVE)) |
| 69 | + { |
| 70 | + if (message.message == Interop.WM_QUIT) |
| 71 | + { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + Interop.TranslateMessage(message); |
| 76 | + Interop.DispatchMessage(message); |
| 77 | + } |
| 78 | + |
| 79 | + // We're crazy fast over 25 years past the source sample, |
| 80 | + // sleeping to make this a bit more interesting. |
| 81 | + Thread.Sleep(100); |
| 82 | + DrawRectangle(hwnd); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static int s_cxClient, s_cyClient; |
| 87 | + private static readonly Random s_rand = new(); |
| 88 | + |
| 89 | + private static LRESULT WindowProcedure(HWND window, uint message, WPARAM wParam, LPARAM lParam) |
| 90 | + { |
| 91 | + switch ((MessageType)message) |
| 92 | + { |
| 93 | + case MessageType.Size: |
| 94 | + s_cxClient = lParam.LOWORD; |
| 95 | + s_cyClient = lParam.HIWORD; |
| 96 | + return (LRESULT)0; |
| 97 | + case MessageType.Destroy: |
| 98 | + Interop.PostQuitMessage(0); |
| 99 | + return (LRESULT)0; |
| 100 | + } |
| 101 | + |
| 102 | + return Interop.DefWindowProc(window, message, wParam, lParam); |
| 103 | + } |
| 104 | + |
| 105 | + private static void DrawRectangle(HWND window) |
| 106 | + { |
| 107 | + if (s_cxClient == 0 || s_cyClient == 0) |
| 108 | + return; |
| 109 | + |
| 110 | + Rectangle rect = Rectangle.FromLTRB( |
| 111 | + s_rand.Next() % s_cxClient, |
| 112 | + s_rand.Next() % s_cyClient, |
| 113 | + s_rand.Next() % s_cxClient, |
| 114 | + s_rand.Next() % s_cyClient); |
| 115 | + |
| 116 | + using HBRUSH brush = HBRUSH.CreateSolid( |
| 117 | + Color.FromArgb((byte)(s_rand.Next() % 256), (byte)(s_rand.Next() % 256), (byte)(s_rand.Next() % 256))); |
| 118 | + using DeviceContext dc = window.GetDeviceContext(); |
| 119 | + dc.FillRectangle(rect, brush); |
| 120 | + } |
| 121 | +} |
0 commit comments