Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:2k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::PaintText(TCanvas *cv,int x1,int y1,int x2,int y2,AnsiString text,int align)
- {
- tagRECT drect;
- int xx,yy,hh,ww,ll;
- //计算Y
- hh=cv->TextHeight(text);
- hh=y2-y1-hh;
- hh/=2;
- if(hh<0)hh=0;
- yy=y1+hh;
- //超宽检查
- ww=cv->TextWidth(text);
- if(ww>(x2-x1))
- {
- for(;;)
- {
- if(cv->Font->Size<=8)break;
- cv->Font->Size--;
- ww=cv->TextWidth(text);
- if(ww<=(x2-x1))break;
- }
- }
- //
- ww=cv->TextWidth(text);
- if(ww>(x2-x1))//如果再超宽
- {
- drect.left=x1;
- drect.top=y1;
- drect.right=x2;
- drect.bottom=y2;
- ll=text.Length();
- DrawText(cv->Handle,text.c_str(),
- ll,&drect,DT_WORDBREAK);
- }
- else//按对齐方式输出文本
- {
- if(align==0)//居左
- {
- xx=x1;
- }
- else if(align==2)//居右
- {
- ll=x2-x1-ww;
- if(ll<0)ll=0;
- xx=x1+ll;
- }
- else//居中
- {
- ll=x2-x1-ww;
- ll/=2;
- if(ll<0)ll=0;
- xx=x1+ll;
- }
- cv->TextOut(xx,yy,text);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- int xx1,xx2,yy1,yy2;
- xx1=20;
- xx2=120;
- yy1=20;
- yy2=60;
- Canvas->Brush->Color=clWhite;
- Canvas->FillRect(TRect(xx1,yy1,xx2,yy2));
- Canvas->Font->Size=12;
- PaintText(Canvas,xx1,yy1,xx2,yy2,Edit1->Text,RadioGroup1->ItemIndex);
- }
- //---------------------------------------------------------------------------