Solved

Can I resize a TImage by dragging ?

Posted on 2002-05-20
6
467 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
[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
  • 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
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

707 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