Recursive AI

Drag a native window from a custom title bar in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Call StartMove() on a press in your custom title bar on Linux or Windows. On Windows it starts the system’s title-bar drag. On macOS it does nothing.

On Linux, use Wayland or X11 with a window manager that handles _NET_WM_MOVERESIZE.

Terminal
dotnet add package RecursiveAI.OnePlatform

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePlatform;
using Kind = OnePlatform.PlatformEventKind;
 
using var app = PlatformApp.Create();
var window = app.CreateWindow(new()
{
    Title = "Drag the top strip",
    Decorations = DecorationPreference.None,
    Width = 480,
    Height = 240
});
 
while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var e))
    {
        if (e.Kind is Kind.WindowCloseRequested
            or Kind.QuitRequested ||
            (e.Kind == Kind.KeyDown &&
             e.Key == Key.Escape))
            return;
 
        if (e.WindowId == window.Id &&
            e.Kind == Kind.PointerButtonDown &&
            e.Button == MouseButton.Left
            && e.Y >= 0 && e.Y < 32)
        {
            window.StartMove();
            const string message =
                "Move requested " +
                "from the top strip.";
            Console.WriteLine(message);
        }
    }
}

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Press and hold the left button in the top 32 pixels of the blank window, drag, then release. Press Escape to exit; the example has no title bar or close button (×).

The example printed this on Linux (X11). We didn’t run it on Windows.

Text
Move requested from the top strip.

Variations

Leave room on the right for title-bar buttons by narrowing the hit test in the pointer handler:

C#
&& e.Y >= 0 && e.Y < 32
&& e.X >= 0 && e.X < window.Width - 96

The short answer

For a borderless OnePlatform window, handle PointerButtonDown in your title-bar hit area. When the primary button goes down there, call window.StartMove() on the window that received the press. The desktop handles moving the whole window until the user releases the button. Don’t wait for PointerMoved or for the button to come back up before handing off the drag.

How do you make a borderless C# window draggable?

Use DecorationPreference.None to remove the system frame, then choose a drag region inside the window. The example reserves a full-width strip at the top. In an app with its own title bar, draw your title and controls separately, and start a move only on the part that isn’t a button, menu, or other interactive control. The variation above shows how to exclude a block on the right.

Check the event’s WindowId in a multi-window app so a press in one window doesn’t move a different one. window.Width is the width of the window’s content area in physical pixels. The 32-pixel strip and the variation’s 96-pixel exclusion use the same units as e.X and e.Y, so no conversion is needed. If your layout uses logical units, multiply its bounds by window.Scale before comparing them with the pointer coordinates.

Why isn’t dragging my custom title bar moving the window?

On Wayland, OnePlatform uses a serial (an identifier from a recent input event) to ask the compositor for a native move. Handle the button press directly in the event loop rather than shipping it to a background task. A successful example prints that it requested a move, not that the desktop completed it.

The desktop window docs also distinguish native dragging from the built-in frame. If you keep the default frame, its title bar already supports dragging. On macOS the system frame handles moves, but StartMove() itself is a no-op in this release, so this custom-strip technique isn’t a macOS solution.

All oneplatform recipes