分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

}

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有