WPF Custom window, maximized without overwriting the taskbar

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/jying/archive/2013/07/09/3180194.html


It is believed that many people use WPF to choose a custom beautiful window, so the windowstyle= "None" will be set to remove the title bar from the band. But this uses windowstate= "maximized" or background this. WindowState = System.Windows.WindowState.Maximized; The maximized window overwrites the system taskbar, which is full screen. It's not really a good experience.

Find the answer on the Internet, the top ranked is to provide hook hooks, length, such as: http://www.cnblogs.com/zhouyinhui/archive/2008/11/04/1326188.html

Personally feel such a small function to add so much code is inhuman, so continue to look for, and finally see the dawn of the Dawn:

         Rect rcnormal;//defines a global rect record for the position and size of the window in the restore state. <summary>//Maximize///</summary> private void Btnmaximize_click (object sender, Rou
            Tedeventargs e) {this.btnMaximize.Visibility = visibility.collapsed;
            this.btnNormal.Visibility = visibility.visible; Rcnormal = new Rect (this. Left, this. Top, this. Width, this. Height);//Save the current position and size of this. left = 0;//Set position this.            
            Top = 0; Rect rc = systemparameters.workarea;//Gets the size of the workspace this. Width = RC.
            Width; This. Height = RC.
        Height; }///<summary>//restore//</summary> private void Btnnormal_click (object send Er, RoutedEventArgs e) {this. left = Rcnormal.
            Left; This. Top = Rcnormal.
            Top; This. Width = Rcnormal.
            Width; This. Height = Rcnormal.
            Height; This.btnMaximize.Visibility = Visibility.Visible;
        this.btnNormal.Visibility = visibility.collapsed; }

OK, maximize and minimize the event customization OK. Well, the function should be enough. Actually not enough, let us think, usually when we drag the window, if the window is dragged to the top of the mouse out of bounds, the window will be maximized is not. In WPF windowstyle= "None" is also full-screen effect, and will overwrite our custom effect, you can try, this time your this.width and this.height are useless.

What should I do then? Look below:

Add at foreground:

Sizechanged= "Window_sizechanged"

Background:

        private void Window_sizechanged (object sender, Sizechangedeventargs e)
        {
            if (this. ActualHeight > SystemParameters.WorkArea.Height | | This. ActualWidth > SystemParameters.WorkArea.Width)
            {this
                . WindowState = System.Windows.WindowState.Normal;
                Btnmaximize_click (null, NULL);
            }
        }

OK, get it done.

So simple code, I believe you can read it ~ ~

Another double-click title bar event:

        private void Grid_mousedown (object sender, MouseButtonEventArgs e)
        {
            if (E.clickcount = = 2)
            {
                if (this. ActualWidth = = SystemParameters.WorkArea.Width)
                {
                    btnnormal_click (null, NULL);
                }
                else
                {
                    btnmaximize_click (null, NULL);
                }
            }
        }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.