C++Builder 自编常用函数库(1)

类别:编程语言 点击:0 评论:0 推荐:


#ifndef DUJUNLITOOLSCPP
#define DUJUNLITOOLSCPP
//函数库2002-11-15 19:26
#include <vcl.h>
#include <grids.hpp>
#include <stdio.h>
#include <utilcls.h>
#include <printers.hpp>
#include <IdBaseComponent.hpp>
#include <IdCoder.hpp>
#include <IdCoderMessageDigest.hpp>
///////////////////////////md5
AnsiString __fastcall Strip(AnsiString S)
{
return S.Delete(1, 2);
}

AnsiString md5ToString(AnsiString S)
{
    AnsiString AHex;

    for (int i=1; i<=S.Length(); i++)
    {
     AHex += IntToHex((unsigned char)S[i], 2);// + " ";
    }

    return AHex;
}
String Md5Str(String src)
{
TIdCoderMD5 *md5=new TIdCoderMD5(NULL);
md5->Reset();
md5->AutoCompleteInput = true;
String tmp=md5ToString(Strip(md5->CodeString(src)));
delete md5;
md5=NULL;
return tmp;
}
//////////////////////////md5 end
bool OpenForm(TForm * WForm)
{

  //if(OpenForm(frmmem)==False)
  // {
  // frmmem=new Tfrmmem(Application);
  // OpenForm(frmmem);
  // }//使用例子
  int i;
  bool FormExist;

  if(WForm==NULL) return False;
  FormExist=False;
  for(i=0;i<Screen->FormCount;i++)
      if(Screen->Forms[i]->ClassType()==WForm->ClassType())
        {
          FormExist=True;
          break;
        }
  if(FormExist==False) return FormExist;

  if(WForm->WindowState==wsMinimized)
    ShowWindow(WForm->Handle,SW_SHOWNORMAL);
  else
    ShowWindow(WForm->Handle,SW_SHOWNA);
  if(!WForm->Visible) WForm->Visible=True;

  if(GetWindowLong(WForm->Handle,GWL_EXSTYLE&WS_EX_APPWINDOW)!=WS_EX_APPWINDOW)
     SetWindowLong(WForm->Handle,GWL_EXSTYLE,WS_EX_APPWINDOW);

  WForm->BringToFront();
  WForm->SetFocus();
  return True;
}
//----------------------------------------------------------------
String repl(String sstr,String fstr,String repstr)
{
//替换字符串函数
String tmpstr="";
int x = sstr.AnsiPos(fstr);
if(x==0)
   {
   tmpstr=sstr;
   //Application->MessageBoxA(sstr.c_str(),"",MB_OK); //未找到查找字串,原样返回
   return tmpstr;
   }
while(x != 0)
  {
  tmpstr +=sstr.SubString(1,x-1) + repstr;
  sstr = sstr.SubString(x+fstr.Length(),sstr.Length());
  x = sstr.AnsiPos(fstr);
  }
tmpstr+=sstr;
//Application->MessageBoxA(tmpstr.c_str(),"",MB_OK);
return tmpstr;
}
//---------------------------------------------
String sqlstr(String sqlstr)
{
//
if(sqlstr!="")
  {
  sqlstr=repl(sqlstr,"'","''");
  }
return sqlstr;
}
//-------------------------------------
TDateTime pmon(TDateTime dt)
{//取上一个月的日期
  unsigned short year, mon, day, hour, min, sec,msec;
  dt.DecodeDate( &year,&mon,&day);
  dt.DecodeTime( &hour, &min, &sec, &msec );
  if(mon<=1)
    {
      year-=1;
      mon=12;
    }
  else
    {
    mon-=1;
    }
  dt=::EncodeDate(year,mon,day);
  return dt;
}
TDateTime nmon(TDateTime dt)
{//取下一个月的日期
  unsigned short year, mon, day, hour, min, sec,msec;
  dt.DecodeDate( &year,&mon,&day);
  dt.DecodeTime( &hour, &min, &sec, &msec );
  if(mon>=12)
    {
      year+=1;
      mon=1;
    }
  else
    {
    mon+=1;
    }
  dt=::EncodeDate(year,mon,day);
  return dt;
}
//////////////////////////////////////////////////////////////////////////////////////
//将数据集得数据导出到Excel;
//void DataSetToExcel(TDataSet* DataSet,bool H); //H:表按行排(正常)
//Variant Ex,Wbs,Wb,Sh1;
//////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
/*
//不完善,错误为:如果数据含有过滤属性,则用RecCount就不合适
void DataSetToExcel(TDataSet* DataSet,bool H)
{
  Variant Ex,Wbs,Wb,Sh1;
  int CurrRow,CurrCol;
  CurrCol=0;
  CurrRow=0;
  Screen->Cursor=crHourGlass ;
  if(Ex.IsEmpty())
      try
        {
        HWND hPrevApp = ::FindWindow(NULL,"Microsoft Excel");
        if(!hPrevApp)
          {
          Ex=Variant::CreateObject("Excel.Application");
          }
        else
          {
          Ex=Variant::GetActiveObject("Excel.Application");
          }
        }
      catch(...)
        {
        Screen->Cursor=crDefault;
        ShowMessage("打开Excel出错,请确认你已经正确安装了MS Office!");
        return;
        }
    try
      {

      if(Ex.OlePropertyGet("ActiveWorkBook").IsEmpty())
        Ex.OlePropertyGet("WorkBooks").OleProcedure("ADD");
      if(Wb.IsEmpty())
        Wb=Ex.OlePropertyGet("ActiveWorkBook");
      Sh1=Wb.OlePropertyGet("Sheets").OleFunction("Add");
      Ex.OlePropertySet("Visible",true);
      }
    catch(...)
      {
      Ex=Ex.Empty();
      Wb=Wb.Empty();
      DataSetToExcel(DataSet,H);
      }
    if(H)
      {

      for (int j=0;j<DataSet->Fields->Count;j++)
            {
            if(DataSet->Fields->Fields[j]->Visible)
              {
                CurrCol++;
                Sh1.OlePropertyGet("Cells",1,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
              }
            }
      DataSet->First();
      for (int i=0;i<DataSet->RecordCount;i++)
        {
          CurrCol=0;
          for (int j=0;j<DataSet->Fields->Count;j++)
            {
            if(DataSet->Fields->Fields[j]->Visible)
              {
                CurrCol++;
                Sh1.OlePropertyGet("Cells",i+2,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
              }
            }
          Application->ProcessMessages();
          DataSet->Next();
        }
      }
    else
      {
        for (int j=0;j<DataSet->Fields->Count;j++)
        {
          if(DataSet->Fields->Fields[j]->Visible)
            {
            CurrRow++ ;
            Sh1.OlePropertyGet("Cells",CurrRow,1).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
            DataSet->First();
            for (int i=0;i<DataSet->RecordCount;i++)
              {
                Sh1.OlePropertyGet("Cells",CurrRow,i+2).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
                DataSet->Next();
              }
            }
        }
      }
    Screen->Cursor=crDefault;
} */
void DataSetToExcel(TDataSet* DataSet,bool H)
{
  Variant Ex,Wbs,Wb,Sh1;
  int CurrRow,CurrCol;
  CurrCol=0;
  CurrRow=0;
  Screen->Cursor=crHourGlass ;
  if(Ex.IsEmpty())
      try
        {
        HWND hPrevApp = ::FindWindow(NULL,"Microsoft Excel");
        if(!hPrevApp)
          {
          Ex=Variant::CreateObject("Excel.Application");
          }
        else
          {
          Ex=Variant::GetActiveObject("Excel.Application");
          }
        }
      catch(...)
        {
        Screen->Cursor=crDefault;
        ShowMessage("打开Excel出错,请确认你已经正确安装了MS Office!");
        return;
        }
    try
      {

      if(Ex.OlePropertyGet("ActiveWorkBook").IsEmpty())
        Ex.OlePropertyGet("WorkBooks").OleProcedure("ADD");
      if(Wb.IsEmpty())
        Wb=Ex.OlePropertyGet("ActiveWorkBook");
      Sh1=Wb.OlePropertyGet("Sheets").OleFunction("Add");
      }
    catch(...)
      {
      Ex=Ex.Empty();
      Wb=Wb.Empty();
      DataSetToExcel(DataSet,H);
      }
    if(H)
      {

      for (int j=0;j<DataSet->Fields->Count;j++)
            {
            if(DataSet->Fields->Fields[j]->Visible)
              {
                CurrCol++;
                Sh1.OlePropertyGet("Cells",1,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
              }
            }
      DataSet->First();
/*      for (int i=0;i<DataSet->RecordCount;i++)
        {
          CurrCol=0;
          for (int j=0;j<DataSet->Fields->Count;j++)
            {
            if(DataSet->Fields->Fields[j]->Visible)
              {
                CurrCol++;
                Sh1.OlePropertyGet("Cells",i+2,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
              }
            }
          Application->ProcessMessages();
          DataSet->Next();
        }
*/
          int i=0;
          while(!DataSet->Eof)
             {
              CurrCol=0;
              for (int j=0;j<DataSet->Fields->Count;j++)
                {
                if(DataSet->Fields->Fields[j]->Visible)
                  {
                    CurrCol++;
                    Sh1.OlePropertyGet("Cells",i+2,CurrCol).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
                  }
                }
              Application->ProcessMessages();
              DataSet->Next();
              i++;
              }
      }
    else
      {
        for (int j=0;j<DataSet->Fields->Count;j++)
        {
          if(DataSet->Fields->Fields[j]->Visible)
            {
            CurrRow++ ;
            Sh1.OlePropertyGet("Cells",CurrRow,1).OlePropertySet("value",DataSet->Fields->Fields[j]->FieldName);
            DataSet->First();
            int i=0;
            while(!DataSet->Eof)
              {
                Sh1.OlePropertyGet("Cells",CurrRow,i+2).OlePropertySet("value",DataSet->Fields->Fields[j]->AsString);
                Application->ProcessMessages();
                DataSet->Next();
                i++;
              }
            }
        }
      }
    Ex.OlePropertySet("Visible",true);
    Screen->Cursor=crDefault;
}
//-------------------------------------------------------------------------
String makestr(String mstr,char b,int len,bool QH)//QH在前或者后加字符
{
int slen=mstr.Length();
        String tmp="";
        if(slen<len)
          {
             if(QH)
               {
                tmp=AnsiString::StringOfChar(b,len-slen)+mstr;
               }
             else
               {
                tmp=mstr+AnsiString::StringOfChar(b,len-slen);
               }
          }
        else
          {
             tmp=mstr;
          }
        return tmp;
}
//------------------------------------------------------------------------
String getdtbh()
{
        //按日期生成类似20001025--000001的编号
        TDateTime dt;
        unsigned short year,month,day;
        ::DecodeDate(dt.CurrentDate(),year,month,day);
        String tmp="";
        tmp=String(year)+makestr(month,'0',2,true)+makestr(day,'0',2,true);
        return tmp;
}
//------------------------------------------------------------------------

//=============================================================================
//   函数使用说明:输入表名称,取得所有纪录的最大值加一,转换成n位的00001形式
//   输入:cTableName 数据库中的某一表的名称
//         nNumkeep 格式化后字符串的总长度
//=============================================================================
AnsiString  __fastcall TMainForm::GetPerId(AnsiString cTableName,int nNumkeep)
{

      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add("Select Count(*) AS curn from "+cTableName);
      ADOQuery1->Prepared;
      ADOQuery1->Active=true;
      ADOQuery1->Open();
//      ShowMessage(ADOQuery1->Fields->Fields[0]->AsString) ;
      int ngetn=ADOQuery1->FieldByName("curn")->AsInteger;
      ADOQuery1->Active=false;
      ADOQuery1->Close();
      return Get00Str(ngetn+1,nNumkeep);
}

/*-----------------------------------------------------------------------
  函数:Get00Str
  功能:生成 0000000x 形字符串
  输入: oragn 被转换的整形数
         nNumkeep 格式化后字符串的总长度
------------------------------------------------------------------------*/

AnsiString  __fastcall TMainForm::Get00Str(int oragn,int nNumkeep)
{

         AnsiString cc=IntToStr(oragn);
          cc=cc.Trim();
          AnsiString tmp0="";
          int Flen=cc.Length();
          for(int j=0;j<nNumkeep-Flen;j++)
           {
            tmp0="0"+tmp0;
           }
          cc=tmp0+cc;
          return cc;
}

//==============================================================================

本文地址:http://com.8s8s.com/it/it22853.htm