Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

·Scroll (drag) a TImage

Posted on 1999-07-21
13
Medium Priority
?
371 Views
Last Modified: 2010-04-04
I have a TImage which is. i.e. 2000x1000, so I want to be able to *drag* it, by clicking on it with the mouse, I mean, scroll it.. like in ACDSee..

thanx
0
Comment
Question by:bryan7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1390250
There was a recent thread on this very topic, don't remember the number though :-)

Cheers,

Raymond.

0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 80 total points
ID: 1390251
Here is the answer to that Q, I think its what you want...

(Credit to Kretschmar for the code)

try this sample, for scrolling the image, just drag it with left mousebutton

unit move_pic_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;  //is parent from image1
    Image1: TImage;  //align = alNone; AutoSize = True
    Panel2: TPanel;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormResize(Sender: TObject);
  private
    Procedure RefitImage;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var xx, yy : Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    Image1.Picture.LoadFromFile(Opendialog1.FileName);
end;

procedure TForm1.ReFitImage;
begin

  //Check Horiz, if image smaller, then center

  If Image1.Width < Panel1.ClientWidth then
    Image1.Left := (Panel1.ClientWidth - Image1.Width) Div 2
  else
    If Image1.Width = Panel1.ClientWidth then
      Image1.Left := 0
    else
      if Image1.Left + Image1.Width - Panel1.ClientWidth < 0 then
        Image1.Left := Panel1.ClientWidth - Image1.Width
      else
        if (Image1.Left > 0) and (Image1.Width > Panel1.ClientWidth) then
          Image1.Left := 0;

  //Check Vert,if image smaller, then center

  If Image1.Height < Panel1.ClientHeight then
    Image1.Top := (Panel1.ClientHeight - Image1.Height) Div 2
  else
    If Image1.Height = Panel1.ClientHeight then
      Image1.Top := 0
    else
      if Image1.Top + Image1.Height - Panel1.ClientHeight < 0 then
        Image1.Top := Panel1.ClientHeight - Image1.Height
      else
        if (Image1.Top > 0) and (Image1.Height > Panel1.ClientHeight) then
          Image1.Top := 0;

end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ssleft in shift then
  begin
    xx := x;
    yy := y;
   // Screen.Cursor := crHandGrab; // Change cursor
  end;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if ssleft in shift then
  begin
    if yy - y > 0 then
      if Image1.Top + Image1.Height - Panel1.ClientHeight > 0 then
        Image1.Top := Image1.Top - (yy - y);
    if yy - y < 0 then
      if Image1.Top < 0 then
        Image1.Top := Image1.Top  - (yy - y);
    if xx - x > 0 then
      if Image1.Left + Image1.Width - Panel1.ClientWidth > 0 then
        Image1.Left := Image1.Left - (xx - x);
    if xx - x < 0 then
      if Image1.Left < 0 then
        Image1.Left := Image1.Left - (xx - x);
    ReFitImage;
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
//  Screen.Cursor := crDefault; //Change Cursor
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  ReFitImage;
end;

end.

Cheers,

Raymond.
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390252
worked fine..

how would I do it if the image wasn't so big ? I mean, panel1 is 200x200 and image1 is also 200x200, how could I move it ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1390253
The code will still allow you to move it, but the ReFitImage method will then reposition the image. A bit of tweaking here should be all you need to do.

Cheers,

Raymond.

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1390254
;-)
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390255
""u to move it, but the ReFitImage method will then reposition the image. A
      bit of tweaking here should be all you need to do. ""

um,, how ?
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1390256
Try this and you will see what I mean.

Put an exit; statements as the first statement in the RefitImage method...

Cheers,

Raymond.

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1390257
hi bryan,

my code above is designed, that the image can't leave the containerbounds,
if you remove ReFitImage, and the if clauses in the mousemove procedure, you can free move the image and if you drag fast then you will lost the image outside the container and you can't get it back, therefore the checks.

regarding your zooming question in the other thread, just combine it with this code.

meikl
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390258
""regarding your zooming question in the other thread, just combine it with this code.""

that's what I thought of.. I have a form which shows an image inside a panel, which is resized automatically depending on the image size.

I have an image which is i.e. 3000x400..
I'd like a trackbar for the zooming, and also being able to move the image inside the panel at anytime.. any size..
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390259
finally I ended up with this after 'playing a bit': ( Panel.Align= alClient, image = alNone )

unit ViewForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, TJpgImg, ComCtrls, StdCtrls, WLblButton, Buttons;

type
  TView = class(TForm)
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    WLblButton1: TWLblButton;
    TrackBar1: TTrackBar;
    Panel3: TPanel;
    Panel1: TPanel;
    FEJpeg1: TFEJpeg;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    procedure FEJpeg1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure WLblButton1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure ReFitImage;
    procedure FEJpeg1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FEJpeg1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FEJpeg1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton3Click(Sender: TObject);
  private
procedure CreateParams(var Params : TCreateParams); override;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  View: TView;
  xx, yy : Integer;

implementation

uses Zoom;

{$R *.DFM}

