C# 屏蔽WebBrowser页面中的声音

本文转载自:http://blog.sina.com.cn/s/blog_48a673bd0100lg4e.html,感谢原作:追踪者


首先感谢一下 CSDN网友 pathletboy chys3584;还要重点感谢一下同学K`one,给予的无私直到;

 

程序功能:

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

 

核心代码使用CSDN网友 pathletboy 的代码;

如下:注意原文是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看看效果如何;

 

文件下载地址:http://dl.dbank.com/c0ne3sr5o1 Dbank数据银行~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值