Delphi常見的使用方法

來源:互聯網
上載者:User

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.