컴포넌트 교체식으로 구현된 Bitmap 그림을 넣을 수 있는 TShape 컴포넌트 입니다.
화면을 이쁘게 만들때 가끔, TShape의 단순한 모양이 아닌 그림을 표시하고 싶을때가 있습니다.
물론 이럴때는 TImage를 사용하면 됩니다.
그러나 이미 코딩되었는 많은 루틴을 건드리지 않고, 살짝 Bitmap 표시 기능만 덧붙이고자 할때 유용합니다.
기존 컴포넌트에서 기능 일부만 살짝 바꿀때는 컴포넌트 교체식이 짱입니다.
기존 Shape 대신 Bitmap을 표시할 수도 있고,
Shape을 유지한 위에 덧그릴 수도 있습니다.
비트맵의 투명화는 지원하지 않으니, 이를 가능하게 하고 싶다면 조금 연구를 해야 합니다.
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
//---------------------------------------------------------------------------
// Bitmap 그림을 표시할 수 있는 TShape 컴포넌트.
// Written by KTS.
class TShape : public Extctrls::TShape
{
typedef Extctrls::TShape inherited;
public:
Graphics::TBitmap *Bmp;
int BmpX, BmpY;
bool bStretch;
bool bKeepShape;
__fastcall virtual TShape(TComponent* Owner) : inherited(Owner)
{
Bmp = NULL;
BmpX = BmpY = 0;
bStretch = true;
bKeepShape = false;
}
void __fastcall Paint()
{
if (Bmp == NULL)
inherited::Paint();
else
{
if (bKeepShape)
inherited::Paint();
if (bStretch)
Canvas->StretchDraw(ClientRect, Bmp);
else
Canvas->Draw(BmpX, BmpY, Bmp);
}
}
void SetBitmap(Graphics::TBitmap *bmp)
{
Bmp = bmp;
Invalidate();
}
};
#define TShape ::TShape
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TShape *Shape1;
TBitBtn *BitBtn1;
TBitBtn *BitBtn2;
TImage *Image1;
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall BitBtn2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// set bitmap
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Shape1->BmpX = 100;
Shape1->BmpY = 100;
Shape1->bKeepShape = true;
Shape1->bStretch = false;
Shape1->SetBitmap(여기에 그릴 비트맵을 지정하면 됩니다);
}
//---------------------------------------------------------------------------
// clear bitmap
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Shape1->SetBitmap(NULL);
}
//---------------------------------------------------------------------------
그럼.