当前位置:早雪网网络学院编程文档其他语言 → C++Builder 自编常用函数库(1)

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

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:56:52


#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 E

[1] [2]  下一页


Tags:++,Builder,自编,常用,函数
[数据载入中...] [返回上一页] [打 印]