8.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T03:16:58.945801+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 32ac28362ec2b57c |
WinApi
Documentation: WindowsAPIHelpers.cs
1. Purpose
This module provides interoperability helpers for Windows API integration, specifically to support custom window sizing and positioning behavior in WPF applications. It defines P/Invoke-compatible structures (POINT, RECT, MINMAXINFO, MONITORINFO, WINDOWPOS, WM, SWP) and exposes two Win32 functions (GetMonitorInfo, MonitorFromWindow) to enable accurate adjustment of a window’s maximized size and position to match the work area of the monitor on which the window resides—rather than the full screen (which may include taskbars or other non-client areas). This is typically used in response to the WM_GETMINMAXINFO message to prevent maximized windows from overlapping system UI elements.
2. Public Interface
Structs
-
POINT[StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; public POINT(int x, int y); }Represents a point in 2D space with integer coordinates. Used as a field in other structures.
-
RECT[StructLayout(LayoutKind.Sequential, Pack = 0)] public struct RECT { public int left, top, right, bottom; public static readonly RECT Empty; public int Width { get; } public int Height { get; } public RECT(int left, int top, int right, int bottom); public bool IsEmpty { get; } public override string ToString(); public override bool Equals(object obj); public override int GetHashCode(); public static bool operator ==(RECT, RECT); public static bool operator !=(RECT, RECT); }Represents a rectangle with integer coordinates. Includes computed
WidthandHeightproperties, equality operators, and anIsEmptycheck (true ifleft >= rightortop >= bottom). Note:Heightis computed asbottom - top(notMath.Abs), so negative heights are possible ifbottom < top. -
MINMAXINFO[StructLayout(LayoutKind.Sequential)] public struct MINMAXINFO { public POINT ptReserved; public POINT ptMaxSize; public POINT ptMaxPosition; public POINT ptMinTrackSize; public POINT ptMaxTrackSize; }Used to convey minimum/maximum tracking and maximized window geometry. Populated by
GetMinMaxInfo. -
MONITORINFO[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class MONITORINFO { public int cbSize = Marshal.SizeOf(typeof(MONITORINFO)); public RECT rcMonitor = new RECT(); public RECT rcWork = new RECT(); public int dwFlags = 0; }Contains monitor-specific information.
cbSizeis auto-initialized to the unmanaged size of the struct.rcMonitoris the full monitor rectangle;rcWorkis the work area (excluding taskbars, etc.). -
WINDOWPOS[StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x, y, cx, cy; public int flags; }Used in
WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGEDmessages. Not used directly in this file but defined for completeness.
Enums
-
WMpublic enum WM { WINDOWMAX = 0x0024, WINDOWPOSCHANGING = 0x0046; }Windows message constants.
WINDOWMAXis likely a typo or nonstandard alias (standard isWM_SYSCOMMANDwithSC_MAXIMIZE, orWM_SIZEwithSIZE_MAXIMIZED).WINDOWPOSCHANGINGisWM_WINDOWPOSCHANGING. -
SWPpublic enum SWP { NOMOVE = 0x0002; }Flags for
SetWindowPos.NOMOVEpreserves current position.
Methods
GetMinMaxInfo(IntPtr hwnd, IntPtr lParam)Handles thepublic static void GetMinMaxInfo(IntPtr hwnd, IntPtr lParam)WM_GETMINMAXINFOmessage by adjustingptMaxSizeandptMaxPositionin theMINMAXINFOstructure to fit the work area of the monitor containinghwnd. UsesMonitorFromWindowwithMONITOR_DEFAULTTONEAREST(0x00000002) to locate the monitor, then callsGetMonitorInfoto retrieve monitor geometry. Writes the modifiedMINMAXINFOback tolParam.
P/Invoke Exports
-
GetMonitorInfo[DllImport("user32")] public static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);Retrieves monitor information. Caller must initialize
MONITORINFO.cbSizebefore calling. -
MonitorFromWindow[DllImport("User32")] public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);Returns a handle to the monitor that contains the window.
flagsis typicallyMONITOR_DEFAULTTONEAREST(0x00000002).
3. Invariants
MONITORINFO.cbSizemust be set toMarshal.SizeOf(typeof(MONITORINFO))before callingGetMonitorInfo; otherwise, the call will fail (returnsfalse).RECT.IsEmptyis defined asleft >= right || top >= bottom. This implies a rectangle withleft == rightortop == bottomis considered empty.RECT.Heightis computed asbottom - top(notMath.Abs(bottom - top)), so ifbottom < top,Heightis negative. This may indicate an inverted rectangle (e.g., from drag-select), but the code does not enforce non-negative height.GetMinMaxInfoassumes the window is on a valid monitor. IfMonitorFromWindowreturnsIntPtr.Zero, no adjustment is made, and the originalMINMAXINFOvalues are preserved.- The
ptMaxPositionvalues are computed as absolute offsets from the monitor’s left/top edge (Math.Abs(rcWorkArea.left - rcMonitorArea.left)), which correctly yields the top-left corner of the work area relative to the monitor.
4. Dependencies
Dependencies on this module:
- WPF/Win32 interop layer: This module is intended for use in WPF applications (evidenced by
System.Windowsnamespace usage, though not directly referenced in this file). Likely consumed by window message handlers (e.g.,Window.SourceInitializedorOnSourceInitializedoverrides) to subclass window procedures and handleWM_GETMINMAXINFO. System.Runtime.InteropServices: Required forStructLayout,DllImport,Marshal.PtrToStructure, andMarshal.StructureToPtr.
Dependencies of this module:
user32.dll: ForGetMonitorInfoandMonitorFromWindow.- No external libraries beyond .NET Framework core types (no NuGet dependencies implied).
Known consumers (inferred):
- Any class handling
WM_GETMINMAXINFO(e.g., viaHwndSource.AddHook) will callWindowsAPIHelpers.GetMinMaxInfo. - Likely used in custom window management logic (e.g.,
MainWindow.xaml.csor a base window class).
5. Gotchas
RECT.Heightis not absolute:Height => bottom - top(notMath.Abs). This may cause unexpected negative heights if rectangles are inverted. UseMath.Absexternally if needed.MONITORINFOis aclass, not astruct: This is unusual for P/Invoke marshaling (typicallystructis preferred). While it works due toStructLayout, it introduces heap allocation and reference semantics. Ensure the instance is not reused across threads without synchronization.WINDOWMAXenum value is nonstandard: The Win32 constant for maximization isWM_SYSCOMMANDwithSC_MAXIMIZE(0xF030), not0x0024.0x0024isWM_USER + 36in some contexts but not a standard system message. This may be a typo or internal alias.rcMonitorvsrcWorksemantics:rcMonitoris the full monitor bounds;rcWorkexcludes taskbars, etc. The code correctly usesrcWorkforptMaxSize/ptMaxPosition, but callers must understand this distinction.- No validation of
lParaminGetMinMaxInfo: AssumeslParampoints to a validMINMAXINFOstructure. PassingIntPtr.Zeroor invalid memory will cause a crash. - Missing
WMconstants: Only twoWMvalues are defined (WINDOWMAX,WINDOWPOSCHANGING). Other common messages (e.g.,WM_SIZE,WM_GETMINMAXINFO) are not included, suggesting this is a partial set. MONITOR_DEFAULTTONEARESTflag is hardcoded: TheMonitorFromWindowcall uses0x00000002directly instead of a named constant (e.g.,MONITOR_DEFAULTTONEAREST = 2). While correct, it reduces readability.
None identified beyond these.