常用
常用
编译警告 | {$WARN UNIT_PLATFORM OFF} {$WARN SYMBOL_PLATFORM OFF} 如何同步 |
调试 | if DebugHook<>1 then begin //Exe环境下 end else begin //开发环境下 xxxxx end; |
错误处理 | try try
finally
end; except on e: exception do begin f_ShowTS(E.Message); Windows.PostMessage(Self.Handle, WM_CLOSE, 0, 0); //发消息关闭自己 application.Terminate; exit; end; end; |
窗体调用 | if not Assigned(frmSFZ) then frmSFZ := TfrmSFZ.Create(Application); try frmSFZ.ShowModal; finally frmSFZ.Free; frmSFZ := nil; end; |
文件/路径 | gPath := f_PathStr(ExtractFilePath(ParamStr(0))); //程序路径 |
if FileExists('路径') if DirectoryExists('路径') 文件和路径是否存在 ExtractFilePath(ParamStr(0)) 当前EXE文件所在的目录 ExtractFileDrive :返回完整文件名中的驱动器,如"C:" ExtractFilePath:返回完整文件名中的路径,最后带"/",如"C:\test\" ExtractFileDir:返回完整文件名中的路径,最后不带"/" ,如"C:\test" ExtractFileName:返回完整文件名中的文件名称 (带扩展名),如"mytest.doc" ExtractFileExt 返回完整文件名中的文件扩展名(带.),如".doc" | |
if FileExists(mmFName) then DeleteFile(mmFName); if FileExists(mmFName) then raise Exception.Create('无法删除'+mmFName); if not CopyFile(pChar(mmSName),pChar(mmFName),true) then raise Exception.Create('无法拷贝'+mmSName+' 到 '+mmFName); | |
if Not DirectoryExists(gMyApp.Path_ServerDB) then ForceDirectories(gMyApp.Path_ServerDB); if Not DirectoryExists(gMyApp.Path_ServerDB) then raise Exception.Create('无法创建目录'+gMyApp.Path_ServerDB); | |
字符串 | mmStr := StringReplace(mmStr,'<BR>','<br>',[rfReplaceAll,rfIgnoreCase]); //大小写问题 |
其他 | Application.ProcessMessages; |
StringList.String[0] | |
mmQuery : TUniQuery; try mmQuery := TUniQuery.Create(nil); try
finally mmQuery.Close; mmQuery.Free; end; except on e: exception do begin end; end; | |
自定义消息
定义 | TMainMsg = record MHandle : THandle; //接收句柄 GatherVal : DWord; //采集返回值 end;
gMainMsg : TMainMsg; |
初始化 | mmGPID := '{4F37F311-5EFB-4ACB-902F-BFC3222F5904}'; //Ctrl+Shift+G gMainMsg.MHandle := self.Handle; //自定义系统唯一消息 gMainMsg.GatherVal := RegisterWindowMessage(PChar(mmGPID+'gMainMsg.GatherVal_201603291409')); |
消息发送 | new(mmpGatherVal); //结构数据 mmpGatherVal^.Addr := strtoint(mmATinfo.Addr); // ..... SendMessage(gMainMsg.MHandle, gMainMsg.GatherVal, 0, Integer(mmpGatherVal)); Dispose(mmpGatherVal);
//字符串\数字 |
消息处理 | protected procedure WndProc(var Message:TMessage); override; //处理消息 |
procedure TfrmMain.WndProc(var Message: TMessage); var mmpGatherVal : pGatherVal; begin try with Message do begin if Msg=gMainMsg.GatherVal then begin mmpGatherVal := Pointer(Message.LParam); //取数据 f_CodeSite(Format('Addr=%s CH=%d Str=%s Val=%g', [fAdd0Num(mmpGatherVal^.Addr,4), mmpGatherVal^.CH, mmpGatherVal^.ValStr, mmpGatherVal^.Val])); end; end; except on e: exception do begin end; end; inherited WndProc(Message); end; | |
数据库
MySQL
mmConn : TUniConnection;
mmQuery : TUniQuery;
SQLServer
线程数据库操作初始化 | 错误= OLE DB error occured. Code 800401F0h. CoInitialize has not been called. 引入Activex单元 initialization CoInitializeEx(NIL, COINIT_MULTITHREADED); finalization CoUninitialize; |
Access-MDB
引用ComObj, adoint; 定义 mmAdoCat, mmAdoConn, mmRS : Variant;
创建数据库
mmPWD := '';
mmDBFile := gMyApp.Path + 'Test.MDB';
mmCreatMDBString:= 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + mmDBFile + ';' +
'Jet OLEDB:Database Password=' + mmPWD;
if FileExists(mmDBFile) then begin
//Showmessage(mmDBFile +'存在');
DeleteFile(mmDBFile);
//Showmessage('删除' + mmDBFile );
end;
mmAdoCat:=CreateOleObject('ADOX.Catalog');//创建数据库
mmAdoCat.Create(mmCreatMDBString);
连接
if mmPWD='' then mmSQL:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+mmDBFile+';Persist Security Info=False'
else mmSQL:='Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password='+mmPWD+';Data Source='+mmDBFile+';Persist Security Info=True';
mmAdoConn:=CreateOleObject('ADODB.Connection');
mmAdoConn.open(mmSQL);
建表
mmSQL:='Create Table ' + 'Biao1' + ' (MyKey int Identity Not Null Primary Key';
mmSQL:=mmSQL+','+'aSmallInt SmallInt Not Null Default 0';
mmSQL:=mmSQL+','+'aInt Int Not Null Default 0';
mmSQL:=mmSQL+','+'aBit Bit Not Null Default false';
mmSQL:=mmSQL+','+'aChar nvarchar(255) Not Null Default ""';
mmSQL:=mmSQL+','+'aMoney Money Not Null Default 0';
mmSQL:=mmSQL+','+'aFloat Float Not Null Default 0';
mmSQL:=mmSQL+','+'aTime dateTime Not Null Default Now()';
mmSQL:=mmSQL+','+'aText text Not Null Default ""';
mmSQL:=mmSQL+')';
mmAdoConn.Execute(mmSQL);
访问
mmRS := CreateOleObject('ADODB.RecordSet');
mmSQL:= 'Select * From Biao1';
mmRS.Open(mmSQL,mmAdoConn,adOpenStatic,AdLockOptimistic,adCmdText);
while not mmRS.Eof do begin
//memInfo.Lines.Add('Biao1: '+ mmRS.Fields['wFile'].value);
mmRS.MoveNext;
end;
?
线程
定义 | |
Next控件
单元
标准 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; |
常用 | CodeSiteLogging, StrUtils, DateUtils, |
Unidac | DB, MemDS, DBAccess, Uni, MySQLUniProvider, UniProvider, SQLServerUniProvider, |
ADO | ComObj, adoint; 定义 mmAdoCat, mmAdoConn, mmRS : Variant; |
Next | NxScrollControl, NxInspector, NxPropertyItemClasses, NxCustomGridControl, NxCustomGrid, NxGrid, NxClasses, NxColumnClasses, NxColumns, NxEdit, NxPropertyItems, |
代码
开机启动 | {设置程序开机自启动 1.名称 2.Exe全路径 3.是否仅开机运行一次} function fSetAutoStart(aName,aPath:String; IsOnlyOne:Boolean=false):String; var mmReg : TRegistry; //Use Registry; 首先定义一个TRegistry类型的变量Reg mmKeyStr : String; begin result := ''; try mmReg := TRegistry.Create; //设置开机启动; 创建一个新键 try mmReg.RootKey:=HKEY_LOCAL_MACHINE; //将根键设置为HKEY_LOCAL_MACHINE
if IsOnlyOne then mmKeyStr := 'SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' //仅运行一次 else mmKeyStr := 'SOFTWARE\Microsoft\windows\CurrentVersion\Run'; mmReg.OpenKey(mmKeyStr, true); //打开一个键 mmReg.WriteString(aName, aPath); //在Reg这个键中写入数据名称和数据数值 finally mmReg.CloseKey; mmReg.Free; end; except on E:exception do result:=E.Message; end; end; |
禁止 | {禁止程序开机自启动 1.名称 2.Exe全路径 3.是否仅开机运行一次} function fDelAutoStart(aName:String; IsOnlyOne:Boolean=false):String; var mmReg : TRegistry; //Use Registry; 首先定义一个TRegistry类型的变量Reg mmKeyStr : String; begin result := ''; try mmReg := TRegistry.Create; //设置开机启动; 创建一个新键 try mmReg.RootKey:=HKEY_LOCAL_MACHINE; //将根键设置为HKEY_LOCAL_MACHINE
if IsOnlyOne then mmKeyStr := 'SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' //仅运行一次 else mmKeyStr := 'SOFTWARE\Microsoft\windows\CurrentVersion\Run'; mmReg.OpenKey(mmKeyStr, true); //打开一个键
if Not mmReg.DeleteValue(aName) then raise Exception.Create('删除<'+aName+'>失败'); finally mmReg.CloseKey; mmReg.Free; end; except on E:exception do result:=E.Message; end; end; |
?