danielj您好: 這有個TImagePanel的元件,您可以參看看他的寫法,或者直接使用這個元件: 官方網站:http://www.leunen.com/
官方下載:http://www.leunen.com/download/imgpanel.zip
本地下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=11201344&CC=250516 /*---------------------------------------------------------------------------
Program : ImagePanel.cpp Version 1.1
TImagePanel Component for BCB1 and BCB3/4/5
Author : Michel Leunen
Email : leu@rtbf.be
Michel@leunen.com
Date : 04/08/1998
Description : Component for Borland C++ Builder V1 that draws a bitmap on a
TPanel. The image could be stretchd, tiled or drawn alone.
*/
//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop #include "ImagePanel.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TImagePanel *)
{
new TImagePanel(NULL);
}
//---------------------------------------------------------------------------
__fastcall TImagePanel::TImagePanel(TComponent* Owner)
: TGraphicControl(Owner)
{
FBitmap=new Graphics::TBitmap();
FImageStyle=ipSingle;
FXPosition=0;
FYPosition=0;
Width=100;
Height=36;
Caption="";
}
//---------------------------------------------------------------------------
__fastcall TImagePanel::~TImagePanel()
{
delete FBitmap;
FBitmap=NULL;
}
//---------------------------------------------------------------------------
namespace Imagepanel
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TImagePanel)};
RegisterComponents("Leunen", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TImagePanel::Paint(void)
{
//Paint Rect around control
Canvas->Pen->Style=psSolid;
Canvas->Pen->Color=clBlack;
Canvas->LineTo(Width-1,0);
Canvas->Pen->Color=clWhite;
Canvas->LineTo(Width-1,Height-1);
Canvas->LineTo(0,Height-1);
Canvas->Pen->Color=clBlack;
Canvas->LineTo(0,0); if(ValidBmp && FBitmap->Modified)
{
tmpBitmap=new Graphics::TBitmap();
tmpBitmap->Assign(FBitmap);
if(FBitmap)
{
int i,j,bmpWidth,bmpHeight;
TRect rect;
rect=ClientRect;
rect.Top++;
rect.Right--;
rect.Bottom--;
rect.Left++;
switch ((int)FImageStyle)
{
case ipSingle:
if(tmpBitmap->Width>rect.Right-FXPosition)
bmpWidth=rect.Right-FXPosition;
else bmpWidth=tmpBitmap->Width;
if(tmpBitmap->Height>rect.Bottom-FYPosition)
bmpHeight=rect.Bottom-FYPosition;
else bmpHeight=tmpBitmap->Height;
::BitBlt(Canvas->Handle,
FXPosition,
FYPosition,
bmpWidth,
bmpHeight,
tmpBitmap->Canvas->Handle,
0,
0,
SRCCOPY);
break; case ipStretch:
Canvas->StretchDraw(rect,tmpBitmap);
break; case ipTile:
for(i=rect.Top;iHeight)
{
for(j=rect.Left;jWidth)
{
if(jWidth)bmpWidth=tmpBitmap->Width;
else bmpWidth=rect.Right-j;
if(iHeight)bmpHeight=tmpBitmap->Height;
else bmpHeight=rect.Bottom-i;
::BitBlt(Canvas->Handle,
j,
i,
bmpWidth,
bmpHeight,
tmpBitmap->Canvas->Handle,
0,
0,
SRCCOPY);
}
}
}
}
delete tmpBitmap;
}
}
//---------------------------------------------------------------------------
void __fastcall TImagePanel::SetBitmap(Graphics::TBitmap *ABitmap)
{
if (ABitmap!=NULL)ValidBmp=true;
else ValidBmp=false;
FBitmap->Assign(ABitmap);
Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TImagePanel::SetXPos(int AXPosition)
{
FXPosition=AXPosition;
Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TImagePanel::SetYPos(int AYPosition)
{
FYPosition=AYPosition;
Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TImagePanel::SetImageStyle(TImageDrawStyle AImageStyle)
{
FImageStyle=AImageStyle;
Repaint();
}
//--------------------------------------------------------------------------- /*---------------------------------------------------------------------------
Program : ImagePanel.cpp Version 1.1
TImagePanel Component for BCB1 and BCB3/4/5
Author : Michel Leunen
Email : leu@rtbf.be
Michel@leunen.com
Date : 04/08/1998
Description : Component for Borland C++ Builder V1 that draws a bitmap on a
TPanel. The image could be stretchd, tiled or drawn alone.
*/
//---------------------------------------------------------------------------
#ifndef ImagePanelH
#define ImagePanelH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
enum TImageDrawStyle {ipSingle,ipStretch,ipTile}; //Enum drawing Style
class PACKAGE TImagePanel : public TGraphicControl
{
private:
Graphics::TBitmap *FBitmap,*tmpBitmap;
int FXPosition; //x origin
int FYPosition; // y origin
bool ValidBmp;
TImageDrawStyle FImageStyle; // drawing style
void __fastcall SetBitmap(Graphics::TBitmap *ABitmap);
void __fastcall SetImageStyle(TImageDrawStyle AImageStyle);
void __fastcall SetXPos(int AXPosition);
void __fastcall SetYPos(int AYPosition); protected:
void __fastcall Paint(void); // override paint() public:
__fastcall TImagePanel(TComponent* Owner);
__fastcall ~TImagePanel(); __published:
__property Graphics::TBitmap *Bitmap = {read=FBitmap, write=SetBitmap};
__property TImageDrawStyle DrawingStyle = {read=FImageStyle, write=SetImageStyle};
__property int XPosition = {read=FXPosition, write=SetXPos};
__property int YPosition = {read=FYPosition, write=SetYPos};
__property Align;
__property ShowHint;
__property ParentShowHint;
__property Canvas;
__property Visible;
__property OnClick;
__property OnMouseMove;
__property OnMouseDown;
__property OnMouseUp; };
//---------------------------------------------------------------------------
#endif
HAVE A NICE DAY FOR YOU