SoFunction
Updated on 2025-03-07

Solution to the problem of form maximization in WPF

Preface

When creating a WPF application, the first thing you see is the form class. It serves as the basis of the form and provides standard borders, toolbars, maximization, minimization and closing buttons. WPF forms are a mixture of XAML files and background code files.

This article will introduce the relevant content on the issue of maximizing WPF forms in detail. I won’t say much below. Let’s take a look at the detailed introduction together.

Problem information encountered

Question: WhenWindowStyle=NoneWhen the window is maximized, the taskbar is not displayed - that is, the form has a full-screen effect.

List of problems encountered during solution [mainly involves handling some problems after the taskbar changes]:

  • When maximizing, the taskbar is covered;
  • After maximization, drag the taskbar and the form cannot be adapted to;
  • After maximization, drag the taskbar, restore the form, and restore the data is lost, and the maximum display is always displayed;
  • After maximization, drag the taskbar, restore the form, set the previously saved form position data, and set it again. As the same as before, the form position information does not take effect;

Solution

Ideas:When the form is maximized, the form is transparent and the Margin attribute of the internal element Grid is set to display the taskbar.

step:

1. Set the form-related properties:WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanMinimize"

The form needs to support transparency, and set the form to transparency; set ResizeMode, otherwise the border will have an impact when maximizing.

2. Add the form to maximize/restore code as follows:

double normaltop;
double normalleft;
double normalwidth;
double normalheight;
/// <summary>
/// Maximize/restore processing/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_maximize_Click(object sender, RoutedEventArgs e)
{
 //WPF maximization full screen display taskbar processing if ( == )
 {
 normaltop = ;
 normalleft = ;
 normalwidth = ;
 normalheight = ;

 double top = ;
 double left = ;
 double right =  - ;
 double bottom =  - ;
 gd_main.Margin = new Thickness(left, top, right, bottom);

  = ; 
 }
 else
 {
  = ;

 //It must be set to 0 first, and the value is reset. If the values ​​before and after are the same, it will fail - after dragging the taskbar, restore - always displayed in the upper left corner of the screen  = 0;
  = 0;
  = 0;
  = 0;

  = normaltop;
  = normalleft;
  = normalwidth;
  = normalheight;

 gd_main.Margin = new Thickness(0);
 }
}

3. Add taskbar change processing

Notice:This section implementation is only applicable to .Net Framework 4.5 and above. Because the StaticPropertyChanged event is not included in the versions 4.0 and previous.
However, it can be implemented by WndProc yourself, and there will be a problem: using WndProc to listen, the result is relatively real-time, while the real-time value in SystemParameters may not keep up, so the obtained value is still old.

There are two solutions to this:

* 1. Add a Timer or Sleep directly and wait for the value in SystemParameters [This method is relatively simple, but it cannot be completely guaranteed to be valid. After all, the time when the value of SystemParameters is updated depends on the .Net Framework];

* 2. Use the Windows API to read system values, and use the method SystemParametersInfo to obtain SPI_GETWORKAREA [Microsoft official actually used this to obtain it, which is more efficient than my following]

In addition: The following method may have system compatibility issues. I passed it on Windows 10, but on Windows 8.1, there is a problem with the boundary [not sure if there is a bug in the Framework on the system]

Registration Events: += SystemParameters_StaticPropertyChanged;

Add the following code:

private void SystemParameters_StaticPropertyChanged(object sender,  e)
{
 if ( == "WorkArea")
 {
 if ( == )
 {
  double top = ;
  double left = ;
  double right =  - ;
  double bottom =  - ;
  gd_main.Margin = new Thickness(left, top, right, bottom);
 }
 }
}

Related downloads

Click to view the full source code

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.