Thema: Delphi RichEdit Link öffnen

Einzelnen Beitrag anzeigen

Benutzerbild von RWarnecke
RWarnecke

Registriert seit: 31. Dez 2004
Ort: Stuttgart
4.408 Beiträge
 
Delphi XE8 Enterprise
 
#3

Re: RichEdit Link öffnen

  Alt 11. Jun 2009, 10:52
Diese Variante habe ich mal im Internet gefunden :
Delphi-Quellcode:
////////////////////////////////////////////////////////////////////////////////
///
/// If the a url under the mouse cursor and when the left mouse button is used,
/// then open the url in the standard browser
///
procedure TMainForm.RichEditMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (TRichEdit(Sender).Cursor = crHandPoint) and (Button = mbLeft) then
    if pos('http://', TRichEdit(Sender).Hint) > 0 then
      ShellExecute(Application.Handle,'open', PChar(TRichEdit(Sender).Hint), nil, nil, 1);
end;
////////////////////////////////////////////////////////////////////////////////
///
/// To determine if there a url under the mouse cursor and show it in the hint
/// of the TRichEdit component
///
procedure TMainForm.RichEditMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  iCharIndex, iLineIndex, iCharOffset, i, j: Integer;
  Pt: TPoint;
  s: string;
begin
  with TRichEdit(Sender) do
  begin
    Pt := Point(X, Y);
    // Get Character Index from word under the cursor
    iCharIndex := Perform(Messages.EM_CHARFROMPOS, 0, Integer(@Pt));
    if iCharIndex < 0 then Exit;
    // Get line Index
    iLineIndex := Perform(EM_EXLINEFROMCHAR, 0, iCharIndex);
    iCharOffset := iCharIndex - Perform(EM_LINEINDEX, iLineIndex, 0);
    if Lines.Count - 1 < iLineIndex then Exit;
    // store the current line in a variable
    s := Lines[iLineIndex];
    if pos('http://', s) > 0 then
    begin
      // Search the beginning of the word
      i := iCharOffset + 1;
      while (i > 0) and (s[i] <> ' ') do Dec(i);
      // Search the end of the word
      j := iCharOffset + 1;
      while (j <= Length(s)) and (s[j] <> ' ') do Inc(j);
      // Display Text under Cursor
      s := Copy(s, i + 1, j - i);
      ShowHint := true;
      Hint := s;
      Cursor := crHandPoint;
    end
    else
    begin
      ShowHint := false;
      Hint := '';
      Cursor := crDefault;
    end;
  end;
end;
erstellt durch Code-Orakel und dem BBCode-Plugin.
Rolf Warnecke
App4Mission
  Mit Zitat antworten Zitat