“Blog move” original address: CSDN original published time: 2016-11-07

This paper is to achieve a cool GUI window based on WPF, trying to achieve a really usable and practical GUI window.

The effect to be achieved in this article is shown in the figure below:

The results are summarized as follows:

  • No title bar, or a custom title bar
  • Translucent window effect
  • Windows can be dragged freely
  • Default keys such as Enter and Esc
  • Right-click menu

1. Window effect realization

1.1 Window appearance

First create a new window and change the appearance of the window. Set the properties of the window as follows:

  • In appearance Settings, check the “AllowsTransparency” check box to make the window transparent, and “WindowStyle” will automatically change to “None”. The default title bar of the window will disappear. In this case, you can choose to customize the title bar.

  • Once the window is transparent, set the Background and border color of the form in the options “Background”, “BorderBrush”, etc. You can set it to a solid color or a variety of gradient shapes and a variety of transparent shapes.

  • Use the “BorderThickness” TAB to set the border width of the form.

  • “Opacity” sets the Opacity of the entire interface

The setting interface is as follows:

1.2 Window size and position are related

To change the position of the window, set the following properties:

  • WindowStartupLocation: Changes the location when a window is opened
  • WindowState: Changes the size state of the window when it is opened. You can set the full-screen window
  • Topmost: This window is the top
  • Width and Height: dimensions when the window is open
  • MinWidth and MinHeight: Minimum window size

2. Facilitate the realization of the function of window operation

To facilitate window operation, you can set the following parameters:

  • Set the properties of a button, IsCancel and IsDefault, and the buttons triggered by the Enter and Esc keys.

  • Set drag window can be dragged anywhere, you can set the window event call method, set MouseLeftButtonDown event call method.

The setting method is as follows:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
}
Copy the code

Click the close button to end the entire application, you can set the callback method of the close event, also can set the method of the button click event, as follows:

private void btnExit_Click(object sender, RoutedEventArgs e)
{
    Environment.Exit(0);
}
Copy the code

To implement the window’s right-click menu and click events, you need to set the window’s “ContextMenu” property, or add the following content in the Xaml file:

<Window.ContextMenu>
    <ContextMenu Name="ChangePassword">
        <MenuItem Header="Change login Password"
                  Click="MenuItemChangePassword_Click" />
        <MenuItem Header="About"
                  Click="MenuItemAbout_Click" />
    </ContextMenu>
</Window.ContextMenu>
Copy the code