美文网首页
WPF中Popup控件在Win7以及Win10等中的对齐点方式不

WPF中Popup控件在Win7以及Win10等中的对齐点方式不

作者: 虫虫侠教育 | 来源:发表于2019-02-18 13:40 被阅读0次

    最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是弹出在左边,Win7上是弹出在右边)。现在记录解决方案于此:

    修改弹出菜单相对于相应菜单项是左对齐还是右对齐

    // 解决Popup控件在Win7以及Win10等系统中的对齐点方式不一样的问题(以下两种方法均OK)

    using System.Reflection;    // 方法一

    using System.Runtime.InteropServices;    // 方法二

            [DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)]

            public static extern bool SystemParametersInfoSet(uint action, uint uiParam, uint vparam, uint init);

            public static void FixPopupBug()

            {

                //// 方法一

                //var ifLeft = SystemParameters.MenuDropAlignment;

                //if (ifLeft)

                //{

                //    var t = typeof(SystemParameters);

                //    var field = t.GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static);

                //    field.SetValue(null, false);

                //}

                // 方法二

                SystemParametersInfoSet(0x001C /*SPI_SETMENUDROPALIGNMENT*/, 0, 0, 0);

            }

    将win10下弹出菜单默认弹出到左边改为右边。 在App.xaml.cs的OnStartup函数里调用一下。

    protected override void OnStartup(StartupEventArgs e)

    {

    base.OnStartup(e);

    FixPopupBug();

    }

    相关文章

      网友评论

          本文标题:WPF中Popup控件在Win7以及Win10等中的对齐点方式不

          本文链接:https://www.haomeiwen.com/subject/wlreixtx.html