⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 有视频
💻 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 + -