开发者

WPF Fullscreen in RibbonWindow

开发者 https://www.devze.com 2023-03-26 07:40 出处:网络
I created an application using Microsoft ribbon for WPF. I used RibbonWindow instead of simple Window to place QuickAccessToolbar to window header.

I created an application using Microsoft ribbon for WPF. I used RibbonWindow instead of simple Window to place QuickAccessToolbar to window header.

The problem is that normal Window becomes fullscreen when i set

  WindowStyle="None"
  WindowState="Maximized"

But RibbonWindow becomes bigger and its bottom part hides behind开发者_JAVA技巧 taskbar.

I suppose that the only difference between window and RibbonWindows is the controlTemplate. But i dont actually understand how can i via template place the window above the taskbar.

Any ideas how to show my RibbonWindow above taskbar just as normal window does?

Link to the VS2010 project (10KiB) (Microsoft Ribbon For WPF isn't included)


RibbonWindow uses custom WindowChrome. That is the reason of incorrect behavior. Try this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Windows.Shell;

namespace Utils
{
    public static class WindowHelper
    {
        public static void Fullscreen(Window p_oWindow)
        {
            p_oWindow.WindowStyle = WindowStyle.None;
            p_oWindow.Topmost = true;
            p_oWindow.WindowState = WindowState.Maximized;
            WindowChrome.SetWindowChrome(p_oWindow, null);
        }

        public static void UndoFullscreen(Window p_oWindow)
        {
            p_oWindow.WindowStyle = WindowStyle.SingleBorderWindow;
            p_oWindow.Topmost = false;
            p_oWindow.WindowState = WindowState.Normal;
            WindowChrome.SetWindowChrome(p_oWindow, GetChrome());
        }

        public static WindowChrome GetChrome()
        {
            WindowChrome oCustomChrome = new WindowChrome();
            oCustomChrome.CornerRadius = new System.Windows.CornerRadius(0, 0, 0, 0);
            oCustomChrome.CaptionHeight = 0;
            oCustomChrome.ResizeBorderThickness = new System.Windows.Thickness(2, 2, 2, 2);
            return oCustomChrome;
        }
    }
}


The problem is the WindowStyle. This happens in all WPF windows when you remove the chrome. (They really optimized this feature for full-screen kiosk apps).

In order to handle maximizing your window to the correct size, you're going to need to handle it yourself. When you maximize button is clicked, you will need to get the working area of the current monitor you're on. There isn't a WPF way of doing this, but you can use the WinForms Screen class.

Your maximize method would look like so:

// you must save the restore bounds, since we never set Window.WindowState,
// so the RestoreBounds property will get set to the maximized bounds
private System.Drawing.Rectangle restoreBounds;

private void DoMaximize(object sender, EventArgs e)
{
    var bounds = new System.Drawing.Rectangle((int)Left, (int)Top,
        (int)ActualWidth, (int)ActualHeight));
    var screen = System.Windows.Forms.Screen.FromRectangle(bounds);
    var area = screen.WorkingArea;

    restoreBounds = bounds; 

    Left = area.X;            
    Top = area.Y;          
    Width = area.Width;
    Height = area.Height;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消