ScrollBox to move Image with mouse

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

ScrollBox to move Image with mouse

Postby mark_c » Fri Apr 07, 2017 1:58 am

Hello,
I'm trying to write a program, similar to the Windows Photo Viewer (windows 7/8), but I do not understand if it uses one ScrollBox. When the image is shown i real size, it is possible to move it with the mouse. I tried through the ScrollBox but it is the right way?

thank you

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

bool MouseLeftButton;

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Image1->Picture->Bitmap->LoadFromFile("my.bmp");
        Image1->AutoSize=true;

        ScrollBox1->HorzScrollBar->Range = Image1->Width;
        ScrollBox1->VertScrollBar->Range = Image1->Height;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        if(MouseLeftButton)
        {
                int maxX = Image1->Picture->Bitmap->Width;
                int maxY = Image1->Picture->Bitmap->Height;

                Label1->Caption=ScrollBox1->HorzScrollBar->Position;
                Label2->Caption=ScrollBox1->VertScrollBar->Position;

                if(X >=0 && X <= maxX  && Y >=0 && Y <= maxY )
                {
                        ScrollBox1->HorzScrollBar->Position=X;
                        ScrollBox1->VertScrollBar->Position=Y;
                }
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        MouseLeftButton=true;       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        MouseLeftButton=false;       
}
//---------------------------------------------------------------------------

mark_c
BCBJ Veteran
BCBJ Veteran
 
Posts: 86
Joined: Thu Jun 21, 2012 1:13 am

Re: ScrollBox to move Image with mouse

Postby mark_c » Mon Apr 10, 2017 5:15 am

solved,
I leave my contribution to the community, good works

Mark

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"

bool MouseLeftButton;
POINT pt;
POINT FLastDown;

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Image1->Picture->Bitmap->LoadFromFile("my.bmp");
        Image1->AutoSize=true;

        ScrollBox1->HorzScrollBar->Range=Image1->Picture->Bitmap->Width;
        ScrollBox1->VertScrollBar->Range=Image1->Picture->Bitmap->Height;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        MouseLeftButton=true;
        GetCursorPos(&FLastDown);

        Label1->Caption=FLastDown.x;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        MouseLeftButton=false;
        FLastDown = Point(-1, -1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        if(MouseLeftButton && FLastDown.x >= 0)
        {

                        GetCursorPos(&pt);

                        ScrollBox1->VertScrollBar->Position = ScrollBox1->VertScrollBar->Position + FLastDown.y - pt.y;
                        ScrollBox1->HorzScrollBar->Position = ScrollBox1->HorzScrollBar->Position + FLastDown.x - pt.x;

                        FLastDown = pt;
        }       
}
//---------------------------------------------------------------------------
mark_c
BCBJ Veteran
BCBJ Veteran
 
Posts: 86
Joined: Thu Jun 21, 2012 1:13 am

Re: ScrollBox to move Image with mouse

Postby rlebeau » Mon Apr 10, 2017 6:03 pm

mark_c wrote:I leave my contribution to the community, good works


If you want to load other kinds of images besides BMPs, your code as-is will fail. You would need to use Picture->LoadFromFile() instead of Picture->Bitmap->LoadFromFile(), and then use Picture->Graphic instead of Picture->Bitmap when reading the image dimensions:

Code: Select all
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Image1->Picture->LoadFromFile(...);
    Image1->AutoSize = true;

    ScrollBox1->HorzScrollBar->Range = Image1->Picture->Graphic->Width;
    ScrollBox1->VertScrollBar->Range = Image1->Picture->Graphic->Height;
}


Also, don't use GetCursorPos(), use the X/Y coordinates that are given to you by the events. And don't forget to check which mouse button is actually being clicked:

Code: Select all
bool MouseLeftButton = false;
TPoint FLastDown = Point(-1, -1);

void __fastcall TForm1::Image1MouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if (Button == mbLeft)
    {
        FLastDown = Point(X, Y);
        MouseLeftButton = true;
        Label1->Caption = X;
    }
}

void __fastcall TForm1::Image1MouseUp(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if (Button == mbLeft)
    {
        MouseLeftButton = false;
        FLastDown = Point(-1, -1);
    }
}

void __fastcall TForm1::Image1MouseMove(TObject *Sender,
    TShiftState Shift, int X, int Y)
{
    if (MouseLeftButton)
    {
        TPoint pt = Point(X, Y);

        ScrollBox1->HorzScrollBar->Position = ScrollBox1->HorzScrollBar->Position + (FLastDown.X - pt.X);
        ScrollBox1->VertScrollBar->Position = ScrollBox1->VertScrollBar->Position + (FLastDown.Y - pt.Y);

        FLastDown = pt;
    }
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 17 guests

cron