How to remove window's caption and border in WPF project?

aluzi liu 486 Reputation points

VS2022 .NET 6.0 I want a window without caption bar and border, but keep resizeable, I can do this by setting AllowTransparency=True WindowStyle=None plus handle WM_HITTEST message and return HTLEFT, HTRIGHT, HTBOTTOMRIGHT... properly, it can restore the resize function it look like: wpf-01

The problem is, AllowTranparency will casuse performance issue, I don't want to use it. I read some Window API documentation, I try this:

        protected override void OnSourceInitialized(EventArgs e)

            var hwnd = new WindowInteropHelper(Window.GetWindow(this)).Handle;
            //Install PInvoke.User32 nuget package            
            _ = SetWindowLong(hwnd, WindowLongIndexFlags.GWL_STYLE, (SetWindowLongFlags)(GetWindowLong(hwnd, WindowLongIndexFlags.GWL_STYLE) & ~(int)SetWindowLongFlags.WS_CAPTION));

And I got this: wpf-02

The window is still bordered and mini caption bar on top, so I try again:

_ = SetWindowLong(hwnd, WindowLongIndexFlags.GWL_STYLE, (SetWindowLongFlags)(GetWindowLong(hwnd, WindowLongIndexFlags.GWL_STYLE) & ~(int)SetWindowLongFlags.WS_CAPTION & ~(int)SetWindowLongFlags.WS_SIZEBOX));

I remove the "WS_SIZEBOX" style, and I got what I want except that the window can not resize, even I handle WM_HITTEST message So, how to achieve same effect as AlllowTransparency but without using AlllowTransparency?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 82,036 Reputation points

    A way is to do it in XAML. For example :

            ResizeBorderThickness="3" />