为什么在Delphi中转移BitMap的顺序相反?

时间:2018-09-21 14:05:08

标签: delphi

单击该按钮时,应将图像从richedit1传输到richedit2并按顺序显示,然后反向显示。如何解决?下面是代码。

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
    InsertBitmapToRE(RichEdit1.Handle, Image1.Picture.Bitmap.Handle);
  end;
end;

Function MAP_LOGHIM_TO_PIX(Const Val: Integer; Const Log: Integer): Integer;
Const
  HIMETRIC_PER_INCH=2540;
Begin
  Result:=MulDiv(Val, Log, HIMETRIC_PER_INCH);
End;

Function MAP_LOGHIMPT_TO_PT(Const Val: TPoint; Const Handle: HWND = 0): TPoint;
Var
  DC: HDC;
Begin
  DC:=GetDC(Handle);
  Result.X:=MAP_LOGHIM_TO_PIX(Val.X, GetDeviceCaps(DC, LOGPIXELSX));
  Result.Y:=MAP_LOGHIM_TO_PIX(Val.Y, GetDeviceCaps(DC, LOGPIXELSY));
  ReleaseDC(Handle, DC);
End;

procedure TForm1.Button2Click(Sender: TObject);
Var
  IREO: IRichEditOle;
  OleClientSite: IOleClientSite;
  ReObject: TReObject;
  I: Integer;
  ViewObject2: IViewObject2;
  Rc: TRect;
  Path:String;
  bmp:TBitmap;
  Pt: TPoint;
begin
  Path:='C:\temp\richedit\';
  SendMessage(RichEdit1.Handle, EM_GETOLEINTERFACE, 0, Longint(@IREO));
  IREO.GetClientSite(OleClientSite);
  For I:=IREO.GetObjectCount-1 Downto 0 Do
  Begin
    ZeroMemory(@ReObject, SizeOf(ReObject));
    ReObject.cbStruct:=SizeOf(ReObject);
    If Succeeded(IREO.GetObject(I, ReObject, $00000001)) Then
    If Succeeded(ReObject.poleobj.QueryInterface(IViewObject2, ViewObject2)) Then
    Begin
      ViewObject2.GetExtent(DVASPECT_CONTENT, -1, Nil, Pt);
      Pt:=MAP_LOGHIMPT_TO_PT(Pt, RichEdit1.Handle);
      bmp:=TBitmap.Create;
      Bmp.Height := Pt.Y;
      Bmp.Width := Pt.X;
      SetRect(Rc, 0, 0, Bmp.Width, Bmp.Height);
      OleDraw(ReObject.poleobj, DVASPECT_CONTENT, bmp.Canvas.Handle, Rc);
      bmp.SaveToFile(Path+'Img'+IntToStr(I+1)+'.bmp');
      InsertBitmapToRE(RichEdit2.Handle, bmp.Handle);
    End
    Else
      ShowMessage('Error: Can''t get IViewObject2');
  End;
end;

屏幕截图

Screenshot

1 个答案:

答案 0 :(得分:4)

在for循环中使用downto 0是有效的,但是要反向计数,因此并不总是希望的。之所以如此,是因为您将循环变量用作索引并需要特定的处理顺序。

For I:= 0 to IREO.GetObjectCount-1 Do