Solved

Can I resize a TImage by dragging ?

Posted on 2002-05-20
6
463 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now