How can I style the border and title bar of a wind

2019-01-08 18:25发布

问题:

We are developing a WPF application which uses Telerik's suite of controls and everything works and looks fine. Unfortunately, we recently needed to replace the base class of all our dialogs, changing RadWindow by the standard WPF window (reason is irrelevant to this discussion). In doing so, we ended up having an application which still looked pretty on all developer's computers (Windows 7 with Aero enabled) but was ugly when used in our client's environment (Terminal Services under Windows Server 2008 R2).

Telerik's RadWindow is a standard user control that mimicks a dialog's behaviour so styling it was not an issue. With WPF's Window though, I have a hard time changing its "border". What I mean by "border" here is both the title bar with the icon and the 3 standard buttons (Minimize, Maximize/Restore, Close) and the resize grip around the window.

How can I change the looks of these items:

  • Title bar color
  • 3 standard buttons
  • Window's real border color

With round corners if possible.

回答1:

Those are "non-client" areas and are controlled by Windows. Here is the MSDN docs on the subject (the pertinent info is at the top).

Basically, you set your Window's WindowStyle="None", then build your own window interface. (similar question on SO)



回答2:

You need to set

WindowStyle="None", AllowsTransparency="True" and optionally ResizeMode="NoResize"
and then set the Style property of the window to your custom window style, where you design the appearance of the window (title bar, buttons, border) to anything you want and display the window contents in a ContentPresenter.

This seems to be a good article on how you can achieve this, but there are many other articles on the internet.



回答3:

I found a more straight forward solution from @DK comment in this question, the solution is written by Alex and described here with source, To make customized window:

  1. download the sample project here
  2. edit the generic.xaml file to customize the layout.
  3. enjoy :).


回答4:

Check the following sample WPF customizing the appearance of a window in XAML

This sample shows how to fully customize the style/appearance of a window, including the non-client areas: title bar, borders, max, min and close buttons whilst still providing all the expected functionality.



回答5:

I suggest that you start from a base solution and customize it to fit your needs, that's better then starting from scratch!

I was looking for the same thing and I fall on this open source solution, I hope it will help.



回答6:

If someone is saying you that you can't because that would be the non-client area and only Windows can control this, they're wrong!

That is just an half-truth because Windows lets you specify sizes to non-client area. The fact is only that this is possible throughout the Windows' kernel methods, and you're in .NET, not C++. Anyway, don't worry! P/Invoke is there just for such a thing! So, you can invoke kernel methods to setup the non-client area.

However, this is a really complicated solution I came up many times ago. Luckily, starting from .NET 4.5, you can use WindowChrome class to setup the non-client area as you want. Here you can get into MSDN.

In order to make things simpler and cleaner, I'll redirect you here, a guide to change the window border size to what you want. Setting it to 0, you'll be able to implement your custom window border in place of the system's one.

I apologize for not posting a clear example, but later I will for sure.



标签: wpf xaml styling