wpf Popup Win8.0 bug HorizontalOffset 弹出位置偏移

2022-10-15,,,,

问题描述参考

wpf 客户端【JDAgent桌面助手】开发详解(四) popup控件的win8.0的bug

当开发完程序后,我们在多操作系统测试时候发现:win8.0  系统中 popup 弹出位置老是不对。

如下图:

WinXp /Vista/Win7/Win8.1 下面windows 右键菜单弹出的位置(红色圈为 鼠标指针位置)

Win8.0 下面windows 右键菜单弹出的位置(红色圈为 鼠标指针位置)

Popup 控件也是这个情况。需要在代码里面判断windows的版本。如果是win8.0 popup 的HorizontalOffset、VerticalOffset值需要调整。

解决方案一。。通过注册表来获取当前系统版本

    因为  Environment.OSVersion.Version 获取到的win8.0跟win8.1信息是一样的。

    通过注册表获取win8.0代码为6.2 ,win8.1代码为6.3

       public static string SetRegister()
{
string regpath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
RegistryKey uaes = Registry.LocalMachine.OpenSubKey(regpath,
RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
if (uaes == null)
uaes = Registry.LocalMachine.CreateSubKey(regpath, RegistryKeyPermissionCheck.ReadWriteSubTree); var registData = uaes.GetValue("CurrentVersion").ToString(); return registData;
}

    注意,,这种方法需要添加管理员权限。。

解决方案二。修改弹出菜单相对于相应菜单项是左对齐还是右对齐。(组长调出的)

  

        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);
} }

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

wpf Popup Win8.0 bug HorizontalOffset 弹出位置偏移的相关教程结束。

《wpf Popup Win8.0 bug HorizontalOffset 弹出位置偏移.doc》

下载本文的Word格式文档,以方便收藏与打印。