利用ActiveX組件控制其所在的IE視窗

來源:互聯網
上載者:User
在實際的項目開發過程中,有時我們需要利用ActiveX組件控制其所在的IE視窗,如隱藏工具條和狀態列、全屏等,以使程式的工作區域最大。

 1.  用Delphi建立一個ActiveXForm工程,添加對SHDocVw單元的引用;2.  在Interface段聲明一個自訂的訊息常量,如下所示:const

  WM_UPDATE = WM_USER + 1;

3.  在類的私人段定義如下變數和函數:    private       FWebBrowser: IWebBrowser2;       procedure WMUpdate(var Msg: Integer); message WM_UPDATE;       function FindIEWebBrowser: IWebBrowser2;       function FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;4.  按Ctrl+Shift+C自動產生函數體的代碼,完善代碼內容如下:procedure TActiveFormX.WMUpdate(var Msg: Integer);begin FWebBrowser := FindIEWebBrowser; if FWebBrowser <> nil then begin    FWebBrowser.ToolBar := 0;    FWebBrowser.StatusBar := false; end;end; function TActiveFormX.FindIEWebBrowser: IWebBrowser2;var tmpShell: IShellWindows; tmpIntf: IDispatch; tmpIE: IWebBrowser2; i: Integer;begin try    tmpSHell := CoShellWindows.Create;    for i := 0 to tmpShell.Count - 1 do    begin      tmpIntf := tmpShell.Item(i);      if tmpIntf = nil then continue;      tmpIntf.QueryInterface(IID_IWebBrowser2, tmpIE);      if tmpIE = nil then Continue;      if (Integer(Handle) = tmpIE.HWND) or FindIEWindow(Integer(tmpIE.HWND), Handle) then     begin        Result := tmpIE;        Exit;      end;    end; except end;end; function TActiveFormX.FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;var tmpHandle : HWND;begin tmpHandle := GetParent(ChildHandle); if tmpHandle = 0 then begin    Result := False;    Exit; end else begin    if tmpHandle = ParentHandle then    begin     Result := True;     Exit;    end else    begin      Result := FindIEWindow(ParentHandle, tmpHandle);    end; end;end;5.  雙擊主表單,在ActiveFormCreate事件中添下如下代碼:procedure TActiveFormX.ActiveFormCreate(Sender: TObject);begin PostMessage(Handle, WM_UPDATE, 0, 0);end;6.  在表單上添加兩個Button,分別命名為btnFullScreen和btnUnFullScreen,為其OnClick事件添加如下代碼:procedure TActiveFormX.btnFullScreenClick(Sender: TObject);begin if FWebBrowser <> nil then begin    FWebBrowser.FullScreen := true; end;end; procedure TActiveFormX.btnUnFullScreenClick(Sender: TObject);begin if FWebBrowser <> nil then begin    FWebBrowser.FullScreen := false; end;end;7.  Build工程將其發布,運行產生的Html頁面,組件載入成功後IE視窗的工具列和狀態條被隱藏了,通過表單上的兩個按鈕可以控制IE視窗的全屏狀態。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.