//---------------------------------------------------------------------------
#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DrawGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
//获得DrawGrid的画布Canvas
TCanvas *Canvas = DrawGrid1->Canvas;
//照片文件名为cc (1).jpg到cc (9).jpg,根据单元格的ACol和ARow而定
AnsiString fileName="res\\cc ("+IntToStr(ARow*DrawGrid1->ColCount+ACol+1)
+").jpg";
// ShowMessage(fileName);
//照片不存在时显示提示信息并返回
if(!FileExists(fileName))
{
Canvas->TextRect(Rect,Rect.Left,Rect.Top,fileName+" 不存在");
return;
}
//新建TJPEGImage对象jpgimg
TJPEGImage *jpgimg;
jpgimg=new TJPEGImage();
//读取照片
jpgimg->LoadFromFile(fileName);
//向单元格区域绘制照片
if((float)jpgimg->Width/Rect.Width()==(float)jpgimg->Height/Rect.Height())
Canvas->StretchDraw(Rect, jpgimg);
else if((float)jpgimg->Width/Rect.Width()<(float)jpgimg->Height/Rect.Height())
{
int bW=jpgimg->Width*Rect.Height()/jpgimg->Height;
int left=Rect.left+(Rect.Width()-bW)/2;
TRect rect=TRect(left,Rect.top,left+bW,Rect.Bottom);
Canvas->StretchDraw(rect,jpgimg);
}
else
{
int bH=jpgimg->Height*Rect.Width()/jpgimg->Width;
int top=Rect.top+(Rect.Height()-bH)/2;
TRect rect=TRect(Rect.Left,top,Rect.Right,top+bH);
Canvas->StretchDraw(rect,jpgimg);
}
//删除jpgimg对象
delete jpgimg;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage("亲,给个赞吧!");
}
//---------------------------------------------------------------------------