beball您好:
看您得程式宣告片段實在看不出你實作部分動了哪些手腳,如果要別人幫忙建議您把完整的內容貼出.. 給您一個範例您參考看看: CGiButton元件檔案下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=15083796&CC=337344 CGiButton.h
#ifndef CGiButtonH
#define CGiButtonH
//---------------------------------------------------------------------------
#include
#include
#include
//---------------------------------------------------------------------------
class TRGB : public TPersistent
{
private:
int FpasR, FpasG, FpasB;
TNotifyEvent FOnChange;
public:
__fastcall TRGB();
protected:
virtual void __fastcall Change();
virtual void __fastcall SetpasR(int Value);
virtual void __fastcall SetpasG(int Value);
virtual void __fastcall SetpasB(int Value);
__published:
__property int pasR = {read=FpasR, write=SetpasR, default = 1};
__property int pasG = {read=FpasG, write=SetpasG, default = 1};
__property int pasB = {read=FpasB, write=SetpasB, default = 1};
__property TNotifyEvent OnChange = {read=FOnChange, write=FOnChange};
};
//---------------------------------------------------------------------------
class PACKAGE TCGiButton : public TCustomControl
{
private:
TRGB *FRGB;
TFont *FFont;
bool BtnEnfonce;
bool BtnEnfonceBis;
bool MouseSurBtn;
bool ALeFocus;
protected:
void __fastcall RGBChange(TObject *Sender);
void __fastcall Paint();
DYNAMIC void __fastcall DoEnter();
DYNAMIC void __fastcall DoExit();
DYNAMIC void __fastcall MouseDown(TMouseButton Button,
Classes::TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall MouseUp(TMouseButton Button,
Classes::TShiftState Shift, int X, int Y);
DYNAMIC void __fastcall KeyDown(Word &Key, Classes::TShiftState Shift);
DYNAMIC void __fastcall KeyUp(Word &Key, Classes::TShiftState Shift);
bool __fastcall CanResize(int &NewWidth, int &NewHeight);
virtual void __fastcall SetFont(TFont *Value);
virtual void __fastcall SetRGB(TRGB *Value);
void __fastcall FontChanged(TObject *Sender);
virtual void __fastcall MouseLeave(TMessage &Msg);
virtual void __fastcall MouseEnter(TMessage &Msg);
virtual void __fastcall TextChanged(TMessage &Msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, MouseLeave)
MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, MouseEnter)
MESSAGE_HANDLER(CM_TEXTCHANGED, TMessage, TextChanged)
END_MESSAGE_MAP(TCustomControl)
public:
__fastcall TCGiButton(TComponent* Owner);
virtual __fastcall ~TCGiButton();
__published:
__property Caption;
__property OnClick;
__property OnKeyUp;
__property OnKeyPress;
__property OnKeyDown;
__property OnEnter;
__property OnExit;
__property TabOrder;
__property TabStop;
// nouvelles propriétés :
__property TFont *Font = {read=FFont, write=SetFont};
__property TRGB *CRGB = {read=FRGB, write=SetRGB}; };
//---------------------------------------------------------------------------
#endif CGiButton.cpp
#include
#pragma hdrstop
#include "CGiButton.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé pour vérifier que les composants créés n'ont
// aucune fonction virtuelle pure.
//
static inline void ValidCtrCheck(TCGiButton *)
{
new TCGiButton(NULL);
}
//---------------------------------------------------------------------------
// Implémentation de la classe TRGB
//---------------------------------------------------------------------------
__fastcall TRGB::TRGB() : TPersistent()
{
FpasR = 1;
FpasG = 1;
FpasB = 1;
}
//---------------------------------------------------------------------------
void __fastcall TRGB::Change()
{
if(FOnChange) FOnChange(this);
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasR(int Value)
{
if (Value < 6 && Value >= 0) FpasR = Value;
Change();
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasG(int Value)
{
if (Value < 6 && Value >= 0) FpasG = Value;
Change();
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasB(int Value)
{
if (Value < 6 && Value >= 0) FpasB = Value;
Change();
} //---------------------------------------------------------------------------
// Implémentation tu composant TCGiButton
//---------------------------------------------------------------------------
__fastcall TCGiButton::TCGiButton(TComponent* Owner)
: TCustomControl(Owner)
{
Width = 75;
Height = 25;
TabStop = true;
BtnEnfonce = false;
BtnEnfonceBis = false;
MouseSurBtn = false;
ALeFocus = false;
FFont = new TFont();
FFont->OnChange = FontChanged;
FRGB = new TRGB();
FRGB->OnChange = RGBChange;
DoubleBuffered = true;
}
//---------------------------------------------------------------------------
__fastcall TCGiButton::~TCGiButton()
{
delete FFont;
delete FRGB;
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::Paint()
{
RECT CoRect;
CoRect.left = 0;
CoRect.top = 0;
CoRect.right = Width;
CoRect.bottom = Height;
Canvas->Font = Font;
int h = Canvas->TextHeight(Caption);
int y = (Height-h)/2;
int l = Canvas->TextWidth(Caption);
int x = (Width-l)/2; Canvas->Brush->Style = bsClear;
if (BtnEnfonce && MouseSurBtn || BtnEnfonceBis)
{
for (int y=0; y < Height; y++)
{
Canvas->MoveTo(0, y);
Canvas->LineTo(Width, y );
Canvas->Pen->Color= (TColor)(clWhite - (Height-y)*CRGB->pasR*0x10000 -
(Height-y)*CRGB->pasG*0x100 - (Height-y)*CRGB->pasB);
}
Canvas->TextOut(x+1,y+1,Caption);
}
else
{
for (int y=0; y < Height; y++)
{
Canvas->MoveTo(0, y);
Canvas->LineTo(Width, y );
Canvas->Pen->Color= (TColor)(clWhite - y*CRGB->pasR*0x10000 -
y*CRGB->pasG*0x100 - y*CRGB->pasB);
}
Canvas->TextOut(x,y,Caption);
} Canvas->Pen->Color = clBlack;
Canvas->Brush->Color = clWhite;
if (ALeFocus)
{
TRect FocRect;
FocRect.Top = 4;
FocRect.Left = 4;
FocRect.Right = Width - 4;
FocRect.Bottom = Height - 4;
Canvas->DrawFocusRect(FocRect);
}
Canvas->Brush->Color = clBlack;
Canvas->FrameRect(CoRect);
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseDown(TMouseButton Button,
Classes::TShiftState Shift, int X, int Y)
{
BtnEnfonce = true;
MouseSurBtn = true;
SetFocus();
ALeFocus = true;
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseUp(TMouseButton Button,
Classes::TShiftState Shift, int X, int Y)
{
BtnEnfonce = false;
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::KeyDown(Word &Key, Classes::TShiftState Shift)
{
TCustomControl::KeyDown(Key, Shift);
if (Key == 32)
{
BtnEnfonceBis = true;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::KeyUp(Word &Key, Classes::TShiftState Shift)
{
TCustomControl::KeyUp(Key, Shift);
if (Key == 32)
{
BtnEnfonceBis = false;
Click();
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::DoEnter()
{
TCustomControl::DoEnter();
ALeFocus = true;
Invalidate(); // Pour afficher le rectangle de focus.
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::DoExit()
{
TCustomControl::DoExit();
ALeFocus = false;
Invalidate(); // Pour effacer le rectangle de focus.
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::SetFont(TFont *Value)
{
FFont->Assign(Value);
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::SetRGB(TRGB *Value)
{
FRGB->Assign(Value);
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::FontChanged(TObject *Sender)
{
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseLeave(TMessage &Msg)
{
if(BtnEnfonce)
{
MouseSurBtn = false;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseEnter(TMessage &Msg)
{
if(BtnEnfonce)
{
MouseSurBtn = true;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::TextChanged(TMessage &Msg)
{
Invalidate(); // Pour afficher la modification du texte.
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::RGBChange(TObject *Sender)
{
int h ;
h = (CRGB->pasR > CRGB->pasG) ? CRGB->pasR : CRGB->pasG ;
h = (h > CRGB->pasB) ? h : CRGB->pasB;
if (h>0) if (Height > (255/h)) Height = 255/h;
Invalidate();
}
//---------------------------------------------------------------------------
bool __fastcall TCGiButton::CanResize(int &NewWidth, int &NewHeight)
{
int h ;
h = (CRGB->pasR > CRGB->pasG) ? CRGB->pasR : CRGB->pasG ;
h = (h > CRGB->pasB) ? h : CRGB->pasB;
if (h==0) if (NewHeight > (255/h)) NewHeight = 255/h;
return true;
}
//---------------------------------------------------------------------------
namespace Cgibutton
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TCGiButton)};
RegisterComponents("MesCompo", classes, 0);
}
}
/*生活是一種藝術,用心生活才能享受生活*/
發表人 - axsoft 於 2004/04/13 18:13:12