Delphi公用函数单元.docx
《Delphi公用函数单元.docx》由会员分享,可在线阅读,更多相关《Delphi公用函数单元.docx(66页珍藏版)》请在冰豆网上搜索。
Delphi公用函数单元
unitGlobalFunction;
interface
uses
ComCtrls,Forms,Windows,Classes,SysUtils,ComObj,ActiveX,ShlObj,Messages,
Graphics,Registry,Dialogs,Controls,uProcess,uCpuUsage,StrUtils,CommCtrl,
jpeg,WinInet,ShellAPI,SHFolder,ADODB,WinSock;
{保存日志文件}
procedureYzWriteLogFile(Msg:
String);
{延时函数,单位为毫秒}
procedureYzDelayTime(MSecs:
Longint);
{判断字符串是否为数字}
functionYzStrIsNum(Str:
string):
boolean;
{判断文件是否正在使用}
functionYzIsFileInUse(fName:
string):
boolean;
{删除字符串列表中的空字符串}
procedureYzDelEmptyChar(AList:
TStringList);
{删除文件列表中的"Thumbs.db"文件}
procedureYzDelThumbsFile(AList:
TStrings);
{返回一个整数指定位数的带"0"字符串}
functionYzIntToZeroStr(Value,ALength:
Integer):
string;
{取日期年份分量}
functionYzGetYear(Date:
TDate):
Integer;
{取日期月份分量}
functionYzGetMonth(Date:
TDate):
Integer;
{取日期天数分量}
functionYzGetDay(Date:
TDate):
Integer;
{取时间小时分量}
functionYzGetHour(Time:
TTime):
Integer;
{取时间分钟分量}
functionYzGetMinute(Time:
TTime):
Integer;
{取时间秒钟分量}
functionYzGetSecond(Time:
TTime):
Integer;
{返回时间分量字符串}
functionYzGetTimeStr(ATime:
TTime;AFlag:
string):
string;
{返回日期时间字符串}
functionYzGetDateTimeStr(ATime:
TTime;ADate:
TDate):
string;
{获取计算机名称}
functionYzGetComputerName():
string;
{通过窗体子串查找窗体}
procedureYzFindSpecWindow(ASubTitle:
string);
{判断进程CPU占用率}
procedureYzJudgeCPU(ProcessName:
string;CPUUsage:
Single);
{分割字符串}
procedureYzSeparateTerms(Source:
string;Separator:
Char;Terms:
TStringList);
{切换页面控件的活动页面}
procedureYzGotoNewPage(AOwerPage:
TPageControl;ANewPage:
TTabSheet);
{设置页面控件标签的可见性}
procedureYzSetTableVisible(PageControl:
TPageControl;ShowFlag:
Boolean);
{根据产品名称获取产品编号}
functionYzGetLevelCode(AName:
string;ProductList:
TStringList):
string;
{取文件的主文件名}
functionYzGetMainFileName(AFileName:
string):
string;
{按下一个键}
procedureYzPressOneKey(AByteCode:
Byte);overload;
{按下一个指定次数的键}
procedureYzPressOneKey(AByteCode:
Byte;ATimes:
Integer);overload;
{按下二个键}
procedureYzPressTwoKeys(AFirstByteCode,ASecByteCode:
Byte);
{按下三个键}
procedureYzPressThreeKeys(AFirstByteCode,ASecByteCode,AThirdByteCode:
Byte);
{创建桌面快捷方式}
procedureYzCreateShortCut(constsPath:
string;sShortCutName:
WideString);
{删除桌面快捷方式}
procedureYzDeleteShortCut(sShortCutName:
WideString);
{通过光标位置进行鼠标左键单击}
procedureYzMouseLeftClick(X,Y:
Integer);overload;
{鼠标左键双击}
procedureYzMouseDoubleClick(X,Y:
Integer);
{通过窗口句柄进行鼠标左键单击}
procedureYzMouseLeftClick(lpClassName,lpWindowName:
PAnsiChar);overload;
{通过光标位置查找窗口句柄}
functionYzWindowFromPoint(X,Y:
Integer):
THandle;
{等待窗口在指定时间后出现}
functionYzWaitWindowExist(lpClassName,lpWindowName:
PAnsiChar;
ASecond:
Integer=0):
THandle;overload;
{通光标位置,窗口类名与标题查找窗口是否存在}
functionYzWaitWindowExist(X,Y:
Integer;AClassName,AWinName:
string;
ASecond:
Integer=0):
THandle;overload;
{等待指定窗口消失}
procedureYzWaitWindowClose(lpClassName,lpWindowName:
PAnsiChar;
ASecond:
Integer=0);
{通过窗口句柄设置文本框控件文本}
procedureYzSetEditText(lpClassName,lpWindowName:
PAnsiChar;
AText:
string);overload;
{通过光标位置设置文本框控件文本}
procedureYzSetEditText(X,Y:
Integer;AText:
string);overload;
{获取Window操作系统语言}
functionYzGetWindowsLanguageStr:
String;
{清空动态数组}
procedureYzDynArraySetZero(varA);
{动态设置屏幕分辨率}
functionYzDynamicResolution(X,Y:
WORD):
Boolean;
{检测系统屏幕分辨率}
functionYzCheckDisplayInfo(X,Y:
Integer):
Boolean;
type
TFontedControl=class(TControl)
public
propertyFont;
end;
TFontMapping=record
SWidth:
Integer;
SHeight:
Integer;
FName:
string;
FSize:
Integer;
end;
procedureYzFixForm(AForm:
TForm);
procedureYzSetFontMapping;
{---------------------------------------------------
以下是关于获取系统软件卸载的信息的类型声明和函数
----------------------------------------------------}
type
TUninstallInfo=arrayofrecord
RegProgramName:
string;
ProgramName:
string;
UninstallPath:
string;
Publisher:
string;
PublisherURL:
string;
Version:
string;
HelpLink:
string;
UpdateInfoURL:
string;
RegCompany:
string;
RegOwner:
string;
end;
{GetUninstallInfo返回系统软件卸载的信息}
functionYzGetUninstallInfo:
TUninstallInfo;
{检测Java安装信息}
functionYzCheckJavaInfo(AUninstallInfo:
TUninstallInfo;CheckJava6:
Boolean=False):
Boolean;
{窗口自适应屏幕大小}
procedureYzAdjustForm(Form:
TForm;constOrgWidth,OrgHeight:
integer);
{设置窗口为当前窗体}
procedureYzBringMyAppToFront(AppHandle:
THandle);
{获取文件夹大小}
functionYzGetDirSize(Dir:
string;SubDir:
Boolean):
LongInt;
{获取文件夹文件数量}
functionYzGetDirFiles(Dir:
string;SubDir:
Boolean):
LongInt;
{获取文件大小(KB)}
functionYzGetFileSize(constFileName:
String):
LongInt;
{获取文件大小(字节)}
functionYzGetFileSize_Byte(constFileName:
String):
LongInt;
{算术舍入法的四舍五入取整函数}
functionYzRoundEx(constValue:
Real):
LongInt;
{弹出选择目录对话框}
functionYzSelectDir(constiMode:
integer;constsInfo:
string):
string;
{获取指定路径下文件夹的个数}
procedureYzListFolders(constPath:
String;constShowPath:
Boolean;varList:
TStrings);
{禁用窗器控件的所有子控件}
procedureYzSetSubCtlState(AOwer:
TWinControl;AState:
Boolean);
{模拟键盘按键操作(处理字节码)}
procedureYzFKeyent(byteCard:
byte);overload;
{模拟键盘按键操作(处理字符串}
procedureYzFKeyent(strCard:
string);overload;
{锁定窗口位置}
procedureYzLockWindow(ClassName,WinName:
PChar;poX,poY:
Integer);
{注册一个DLL形式或OCX形式的OLE/COM控件
参数strOleFileName为一个DLL或OCX文件名,
参数OleAction表示注册操作类型,1表示注册,0表示卸载
返回值True表示操作执行成功,False表示操作执行失败
}
functionYzRegisterOleFile(strOleFileName:
STRING;OleAction:
Byte):
BOOLEAN;
functionYzListViewColumnCount(mHandle:
THandle):
Integer;
functionYzGetListViewText(mHandle:
THandle;mStrings:
TStrings):
Boolean;
{删除目录树}
functionYzDeleteDirectoryTree(Path:
string):
boolean;
{Jpg格式转换为bmp格式}
functionJpgToBmp(Jpg:
TJpegImage):
TBitmap;
{设置程序自启动函数}
functionYzSetAutoRun(AFilePath:
string;AFlag:
Boolean):
boolean;
{检测URL地址是否有效}
functionYzCheckUrl(url:
string):
Boolean;
{获取程序可执行文件名}
functionYzGetExeFName:
string;
{目录浏览对话框函数}
functionYzBrowseFolder(AOwer:
TWinControl;ATitle:
string):
string;
{重启计算机}
functionYzShutDownSystem(AFlag:
Integer):
BOOL;
{程序运行后删除自身}
procedureYzDeleteSelf;
{程序重启}
procedureYzAppRestart;
{压缩Access数据库}
functionYzCompactAccessDB(constAFileName,APassWord:
string):
Boolean;
{标题:
获取其他进程中TreeView的文本}
functionYzTreeNodeGetNext(mHandle:
THandle;mTreeItem:
HTreeItem):
HTreeItem;
functionYzTreeNodeGetLevel(mHandle:
THandle;mTreeItem:
HTreeItem):
Integer;
functionYzGetTreeViewText(mHandle:
THandle;mStrings:
TStrings):
Boolean;
{获取本地ApplicationData目录路径}
functionYzLocalAppDataPath:
string;
{获取Windows当前登录的用户名}
functionYzGetWindwosUserName:
String;
{枚举托盘图标}
functionYzEnumTrayNotifyWnd(AFindList:
TStringList;ADestStr:
string):
BOOL;
{获取SQLServer用户数据库列表}
procedureYzGetSQLServerDBList(ADBHostIP,ALoginPwd:
string;ADBList:
TStringList);
{读取据库中所有的表}
procedureYzGetTableList(ConncetStr:
string;ATableList:
TStringList);
{将域名解释成IP地址}
functionYzDomainToIP(HostName:
string):
string;
{等待进程结束}
procedureYzWaitProcessExit(AProcessName:
string);
{移去系统托盘失效图标}
procedureYzRemoveDeadIcons();
{转移程序占用内存至虚拟内存}
procedureYzClearMemory;
{检测允许试用的天数是否已到期}
functionYzCheckTrialDays(AllowDays:
Integer):
Boolean;
{指定长度的随机小写字符串函数}
functionYzRandomStr(aLength:
Longint):
string;
var
FontMapping:
arrayofTFontMapping;
implementation
uses
uMain;
{保存日志文件}
procedureYzWriteLogFile(Msg:
String);
var
FileStream:
TFileStream;
LogFile:
String;
begin
try
{每天一个日志文件}
Msg:
='['+DateTimeToStr(Now)+']'+Msg;
LogFile:
=ExtractFilePath(Application.ExeName)+'/Logs/'+DateToStr(Now)+'.log';
ifnotDirectoryExists(ExtractFilePath(LogFile))then
CreateDir(ExtractFilePath(LogFile));
ifFileExists(LogFile)then
FileStream:
=TFileStream.Create(LogFile,fmOpenWriteorfmShareDenyNone)
else
FileStream:
=TFileStream.Create(LogFile,fmCreateorfmShareDenyNone);
FileStream.Position:
=FileStream.Size;
Msg:
=Msg+#13#10;
FileStream.Write(PChar(Msg)^,Length(Msg));
FileStream.Free;
except
end;
end;
{延时函数,单位为毫秒}
procedureYZDelayTime(MSecs:
Longint);
var
FirstTickCount,Now:
Longint;
begin
FirstTickCount:
=GetTickCount();
repeat
Application.ProcessMessages;
Now:
=GetTickCount();
until(Now-FirstTickCount>=MSecs)or(Nowend;
{判断字符串是否为数字}
functionYzStrIsNum(Str:
string):
boolean;
var
I:
integer;
begin
ifStr=''then
begin
Result:
=False;
Exit;
end;
forI:
=1tolength(str)do
ifnot(Str[I]in['0'..'9'])then
begin
Result:
=False;
Exit;
end;
Result:
=True;
end;
{判断文件是否正在使用}
functionYzIsFileInUse(fName:
string):
boolean;
var
HFileRes:
HFILE;
begin
Result:
=false;
ifnotFileExists(fName)thenexit;
HFileRes:
=CreateFile(pchar(fName),GENERIC_READorGENERIC_WRITE,0,nil,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
Result:
=(HFileRes=INVALID_HANDLE_VALUE);
ifnotResultthenCloseHandle(HFileRes);
end;
{删除字符串列表中的空字符串}
procedureYzDelEmptyChar(AList:
TStringList);
var
I:
Integer;
TmpList:
TStringList;
begin
TmpList:
=TStringList.Create;
forI:
=0toAList.Count-1do
ifAList.Strings[I]<>''thenTmpList.Add(AList.Strings[I]);
AList.Clear;
AList.Text:
=TmpList.Text;
TmpList.Free;
end;
{删除文件列表中的"Thumbs.db"文件}
procedureYzDelThumbsFile(AList:
TStrings);
var
I:
Integer;
TmpList:
TStringList;
begin
TmpList:
=TStringList.Create;
forI:
=0toAList.Count-1do
ifExtractFileName(AList.Strings[I])<>'Thumbs.db'then
TmpList.Add(AList.Strings[I]);
AList.Clear;
AList.Text:
=TmpList.Text;
TmpList.Free;
end;
{-------------------------------------------------------------
功能:
返回一个整数指定位数的带"0"字符串
参数:
Value:
要转换的整数ALength:
字符串长度
返回值:
string
--------------------------------------------------------------}
functionYzIntToZeroStr(Value,ALength:
Integer):
string;
var
I,ACount:
Integer;
begin
Result:
=