【WPF小功能】自定义窗体 最大化的时候遮挡任务栏--解决方案

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_29484257/article/details/87938151
public class CustomWindow:Window
    {
        public CustomWindow()
        {
            DefaultStyleKey = typeof(CustomWindow);
            CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow, CanMinimizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow));
            CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu));
            this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        }

碰到这个问题的时候,百度了很多,都是用钩子啊windowsAPI 或者winform的解决方法,或者还有用屏幕实际宽高来算Left,Top的,就算能实现 感觉写那么多代码就是把简单的问题变得复杂,心理不爽呀。

然后就看到了这个答案哈哈

https://stackoverflow.com/questions/13425088/is-it-possible-to-specify-the-maximizedbounds-of-a-wpf-window-via-pinvoke

简单易懂哈哈哈,在窗体定义的时候 限定掉最大宽高就行啦哈哈哈哈,这个真的就两句话结束。

一定要在窗体定义哦,不要放最大化按键时间那里。 否则拖动或者双击标题栏还是会遮挡任务栏的。

搜了好多博客都没有这个答案,先记录下

以上。

展开阅读全文

WPF进制窗体最大化功能

09-02

如题,想让该窗体不可以最大化 该窗体为无边框的 可以改变大小的功能都已经实现 现在点中窗体的标题部分(自己做的假标题,只是为了能移动窗体),向上移动到最上边,该窗体就会变大,布局也乱了,有什么办法可以让该窗体不能最大化而且不影响改变大小的代码呢?rnrnrn说白了点就是最好能让窗体在点中标题拖动到屏幕最上边的时候不最大化 ResizeMode=Norisize;可以实现但是却改变不了大小了rnrnrn下边是改变窗体大小的代码rnrn[code=csharp]#region 配合改变窗口大小的枚举rnrn //写在当前项目的命名空间中即可rnrn public enum HitTest : intrn rn HTERROR = -2,rn HTTRANSPARENT = -1,rn HTNOWHERE = 0,rn HTCLIENT = 1,rn HTCAPTION = 2,rn HTSYSMENU = 3,rn HTGROWBOX = 4,rn HTMENU = 5,rn HTHSCROLL = 6,rn HTVSCROLL = 7,rn HTMINBUTTON = 8,rn HTMAXBUTTON = 9,rn HTLEFT = 10,rn HTRIGHT = 11,rn HTTOP = 12,rn HTTOPLEFT = 13,rn HTTOPRIGHT = 14,rn HTBOTTOM = 15,rn HTBOTTOMLEFT = 16,rn HTBOTTOMRIGHT = 17,rn HTBORDER = 18,rn HTOBJECT = 19,rn HTCLOSE = 20,rn HTHELP = 21,rn rn #endregion[/code]rnrn[code=csharp]#region 改变窗体大小rn//写入Class中rn protected override void OnSourceInitialized(EventArgs e)rn rn base.OnSourceInitialized(e);rnrn HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;rn if (hwndSource != null)rn rn hwndSource.AddHook(new HwndSourceHook(this.WndProc));rn rn rnrn private const int WM_NCHITTEST = 0x0084;rn private readonly int agWidth = 12; //拐角宽度rn private readonly int bThickness = 6; // 边框宽度rn private Point mousePoint = new Point(); //鼠标坐标rnrn protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)rn rn switch (msg)rn rn case WM_NCHITTEST:rn this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);rn this.mousePoint.Y = (lParam.ToInt32() >> 16);rnrn #region 测试鼠标位置rnrn // 窗口左上角rn if (this.mousePoint.Y - this.Top <= this.agWidthrn && this.mousePoint.X - this.Left <= this.agWidth)rn rn handled = true;rn return new IntPtr((int)HitTest.HTTOPLEFT);rn rn // 窗口左下角 rn else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidthrn && this.mousePoint.X - this.Left <= this.agWidth)rn rn handled = true;rn return new IntPtr((int)HitTest.HTBOTTOMLEFT);rn rn // 窗口右上角rn else if (this.mousePoint.Y - this.Top <= this.agWidthrn && this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)rn rn handled = true;rn return new IntPtr((int)HitTest.HTTOPRIGHT);rn rn // 窗口右下角rn else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidthrn && this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth)rn rn handled = true;rn return new IntPtr((int)HitTest.HTBOTTOMRIGHT);rn rn // 窗口左侧rn else if (this.mousePoint.X - this.Left <= this.bThickness)rn rn handled = true;rn return new IntPtr((int)HitTest.HTLEFT);rn rn // 窗口右侧rn else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)rn rn handled = true;rn return new IntPtr((int)HitTest.HTRIGHT);rn rn // 窗口上方rn else if (this.mousePoint.Y - this.Top <= this.bThickness)rn rn handled = true;rn return new IntPtr((int)HitTest.HTTOP);rn rn // 窗口下方rn else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)rn rn handled = true;rn return new IntPtr((int)HitTest.HTBOTTOM);rn rn else // 窗口移动rn rn handled = false;rn return new IntPtr((int)HitTest.HTCAPTION);rn rn #endregionrn rn return IntPtr.Zero;rn rn #endregion[/code] 论坛

求教多显示器下WPF自定义窗体最大化问题!!!

11-26

