Set MinWidth, MinHeight, MaxWidth, and MaxHeight when you create the window.
dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System; using OnePlatform; using static OnePlatform.PlatformEventKind; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Width = 640, Height = 480, MinWidth = 320, MinHeight = 240, MaxWidth = 960, MaxHeight = 720, SoftwareSurface = true }); Console.WriteLine( $"{window.Width}x{window.Height}"); while (true) { app.PumpEvents(1000); while (app.TryGetEvent(out var evt)) if (evt.Kind is WindowCloseRequested or QuitRequested) return; }
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The snippet opens a resizable window at 640 × 480 pixels. Use the window’s Close button to exit. It prints the starting client size:
640x480The full program
Run from a graphical desktop session. Try dragging the frame: the requested client-size range is 320 × 240 to 960 × 720 pixels. These size limits apply to desktop windows on Windows, macOS, and Linux (Wayland and X11).
using System; using OnePlatform; using static OnePlatform.PlatformEventKind; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Title = "Size limits", Width = 640, Height = 480, MinWidth = 320, MinHeight = 240, MaxWidth = 960, MaxHeight = 720, SoftwareSurface = true }); Console.WriteLine( $"{window.Width}x{window.Height}"); while (true) { app.PumpEvents(1000); while (app.TryGetEvent(out var evt)) if (evt.Kind is WindowCloseRequested or QuitRequested) return; }
640x480On X11 the limits are hints to the window manager, so an X server with no window manager won’t enforce them by itself. On Wayland, without SoftwareSurface = true, this example has nothing to draw, so the window won’t appear.
Variations
To change the range after opening the window, add this line after CreateWindow or call it later when your layout changes:
window.SetSizeLimits(400, 300, 800, 600);The arguments are minimum width, minimum height, maximum width, and maximum height. This changes the resize range, not the example’s starting size.
To disable user resizing entirely, add this to the CreateWindow options:
Resizable = false,Related recipes
The short answer
Set MinWidth, MinHeight, MaxWidth, and MaxHeight on the WindowOptions you pass to CreateWindow. These four values constrain user resizing; Width and Height are the starting size. The limits are measured in client pixels, meaning the part where your app draws, not the outer dimensions of the title bar and frame. Use SetSizeLimits to update an existing window instead of creating another one.
How do I limit how far a window can be resized?
Choose the lower bound from the smallest usable layout, then choose an upper bound if there’s a size beyond which the content shouldn’t stretch. Leave Resizable at its default of true so the user can still drag within the range. The size options describe 0 as no limit; leave the maximum options unset if you only need a minimum. Keep the initial width and height within the range so opening the window and dragging it are two separate, predictable operations.
The same options reach different native facilities. On X11, OnePlatform puts the bounds in WM_NORMAL_HINTS. On Wayland it calls set_min_size and set_max_size, including its own title bar in the heights it sends if it draws one. On macOS it converts the pixel limits by the scale and applies them to the native content size. On Windows it converts them to the outer tracking bounds used during resizing. Those conversions are why you supply the content dimensions rather than counting frame pixels yourself.
How do I force a window to be a certain size?
Decide whether you want an initial size, a resize range, or no manual resizing. For an initial size, use Width and Height, as the example does. For a range, keep the window resizable and use the four limits. If you don’t want the user to resize it at all, use the Resizable option instead. Resize limits are not a request to immediately move or resize an already open window, and they do not make a window start maximized. To read the actual client size after a resize, use Window.Width and Window.Height when you handle WindowResized.