windows 我可以使用 C#/.NET 以编程方式禁用窗口自动播放功能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2732792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:22:39  来源:igfitidea点击:

Can I disable window autoplay function programatically with C#/.NET?

c#.netwindowsautoplay

提问by isamux

Does anybody know a way to deactivate the autoplay function of windows using c#/.NET?

有人知道使用 c#/.NET 停用 windows 自动播放功能的方法吗?

回答by isamux

A little summary, for all the others looking for a good way to disable/supress autoplay. So far I've found 3 methods to disable autoplay programatically:

一个小总结,对于所有其他正在寻找禁用/抑制自动播放的好方法的人。到目前为止,我已经找到了 3 种以编程方式禁用自动播放的方法:

  1. Intercepting the QueryCancelAutoPlay message
  2. Using the Registry
  3. Implementing the COM Interface IQueryCancelAutoPlay
  1. 拦截QueryCancelAutoPlay消息
  2. 使用注册表
  3. 实现 COM 接口IQueryCancelAutoPlay

In the end I chose the 3rd method and used the IQueryCancelAutoPlay interface because the others had some signifcant disadvantages:

最后我选择了第三种方法并使用了 IQueryCancelAutoPlay 接口,因为其他方法有一些明显的缺点:

  • The first method (QueryCancelAutoPlay) was only able to suppress autoplay if the application window was in the foreground, cause only the foreground window receives the message
  • Configuring autoplay in the registry worked even if the application window was in the background. The downside: It required a restart of the currently running explorer.exeto take effect...so this was no solution to temporarily disable autoplay.
  • 第一种方法(QueryCancelAutoPlay)只能在应用程序窗口在前台时才能抑制自动播放,导致只有前台窗口收到消息
  • 即使应用程序窗口在后台,在注册表中配置自动播放也能工作。缺点:它需要重新启动当前正在运行的explorer.exe才能生效......所以这不是暂时禁用自动播放的解决方案。


Examples for the implementation

实施示例

1. QueryCancelAutoPlay

1.查询取消自动播放

Note: If your application is using a dialog box you need to call SetWindowLong(signature) instead of just returning false. See herefor more details)

注意:如果您的应用程序正在使用对话框,您需要调用SetWindowLong签名)而不是仅仅返回 false。有关更多详细信息,请参见此处

2. Registry

2. 注册表

Using the registry you can disables AutoRun for specified drive letters (NoDriveAutoRun) or for a class of drives (NoDriveTypeAutoRun)

使用注册表,您可以禁用指定驱动器号 (NoDriveAutoRun) 或一类驱动器 ( NoDriveTypeAutoRun) 的AutoRun

3. IQueryCancelAutoPlay

3.IQueryCancelAutoPlay

Some other links:

其他一些链接:

回答by Kyle Rozendo

RegisterWindowMessage is a Win32 API call. So you will need to use PInvoke to make it work..

RegisterWindowMessage 是一个 Win32 API 调用。所以你需要使用 PInvoke 来让它工作..

using System.Runtime.InteropServices;

class Win32Call
{
[DllImport("user32.dll")]
   public static extern int RegisterWindowMessage(String strMessage);
}

// In your application you will call

Win32Call.RegisterWindowMessage("QueryCancelAutoPlay");

From here(The Experts-Exchange link at the top). There is additional help on that site with some more examples that may be a little more comprehensive than the above. The above does however solve the problem.

从这里开始(顶部的专家交流链接)。该站点上还有其他帮助,其中包含更多示例,这些示例可能比上述示例更全面一些。不过以上确实解决了问题。

回答by isamux

Some additional links that might be helpful:

一些可能有用的附加链接:

回答by Manjunath Bilwar

Try this code work for me :) For more info check out this reference link : http://www.pinvoke.net/default.aspx/user32.registerwindowmessage

试试这个代码对我有用:) 有关更多信息,请查看此参考链接:http: //www.pinvoke.net/default.aspx/user32.registerwindowmessage

using System.Runtime.InteropServices;

//provide a private internal message id
private UInt32 queryCancelAutoPlay = 0;

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

/* only needed if your application is using a dialog box and needs to 
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
*/

protected override void WndProc(ref Message m)
{
    //calling the base first is important, otherwise the values you set later will be lost
    base.WndProc (ref m);

    //if the QueryCancelAutoPlay message id has not been registered...
    if (queryCancelAutoPlay == 0)
        queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");

    //if the window message id equals the QueryCancelAutoPlay message id
    if ((UInt32)m.Msg == queryCancelAutoPlay)
    {
        /* only needed if your application is using a dialog box and needs to
        * respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
        SetWindowLong(this.Handle, 0, 1);
        */
        m.Result = (IntPtr)1;
    }
} //WndProc