Olson CloudWorks 🚀

How to hide close button in WPF window

September 19, 2026

📂 Categories: C#
How to hide close button in WPF window

Windows Presentation Foundation (WPF) offers developers a robust framework for building visually stunning and feature-rich desktop applications. One common customization request when crafting WPF applications is modifying the appearance and behavior of the window, including the ability to hide close button in WPF window. While the default window chrome provides standard system buttons like minimize, maximize, and close, there are situations where developers need more granular control. Perhaps you’re creating a kiosk application, a modal dialog that requires specific user interaction before closing, or simply want a more streamlined user interface. Understanding how to manipulate these window elements is crucial for creating a polished and professional application experience. This article will explore various techniques to hide close button in WPF window, providing clear examples and best practices to ensure your application behaves exactly as intended. We’ll delve into different approaches, from simple XAML manipulations to more advanced code-behind solutions, empowering you to tailor your WPF application windows to meet your specific design requirements.

Understanding WPF Window Styles and Chrome

Before diving into specific techniques for hiding the close button, it’s important to understand the underlying structure of a WPF window. WPF windows are composed of two primary parts: the non-client area (also known as the window chrome) and the client area. The non-client area is managed by the operating system and includes the title bar, system menu, and the minimize, maximize, and close buttons. The client area is where your application content resides – the controls, layouts, and other visual elements that define your application’s functionality. When you want to hide close button in WPF window, you’re essentially manipulating the non-client area.

WPF provides several ways to customize the window chrome. One common approach is to set the WindowStyle property to None. This removes the entire window chrome, including the title bar and system buttons, giving you complete control over the window’s appearance. However, this also means you’ll need to implement your own mechanisms for moving, resizing, and closing the window. Another approach involves using the WindowChrome class, which allows you to customize the non-client area while still retaining some of the default system behaviors. Understanding these fundamental concepts is crucial for choosing the right method for hiding the close button in your specific scenario. Consider the trade-offs between complete control and maintaining standard window behaviors.

It’s also worth noting that the appearance of the window chrome can be influenced by the operating system’s theme and settings. Therefore, your application should be designed to adapt gracefully to different environments. By leveraging WPF’s styling and templating capabilities, you can create a consistent and visually appealing user experience across various operating systems and themes. Remember, accessibility is key, and any custom window chrome implementation should ensure that users can still easily interact with the window using keyboard navigation and screen readers. Microsoft’s documentation on WPF windows offers a comprehensive overview of these concepts.

Methods to Hide the Close Button

There are several ways to hide close button in WPF window, each with its own advantages and disadvantages. The best approach depends on your specific requirements and the level of customization you need. Let’s explore some of the most common methods:

  • Using WindowStyle=“None”: This is the most straightforward approach, completely removing the window chrome. However, it requires you to implement your own title bar, system buttons, and window dragging/resizing logic.
  • Interoperating with Win32 API: This method involves using platform-specific code to directly manipulate the window’s style. It offers fine-grained control but increases complexity and reduces portability.
  • Using a Custom WindowChrome: This approach allows you to customize the non-client area while retaining some of the default system behaviors. It provides a balance between control and convenience.

Featured Snippet: One of the most common and simplest methods to hide close button in WPF window is by using interop with the Win32 API. By accessing the window’s handle and modifying its style flags, you can effectively disable the close button. This involves using the GetWindowLong and SetWindowLong functions from the user32.dll library. The GWL_STYLE flag is used to retrieve the current window style, and the WS_SYSMENU flag (combined with ~ for bitwise NOT) is used to remove the system menu, which includes the close button.

Here’s a breakdown of each method:

Using WindowStyle=“None”

Setting the WindowStyle property to None in your XAML code is the simplest way to remove the close button. This effectively removes the entire window chrome, giving you complete control over the window’s appearance. However, it also means you’re responsible for implementing your own title bar, system buttons (minimize, maximize, close), and window dragging/resizing logic. This approach is suitable for applications that require a highly customized window appearance and don’t rely on the default window behaviors. For example, a full-screen kiosk application might benefit from this approach. When using WindowStyle=“None”, it’s crucial to provide alternative ways for users to interact with the window, such as custom buttons or keyboard shortcuts to close or minimize the application.

To implement this, simply add WindowStyle=“None” to your Window element in XAML:

xml Remember that you’ll need to handle window dragging and closing manually. You can implement window dragging by handling the MouseDown event on a custom title bar and using the DragMove() method. For closing the window, you can add a custom button with a Click event handler that calls the Close() method.

Interoperating with Win32 API

For more fine-grained control, you can interoperate with the Win32 API to directly manipulate the window’s style. This involves using platform-specific code, making your application less portable. However, it allows you to selectively disable the close button without removing the entire window chrome. This method is particularly useful when you want to retain the default title bar and other system buttons while preventing the user from closing the window. Interoperability with Win32 API is an LSI keyword.

