Skip to content

Instantly share code, notes, and snippets.

@TheLeftExit
Created February 14, 2023 17:23
Show Gist options
  • Save TheLeftExit/43c427fc9184043ad3a69a4584d0a62a to your computer and use it in GitHub Desktop.
Save TheLeftExit/43c427fc9184043ad3a69a4584d0a62a to your computer and use it in GitHub Desktop.
C# window message loop generated by ChatGPT
using System;
using System.Runtime.InteropServices;
public static class Program
{
[DllImport("user32.dll")]
private static extern bool RegisterClassEx(ref WNDCLASSEX wcex);
[DllImport("user32.dll")]
private static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool UpdateWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")]
private static extern bool TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
private static extern IntPtr DispatchMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
private static extern bool DestroyWindow(IntPtr hWnd);
private const string WindowClassName = "MyWindowClass";
private static IntPtr _hWnd;
[StructLayout(LayoutKind.Sequential)]
private struct WNDCLASSEX
{
public int cbSize;
public uint style;
public IntPtr lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSG
{
public IntPtr hWnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public POINT pt;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
private static IntPtr WindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return IntPtr.Zero;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
private const uint CS_HREDRAW = 0x0002;
private const uint CS_VREDRAW = 0x0001;
private const uint CW_USEDEFAULT = 0x80000000;
private const uint WS_OVERLAPPEDWINDOW = 0xcf0000;
private const uint WM_DESTROY = 0x0002;
[DllImport("user32.dll")]
private static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern void PostQuitMessage(int nExitCode);
public static void Main()
{
var hInstance = Marshal.GetHINSTANCE(typeof(Program).Module);
var wcex = new WNDCLASSEX
{
cbSize = Marshal.SizeOf<WNDCLASSEX>(),
style = CS_HREDRAW | CS_VREDRAW,
lpfnWndProc = Marshal.GetFunctionPointerForDelegate<WndProc>(WindowProc),
hInstance = hInstance,
hCursor = LoadCursor(IntPtr.Zero, IDC_ARROW),
hbrBackground = (IntPtr)(COLOR_WINDOW),
lpszClassName = WindowClassName
};
RegisterClassEx(ref wcex);
unchecked
{
_hWnd = CreateWindowEx(
0,
WindowClassName,
"My Window",
WS_OVERLAPPEDWINDOW,
(int)CW_USEDEFAULT,
(int)CW_USEDEFAULT,
(int)CW_USEDEFAULT,
(int)CW_USEDEFAULT,
IntPtr.Zero,
IntPtr.Zero,
hInstance,
IntPtr.Zero);
}
if (_hWnd == IntPtr.Zero)
{
throw new InvalidOperationException("Failed to create window.");
}
ShowWindow(_hWnd, 1);
UpdateWindow(_hWnd);
var msg = new MSG();
while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
DestroyWindow(_hWnd);
}
private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private const uint IDC_ARROW = 32512;
private const uint COLOR_WINDOW = 5;
private const int SW_SHOWNORMAL = 1;
[DllImport("user32.dll")]
private static extern IntPtr LoadCursor(IntPtr hInstance, uint lpCursorName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment