Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WPF Window.WindowStyle=none保持任务栏的可见性_Wpf_Window_Taskbar_Window Style - Fatal编程技术网

使用WPF Window.WindowStyle=none保持任务栏的可见性

使用WPF Window.WindowStyle=none保持任务栏的可见性,wpf,window,taskbar,window-style,Wpf,Window,Taskbar,Window Style,我正在开发一个WPF应用程序。我完全摆脱了标准的windows边框,通过将WindowsStyle设置为None并将AllowTransparency设置为true创建了自己的边框。唯一的问题是,当窗口最大化时,它会填满整个屏幕,覆盖任务栏。有没有办法创建自己的边框而不发生这种情况?请查看Microsoft.Windows.Shell。它为你做了所有这些和更多。追踪它有点令人沮丧。我认为最好使用的是随附的。看看Microsoft.Windows.Shell。它为你做了所有这些和更多。追踪它有点令

我正在开发一个WPF应用程序。我完全摆脱了标准的windows边框,通过将WindowsStyle设置为None并将AllowTransparency设置为true创建了自己的边框。唯一的问题是,当窗口最大化时,它会填满整个屏幕,覆盖任务栏。有没有办法创建自己的边框而不发生这种情况?

请查看Microsoft.Windows.Shell。它为你做了所有这些和更多。追踪它有点令人沮丧。我认为最好使用的是随附的。

看看Microsoft.Windows.Shell。它为你做了所有这些和更多。追踪它有点令人沮丧。我认为最好使用的方法是。

使用方法兼容性最大化窗口中包含的方法

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

using Point = System.Drawing.Point;

internal static class WindowExtensions
{
    public static PointF StandartDPI = new PointF(96f, 96f);
    private static PointF? currentDpi = null;
    public static float WidthZoom {get{return CurrentDPI.X/ StandartDPI.X; } }
    public static float HeigthZoom { get { return CurrentDPI.Y / StandartDPI.Y; } }

    public static PointF CurrentDPI
    {
        get
        {
            if (!currentDpi.HasValue)
            {
                var wiHelper = new WindowInteropHelper(App.Current.MainWindow);
                Graphics g = Graphics.FromHwnd(wiHelper.Handle);
                try
                {
                    currentDpi = new PointF(g.DpiX, g.DpiY);
                }
                catch
                {
                    currentDpi = StandartDPI;
                }
                finally
                {
                    g.Dispose();
                }
            }
            return currentDpi.Value;
        }
    }


    public static Window GetWindowFromTemplate(this object templateFrameworkElement)
    {
        var window = ((FrameworkElement)templateFrameworkElement).TemplatedParent as Window;
        return window;
    }

    private static int minWidth;
    private static int minHeight;

    public static void CompatibilityMaximizedNoneWindow(this Window window)
    {
        var wiHelper = new WindowInteropHelper(window);
        minHeight = (int)window.MinHeight;
        minWidth = (int)window.MinWidth;

        var handle = wiHelper.Handle;
        var hwndSource = HwndSource.FromHwnd(handle);
        if (hwndSource != null)
        {
            hwndSource.AddHook(CompatibilityMaximizedNoneWindowProc);
        }
    }

    [DllImport("user32")]
    private static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);[DllImport("user32")]
    private static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
    private class MONITORINFO
    {
        public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
        public RECT rcMonitor = new RECT();
        public RECT rcWork = new RECT();
        public int dwFlags = 0;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int x;
        public int y;

        public POINT(int x, int y)
        {
            this.x = x; this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    }

    private static IntPtr CompatibilityMaximizedNoneWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        const int WM_GETMINMAXINFO = 0x0024;
        const int MONITOR_DEFAULTTONEAREST = 0x00000002;
        switch (msg)
        {
            case WM_GETMINMAXINFO:
                var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
                var monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
                if (monitor != IntPtr.Zero)
                {
                    var monitorInfo = new MONITORINFO();
                    GetMonitorInfo(monitor, monitorInfo);
                    var rcWorkArea = monitorInfo.rcWork;
                    var rcMonitorArea = monitorInfo.rcMonitor;
                    mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                    mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                    mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                    mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
                    //Hack для ограничения минимальных размеров окна
                    mmi.ptMinTrackSize.x = (int)(minWidth*WidthZoom);
                    mmi.ptMinTrackSize.y = (int)(minHeight*HeigthZoom);
                    //Hack для нормальной работы всплывающей панели задач
                    var taskbar = new TaskbarInfo();
                    if (taskbar.AutoHide)
                    {
                        switch (taskbar.Position)
                        {
                            case TaskbarInfo.TaskbarPosition.Top:
                                mmi.ptMaxPosition.y++;
                                mmi.ptMaxSize.y--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Bottom:
                                mmi.ptMaxPosition.y--;
                                mmi.ptMaxSize.y--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Left:
                                mmi.ptMaxPosition.x++;
                                mmi.ptMaxSize.x--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Right:
                                mmi.ptMaxPosition.x--;
                                mmi.ptMaxSize.x--;
                                break;
                        }
                    }
                }
                Marshal.StructureToPtr(mmi, lParam, true); handled = true; break;
        }
        return (IntPtr)0;
    }
}

