Board index » cppbuilder » Alternate TButton - derive or override?

Alternate TButton - derive or override?


2004-09-15 04:24:54 PM
cppbuilder25
Hi,
Ok, what I'm trying to do is to create a component, it behave like a normal
TButton i.e. it response to mousr click, recieve focus, etc.
The only thing I would like to change is the way the button is drawn. I was
thinking of using a bitmap image to replace the rectangular boxed button (or
even drawing the whole button the way I like - like in those games
interface). What would be the best approach to this? Override the TButton's
draw method or derive a TButton subclass? And where is the original
TButton's draw method, so that I could find out what parameters and the
excat name of it if I have to override it?
Normally I'd use a TImage to mimic this behavior, but I'd like to know is
there any better way to do this, as I'm trying to dive deeper into C++/OOP
(I'm an old-timer DOS based C programmer...)
Regards.
 
 

Re:Alternate TButton - derive or override?

youŽd better derive it from tbutton.
- call TButton::CreateParams(Params) in the overrided version
of the same.
and later:
- declare TCanvas *FCanvas property
- create message handlers for:
CN_MEASUREITEM................ask for control dimensions each time windows needs to draw it. The Witdth/Height struct must be filled.
CN_DRAWITEM...................draws the component. assign the
handle device context received in the cnmeasuereitem and
draw in the canvas your own shape.
- at the end of drawitem procedure, FCanvas->Handle must be null
H2H
 

Re:Alternate TButton - derive or override?

"newsreader" < XXXX@XXXXX.COM >wrote:
Quote
What would be the best approach to this?
Override the TButton's draw method or derive
a TButton subclass?
Those are the same solution. You have to derive a new class in order to override any methods.
The alternative is to simply give the standard TButton the BS_OWNERDRAW style via SetWindowLong() and then subclass the TButton's WindowProc property to intercept WM_DRAWITEM messages directly.
Quote
And where is the original TButton's draw method
There isn't any. By default, buttons are drawn by the OS itself automatically.
Gambit
 

{smallsort}

Re:Alternate TButton - derive or override?

Okay, I've tried reading the senseless Windows API files and bumped around.
I have to ask for more detail of how to do this...
1) In SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong), what value
should I use fot nIndex?? Is it GWL_EXSTYLE or GWL_STYLE?? I'm not posting
in a game group so I don't mind spoiler, spoil me please...;-)
2) How do I subclass the TButton's WindowProc?? Where should I do it?
"Remy Lebeau [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
The alternative is to simply give the standard TButton the BS_OWNERDRAW
style via SetWindowLong() and then subclass the TButton's WindowProc
property to intercept WM_DRAWITEM messages directly.
 

Re:Alternate TButton - derive or override?

"newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
1) In SetWindowLong(HWND hWnd, int nIndex, LONG dwNewLong),
what value should I use fot nIndex?? Is it GWL_EXSTYLE or GWL_STYLE??
GWL_STYLE
Quote
2) How do I subclass the TButton's WindowProc?? Where should I do it?
Sublcass a windows control
www.bcbdev.com/faqs/faq76.htm
Gambit
 

Re:Alternate TButton - derive or override?

Thanks for your link to the example, I tried the site you recommended, but I
still can't get the button to show as I wished...I followed the example and
substitute Button1 for the contrl in the example.
//---header---//
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
private: // User declarations
TWndMethod OriProc;
void __fastcall ButtonProc(TMessage &msg);
void __fastcall PaintButton(HDC &dc);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1(void);
};
//---end header---//
//---code----//
_fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
LONG exstyle = GetWindowLong( Button1->Handle, GWL_STYLE );
::SetWindowLong(Button1->Handle, GWL_STYLE, exstyle | BS_OWNERDRAW);
OriProc = Button1->WindowProc;
Button1->WindowProc = ButtonProc;
}
//--------------------------------------------------------------------------
-
__fastcall TForm1::~TForm1(void)
{
Button1->WindowProc = OriProc;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ButtonProc(TMessage &msg)
{
msg.Result = 0;
switch(msg.Msg)
{
case WM_DRAWITEM: PaintButton((HDC)msg.WParam);
msg.Result = 0;
break;
default: OriProc(msg);
break;
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::PaintButton(HDC &dc)
{
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = Button1->Width;
bmp->Height =Button1->Height;
bmp->Canvas->Pen->Style = psSolid;
bmp->Canvas->Brush->Style = bsSolid;
bmp->Canvas->Brush->Color = clSkyBlue;
bmp->Canvas->RoundRect(0, 0, bmp->Width, bmp->Height, 5, 5);
bmp->Canvas->TextOut(5, 5, Button1->Caption);
::BitBlt(dc, 0, 0, bmp->Width, bmp->Height, bmp->Canvas->Handle, 0, 0,
SRCCOPY);
delete bmp;
}
//---end code----//
I have painted only a roundrect and the caption since I'm just testing it
out. However I still see the same old boring button.... What have I done
wrong? What am I missing here? Please help me. Thanks in advance.
Regards.
p/s: there was once I tried to intercept the WM_ERASEBKGND and do the
painting for that message, I did see a skyblue box flashed a brief moment at
the button's location when I put the button on the form, but it's soon
replaced by the boring old button again.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
>2) How do I subclass the TButton's WindowProc?? Where should I do it?

Sublcass a windows control
www.bcbdev.com/faqs/faq76.htm


Gambit