參考 Delphi 版本,利用 TDdeClientConv 透過 DDE 協定從 Internet Explorer 取得當前視窗 URL,移植至 C++Builder。
// Unit1.cpp
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}
// 取得指定服務的 URL
String TForm1::Get_URL(AnsiString Servicio) {
TDdeClientConv *Cliente_DDE = new TDdeClientConv(NULL);
char *temp;
Cliente_DDE->SetLink(Servicio, "WWW_GetWindowInfo"); // 連接 DDE 服務與主題
temp = Cliente_DDE->RequestData("0xFFFFFFFF"); // 要求資料
Cliente_DDE->CloseLink();
delete Cliente_DDE;
return String(temp);
}
void __fastcall TForm1::Button1Click(TObject *Sender) {
ShowMessage(Get_URL("Iexplore")); // 取得 IE 當前 URL
}
// Unit1.h
#ifndef Unit1H
#define Unit1H
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "ddeman.hpp"
class TForm1 : public TForm {
__published:
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private:
String Get_URL(AnsiString Servicio);
public:
__fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1;
#endif
"Iexplore" 對應 Internet Explorer。"WWW_GetWindowInfo" 是 IE 支援的標準主題。"0xFFFFFFFF" 代表當前活動視窗。點擊按鈕即彈出訊息框顯示當前 IE 視窗的完整 URL。此法簡單直接,適用於需快速整合瀏覽器 URL 的輕量級工具。