Solved

Can I resize a TImage by dragging ?

Posted on 2002-05-20
6
464 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

11 Experts available now in Live!

Get 1:1 Help Now