最近項(xiàng)目中使用彈出控件Popup扭吁,發(fā)現(xiàn)彈出框的對(duì)齊方式在不同的系統(tǒng)中存在不同(Popup在win10上是彈出在左邊,Win7上是彈出在右邊)◎蚬睿現(xiàn)在記錄解決方案于此:
修改彈出菜單相對(duì)于相應(yīng)菜單項(xiàng)是左對(duì)齊還是右對(duì)齊
// 解決Popup控件在Win7以及Win10等系統(tǒng)中的對(duì)齊點(diǎn)方式不一樣的問題(以下兩種方法均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下彈出菜單默認(rèn)彈出到左邊改為右邊送漠。 在App.xaml.cs的OnStartup函數(shù)里調(diào)用一下由蘑。
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
FixPopupBug();
}