WPF Win10禁用自动播放

时间:2018-05-01 17:55:24

标签: wpf autoplay wndproc

我的程序正在捕获何时插入/拔出USB设备并执行它应该执行的操作。

有一件烦人的事:explorer打开一个窗口。猜猜自动播放是罪魁祸首。完全禁用该计算机上的自动播放不是一个选项,因此必须在此应用程序运行时禁用它。

我通过捕获Windows消息找到了大量有关如何执行此操作的文章。让我们来看下面的代码,它是在windowsmessages到来时编译和触发的。但遗憾的是,QueryCancelAutoPlay似乎从未被调用过。

protected override void OnSourceInitialized(EventArgs e)
{
   base.OnSourceInitialized(e);
   HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
   source.AddHook(WndProc);
}

private UInt32 queryCancelAutoPlay = 0;

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);


private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (queryCancelAutoPlay == 0)
    {
       queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");
    }

    if (msg == queryCancelAutoPlay)
    {
        return (IntPtr)1;
    }
    return IntPtr.Zero;
  }

有什么想法? 我在win10机器上开发它。

1 个答案:

答案 0 :(得分:0)

public partial class MainWindow : NavigationWindow
    {
        public int queryCancelAutoPlay = 0;
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int RegisterWindowMessage(string lpString);

        public MainWindow()
        {
            InitializeComponent();
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            MainWindow_Closing(this, e);
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            MainWindow_SourceInitialized(this, e);
        }

        private void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            Debug.WriteLine("Initialize MainWindow");
            IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
            HwndSource src = HwndSource.FromHwnd(windowHandle);
            src.AddHook((WndProc));
        }

        private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //if the QueryCancelAutoPlay message id has not been registered...

            if (queryCancelAutoPlay == 0)
            {
                queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");
                Debug.WriteLine("cancel autoplay registered. msg id: " + queryCancelAutoPlay.ToString());
            }
            //if the window message id equals the QueryCancelAutoPlay message id
            if (msg == queryCancelAutoPlay)
            {
                Debug.WriteLine("Autoplay cancelled");
                queryCancelAutoPlay = 0;
                handled = true;
                return new IntPtr(1);
            }
            return new IntPtr(msg);

        }

        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            Debug.WriteLine("Close MainWindow");
            HwndSource source = (HwndSource)HwndSource.FromVisual(this);
            source.RemoveHook(new HwndSourceHook(this.WndProc));
        }

    }