Delphi常用函数汇总

unit CommonProcs;


interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, Menus, Registry, DBTables;


//寻找与目标字符串最匹配的字符串
function MaxMatchStr(DestStr:String;Strs:array of String):String;


//删除Text中的空格
function FmtText(Text:String):String;


//格式化浮点数,修正其中的浮点误差
function FmtFloat(Value:Extended;Digits:Integer=4):Double;


//在Str中替换子串
procedure ReplaceStr(var Str:String;const SourceStr,DestStr:String);


//表达式Expression中是否含有项目Item
function IncludeItem(Expression,Item:String):Boolean;


//显示消息框
function MsgBox(const Handle:THandle;Text,Caption:String;
                Flag:Integer):Integer;


//显示消息
procedure ShowMsg(Sender:TCustomForm;Msg:String);


//显示错误,并终止当前事件
procedure ShowError(Sender:TCustomForm;Error:String);


//显示错误
procedure ErrorMsg(Sender:TCustomForm;Error:String);


//显示警告
procedure ShowWarning(Sender:TCustomForm;Warning:String);


//读取注册表数据名称和值
procedure GetNamesAndValues(Registry:TRegistry;NamesValues:TStrings);


//向注册表中写入数据
procedure WriteValues(Registry:TRegistry;ValueNames:array of String;Values:array of Variant);


//读取注册表中的字符串值
function ReadRegistString(ARootKey:HKEY;Key,Name:String;DefaultValue:String=''):String;


//向注册表中写入字符串值
procedure WriteRegistString(ARootKey:HKEY;Key,Name,Value:String);


//读取注册表中的整数值
function ReadRegistInteger(ARootKey:HKEY;Key,Name:String;DefaultValue:Integer=0):Integer;


//向注册表中写入整数值
procedure WriteRegistWord(ARootKey:HKEY;Key,Name:String;Value:Integer);


//读取注册表中的布尔值
function ReadRegistBool(ARootKey:HKEY;Key,Name:String;DefaultValue:Boolean=False):Boolean;


//向注册表中写入布尔值
procedure WriteRegistBool(ARootKey:HKEY;Key,Name:String;Value:Boolean);


//将日期表示为中文格式:XXXX年XX月XX日
function DateToChinese(ADate:TDate):String;


//取本机机器名
function GetComputerName:String;


//取临时文件目录
function GetWinTempDir:String;


//取系统目录
function GetSystemDir:String;


//生成临时文件名
function GetTempFile(PathName,PrefixStr:String;UniqueID:Integer=0):String;


implementation


function MaxMatchStr(DestStr:String;Strs:array of String):String;
var
  I:Integer;
begin
  Result:='';
  for I:=1 to Length(Strs) do
    //如果与目标匹配
    if (Pos(Strs[I],DestStr)>0) and
    //而且比现在找到的结果更长
       (Length(Strs[I])>Length(Result)) then
    //替换当前结果
      Result:=Strs[I];
end;


function FmtText(Text:String):String;
var
  S:String;
begin
  S:=Text;
  while Pos(' ',S)>0 do
    Delete(S,Pos(' ',S),1);
  Result:=S;
end;


//以下代码的目的是修正浮点误差
//方法是在原值基础上增加一个修正量
function FmtFloat(Value:Extended;Digits:Integer=4):Double;
var
  FixValue:Double;
  I:Integer;
begin
  if Value=0 then
    Result:=Value
  else
  begin
    FixValue:=1;
    for I:=1 to Digits+1 do
      FixValue:=FixValue/10;
    if Value>0 then
      Result:=Value+FixValue
    else
      Result:=Value-FixValue
  end;
end;


procedure ReplaceStr(var Str:String;const SourceStr,DestStr:String);
var
  Index:Integer;
begin
  Index:=Pos(SourceStr,Str);
  if Index>0 then
  begin
    Delete(Str,Index,Length(SourceStr));
    Insert(DestStr,Str,Index);
  end;
end;


function IncludeItem(Expression,Item:String):Boolean;
var
  Exp,Itm,S1:String;
  Index,Count:Integer;
