?? unit1.cpp
字號:
//---------------------------------------------------------------------------
#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);
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -