WPF creates a borderless form

The form frame and title bar that come with WPF only contain basic functions, which cannot satisfy the visual effect of the project. Often, it is necessary to remove the title bar of the frame.

The summary is as follows:
First, by WindowChromecustomizing the window style :

    <WindowChrome.WindowChrome>
        <WindowChrome CornerRadius="10" GlassFrameThickness="10"  ResizeBorderThickness="4" CaptionHeight="0" />
    </WindowChrome.WindowChrome>

insert image description here
Second, use one StackPanelto simulate the functionality of the title bar :
insert image description here
Finally, define the behavior via code :

public partial class MainWindow : Window
    {
    
    
        public MainWindow()
        {
    
    
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
    
    
            Window_StateChanged(null, null);
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
    
    
            if (e.ClickCount == 2)
            {
    
    
                if (this.ResizeMode != ResizeMode.CanResize && this.ResizeMode != ResizeMode.CanResizeWithGrip)
                {
    
    
                    return;
                }

                if (this.WindowState == WindowState.Maximized)
                {
    
    
                    this.WindowState = WindowState.Normal;
                }
                else
                {
    
    
                    this.WindowState = WindowState.Maximized;
                }
            }
            else
            {
    
    
                this.DragMove();
            }
        }

        private void StackPanel_MouseMove(object sender, MouseEventArgs e)
        {
    
    
            if (e.LeftButton == MouseButtonState.Pressed)
            {
    
    
                var mousePostion = e.MouseDevice.GetPosition(this);
                var point = PointToScreen(mousePostion);
                this.Left = point.X - (this.RestoreBounds.Width * 0.5);
                this.Top = point.Y;
                this.WindowState = WindowState.Normal;
                this.DragMove();
            }
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
    
    
            if (this.WindowState == WindowState.Maximized)
            {
    
    
                this.BorderThickness = new Thickness(8);
            }
            else
            {
    
    
                this.BorderThickness = new Thickness(0);
            }
        }
    }

(Note: When the window is maximized, there will be a negative 8 margin between the content and the screen, so it is Window_StateChangedjudged to manually compensate the positive 8 margin Maximizedat that time .)

Effect:

Borderless form style

Guess you like

Origin blog.csdn.net/catshitone/article/details/130932622