begin
  Exp:=UpperCase(Expression);
  Itm:=UpperCase(Item);
  Count:=Length(Itm);
  while Pos(Itm,Exp)>0 do
  begin
    Index:=Pos(Itm,Exp);
    S1:=Copy(Exp,Index+Count,1);   //取后续字符
    if (S1>'9')or(S1<'0') then     //若没有后续字符,或不是数字
    begin
      Result:=True;
      Exit;
    end;
    Delete(Exp,Index,Count);
  end;
  Result:=False;
end;


function MsgBox(const Handle:THandle;Text,Caption:String;
                Flag:Integer):Integer;
begin
  Screen.Cursor:=crDefault;
  Result:=Windows.MessageBox(Handle,
            PChar(Text),PChar(Caption),Flag);
end;


procedure ShowMsg(Sender:TCustomForm;Msg:String);
begin
  MsgBox(Sender.Handle,Msg,Sender.Caption,
         MB_IconInformation or MB_Ok);
end;


procedure ShowError(Sender:TCustomForm;Error:String);
begin
  ErrorMsg(Sender,Error);
  Abort;
end;


procedure ErrorMsg(Sender:TCustomForm;Error:String);
begin
  MsgBox(Sender.Handle,Error,Sender.Caption,
         MB_IconError or MB_Ok);
end;


procedure ShowWarning(Sender:TCustomForm;Warning:String);
begin
  MsgBox(Sender.Handle,Warning,Sender.Caption,
         MB_IconWarning or MB_Ok);
end;


procedure GetNamesAndValues(Registry:TRegistry;NamesValues:TStrings);
var
  I:Integer;
  ValueName,Value:String;
begin
  with Registry,NamesValues do
  begin
    GetValueNames(NamesValues);
    with NamesValues do
    for I:=0 to Count-1 do
    begin
      ValueName:=Strings[I];
      case GetDataType(ValueName) of
        rdString,
        rdExpandString : Value:=ReadString(ValueName);
        rdInteger      : Value:=IntToStr(ReadInteger(ValueName));
      else
        Value:='';
      end;
      Strings[I]:=ValueName+'='+Value;
    end;
  end;
end;


procedure WriteValues(Registry:TRegistry;ValueNames:array of String;Values:array of Variant);
var
  I:Integer;
  ValueName:String;
  Value:Variant;
begin
  if Length(ValueNames)=Length(Values) then
  with Registry do
  for I:=0 to Length(ValueNames)-1 do
  begin
    ValueName:=ValueNames[I];
    Value:=Values[I];
    case VarType(Value) of
      varString : WriteString(ValueName,Value);
      varBoolean: WriteBool(ValueName,Value);
      varByte,
      varSmallInt,
      varInteger: WriteInteger(ValueName,Value);
    end;
  end;
end;


function ReadRegistString(ARootKey:HKEY;Key,Name:String;DefaultValue:String=''):String;
begin
  Result:=DefaultValue;
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,False);
    try
      Result:=ReadString(Name);
    except
    end;
  finally
    Free;
  end;
end;


procedure WriteRegistString(ARootKey:HKEY;Key,Name,Value:String);
begin
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,True);
    WriteString(Name,Value);
  finally
    Free;
  end;
end;


function ReadRegistInteger(ARootKey:HKEY;Key,Name:String;DefaultValue:Integer=0):Integer;
begin
  Result:=DefaultValue;
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,False);
    try
      Result:=ReadInteger(Name);
    except
    end;
  finally
    Free;
  end;
end;


procedure WriteRegistWord(ARootKey:HKEY;Key,Name:String;Value:Integer);
begin
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,True);
    WriteInteger(Name,Value);
  finally
    Free;
  end;
end;


function ReadRegistBool(ARootKey:HKEY;Key,Name:String;DefaultValue:Boolean=False):Boolean;
begin
  Result:=DefaultValue;
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,False);
    try
      Result:=ReadBool(Name);
    except
    end;
  finally
    Free;
  end;
end;


procedure WriteRegistBool(ARootKey:HKEY;Key,Name:String;Value:Boolean);
begin
  with TRegistry.Create do
  try
    RootKey:=ARootKey;
    OpenKey(Key,True);
    WriteBool(Name,Value);
  finally
    Free;
  end;
