Solved

Can I resize a TImage by dragging ?

Posted on 2002-05-20
6
465 Views
Last Modified: 2011-10-03
I have dynamically created TImage objects that I want to be able to resize. At the moment I am doing it by a mouse click event eg if R_Mouse_Button clicked double in size ... but this is pretty cruddy. I want to be able to grab a handle and resize it to any size. I notice that there is no OnResize() function for a TImage in BCB 4 if you create them at design time.
0
Comment
Question by:sylvrynne
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:mirtol
ID: 7021677
The OnResize event isn't available as the object is not user-resizable - as you want to make it.

I'd recommend creating a descendant of TImage, say TResizImage or the like, then you can publish the OnResize event (as it does exist just hasn't been made available) and write the code to allow the user to resize it.

You'd have to write all the code for resizing it - not too hard, just use the OnMouseMove to check if the cursor is at the edge of the control, if it is change the mousepointer to the appropraite resizer cursor. Then OnMouseDown you capture the cursor and wait till they release it (drawing whatever you like as they move the cursor) and then when they let go, resize it.

If you'd like some sample code just say... I can throw some together.
0
 

Author Comment

by:sylvrynne
ID: 7022631
some sample code would be great !!!!!! if you have the time

thankyou so much .... if i give you the points can you still post comments so I can get the code?
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7022812
The answer is yes you can, but here's the code now...

You need to create a new Package and then a new component based on TImage (which is what I did), then do something like the following. I did this pretty quick so it's not the most efficient code ever, but it works.

If you want to resize in other directions etc you'll have to extend obviously...

I'll put the code in two separate comments, the first is the cpp and the second the header.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Accepted Solution

by:
mirtol earned 100 total points
ID: 7022813
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ResizeImage.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TResizeImage *)
{
    new TResizeImage(NULL);
}
//---------------------------------------------------------------------------
__fastcall TResizeImage::TResizeImage(TComponent* Owner)
    : TImage(Owner)
{
    resizing = false;
}
//---------------------------------------------------------------------------
namespace Resizeimage
{
    void __fastcall PACKAGE Register()
    {
         TComponentClass classes[1] = {__classid(TResizeImage)};
         RegisterComponents("Additional", classes, 0);
    }
}
//---------------------------------------------------------------------------
void __fastcall TResizeImage::mMouseMove(TMessage &Message)
{
    TPoint ms(Message.LParamLo, Message.LParamHi);

    if(resizing)
    {
        DrawSize();
        sw = (signed short)ms.x;
        sh = (signed short)ms.y;
        if(sw < 16)
            sw = 16;
        if(sh < 16)
            sh = 16;
        DrawSize();
    }
    else
    {
        if(ms.x > Width - 6 && ms.y > Height - 6 && ms.x < Width && ms.y < Height)
        {
            Screen->Cursor = crSizeNWSE;
            MouseCapture = true;
        }
        else
        {
            Screen->Cursor = Cursor;
            MouseCapture = false;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TResizeImage::mLButtonDown(TMessage &Message)
{
    // Capture mouse...
    resizing = true;
    MouseCapture = true;
    sw = Width;
    sh = Height;
    DrawSize();
}
//---------------------------------------------------------------------------
void __fastcall TResizeImage::mLButtonUp(TMessage &Message)
{
    if(resizing)
    {
        // Resize the control...
        resizing = false;
        DrawSize();
        Width = sw;
        Height = sh;
    }
}
//---------------------------------------------------------------------------
void __fastcall TResizeImage::DrawSize()
{
    TCanvas *gph = new TCanvas();
    TPoint pnts[5];
    HWND hWnd;

    pnts[0] = TPoint(Left, Top);
    pnts[1] = TPoint(Left, Top + sh);
    pnts[2] = TPoint(Left + sw, Top + sh);
    pnts[3] = TPoint(Left + sw, Top);
    pnts[4] = pnts[0];
    gph->Handle = GetDC(Parent->Handle);
    gph->Pen->Mode = pmNot;
    gph->Polyline(pnts, 4);
    ReleaseDC(Parent->Handle, gph->Handle);

    delete gph;
}
//---------------------------------------------------------------------------
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7022815
//---------------------------------------------------------------------------

#ifndef ResizeImageH
#define ResizeImageH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TResizeImage : public TImage
{
private:
    void __fastcall mMouseMove(TMessage &Message);
    void __fastcall mLButtonDown(TMessage &Message);
    void __fastcall mLButtonUp(TMessage &Message);
    void __fastcall DrawSize();
protected:

    BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER(WM_MOUSEMOVE, TMessage, mMouseMove)
        MESSAGE_HANDLER(WM_LBUTTONDOWN, TMessage, mLButtonDown)
        MESSAGE_HANDLER(WM_LBUTTONUP, TMessage, mLButtonUp)
    END_MESSAGE_MAP(TImage)

public:
    __fastcall TResizeImage(TComponent* Owner);

    bool resizing;
    int sw, sh;
__published:
    __property OnResize;
};
//---------------------------------------------------------------------------
#endif
0
 

Author Comment

by:sylvrynne
ID: 7022882
Thats great ... thanx
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Least Squares Curve Fitting 4 99
Installshield for Embarcadero EX 10.1 Berlin 4 60
How to convert Structure to vector of byte and vice versa 5 24
C++ help/ Toy problem 19 30
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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