Win10设置自动关机:WPF Win10禁用自动播放

关于Win10设置自动关机的问题,在autoplay win 10中经常遇到, 我的程序是当一个 USB 设备插入 / 拔出,并做它应该做什么捕获。

我的程序是当一个 USB 设备插入 / 拔出,并做它应该做什么捕获。

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

我发现了许多关于如何通过捕获 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 机器上开发这个。

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(11)
编码器故障代码:Arduino中的旋转编码器故障
上一篇
G96编程实例:Unity runUIApplicationMainWithArgc:argv:] +96(main.mm:96
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(56条)