C# WebBrowser 屏蔽网页播放声音

warning: 这篇文章距离上次修改已过567天,其中的内容可能已经有所变动。

VB C# WebBrowser 去掉网页播放声音の完美解决方案

程序功能:

    屏蔽当前程序的所有声音,比如:控件WebBrowser在打开页面的时候可能有背景音乐,Falsh动画等所有组件发出的声音,但不影响程序外任何一种可以播放声音的软件;作用范围仅仅局限于当前进程~

 

核心代码如下(其实就是调用api)

如下:注意原文是EXE可执行文件,这里将其封装为DLL stdcall;以满足C#直接调用的需求;

library DSounds;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  Windows,
  SysUtils,
  Classes;


function DSoundsCode():Integer ;stdcall;
var
  hDSound: Cardinal;
  pDirectSoundCreate: Pointer;
  hWinmm: Cardinal;
  pmidiStreamOpen: Pointer;
  pwaveOutWrite: Pointer;
  lp: Cardinal;
begin

  hDSound := LoadLibrary('DSound.dll');
  if hDSound > 0 then
    pDirectSoundCreate := GetProcAddress(hDSound, 'DirectSoundCreate');
  if pDirectSoundCreate <> nil then
  begin
    VirtualProtect(pDirectSoundCreate, 3, PAGE_EXECUTE_READWRITE, lp);
    Move(#$C2#$0C#$00, pDirectSoundCreate^, 3);
  end;

  hWinmm := LoadLibrary('Winmm.dll');
  if hWinmm > 0 then
    pmidiStreamOpen := GetProcAddress(hWinmm, 'midiStreamOpen');
  if pmidiStreamOpen <> nil then
  begin
    VirtualProtect(pmidiStreamOpen, 3, PAGE_EXECUTE_READWRITE, lp);
    Move(#$C2#$04#$00, pmidiStreamOpen^, 3);
  end;

  if hWinmm > 0 then
    pwaveOutWrite := GetProcAddress(hWinmm, 'waveOutWrite');
  if pwaveOutWrite <> nil then
  begin
    VirtualProtect(pwaveOutWrite, 3, PAGE_EXECUTE_READWRITE, lp);
    Move(#$C2#$0C#$00, pwaveOutWrite^, 3);
  end;
    Result := 1597;
end;
{$R *.res}
exports DSoundsCode;
begin
end.


生成DLL之后C#的调用代码

 

    /// <summary>
    /// 调用外部DLL DELPHI
    /// </summary>
    public class AnyCall
    {
        private const string _fileDll = @"DSounds";
        [DllImport(_fileDll, EntryPoint = "DSoundsCode", CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)]
        public static extern int DSoundsCode();

        public int CallDSoundsCode()
        {
            return DSoundsCode();
        }
    }

 

使用方法:

1:将DSounds.dll放到应用程序的bin\Debug目录;

2:在项目中新建一个类,比如 AnCall,并把上面的代码复制到类里面,压缩包中已经附带的有了;

3:程序开始的地方调用AnyCall方法;比如:new JavaScript.AnyCall().CallDSoundsCode();

4:按下F6看看效果如何;

其实直接将它改成类就行了,没必要调用dll,改一下就能换成vb了,呵呵.

none
最后修改于:2023年05月08日 07:58

评论已关闭