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

📄 currencyedit.cpp

📁 学习C_++ builder 时候用到的一些有用的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
__fastcall TCurrencyEdit::TCurrencyEdit(TComponent* Owner)
        : TCustomPanel(Owner)
{
        Color                    = clWhite;
        FPenWidth                = 1;
        FPenColor                = clBlack;
        FAlignment               = taCenter;
        FAbout                   = "Lcsoft Currency Edit";
        FTitle                   = new TCurrencyTitle();
        FTitle->OnChange         = TitleChanged;
        FBody                    = new TCurrencyBody();
        FBody->OnChange          = BodyChanged;
        FTitleLabel              = new TLabel(this);
        FTitleLabel->Parent      = this;
        FTitleLabel->Caption     = "十亿千百十万千百十元角分";
        FTitleLabel->Transparent = FTitle->Transparent;
        FTitleLabel->Top         = FPenWidth;
        for (int i=0; i<NumCount; i++)
        {
          FNumEdit[i]              = new TEdit(this);
          FNumEdit[i]->Parent      = this;
          FNumEdit[i]->BorderStyle = bsNone;
          FNumEdit[i]->ParentColor = true;
          FNumEdit[i]->MaxLength   = 1;
          FNumEdit[i]->Width       = 9;
          FNumEdit[i]->Tag         = i;
          FNumEdit[i]->OnKeyDown   = NumEditKeyDown;
          FNumEdit[i]->OnKeyPress  = NumEditKeyPress;
        }
        DoCurrencyAlignment(FBody->CurrencyAlignment);
        for (int i=0; i<3; i++)
        {
          FNumEdit[i]->Text = "0";
        }
        Height = 100;
        Width  = FTitleLabel->Width + 2 * FPenWidth;
}
//---------------------------------------------------------------------------

__fastcall TCurrencyEdit::~TCurrencyEdit(void)
{
        delete FBody;
        delete FTitle;
        delete []FNumEdit;
        delete FTitleLabel;
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::Paint(void)
{
        TCustomPanel::Paint();
        Caption = "";
        DoCurrencyAlignment(FBody->CurrencyAlignment);
        DoTitlePosition(FTitleLabel, FAlignment, FPenWidth);
        FTitleLeft = FTitleLabel->Left;
        FCurrencyLeft = FTitleLeft - FPenWidth;
        for (int i=0; i<NumCount; i++)
        {
          FNumEdit[i]->Left = FTitleLeft + 12 * (NumCount - 1 - i);
        }
        Canvas->Pen->Width = FPenWidth;
        Canvas->Pen->Color = FPenColor;
        Canvas->MoveTo(FCurrencyLeft, 0);
        Canvas->LineTo(FCurrencyLeft + FTitleLabel->Width, 0);
        Canvas->MoveTo(FCurrencyLeft, FTitleLabel->Top + FTitleLabel->Height);
        Canvas->LineTo(FCurrencyLeft + FTitleLabel->Width, FTitleLabel->Top + FTitleLabel->Height);
        for (int i=0; i<=NumCount; i++)
        {
          if (i == 2)
          {
            Canvas->Pen->Color = FBody->DotLineColor;
          }
          else if (i == 0 || i == NumCount)
          {
            Canvas->Pen->Color = FPenColor;
            if (!FTitle->Transparent && i == 0)
            {
              Canvas->MoveTo(12 * NumCount + FCurrencyLeft, 0);
              Canvas->LineTo(12 * NumCount + FCurrencyLeft, Height);
              continue;
            }
          }
          else if ((i - 2) % 3 == 0)
          {
            Canvas->Pen->Color = FBody->KilobitLineColor;
          }
          else
          {
            Canvas->Pen->Color = FBody->SplitLineColor;
          }
          Canvas->MoveTo(12 * (NumCount - i) + FCurrencyLeft, 0);
          Canvas->LineTo(12 * (NumCount - i) + FCurrencyLeft, Height);
        }
        Canvas->Pen->Color = FPenColor;
        Canvas->MoveTo(FCurrencyLeft, Height - FPenWidth);
        Canvas->LineTo(FCurrencyLeft + FTitleLabel->Width, Height - FPenWidth);
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::DoTitlePosition(TControl *Control, TAlignment Alignment, int PenWidth)
{
        switch (Alignment)
        {
          case taLeftJustify:
              Control->Left = PenWidth;
            break;
          case taCenter:
              Control->Left = (Width - Control->Width) / 2;
            break;
          case taRightJustify:
              Control->Left = Width - Control->Width;
            break;
        }
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::NumEditKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
        int i = dynamic_cast<TEdit*>(Sender)->Tag;
        switch (Key)
        {
          case VK_LEFT:
          case VK_UP:
            if (i < NumCount - 1)
            {
              FNumEdit[i + 1]->SetFocus();
            }
            break;
          case VK_RIGHT:
          case VK_DOWN:
            if (i > 0)
            {
              FNumEdit[i - 1]->SetFocus();
            }
            break;
        }
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::NumEditKeyPress(TObject *Sender, char &Key)
{
        int i = dynamic_cast<TEdit*>(Sender)->Tag;
        if (Key >= '0' && Key <= '9' )
        {
          if (i < NumCount - 1)
          {
            FNumEdit[i + 1]->SetFocus();
          }  
        }
        else
        {
          Key = 0;
        }
}
//---------------------------------------------------------------------------

AnsiString __fastcall TCurrencyEdit::GetValue(void)
{
        FValue = "";
        for (int i=NumCount-1; i>=0; i--)
        {
          if (i == 2)
          {
            FValue += ".";
          }
          FValue += FNumEdit[i]->Text;
        }
        return FValue.Trim();
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::SetAlignment(TAlignment Value)
{
        if (Value != FAlignment)
        {
          FAlignment = Value;
          Invalidate();
        }
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::SetAbout(AnsiString Value)
{
        if (Value != FAbout)
        {
          FAbout = Value;
        }
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::SetTitle(TCurrencyTitle *Value)
{
        FTitle->Assign(Value);
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::TitleChanged(TObject* Sender)
{
        FTitleLabel->Font = FTitle->Font;
        FTitleLabel->Transparent = FTitle->Transparent;
        Invalidate();
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::SetBody(TCurrencyBody *Value)
{
        FBody->Assign(Value);
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::BodyChanged(TObject* Sender)
{
        for (int i=0; i<NumCount; i++)
        {
          FNumEdit[i]->Font = FBody->Font;
          DoCurrencyAlignment(FBody->CurrencyAlignment);
        }
        Invalidate();
}
//---------------------------------------------------------------------------

void __fastcall TCurrencyEdit::DoCurrencyAlignment(TCurrencyAlignment Kind)
{
        switch (Kind)
        {
          case caTop:
            for (int i=0; i<NumCount; i++)
            {
              FNumEdit[i]->Top = FTitleLabel->Top + FTitleLabel->Height + FPenWidth;
            }
            break;
          case caCenter:
            for (int i=0; i<NumCount; i++)
            {
              FNumEdit[i]->Top = (Height - FNumEdit[i]->Height) / 2 + FTitleLabel->Height;
            }
            break;
          case caBottom:
            for (int i=0; i<NumCount; i++)
            {
              FNumEdit[i]->Top = Height - FNumEdit[i]->Height - FPenWidth;
            }
            break;
        }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -