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

📄 ttieditbinaryhex.cpp

📁 十六进制输入控件, 可以在C++ Builder下面使用.
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "TTIEditBinaryHex.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
namespace Ttieditbinaryhex
{
  void __fastcall PACKAGE Register()
  {
     TComponentClass classes[1] = {__classid(TTTI_EditBinaryHex)};
     RegisterComponents("TTI", classes, 0);
  }
}
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TTTI_EditBinaryHex *)
{
  new TTTI_EditBinaryHex(NULL);
}
//---------------------------------------------------------------------------
__fastcall TTTI_EditBinaryHex::TTTI_EditBinaryHex(TComponent* Owner)
  : TCustomPanel(Owner)
{
  this->BevelInner = bvNone;
  this->BevelOuter = bvNone;
  btnHex = new TSpeedButton(this);
  btnHex->Parent = this;
  btnAsc = new TSpeedButton(this);
  btnAsc->Parent = this;
  edtValue = new TEdit(this);
  edtValue->Parent = this;
  //edtValue->CharCase = ecUpperCase;

  btnHex->Caption = "HEX";
  btnHex->ParentFont = false;
  btnHex->Font->Name = "Arial";
  btnHex->Font->Size = 8;
  btnHex->Layout = blGlyphBottom;
  btnHex->GroupIndex = 1;

  btnHex->OnClick = OnChagneHexAsc; 
  btnAsc->Caption = "ASC";
  btnAsc->ParentFont = false;
  btnAsc->Font->Name = "Arial";
  btnAsc->Font->Size = 8;
  btnAsc->Layout = blGlyphBottom;
  btnAsc->GroupIndex = 1;
  btnAsc->OnClick = OnChagneHexAsc;
  Font->Style = Font->Style << fsBold;   
  FDataFormat = dfAsc;
}

//---------------------------------------------------------------------------
__fastcall TTTI_EditBinaryHex::~TTTI_EditBinaryHex()
{
  delete btnHex;
  delete btnAsc;
  delete edtValue;
}
//---------------------------------------------------------------------------
void __fastcall TTTI_EditBinaryHex::SetText(AnsiString value)
{
  //TODO: Add your source code here
  edtValue->Text = value;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TTTI_EditBinaryHex::GetText()
{
  //TODO: Add your source code here
  return edtValue->Text;
}

void __fastcall TTTI_EditBinaryHex::OnResize(TMessage & Msg)
{
  edtValue->Height = 22;
  btnAsc->Width = 34;
  btnAsc->Height = 14;
  btnHex->Width = 34;
  btnHex->Height = 14;
  if (Width < 56)
    Width = 56;
  Height = 28;
  edtValue->Left = 0;
  edtValue->Top  = 2;
  edtValue->Width = Width - btnHex->Width;
  btnHex->Top =0;
  btnHex->Left = Width - btnHex->Width;
  btnAsc->Top =  Height - btnAsc->Height;
  btnAsc->Left = Width - btnAsc->Width;
  this->Resize(); 
}

void __fastcall TTTI_EditBinaryHex::OnChagneHexAsc(TObject * Sender)
{
  if (Sender == btnAsc)
    DataFormat = dfAsc;
  else
    DataFormat = dfHex;
}


//---------------------------------------------------------------------------


void __fastcall TTTI_EditBinaryHex::SetDataFormat(TTI_DataFormat value)
{
  if (FDataFormat == value)
  { 
    return;
  }
  bool CanChange = false;
  if (value == dfHex)
  {
    char * pHexValue = (char*)malloc(edtValue->Text.Length()*2 + 1);
    memset(pHexValue, 0, edtValue->Text.Length()*2 + 1);
    BinToHex(edtValue->Text.c_str(), pHexValue, edtValue->Text.Length());
    edtValue->Text = AnsiString(pHexValue);
    free(pHexValue);
    CanChange = true;
    edtValue->CharCase = ecUpperCase;
  }
  else
  {
    bool IsValidHex = true;
    bool CanConvert = true;
    char * pHexValue = (char*)malloc(edtValue->Text.Length()+1);
    memset(pHexValue, 0, edtValue->Text.Length()+1);
    memcpy(pHexValue, edtValue->Text.c_str(), edtValue->Text.Length());
    if (edtValue->Text.Length() % 2 != 0)
    {
      IsValidHex = false;
    }
    else
    {
      for (int i=0; i < edtValue->Text.Length(); i++)
      {
        if (!((pHexValue[i] >= 0x30)&&(pHexValue[i]<= 0x39) ||
              (pHexValue[i] >= 0x61)&&(pHexValue[i]<= 0x66) ||
              (pHexValue[i] >= 0x41)&&(pHexValue[i]<= 0x46)
           ))
        {
          IsValidHex = false;
          break;
        }
      }
    }

    if (IsValidHex == true)
    {
      char * pBinValue = (char*)malloc(edtValue->Text.Length()/2 + 1);
      memset(pBinValue, 0 , edtValue->Text.Length()/2 + 1);
      HexToBin(pHexValue, pBinValue, edtValue->Text.Length()/2);
      AnsiString strOldHexValue = edtValue->Text;
      edtValue->CharCase = ecNormal;
      edtValue->Text = AnsiString(pBinValue);
      if (edtValue->Text.Length() != strOldHexValue.Length()/2)
      {
        edtValue->Text = strOldHexValue;
        CanConvert = false;
      }
      free(pBinValue);
    }
    free(pHexValue);
    if ((IsValidHex == false) || (CanConvert == false))
    {
      CanChange = false;
    }
    else
      CanChange = true; 
  }
  if (CanChange == true)
    FDataFormat = value;
  this->Invalidate();
}                                                        

TTI_DataFormat __fastcall TTTI_EditBinaryHex::GetDataFormat()
{
  return FDataFormat;
}

void __fastcall TTTI_EditBinaryHex::Paint(void)
{
  if (FDataFormat == dfHex)
    btnHex->Down = true;
  else
    btnAsc->Down = true;
}

void __fastcall TTTI_EditBinaryHex::SetButtonFont(TFont * value)
{
  btnHex->Font = value;
  btnAsc->Font = value;
}
TFont * __fastcall TTTI_EditBinaryHex::GetButtonFont()
{
  return btnHex->Font;
}

bool __fastcall TTTI_EditBinaryHex::StoredDataFormat()
{
  if (FDataFormat == dfAsc)
    return false;
  else
    return true;
}

⌨️ 快捷键说明

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