我想显示一个自定义项目,基本上是一个具有可变列数和宽度的彩色表格。我正在使用c ++ Builder XE2 Rad Studio。
所以,我从TGraphicControl
覆盖void __fastcall Paint(void)
创建了一个新课程。
问题:用文字绘制12个彩色矩形需要相当长的时间(15到30毫秒),所以我希望做错了。我怀疑ShadowRect
做一些可以更好地实现的事情,但我不确定......
有人在这看到我的错误吗?
绘制Paint()
事件的代码:
void __fastcall CustomTrgDrawings::Paint(void){
ResetCanvasTools();
ShadowRect(ClientRect,colBG-0x111111,coladdLight,colText,"");
Canvas->TextOutA(5,8,String().sprintf(L"Logic Box %u",fid));
Canvas->Font->Style = TFontStyles() << fsBold;
ShadowRect(nameRec,colBtnBG,coladdLight,colText,fName);
Canvas->Font->Style = TFontStyles() ;
ShadowRect(ch1Rec,colBtnBG-0x110011,coladdLight,colText,channels->Strings[fch1Id],true,3,true);
ShadowRect(ch2Rec,colBtnBG-0x110011,coladdLight,colText,channels->Strings[fch2Id],true,3,true);
ShadowRect(ch3Rec,colBtnBG-0x110011,coladdLight,colText,channels->Strings[fch3Id],true,3,true);
ShadowRect(ch4Rec,colBtnBG-0x110011,coladdLight,colText,channels->Strings[fch4Id],true,3,true);
ShadowRect(norm1Rec,colBtnBG-0x000011,coladdLight,colText,norms->Strings[fnorm1],true,3,true);
ShadowRect(norm2Rec,colBtnBG-0x000011,coladdLight,colText,norms->Strings[fnorm2],true,3,true);
ShadowRect(norm3Rec,colBtnBG-0x000011,coladdLight,colText,norms->Strings[fnorm3],true,3,true);
ShadowRect(norm4Rec,colBtnBG-0x000011,coladdLight,colText,norms->Strings[fnorm4],true,3,true);
ShadowRect(logicRec,colBtnBG-0x002222,coladdLight,colText,logics->Strings[flogicId],true,3,true);
ShadowRect(normOutRec,colBtnBG-0x002200,coladdLight,colText,norms->Strings[fnormOut],true,3,true);
}
void CustomTrgDrawings::ResetCanvasTools(){
Canvas->Brush->Color=clNone;
Canvas->Brush->Style=bsClear;
Canvas->Pen->Color=clNone;
Canvas->Pen->Mode=pmCopy;
Canvas->Pen->Style=psSolid;
Canvas->Pen->Width=1;
Canvas->Font->Color=clBlack;
Canvas->Font->Size=8;
Canvas->Font->Style=TFontStyles();
}
void CustomTrgDrawings::ShadowRect(const TRect pos,const TColor bg, const TColor add,const TColor fg, const String text,const bool shadow,const int align,const bool comboIcon){
int textLen;
int textX,textY;
int iconWidth=0;
Canvas->Brush->Style=bsSolid;
Canvas->Brush->Color=bg;
Canvas->Pen->Color=bg-4*add;
Canvas->Pen->Style=psSolid;
Canvas->Pen->Width=1;
Canvas->FillRect(pos);
if(shadow){
Canvas->Pen->Color=bg-2*add;
Canvas->MoveTo(pos.Left,pos.Bottom-1);
Canvas->LineTo(pos.Right-1,pos.Bottom-1);
Canvas->LineTo(pos.Right-1,pos.Top);
Canvas->Pen->Color=bg+2*add;
Canvas->MoveTo(pos.Right-1,pos.Top);
Canvas->LineTo(pos.Left,pos.Top);
Canvas->LineTo(pos.Left,pos.Bottom-1);
}
if(comboIcon){
iconWidth=6;
Canvas->Pen->Style=psSolid;
Canvas->Pen->Mode=pmMask;
Canvas->Pen->Width=3;
Canvas->Pen->Color=bg-2*add;
Canvas->MoveTo(pos.Right-iconWidth-5,pos.Top+6);
Canvas->LineTo(pos.Right-iconWidth/2-5,pos.Top+10);
Canvas->LineTo(pos.Right-5,pos.Top+6);
}
Canvas->Brush->Style=bsClear;
Canvas->Pen->Color=fg;
textLen=Canvas->TextWidth(text);
switch(align%3){ //horizontal position
case 0: //left
textX=3;
break;
case 1: //middle
textX=((pos.Width()-iconWidth)-textLen)/2;
break;
case 2: //right
textX=(pos.Width()-iconWidth)-textLen;
break;
}
switch(align/3){ //vertical position
case 0: //top
textY=-1;
break;
case 1: //middle
textY=(pos.Height()-Canvas->TextHeight(text))/2;
break;
case 2: //bottom
textY=pos.Height()-Canvas->TextHeight(text);
break;
}
Canvas->TextOutA(pos.Left+textX,pos.Top+textY,text);
}