Program Description
This program is used to open IE, link to a specified web page, obtain URL address information and window name in the IE address bar, activate the recently opened window, and close the recently opened window. The program interface is shown in Figure 1:
Design Thoughts
There are many ways to program IE using Delphi. In this program, use DDE to control IE. You should pay attention to ensuring that IE has run first, because at this time IE needs to be a DDE server, and the user's program can only be a DDE client. The DDE client cannot exchange data with a server that is not running. In this program, call the API function ShellExecute to open IE.
Figure 1 When using the DDE client in this program, the following functions and procedures of the class TDdeClientConv are used:
function SetLink(Service:String; Topic:String):Boolean; function OpenLink:Boolean; function RequestData(const Item:String):Pchar; procedure CloseLink; |
Among them, the parameter Service is the ApplicationName of the DDE server, which is Iexplore for IE; the parameter Topic is the TopicName of the DDE session, which corresponds to different functions. The parameter Item is the ItemName of the session, which also varies depending on the functions. The function SetLink is used to set the session topic. If it is successfully returned, it will return False; the function RequestData is used to return the session data.
Design steps
Create a new application and add two Edit components and 6 Button components to the form. The properties of each component are shown in Figure 2:
Figure 2 Add references to DDEman, ShellAPE and ComObj units in the uses, and define a global variable DDE of type TDdeClientConv.
Write relevant code
...... var DDE:TDdeClientConv; //DDE is the client global variable implementation {$R *.DFM} //Create the DDE client when creating a form procedure TForm1.FormCreate(Sender: TObject); begin DDE: =TDdeClientConv.Create(Self); end; //Click Open IE to start the default browser and automatically open the specified web page (if IE is not the default browser, it needs to be opened manually) procedure TForm1.Button3Click(Sender: TObject); begin / /Call ShellExecute to open the default browser and set the window mode to SW_SHOWNORMAL ShellExecute(Handle,nil,PChar(''http://www.chinaren.com/index.shtml''),nil,nil,SW_SHOWNORMAL); end; //Click the Get URL button to get the IE address bar URL and corresponding window title procedure TForm1.Button1Click(Sender: TObject); begin //Set the session connection is successful if DDE.SetLink(''Iexplore'', ''WWW_GetWindowInfo'') then begin DDE.OpenLink; //Return information and display Edit1.Text:=DDE.QeququestData(''-1''); DDE.CloseLink; end else ShowMessage(''IE is not running'') ; end; //Click the Open URL button to link to the specified web page procedure TForm1.Button2Click(Sender: TObject); begin if DDE.SetLink(''Iexplore'', ''WWW_ OpenURL'') then begin //Link to the specified Web page DDE.OpenLink; DDE.RequestData(Edit2.Text); DDE.CloseLink; end else ShowMessage(''IE is not running''); end; //Click the Activate IE button to activate the recently opened IE window procedure TForm1 .Button4Click(Sender: TObject); begin if DDE.SetLink(''Iexplore'', ''WWW_Activate'') then begin DDE.OpenLink; DDE.RequestData(''-1''); DDE.CloseLink; end else ShowMessage(''IE is not running''); end; //Click the Close IE button to close the recently opened IE window procedure TForm1.Button5Click(Sender: TObject); begin if DDE.SetLink(''Iexplore'', '' WWW_Exit'') then begin DDE.OpenLink; DDE.RequestData(''WWW_Exit''); DDE.CloseLink; end else ShowMessage(''IE is not running''); end. |