2012-07-31 56 views
1

我正在開發一個Delphi聊天應用程序,該應用程序允許用戶除常規文本以外發送超鏈接。我正在使用RichViewEdit來顯示文本。當用戶發送鏈接時,它應該顯示爲不同的顏色並且可以點擊,以便單擊時它會打開默認的Web瀏覽器。超鏈接不會跳進RichView

我已閱讀RichView文檔(這不是很好),我似乎無法弄清楚如何讓OnMouseMove和OnJump工作,有任何建議或幫助?

OnJump:

procedure TADComClient.RichViewJump(Sender: TObject; id: Integer); 
var 
    ItemNo: Integer; 
    RVData: TCustomRVFormattedData; 
    Link: PWideChar; 
    URL: String; 
begin 
    RichView.GetJumpPointLocation(id, RVData, ItemNo); 
    URL := RichView.GetItemText(ItemNo); 
    ShellExecute(Application.Handle, 'open', PChar(URL), '', '', SW_SHOWNORMAL); 
end; 
+0

事件是否實際射擊? IIRC RichViewEdit在Ctrl-Click上跳轉,而不僅僅是點擊,尤其是在編輯內容時。 – 2012-08-01 06:08:40

+0

下面的代碼有效,但我最終只使用RichView而不是編輯,因爲我不需要編輯功能。 – Aaron 2012-08-10 15:37:07

回答

1

,你需要去這應該讓你: {{ 文章:

Adding URL hyperlink functionality to RichEdit 

http://delphi.about.com/library/weekly/aa111803a.htm 

Here's how to add URL hyperlink functionality to a TRichEdit component - 
whenever the text in a RichEdit matches the format of a URL, the control 
will display it as a hyperlink. Even more: when you click the URL, your 
Web browser will be launched and the link will be loaded into the browser; 
or if the URL is a "mailto:" link, your default email client will be launched, 
thus enabling you to send e-mail messages "from" RichEdit. 
} 

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, ComCtrls; 

type 
    TForm1 = class(TForm) 
    RichEdit1: TRichEdit; 
    RichEdit2: TRichEdit; 
// RichEdit1: TRichEdit; 
// RichEdit2: TRichEdit; 
    procedure FormCreate(Sender: TObject); 
    private 
    procedure InitRichEditURLDetection(RE : TRichEdit); 
    public 
    { Public declarations } 
    protected 
    procedure WndProc(var Msg: TMessage); override; 
    end; 

var 
    Form1: TForm1; 

implementation 
{$R *.dfm} 

uses ShellApi, RichEdit; 

procedure TForm1.InitRichEditURLDetection(RE: TRichEdit); 
var 
    mask: Word; 
begin 
    mask := SendMessage(RE.Handle, EM_GETEVENTMASK, 0, 0); 
    SendMessage(RE.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK); 
    SendMessage(RE.Handle, EM_AUTOURLDETECT, Integer(True), 0); 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
var 
    s: string; 
begin 
    InitRichEditURLDetection(RichEdit1); 

    s:='Great Delphi tutorials and articles at ' + 
    'http://www.delphi.about.com.' + #13#10 + 
    'About Delphi Programming site!' + #13#10 + 
    'Send an email to your Guide: mailto:[email protected]'; 
    RichEdit1.Text := s; 

    s:= 'http://www.delphi.about.com. ' + 
     ' This Rich Edit does not recognize URLs!'; 
    RichEdit2.Text := s 
end; 

procedure TForm1.WndProc(var Msg: TMessage); 
var 
    p: TENLink; 
    sURL: string; 
    CE : TRichEdit; 
begin 
if (Msg.Msg = WM_NOTIFY) then 
begin 
    if (PNMHDR(Msg.lParam).code = EN_LINK) then 
    begin 
    p := TENLink(Pointer(TWMNotify(Msg).NMHdr)^); 
    if (p.Msg = WM_LBUTTONDOWN) then 
    begin 
    try 
    CE := TRichEdit(Form1.ActiveControl); 
    SendMessage(CE.Handle, EM_EXSETSEL, 0, Longint(@(p.chrg))); 
    sURL := CE.SelText; 
    ShellExecute(Handle, 'open', PChar(sURL), 0, 0, SW_SHOWNORMAL); 
    except 
    end; 
    end; 
    end; 
end; 

inherited; 
end; (* TForm1.WndProc *) 

end. (* unit1.pas *) 


{ 
******************************************** 
Zarko Gajic 
About.com Guide to Delphi Programming 
http://delphi.about.com 
email: [email protected] 
free newsletter: http://delphi.about.com/library/blnewsletter.htm 
forum: http://forums.about.com/ab-delphi/start/ 
******************************************** 
}