views:

94

answers:

2

Hi,

I am trying to inherit from TButton in order to provide some size aware capabilities, where the buttons are able to resize themselves and/or their font size (within certain constraints) to allow for changes in text

e.g.

| small |

or

|   this is a really long   |
| sentence on a button |

could happily be the same button on the same form, all I've done is reset the text and the button copes with the size change itself.

I've implemented all the text measuring functions, and the functionality works to a point.

what I have done is create new properties maxHeight, minHeight, defaultHeight and so forth for Width and Font.

When the user changes the default height, my design time component will change and reflect this new default height.

When the user uses the normal Height & Width properties however (or drags the corner) I don't know how to tie them to the default height and width.

I intercepted OnCanResize and created an event handler and tried to confirm that the new size is within the min max. If it's not, set to the min or max as required, but if within the boundaries then update. I am able to intercept runtime resize events, but not design time.

If it is possible to intercept the design time resizes, does anyone know how?

sorry if that's a bit long-winded, hope it makes sense!

A: 

Keep in mind that a button is still a window, and can (will) respond to WM_GETMINAXINFO. I believe most design tools respect the ptMinTrackSize and ptMaxTrackSize (the names are at least similar to that).

Jerry Coffin
I don't have enough rep to vote you up but I'll try to remember to come back when I do This isn't the solution I ended up going with, but it definitely pointed me in the right directionfor those who stumble across this later, the mistake I was making was to go:  range checking;  set Height
hgh
oops - I assumed same formatting in comments as in questions!
hgh
+1  A: 

Override the virtual SetBounds() method. From there, you can adjust the user's requested dimensions as needed before then passing them to the ancestor SetBounds() method. For example:

class TMyButton : public TButton
{
    typedef TButton inherited;

public:
    ...
    virtual void __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight);

__published:
    __property int MaxHeight = ...;
    __property int MinHeight = ...;
    ...
};

virtual void __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight)
{
    if (AHeight > MaxHeight) AHeight = MaxHeight;
    if (AHeight < MinHeight) AHeight = MinHeight;
    ...
    inherited::SetBounds(ALeft, ATop, AWidth, AHeight);
}
Remy Lebeau - TeamB
interesting - is SetBounds called when just changing height or width though (i.e. not changing height, width and position)? I'm no so sure but if I have time will check and get back (not right now though, since I have a working version) =) Thanks!!
hgh
SetBounds() is called during all move and size operations. It is the operation that actually assigns the Left, Top, Width, and Height properties and then invokes alignment and anchor update operations based on the values used.
Remy Lebeau - TeamB