How to create a single window style of the developed application for all versions of the OS the same?

I looked through various articles, but all the options do not fit, because there is lost the opportunity to expand the window and transfer it around the screen.

Tell me, please, how to create a custom WPF window style without losing all the main functionality, and be able to transfer and resize it?

  • I can advise the video lessons that I was advised at the time. There the author considers the process of creating a custom window with the functions of a standard window. The only negative - lessons in English. - Egor Trutnev
  • @EgorTrutnev does not matter English or Russian) thanks, I will get acquainted with the proposed video) But I hope there will be gurus who can offer their options. - UporotayaPanda

1 answer 1

Nothing complicated:

<Style TargetType="Window"> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Background" Value="DarkGray"/> <Setter Property="Title" Value="My custom window"/> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <WindowChrome ResizeBorderThickness="5" CaptionHeight="30" GlassFrameThickness="0" CornerRadius="0"/> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Window"> <Border BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"> <Grid Background="Transparent"> <!-- Title bar --> <Border Background="White" Height="30" VerticalAlignment="Top"> <Grid> <!-- Window title --> <TextBlock Text="{TemplateBinding Title}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"/> <!-- Window control buttons stack --> <StackPanel HorizontalAlignment="Right"> <!-- Close button, for example --> <Button Width="40" Height="30" Background="White" WindowChrome.IsHitTestVisibleInChrome="True" Click="Button_Click"> <Path Data="M0,0 L20,20 M0,20 L20,0" Stroke="Black" StrokeThickness="1"/> </Button> </StackPanel> </Grid> </Border> <!-- Window content --> <AdornerDecorator> <ContentPresenter Margin="0,30,0,0"/> </AdornerDecorator> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Style> 

Result:

enter image description here

Notes

So, I suppose you are new to this topic (once you have given courses to you) and it would not hurt you to "sort through" everything.

So, since you need the same type of window on different versions of Windows, you cannot do without overriding the frame. You can even remove it and draw your own (set WindowStyle to None ), but we will use a more intelligent solution: we use a special class for this - WindowChrome while not changing WindowStyle (although this makes no difference, just like the standard Windows collapse / expand animations).

WindowChrome.ResizeBorderThickness marks the width of the resize window. GlassFrameThickness can be set to -1 to enable the standard window shadow, but then on Windows 10 systems where ppi is above 130, the effect of distortion of the text will be strongly observed - I do not advise.

CaptionHeight - the CaptionHeight the window title, i.e. area for which you can "drag the window."

Draw the buttons yourself.

In general, read this , and you can also go to my profile - there are a lot of topics with redefining window templates.

  • Thank you very much! I figured it out with the theme, but forgot to thank it (Also, for those who will be implementing this on .Net 4.0 and below, you need to install an additional library. - UporotayaPanda
  • Yes. Because in .NET 4.0 there is no System.Windows.Shell namespace. We'll have to connect this assembly separately and "drag along." On Windows XP where there is no dwm at all it will be useless. - D .Stark