procedure TView.ReFitImage;
begin
    //Check Horiz, if image smaller, then center
    If FeJpeg1.Width < Panel1.ClientWidth then
        FeJpeg1.Left := (Panel1.ClientWidth - FeJpeg1.Width) Div 2
       else
      If FeJpeg1.Width = Panel1.ClientWidth then
        FeJpeg1.Left := 0
       else
        if FeJpeg1.Left + FeJpeg1.Width - Panel1.ClientWidth < 0 then
          FeJpeg1.Left := Panel1.ClientWidth - FeJpeg1.Width
        else
          if (FeJpeg1.Left > 0) and (FeJpeg1.Width > Panel1.ClientWidth) then
           FeJpeg1.Left := 0;

    //Check Vert,if image smaller, then center
    If FeJpeg1.Height < Panel1.ClientHeight then
     FeJpeg1.Top := (Panel1.ClientHeight - FeJpeg1.Height) Div 2
    else
       If FeJpeg1.Height = Panel1.ClientHeight then
        FeJpeg1.Top := 0
      else
        if FeJpeg1.Top + FeJpeg1.Height - Panel1.ClientHeight < 0 then
      FeJpeg1.Top := Panel1.ClientHeight - FeJpeg1.Height
        else
          if (FeJpeg1.Top > 0) and (FeJpeg1.Height > Panel1.ClientHeight) then
            FeJpeg1.Top := 0;
end;

procedure TView.CreateParams(var Params : TCreateParams);
      begin
        inherited CreateParams(Params);
        Params.WndParent := GetDesktopWindow;
      end;

procedure TView.FEJpeg1Click(Sender: TObject);
begin
if SpeedButton3.down= true then Exit;
View.Hide;
FEJpeg1.Picture.Bitmap.FreeImage;
FEJpeg1.Align:= alNone;
end;

procedure TView.FormShow(Sender: TObject);
begin
 Clientwidth:= FEJpeg1.Width;
 Clientheight:= FEJpeg1.height;
// FEJpeg1.Align:= alClient;
 FEJpeg1.Align:= alnone;
 ReFitImage;
end;

procedure TView.Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if ssleft in shift then
 begin
 releasecapture; //restore mouse input to prevent from moving too far
 View.perform(WM_syscommand, $F012, 0);
 end;
end;

procedure TView.WLblButton1Click(Sender: TObject);
begin
if ZoomForm.Visible = False then
  begin
  ZoomForm.Show;
  ZoomForm.Bi_Loupe1.Active:= True;
  ZoomForm.Bi_Loupe1.ZoomLevel:= 1;
  end
   else
  ZoomForm.Close;
end;

procedure TView.SpeedButton1Click(Sender: TObject);
begin
ZoomForm.Bi_Loupe1.ZoomLevel:= ZoomForm.Bi_Loupe1.ZoomLevel+1;
end;

procedure TView.SpeedButton2Click(Sender: TObject);
begin
ZoomForm.Bi_Loupe1.ZoomLevel:= ZoomForm.Bi_Loupe1.ZoomLevel-1;
end;

procedure TView.FormCreate(Sender: TObject);
begin
 Clientwidth:= FEJpeg1.Width;
 Clientheight:= FEJpeg1.height;
end;

procedure TView.TrackBar1Change(Sender: TObject);
begin
if TrackBar1.Position > 0 then
        begin
          FeJpeg1.ClientWidth := Trunc(FeJpeg1.Picture.Width * (1 + TrackBar1.Position/100));
          FeJpeg1.ClientHeight := Trunc(FeJpeg1.Picture.Height * (1 + TrackBar1.Position/100));
        end;
        if TrackBar1.Position < 0 then
        begin
          FeJpeg1.ClientWidth := Trunc(FeJpeg1.Picture.Width *
      Abs((TrackBar1.Min-TrackBar1.Position)/100));
          FeJpeg1.ClientHeight := Trunc(FeJpeg1.Picture.Height *
      Abs((TrackBar1.Min-TrackBar1.Position)/100));
        end;
        if TrackBar1.Position = 0 then
        begin
          FeJpeg1.ClientWidth := FeJpeg1.Picture.Width;
          FeJpeg1.ClientHeight := FeJpeg1.Picture.Height;
        end;
    ReFitImage;
end;

procedure TView.FEJpeg1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
    if ssleft in shift then
    begin
      xx := x;
      yy := y;
     // Screen.Cursor := crHandGrab; // Change cursor
    end;
end;

procedure TView.FEJpeg1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
    if ssleft in shift then
    begin
//      if yy - y > 0 then
        if FeJpeg1.Top + FeJpeg1.Height - Panel1.ClientHeight > 0 then
          FeJpeg1.Top := FeJpeg1.Top - (yy - y);
//      if yy - y < 0 then
        if FeJpeg1.Top < 0 then
          FeJpeg1.Top := FeJpeg1.Top  - (yy - y);
//      if xx - x > 0 then
        if FeJpeg1.Left + FeJpeg1.Width - Panel1.ClientWidth > 0 then
          FeJpeg1.Left := FeJpeg1.Left - (xx - x);
//      if xx - x < 0 then
        if FeJpeg1.Left < 0 then
          FeJpeg1.Left := FeJpeg1.Left - (xx - x);
//      ReFitImage;
    end;
end;

procedure TView.FEJpeg1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //  Screen.Cursor := crDefault; //Change Cursor
end;

procedure TView.SpeedButton3Click(Sender: TObject);
begin
FEJpeg1.ShowHint:= not FEJpeg1.ShowHint;
if FEJpeg1.ShowHint= True then ReFitImage;
end;



end.

0
 
LVL 3

Author Comment

by:bryan7
ID: 1390260
thanx both =)
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390261
meikl: I'll give ya some points also.. wait some days.. I only have 14 now.. =(
0
 
LVL 3

Author Comment

by:bryan7
ID: 1390262
k, I have some points now..  we,, 47, lol, how about 20x4 ? ( with A grade )
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question