Solved

Can I resize a TImage by dragging ?

Posted on 2002-05-20
6
462 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Accepted Solution

by:
mirtol earned 100 total points
Comment Utility
//---------------------------------------------------------------------------

#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
Comment Utility
//---------------------------------------------------------------------------

#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
Comment Utility
Thats great ... thanx
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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.
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.

743 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