使用方法CompatibilityMaximizedNone窗口

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

using Point = System.Drawing.Point;

internal static class WindowExtensions
{
    public static PointF StandartDPI = new PointF(96f, 96f);
    private static PointF? currentDpi = null;
    public static float WidthZoom {get{return CurrentDPI.X/ StandartDPI.X; } }
    public static float HeigthZoom { get { return CurrentDPI.Y / StandartDPI.Y; } }

    public static PointF CurrentDPI
    {
        get
        {
            if (!currentDpi.HasValue)
            {
                var wiHelper = new WindowInteropHelper(App.Current.MainWindow);
                Graphics g = Graphics.FromHwnd(wiHelper.Handle);
                try
                {
                    currentDpi = new PointF(g.DpiX, g.DpiY);
                }
                catch
                {
                    currentDpi = StandartDPI;
                }
                finally
                {
                    g.Dispose();
                }
            }
            return currentDpi.Value;
        }
    }


    public static Window GetWindowFromTemplate(this object templateFrameworkElement)
    {
        var window = ((FrameworkElement)templateFrameworkElement).TemplatedParent as Window;
        return window;
    }

    private static int minWidth;
    private static int minHeight;

    public static void CompatibilityMaximizedNoneWindow(this Window window)
    {
        var wiHelper = new WindowInteropHelper(window);
        minHeight = (int)window.MinHeight;
        minWidth = (int)window.MinWidth;

        var handle = wiHelper.Handle;
        var hwndSource = HwndSource.FromHwnd(handle);
        if (hwndSource != null)
        {
            hwndSource.AddHook(CompatibilityMaximizedNoneWindowProc);
        }
    }

    [DllImport("user32")]
    private static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);[DllImport("user32")]
    private static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
    private class MONITORINFO
    {
        public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
        public RECT rcMonitor = new RECT();
        public RECT rcWork = new RECT();
        public int dwFlags = 0;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int x;
        public int y;

        public POINT(int x, int y)
        {
            this.x = x; this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    }

    private static IntPtr CompatibilityMaximizedNoneWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        const int WM_GETMINMAXINFO = 0x0024;
        const int MONITOR_DEFAULTTONEAREST = 0x00000002;
        switch (msg)
        {
            case WM_GETMINMAXINFO:
                var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
                var monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
                if (monitor != IntPtr.Zero)
                {
                    var monitorInfo = new MONITORINFO();
                    GetMonitorInfo(monitor, monitorInfo);
                    var rcWorkArea = monitorInfo.rcWork;
                    var rcMonitorArea = monitorInfo.rcMonitor;
                    mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                    mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                    mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                    mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
                    //Hack для ограничения минимальных размеров окна
                    mmi.ptMinTrackSize.x = (int)(minWidth*WidthZoom);
                    mmi.ptMinTrackSize.y = (int)(minHeight*HeigthZoom);
                    //Hack для нормальной работы всплывающей панели задач
                    var taskbar = new TaskbarInfo();
                    if (taskbar.AutoHide)
                    {
                        switch (taskbar.Position)
                        {
                            case TaskbarInfo.TaskbarPosition.Top:
                                mmi.ptMaxPosition.y++;
                                mmi.ptMaxSize.y--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Bottom:
                                mmi.ptMaxPosition.y--;
                                mmi.ptMaxSize.y--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Left:
                                mmi.ptMaxPosition.x++;
                                mmi.ptMaxSize.x--;
                                break;
                            case TaskbarInfo.TaskbarPosition.Right:
                                mmi.ptMaxPosition.x--;
                                mmi.ptMaxSize.x--;
                                break;
                        }
                    }
                }
                Marshal.StructureToPtr(mmi, lParam, true); handled = true; break;
        }
        return (IntPtr)0;
    }
}

更简单的方法是,在您的窗口构造函数中设置

this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;

窗口将不会覆盖任务栏。

更简单的方法是,在您的窗口构造函数中,只需设置

this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
窗口不会覆盖任务栏