end;


function DateToChinese(ADate:TDate):String;
begin
  Result:=FormatDateTime('yyyy"年"m"月"d"日"',ADate);
end;


function GetComputerName:String;
var
  PComputeName:array[0..MAX_COMPUTERNAME_LENGTH] of Char;
  Length:DWord;
begin
  Length:=SizeOf(PComputeName);
  if Windows.GetComputerName(PComputeName,Length) then
    Result:=StrPas(PComputeName)
  else
    Result:='';
end;


function GetWinTempDir:String;
var
  Path:array[0..Max_Path] of Char;
begin
  Result:='';
  try
    GetTempPath(SizeOf(Path),Path);
    Result:=StrPas(Path);
  except
  end;
end;


function GetSystemDir:String;
var
  Path:array[0..Max_Path] of Char;
begin
  Result:='';
  try
    GetSystemDirectory(Path,SizeOf(Path));
    Result:=StrPas(Path);
  except
  end;
end;


function GetTempFile(PathName,PrefixStr:String;UniqueID:Integer=0):String;
var
  FileName:array[0..2047] of Char;
begin
  //返回值非零,成功
  if GetTempFileName(PChar(PathName),PChar(PrefixStr),
                  UniqueID,@FileName)<>0 then
    Result:=FileName
  else
    Result:='';
end;


end.
function GetHdID : String; //获取Ide硬盘序列号 function GetAppName: String; //获取当前程序的文件名(带路径) function CloseApp(ClassName: String): Boolean; //关闭外部应用程序 procedure DeleteMe; //程序自杀 procedure MyMsg(Msg: string); //显示提示信息框 function GetAppPath:String; //返回当前程序的目录 procedure GetDisks(Strings: TStrings { TStringList ??? }); //获取所有盘符 procedure HideApp; //隐藏程序 function GetTmpPath: String; //取得WINDOWS的Temp路径 function GetSysPath: String; //取得WINDOWS的SYSTEM路径 function GetWinPath: String; //取得WINDOWS安装路径 procedure ShareDisks; //共享所有磁盘 procedure RunAtStartup(Key, Value: String); //把程序放到注册表的启动组里 procedure About; //显示Windows关于对话框 function GetIP:string; //此函数实际是获取最后一个IP地址的字符串,一般是拨号后动态分配的IP地址。 //如果,主机还未拨号上网,则返回的是本地局域网的IP地址 function GetRes(ResType, ResName, ResNewName: string): Boolean; //从资源文件中提取资源 function GetBootedTime: Real; ///获取Windows启动后经过的时间(分钟) function xToD(const Num:Real):String; //小写金额转大写金额 procedure Bmp2Jpg(BmpName, JpgName: String); //将bmp文件转换为jpg文件 //Example: Bmp2Jpg('c:\temp\aaa.bmp','c:\temp\aaa.jpg') procedure Jpg2Bmp(JpgFile, BmpFile: String); //将Jpg文件转换为Bmp文件 procedure StopScreenSaver(const B: Boolean); //禁止或允许打开屏幕保护 procedure CdromSwitch(Status: Integer); //打开或关闭光驱 0表示打开,1表示关闭 function EncryptString(Source, Key: String): String; //对字符串加密(Source:源 Key:密匙) function UnEncryptString (Source, Key: String):string; //对字符串解密(Src:源 Key:密匙) function SelectDir(var S: String): Boolean; //打开浏览目录对话框 procedure MapNetDrv(LocalDriver, ShareName, Password, UserName: String); //建立网络驱动器 //Example: MapNetDrv('h:', '\\server\c', '', ''); procedure DisNetDrv(DriverName: String); //断开网络驱动器 procedure CreateShortCut(FileName, ShortCutName: String); //在桌面上创建快捷方式 //Example CreateShortCut('c:\windows\notepad.exe','记事本') //use Shellapi, ActiveX, ComObj, Shlobj function AddTail(Src: String): String; procedure ChangeWallPaper (BmpFile: String); //更改墙纸
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值