首先在Form上放置一個RichEdit。
在窗體的構造函數中添加以下代碼:
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
unsigned mask = SendMessage(RichEdit1->Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(RichEdit1->Handle, EM_SETEVENTMASK, 0, mask | ENM_LINK);
SendMessage(RichEdit1->Handle, EM_AUTOURLDETECT, true, 0); //自動檢測URL
RichEdit1->Text = "歡迎訪問C++ Builder\n"
"網址: http://www.ccrun.com\n"
"偶的信箱:\n"
"mailto::[email protected] \n"
"嘿嘿\n";
}
重載窗體的WndProc
1。在.h中添加:
protected:
virtual void __fastcall WndProc(Messages::TMessage &Message);
2。在.cpp中添加:
//---------------------------------------------------------------------------
void __fastcall TMainForm::WndProc(Messages::TMessage &Message)
{
if (Message.Msg == WM_NOTIFY)
{
if (((LPNMHDR)Message.LParam)->code == EN_LINK)
{
ENLINK* p = (ENLINK *)Message.LParam;
if (p->msg == WM_LBUTTONDOWN)
{
SendMessage(RichEdit1->Handle, EM_EXSETSEL, 0, (LPARAM)&(p->chrg));
ShellExecute(Handle, "open", RichEdit1->SelText.c_str(), 0, 0, SW_SHOWNORMAL);
}
}
}
TForm::WndProc(Message);
}
以上代碼均已測試通過。