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!
Adding a hyperlink to a picture in code
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
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.
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;