问题是这样的,设置了WindowStyle="None" AllowsTransparency="True"之后,自己做了个标题栏,实现了缩放与最大化问题,但是最大化时会遮住任务栏。rn然后就上网查了下, 这个问题还是很好解决的。rn但是。。在多显示器情况下不行了rn如果副显示器的分辨率比主显示器分辨率高的话,最大化时窗体仍然会盖住副显示器的任务栏。rn下面上最大化的解决代码,求大神指教。。rn[code=csharp]this.SourceInitialized += MainWindow_SourceInitialized;rnvoid MainWindow_SourceInitialized(object sender, EventArgs e)rn rn HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);rn if (source == null)rn // Should never be null rn throw new Exception("Cannot get HwndSource instance.");rnrn source.AddHook(new HwndSourceHook(this.WndProc));rn rnprivate IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)rn rnrn switch (msg)rn rn case Win32.WM_GETMINMAXINFO: // WM_GETMINMAXINFO message rn WmGetMinMaxInfo(hwnd, lParam);rn handled = true;rn break;rn // case Win32.WM_NCHITTEST: // WM_NCHITTEST message rn // return WmNCHitTest(lParam, ref handled);rn rnrn return IntPtr.Zero; rn rnrn private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)rn rn // MINMAXINFO structure rn Win32.MINMAXINFO mmi = (Win32.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(Win32.MINMAXINFO));rnrn // Get handle for nearest monitor to this window rn WindowInteropHelper wih = new WindowInteropHelper(this);rn IntPtr hMonitor = Win32.MonitorFromWindow(wih.Handle, Win32.MONITOR_DEFAULTTONEAREST);rnrn // Get monitor info rn Win32.MONITORINFOEX monitorInfo = new Win32.MONITORINFOEX();rn monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);rn Win32.GetMonitorInfo(new HandleRef(this, hMonitor), monitorInfo);rnrn // Get HwndSource rn HwndSource source = HwndSource.FromHwnd(wih.Handle);rn if (source == null)rn // Should never be null rn throw new Exception("Cannot get HwndSource instance.");rn if (source.CompositionTarget == null)rn // Should never be null rn throw new Exception("Cannot get HwndTarget instance.");rnrn // Get transformation matrix rn Matrix matrix = source.CompositionTarget.TransformFromDevice;rnrn // Convert working area rn Win32.RECT workingArea = monitorInfo.rcWork;rn Point dpiIndependentSize =rn matrix.Transform(new Point(rn workingArea.Right - workingArea.Left,rn workingArea.Bottom - workingArea.Toprn ));rnrn // Convert minimum size rn Point dpiIndenpendentTrackingSize = matrix.Transform(new Point(rn this.MinWidth,rn this.MinHeightrn ));rnrn // Set the maximized size of the window rn mmi.ptMaxSize.x = (int)dpiIndependentSize.X;rn mmi.ptMaxSize.y = (int)dpiIndependentSize.Y;rnrn // Set the position of the maximized window rn mmi.ptMaxPosition.x = 0;rn mmi.ptMaxPosition.y = 0;rnrn // Set the minimum tracking size rn mmi.ptMinTrackSize.x = (int)dpiIndenpendentTrackingSize.X;rn mmi.ptMinTrackSize.y = (int)dpiIndenpendentTrackingSize.Y;rnrn Marshal.StructureToPtr(mmi, lParam, true);rn rn[/code] 论坛

自绘窗体双击标题最大化遮盖任务栏

03-04

我自绘窗体,窗体风格设置如下: STYLE WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDRENrn双击标题最大化和还原代码如下:rn[code=C/C++]void CMyDlg::OnBtnMax() rnrn //最大化时,为了不遮盖底部状态栏,所以应该取显示器工作区域的高度,而不是显示器的高度rn RECT rWorkArea; rn BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0); rn int iMaxWidth = rWorkArea.right - rWorkArea.left;rn int iMaxHeight = rWorkArea.bottom - rWorkArea.top;rn rn if(!m_bMaxStatus)rn rn //记录对话框最大化前的位置与大小rn GetWindowRect(&mPreviewRect);rn rn MoveWindow(0, 0, iMaxWidth, iMaxHeight);rn rn elsern rn //还原最大化前的位置与大小rn MoveWindow(mPreviewRect.left, mPreviewRect.top, mPreviewRect.Width(), mPreviewRect.Height());rn rn rn m_bMaxStatus = !m_bMaxStatus;rnrnrnrnUINT CMyDlg::OnNcHitTest(CPoint point) rnrn CRect rc;rn GetClientRect(&rc);rnrn if(m_bMaxStatus)rn rn return CDialog::OnNcHitTest(point);rn rnrn[/code]rnrn以上代码已经实现了正常的最大化和还原功能。rnrnrnrn[b][color=#FF0000]我最主要想解决的问题是:rn自绘窗体有个问题没办法解决: 点击window下面任务栏程序的图标不能最小化和还原(未响应?)[/color][/b][b][/b]rn为了解决这个我在程序里修改了窗体风格: rn ModifyStyle(0, WS_MINIMIZEBOX); //为了响应任务栏图标点击事件rn ModifyStyle(0, WS_MAXIMIZEBOX);rn ModifyStyle(0, WS_SYSMENU);rn这样点击任务栏图标就能最小化和还原了,并且右键图标也有系统菜单了。rn但是这时双击标题最大化就出问题了,程序最大化会遮盖任务栏(跟踪代码height是870,显示器高是900,按理并不应该遮盖啊,已经流出了任务栏高度,应该是那个隐藏的菜单引起的)rnrnrn求助内容: 请问这个问题该如何解决呢?rn 如果可以不修改窗体风格,有其他方法响应点击任务栏图标事件,那最好了。rnrn多谢各位,分不够请直说!! 只要你有需要!rn能满意结贴不容易啊! 论坛

没有更多推荐了,返回首页