Adding a hyperlink to a picture in code

General TRichView support forum. Please post your questions here
Post Reply
Vandy1Fan
Posts: 10
Joined: Thu Jun 21, 2012 6:47 pm
Location: United States

Adding a hyperlink to a picture in code

Post by Vandy1Fan »

I would like to add a hyperlink to an image in code. I'm going to setup an action where the user will select an image for use as "Add To Calendar". They will choose the image and my program will add the hyperlink behind it.

How would I do that?

Thanks for all your help. This is a great product!
gavind
Posts: 1
Joined: Wed Jul 11, 2012 11:22 pm

Post by gavind »

+1. I'm looking for the same answer. Image
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Actually, both RichViewActions (TrvActionInsertHyperlink) and the demo in Demos\*\Assorted\Hypertext\CreateHyperlink\ support pictures. If pictures are in the selection, the action or the demo make them hyperlinks.

Ok, if you need a special command for pictures.

First, you need to enable/disable your command. The simplest way to check if there is a single picture selected is RichViewEdit1.GetSelectedImage<>nil. You can do this check in RichViewEdit1.OnSelect (or in action's update method).

As for the command itself.
The selected item is not necessary the current item (the current item is an item in the position of the caret; however, if the image is selected from its end to its beginning, not the image, but the previous item may be current). So you cannot use GetCurrent***/SetCurrent*** methods.
We can use RichViewEdit1.TopLevelEditor.GetSelectionBounds to get the selected item. But for simplification, the code below uses undocumeted RichViewEdit1.TopLevelEditor.RVData.GetSingleSelectedItem method.

Code: Select all

var rve: TCustomRichViewEdit;
    RVData: TCustomRVFormattedData;
    ItemNo: Integer;
begin
  rve := RichViewEdit1.TopLevelEditor;
  if not rve.RVData.GetSingleSelectedItem(RVData, ItemNo) then
    exit;
  // in our case, GetSingleSelectedItem returns RVData = rve.RVData
  {
  // uncomment if you do not want to change a picture that is already a hyperlink
  if rve.GetItemStyle(ItemNo)=rvsHotPicture then
    exit;
  }
  rve.BeginUndoGroup(rvutTag);
  rve.SetUndoGroupMode(True);
  try
    rve.ConvertToHotPicture(ItemNo);
    rve.SetItemTagEd(ItemNo, 'http://www.trichview.com');
  finally
    rve.SetUndoGroupMode(False);
  end;
end;
Post Reply