程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 暫停網頁中的Flash

暫停網頁中的Flash

編輯:Delphi

如何暫停網頁中的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.

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved