我在这里正在看这个很棒的示例代码 Graphics32: Pan with mouse-drag, zoom to mouse cursor with mouse wheel
我正在Win 10 OS上使用Delphi Community。 我运行该程序,变焦将放大,但是当我缩小时,它将变为图像的常规大小,并且不会使图像放大。
procedure TForm3.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
Image.Picture.LoadFromFile('C:\PASCHEMATIC.TIFF');
Image.Stretch := True;
Image.Height := Round(Image.Width * Image.Picture.Height /
Image.Picture.Width);
FOrgImgBounds := Image.BoundsRect;
end;
procedure TForm3.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDragging then
Image.SetBounds(X - FFrom.X, Y - FFrom.Y, Image.Width, Image.Height);
end;
procedure TForm3.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Image.Enabled := True;
FDragging := False;
end;
procedure TForm3.FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
const
ZoomFactor: array[Boolean] of Single = (0.9, 1.1);
var
R: TRect;
begin
MousePos := Image.ScreenToClient(MousePos);
with Image, MousePos do
if PtInRect(ClientRect, MousePos) and ((WheelDelta > 0) and
(Height < Self.ClientHeight) and (Width < Self.ClientWidth)) or
((WheelDelta < 0) and (Height > 20) and (Width > 20)) then
begin
R := BoundsRect;
R.Left := Left + X - Round(ZoomFactor[WheelDelta > 0] * X);
R.Top := Top + Y - Round(ZoomFactor[WheelDelta > 0] * Y);
R.Right := R.Left + Round(ZoomFactor[WheelDelta > 0] * Width);
R.Bottom := R.Top + Round(ZoomFactor[WheelDelta > 0] * Height);
BoundsRect := R;
Handled := True;
end;
end;
procedure TForm3.ImageDblClick(Sender: TObject);
begin
Image.BoundsRect := FOrgImgBounds;
end;
procedure TForm3.ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if not (ssDouble in Shift) then
begin
FDragging := True;
Image.Enabled := False;
FFrom := Point(X, Y);
MouseCapture := True;
end;
end;
问题是如何使图像进一步缩小以炸毁图像?
我玩过ZoomFactor值。 似乎我必须先放大,然后才能缩小。
答案 0 :(得分:2)
首先,定义:zoom in。因此,您的问题可能是如何放大以放大图像?或者,为什么图像没有放大?
答案是在FormMouseWheel
过程中
with Image, MousePos do
if PtInRect(ClientRect, MousePos) and ((WheelDelta > 0) and
(Height < Self.ClientHeight) and (Width < Self.ClientWidth)) or
((WheelDelta < 0) and (Height > 20) and (Width > 20)) then
begin
...
这也揭示了使用with ...
的陷阱:很难看到代码中实际引用了哪些对象:
(Height < Self.ClientHeight)
// Height refers to Image.Height, because it is closer in
// scope (because of the with clause,) than e.g. the form,
// which also has an Height property
// Self.ClientHeight refers to Form.ClientHeight because
// Self refers to the object who's method is running
与
相同(Width < Self.ClientWidth)
// Width refers to Image.Width and Self.ClientWidth refers to Form.ClientWidth
因此,您的问题的答案是,由于代码中施加的限制,图像不能变得超出表单的任一范围。
要删除该约束,请删除
and (Height < Self.ClientHeight) and (Width < Self.ClientWidth)
从FormMouseWheel()
过程开始。
删除这些条件将使Image
变得比表单更大,因此也变得比其中包含的图像更大。在某些时候,还会有其他情况,例如内存限制,开始起作用。