您的位置:首页 > 编程语言 > Delphi

Delphi常见的使用方法

2005-08-12 08:10 471 查看
1.启动程序只允许一个实例
var
Mutex: THandle;
begin
Mutex := CreateMutex(nil, True, '80A8A422-D84F-4FEA-953F-B08148F60ABD');
if GetLastError <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.CreateForm(TFmMonitor, FmMonitor);
Application.Run;
end else
begin
ReleaseMutex(Mutex);
end;
2.写日志
procedure WriteLog(Str: string);
var
F: TextFile;
StrFile: string;
begin
StrFile := AppPath + 'DataEx.log';
{I-}
AssignFile(F, StrFile);
if FileExists(StrFile) then
begin
if FileSizeByName(StrFile) > 1024 * 50 then
ReWrite(F);
end else
ReWrite(F);
Append(F);
Writeln(F, Str);
writeln(F, '');
CloseFile(F);
{I+}
end;
3.启动程序后最小化
procedure TFmMonitor.FormShow(Sender: TObject);
begin
PostMessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
end;
4.多线程同步
线程调用:
hMutex := CreateMutex(nil, false, nil);
FtpDoThread := TFtpThread.Create(True);
FtpDoThread.Resume;

线程定义:
type
TFtpThread = class(TThread)
private
protected
procedure Execute; override;
destructor Destroy; override;
end;
var
FtpDoThread: TFtpThread;
hMutex: THandle = 0;
implementation

destructor TFtpThread.Destroy;
begin
FtpDoThread := nil;
inherited;
end;
procedure TFtpThread.Execute;
begin
FreeOnTerminate := True;
if WaitForSingleObject(hMutex, INFINITE) = WAIT_OBJECT_0 then
begin
.........
end;
ReleaseMutex(hMutex);
Terminate;
end;

5.创建目录
procedure TFmMonitor.TestCreateDir(InStr: string);
begin
if not DirectoryExists(AppPath + InStr) then
begin
{$I-}
CreateDir(AppPath + InStr);
{$I+}
end;
end;
6.获得一个目录的文件(不含子目录)
调用:GetdirFiles(FmMonitor.AppPath + 'Upload/*.*');
function TFtpThread.GetdirFiles(InDirStr: string): TStrings;
var
iFindResult: integer;
SearchRec: TSearchRec;
FileList: TStrings;
begin
FileList := TStringList.Create;
iFindResult := FindFirst(InDirStr, faAnyFile, SearchRec);
while iFindResult = 0 do
begin
if SearchRec.Attr <> faDirectory then
FileList.Add(SearchRec.Name);
iFindResult := FindNext(SearchRec);
end;
FindClose(SearchRec);
Result := FileList;
end;
7.替换及重复函数
AnsiReplaceStr
DupeString
8.防止刷新屏幕时花屏
LockWindowUpdate(getdesktopwindow);
LockWindowUpdate(Handle);
........
LockWindowUpdate(0);
9.执行外部程序
ShellExecute(handle, 'open', pchar(ExtractFilePath(Application.ExeName)
+ 'readme.chm'), nil, nil, SW_SHOWNORMAL)
10.TApplicationEvent组件的ShotCut用法
procedure TDemoForm.AppPressKeyShortCut(var Msg: TWMKey;
var Handled: Boolean);
begin
Case Msg.CharCode of
VK_F7:
begin
end;
end;
end;

11.隐藏Mouse
ShowCursor(False);
12.只能大写字母
KeyPress(Sender: TObject; var Key: Char);
begin
if Ord(Key) in [97..122] then
Key := Chr(ord(Key) - 32);
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: