如何暫停網頁中的Flash?原理很簡單,就是屏蔽Flash的消息即可。屏蔽右鍵也可以通過此方法
直接貼代碼吧,加了注釋,很容易就能懂了
新建工程,加一個WebBrowser,再加兩個按鈕。Flash 11.7.700.169 測試通過
[delphi]
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.
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.