如何在具有管理员权限的C#WPF中使用拖放

时间:2019-02-22 15:59:46

标签: c# wpf drag-and-drop uipi

当我不使用管理员特权但需要使用管理员特权时,可以使用拖放操作将文件从资源管理器拖放到某些控件。当我使用它时,我做不到。我知道这是因为有UIPI,来自没有提升过程的消息无法调用提升过程。

所以在app.manifest中我使用

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

初始化MainWindow后,它会启动MainWindow ViewModel方法

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        const uint WM_DROPFILES = 0x233;
        const uint WM_COPYDATA = 0x4a;
        const uint WM_COPYGLOBALDATA = 0x49;
        CHANGEFILTERSTRUCT changeStruct = new CHANGEFILTERSTRUCT();
        changeStruct.size = Convert.ToUInt32(Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT)));
        ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
        ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
        ChangeWindowMessageFilter(WM_COPYGLOBALDATA, MSGFLT_ADD);
        //bool res1 = ChangeWindowMessageFilterEx((HwndSource.FromVisual(App.Current.MainWindow as MainWindow) as HwndSource).Handle, WM_DROPFILES, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);
        //bool res2 = ChangeWindowMessageFilterEx((HwndSource.FromVisual(App.Current.MainWindow as MainWindow) as HwndSource).Handle, WM_COPYGLOBALDATA, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);
        //bool res3 = ChangeWindowMessageFilterEx((HwndSource.FromVisual(App.Current.MainWindow as MainWindow) as HwndSource).Handle, WM_COPYDATA, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);

    }
    public enum MessageFilterInfo : uint
    {
        None = 0, AlreadyAllowed = 1, AlreadyDisAllowed = 2, AllowedHigher = 3
    };

    public enum ChangeWindowMessageFilterExAction : uint
    {
        Reset = 0, Allow = 1, DisAllow = 2
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct CHANGEFILTERSTRUCT
    {
        public uint size;
        public MessageFilterInfo info;
    };

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint msg, ChangeWindowMessageFilterExAction action, ref CHANGEFILTERSTRUCT changeInfo);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr ChangeWindowMessageFilter(uint message, uint dwFlag);

我尝试使用ChangeWindowMessageFilter和ChangeWindowMessageFilterEx,但是它们都不起作用。第二个返回true,但是changeInfo.info没有返回。

你知道我做错了吗?

提前谢谢您。

0 个答案:

没有答案