当前位置: 移动技术网 > IT编程>开发语言>.net > WPF中窗体最大化问题处理

WPF中窗体最大化问题处理

2018年10月15日  | 移动技术网IT编程  | 我要评论

老年代步车,911生物疗法,黎姿迎44岁生日

遇到的问题信息

  • 问题:当windowstyle=none时,窗口最大化,不显示任务栏 —— 即窗体是全屏效果。
  • 解决中遇到的问题列表【主要涉及到任务栏发生改变后的一些问题处理】:
    • 最大化时,任务栏被遮盖;
    • 最大化后,拖动任务栏,无法自适应窗体;
    • 最大化后,拖动任务栏,窗体还原,还原数据丢失,始终显示最大;
    • 最大化后,拖动任务栏,窗体还原,设置之前保存的窗体位置数据,再次设置,由于和之前一样,窗体位置信息不生效;

解决方案

  • 思路:窗体最大化时,将窗体透明化,设置内部元素grid的margin属性,从而显示出任务栏
  • 步骤:

1、 设置窗体相关属性:windowstyle="none" allowstransparency="true" background="transparent" resizemode="canminimize"

窗体需要支持透明,并将窗体设置为透明;设置resizemode,否则最大化时,边框会有影响。

2、 添加窗体最大化/还原代码如下:

    double normaltop;
    double normalleft;
    double normalwidth;
    double normalheight;
    /// <summary>
    /// 最大化/还原处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_maximize_click(object sender, routedeventargs e)
    {
        //wpf最大化 全屏显示任务栏处理
        if (this.windowstate == windowstate.normal)
        {
            normaltop = this.top;
            normalleft = this.left;
            normalwidth = this.width;
            normalheight = this.height;

            double top = systemparameters.workarea.top;
            double left = systemparameters.workarea.left;
            double right = systemparameters.primaryscreenwidth - systemparameters.workarea.right;
            double bottom = systemparameters.primaryscreenheight - systemparameters.workarea.bottom;
            gd_main.margin = new thickness(left, top, right, bottom);

            this.windowstate = windowstate.maximized;               
        }
        else
        {
            this.windowstate = windowstate.normal;

            //必须先设置为0,在重新设值,若前后值一样,会失效 --拖动任务栏后,还原-始终显示在屏幕最左上方
            this.top = 0;
            this.left = 0;
            this.width = 0;
            this.height = 0;

            this.top = normaltop;
            this.left = normalleft;
            this.width = normalwidth;
            this.height = normalheight;

            gd_main.margin = new thickness(0);
        }
    }

3、添加任务栏变化处理

注意:此节实现仅适用于.net framework 4.5及以上。因为4.0及以前的版本中不包含staticpropertychanged事件。
不过可以通过wndproc来自己实现,其中会有一个问题:利用wndproc来监听,结果是比较实时的,而systemparameters中的值,实时性可能会跟不上,从而获取到的值仍然是旧的。

对此有两种解决方案:

  • 1、添加一个timer 或者 直接sleep,等待一下在systemparameters的值【此法相对简单,但无法完全保证有效,毕竟systemparameters中值更新的时间,还是要看.net framework】;
  • 2、利用windows api读取系统值,用方法systemparametersinfo获取spi_getworkarea【微软官方其实就是用这个获取的,这样比我下面的运行效率还要高些】

另外:下面的方法可能会有系统兼容性问题,我在windows 10上是通过的,但在windows 8.1上,边界存在问题【不确定是不是framework在系统上有bug】

注册事件:systemparameters.staticpropertychanged += systemparameters_staticpropertychanged;
添加如下代码:

    private void systemparameters_staticpropertychanged(object sender, system.componentmodel.propertychangedeventargs e)
    {
        if (e.propertyname == "workarea")
        {
            if (this.windowstate == windowstate.maximized)
            {
                double top = systemparameters.workarea.top;
                double left = systemparameters.workarea.left;
                double right = systemparameters.primaryscreenwidth - systemparameters.workarea.right;
                double bottom = systemparameters.primaryscreenheight - systemparameters.workarea.bottom;
                gd_main.margin = new thickness(left, top, right, bottom);
            }
        }
    }

相关下载

点击查看完整源代码

解:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网