Here’s how to do it:

  1. Add the following code to your code-behind file: csharp using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; public partial class YourWindow : Window { private const int GWL_STYLE = -16; private const int WS_SYSMENU = 0x80000; [DllImport(“user32.dll”, SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport(“user32.dll”, SetLastError = true)] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport(“user32.dll”)] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport(“user32.dll”)] private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); private const uint SC_CLOSE = 0xF060; private const uint MF_GRAYED = 0x00000001; private const uint MF_ENABLED = 0x00000000; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); var hwnd = new WindowInteropHelper(this).Handle; SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU); //Alternatively to disable the close button //IntPtr hMenu = GetSystemMenu(hwnd, false); //if (hMenu != IntPtr.Zero) //{ // EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED); //} } }
  2. Call this code in the OnSourceInitialized event handler of your window. This event is raised after the window’s handle has been created.

This code retrieves the window’s handle, removes the WS_SYSMENU style flag (which includes the close button), and updates the window’s style. This effectively hides the close button while retaining the other system buttons. The alternative approach disables the close menu item.

Using a Custom WindowChrome

The WindowChrome class provides a more flexible way to customize the non-client area of a WPF window. It allows you to define custom regions for the title bar, system buttons, and window borders. This approach is particularly useful when you want to create a custom window appearance while still retaining some of the default system behaviors, such as window dragging and resizing. To use WindowChrome, you need to add the System.Windows.Shell assembly to your project.

Here’s how to use WindowChrome:

  1. Add a reference to the System.Windows.Shell assembly in your project.
  2. Add the following XAML markup to your window: xml shell:windowchrome.windowchrome <shell:windowchrome captionheight=“30” cornerradius=“0” glassframethickness=“0” useaerocaptionbuttons=“False”></shell:windowchrome> </shell:windowchrome.windowchrome>
  3. Customize the WindowChrome properties to define the appearance and behavior of the non-client area. You can set the CaptionHeight to define the height of the title bar, the CornerRadius to control the window’s corners, and the GlassFrameThickness to specify the thickness of the glass frame (if Aero is enabled). Setting UseAeroCaptionButtons=“False” allows you to create your own caption buttons.

With WindowChrome, you can create custom buttons for minimize, maximize, and close and handle their click events to implement the corresponding window behaviors. This gives you complete control over the appearance and functionality of the system buttons. You can also define a custom region for the title bar and handle the MouseDown event to implement window dragging. Learn more about WindowChrome from Microsoft.

Best Practices and Considerations

When deciding how to hide close button in WPF window, there are several best practices and considerations to keep in mind. First and foremost, consider the user experience. Hiding the close button can be confusing or frustrating for users if they don’t understand how to close the window. Ensure that you provide alternative ways to close the window, such as custom buttons or keyboard shortcuts. Clearly communicate to the user how to exit the application or dialog.

Secondly, consider accessibility. Ensure that your application is accessible to users with disabilities. If you’re removing the close button, make sure that users can still close the window using keyboard navigation or screen readers. Provide alternative input methods and ensure that all interactive elements are properly labeled. Following accessibility guidelines ensures inclusivity. According to a report by the World Health Organization, approximately 15% of the world’s population has some form of disability. Designing accessible applications is not only ethical but also expands your user base. Web Content Accessibility Guidelines (WCAG) offer valuable insights.

Finally, consider the maintainability of your code. Choose the method that best balances control, convenience, and maintainability. Avoid using overly complex or platform-specific code unless absolutely necessary. Use clear and concise code that is easy to understand and maintain. Document your code thoroughly to ensure that other developers can easily understand and modify it. Remember, clean and well-documented code is essential for long-term maintainability.

Infographic here
FAQ: Hiding the Close Button in WPF -----------------------------------
****Question & Answer :**** I'm writing a modal dialog in WPF. How do I set a WPF window to not have a close button? I'd still like for its `WindowState` to have a normal title bar.

I found ResizeMode, WindowState, and WindowStyle, but none of those properties allow me to hide the close button but show the title bar, as in modal dialogs.

WPF doesn’t have a built-in property to hide the title bar’s Close button, but you can do it with a few lines of P/Invoke.

First, add these declarations to your Window class:

private const int GWL_STYLE = -16; private const int WS_SYSMENU = 0x80000; [DllImport("user32.dll", SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 

Then put this code in the Window’s Loaded event:

var hwnd = new WindowInteropHelper(this).Handle; SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU); 

And there you go: no more Close button. You also won’t have a window icon on the left side of the title bar, which means no system menu, even when you right-click the title bar - they all go together.

Important note: all this does is hide the button. The user can still close the window! If the user presses Alt+F4, or closes the app via the taskbar, the window will still close.

If you don’t want to allow the window to close before the background thread is done, then you could also override OnClosing and set Cancel to true, as Gabe suggested.