StringGrid设置单元格信息

BCB封装了StringGrid自绘消息OnDrawCell 我们只要重写该事件即可

事实上我们只要判断是否要自绘 但是如果StringGrid作为输入 需要设置任意一个单元格信息 那就要做些额外的工作

我们这里采用list记录每个设置了自绘的单元格

代码如下:

//首先我们需要定义结构来存储自绘信息

 

struct CellInfo { POINT p; struct{ String fontname; TColor c; int size; TFontStyles s; TColor b; }FontInfo; bool operator==(const CellInfo& Info) { if(this->p.x==Info.p.x&& this->p.y==Info.p.y) return true; return false; } }; 

 

#include <list.h>//STL list声明

 

list<CellInfo> CellList;

 

//添加一个PopupMenu、FontDialog

//在PopupMenu菜单事件里响应FontDialog

//将自绘信息插入list

void __fastcall TForm1::N1Click(TObject *Sender) { if(FontDialog1->Execute()) { GellInfo Info; Info.p.x=StringGrid1->Col; Info.p.y=StringGrid1->Row; Info.FontInfo.fontname=FontDialog1->Font->Name; Info.FontInfo.size=FontDialog1->Font->Size; Info.FontInfo.s=FontDialog1->Font->Style; Info.FontInfo.c=FontDialog1->Font->Color; Info.FontInfo.b=clRed; list<CellInfo>::iterator it; it=find(CellList.begin(),CellList.end(),Info); if(it==CellList.end()) { CellList.push_back(Info); } else { (*it).p=Info.p; (*it).FontInfo.fontname=Info.FontInfo.fontname; (*it).FontInfo.c=Info.FontInfo.c; (*it).FontInfo.size=Info.FontInfo.size; (*it).FontInfo.s=Info.FontInfo.s; } } } 

//在OnDrawCell事件编写自绘处理

 

CellInfo Info; Info.p.x=ACol; Info.p.y=ARow; list<CellInfo>::iterator it=find(CellList.begin(),CellList.end(),Info); if(it!=CellList.end()) { dynamic_cast<TStringGrid*>(Sender)->Canvas->Font->Color=(*it).FontInfo.c; dynamic_cast<TStringGrid*>(Sender)->Canvas->Font->Name=(*it).FontInfo.fontname; dynamic_cast<TStringGrid*>(Sender)->Canvas->Font->Size=(*it).FontInfo.size; dynamic_cast<TStringGrid*>(Sender)->Canvas->Font->Style=(*it).FontInfo.s; dynamic_cast<TStringGrid*>(Sender)->Canvas->Brush->Color=(*it).FontInfo.b; dynamic_cast<TStringGrid*>(Sender)->Canvas->FillRect(Rect); dynamic_cast<TStringGrid*>(Sender)->Canvas->TextOutA(Rect.Left+2,Rect.Top+2,StringGrid1->Cells[ACol][ARow]); } 

 

 

你可能感兴趣的:(工作,String,struct,list,iterator,存储)