I'd like to size an image when I paste it into my rve. My SetImageAspect works fine when I insert an image from an open dialog, or when I drop it in (I put my code into after ole drop for that).
procedure TForm1.rvePaste(Sender: TCustomRichViewEdit; var DoDefault: Boolean);
begin
if rve.CurItemStyle = rvsPicture then
begin
rve.BeginUpdate;
SetImageAspect(rve);
rve.EndUpdate;
end;
end;
I know that rvePaste runs as I put a breakpoint into it on the "if rve.CurItemStyle = rvsPicture then" line. But it never seems to know that the photo I pasted is the current item. Is there a way to do that? Or some other way to get the item number of what was pasted in?
For now I can run it from a menu item which works fine, but be more convenient if I can get it to work when pasted.
procedure TForm1.rvePaste(Sender: TCustomRichViewEdit; var DoDefault: Boolean);
var
bmp: TBitmap;
begin
if Clipboard.HasFormat(CF_BITMAP) then
begin
bmp := TBitmap.Create;
bmp.Assign(Clipboard);
if rve.InsertPicture('', bmp, rve.DefaultPictureVAlign) then
SetImageAspect(rve);
DoDefault := False;
end;
end;
procedure TForm1.SetImageAspect(rv:TRichViewEdit);
var
h, w : integer;
arf: Float32;
gr: TGraphic;
Tag: TRVTag;
Align: TRVVAlign;
s: TRVUnicodeString;
rvW, rvH: integer;
begin
if rv = nil then exit;
if rv.CurItemStyle = rvsPicture then
begin
//rve WxH size limits
rvW := rv.ClientWidth - 40;
rvH := rv.ClientHeight - 40;
//get image sizes
rv.GetCurrentPictureInfo( s, gr, Align, Tag );
w := gr.Width;
h := gr.Height;
//rve is "portrait" mode:
if (rvH > rvW) then
begin
if w > rvW then //image is landscape
begin
arf := w / h;
w := rvW;
h := Round(Abs(rvW) / arf);
end
else //image is portrait
begin
arf := h / w;
h := rvH;
w := Round(Abs(rvH) / arf);
end;
end;
//rve is "landscape" mode:
if (rvW >= rvH) then
begin
if h > rvH then //image is portrait
begin
arf := h / w;
h := rvH;
w := Round(Abs(rvH) / arf);
end
else //image is landscape
begin
arf := w / h;
w := rvW;
h := Round(Abs(rvW) / arf);
end;
end;
rv.SetCurrentItemExtraIntProperty(rvepImageWidth, w, True);
rv.SetCurrentItemExtraIntProperty(rvepImageHeight, h, True);
end;
end;