开发者

Restricting the size of components at design time

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开发者_如何学C 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!


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);
}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