Delphi常见的使用方法

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值