【Delphi】暂停网页中的Flash

如何暂停网页中的Flash?原理很简单,就是屏蔽Flash的消息即可。屏蔽右键也可以通过此方法

直接贴代码吧,加了注释,很容易就能懂了

 

新建工程,加一个WebBrowser,再加两个按钮。Flash 11.7.700.169 测试通过

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    WebBrowser1: TWebBrowser;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
    function GetFlashHwnd: HWND;
  public

  end;

var
  Form1: TForm1;
  // Flash组件窗口句柄
  FlashHwnd: HWND = 0;
  // 控制“暂停”的开关变量
  FlashPause: Boolean = False;

implementation

{$R *.dfm}

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  // 处理Flash窗口消息
  if (FlashHwnd <> 0) and (Msg.hwnd = FlashHwnd) then
  begin
    if FlashPause then
    begin
      // 仅仅保留窗口重绘相关消息,其余的消息全部过滤掉
      if not(Msg.message in [WM_PAINT, WM_WINDOWPOSCHANGED]) then
      begin
        Handled := True;
        Exit;
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FlashPause := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FlashPause := False;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 设置进程消息处理过程
  Application.OnMessage := AppMessage;
  WebBrowser1.Navigate('http://www.4399.com/flash/90302_3.htm');
end;

function TForm1.GetFlashHwnd: HWND;
begin
  Result := FindWindowEx(WebBrowser1.Handle, 0, 'Shell DocObject View', nil);
  if Result = 0 then
    Exit;

  Result := FindWindowEx(Result, 0, 'Internet Explorer_Server', nil);
  if Result = 0 then
    Exit;

  Result := FindWindowEx(Result, 0, 'MacromediaFlashPlayerActiveX', nil);
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  // 等页面加载完毕再取得其中的Flash窗口句柄
  if pDisp = WebBrowser1.Application then
    FlashHwnd := GetFlashHwnd;
end;

end.


 

你可能感兴趣的:(【Delphi】暂停网页中的Flash)