📄 unitfrmmain.pas
字号:
unit unitFrmMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, FileCtrl;
type
TForm1 = class(TForm)
Button1: TButton;
DriveComboBox1: TDriveComboBox;
DirectoryListBox1: TDirectoryListBox;
FileListBox1: TFileListBox;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
function FileLocked(Fn: string): Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
if not FileLocked(filelistbox1.FileName) then
Showmessage('文件已被锁定,无法打开');
end;
function Tform1.FileLocked(Fn: string): Boolean;
var
I: Integer;
Struct: TOfStruct;
Style: Cardinal;
Hdl: Hfile;
Drive: string;
begin
Style := OF_Share_Exclusive; //独占方式打开,其他程序不能再打开该文件
Drive := UpperCase(Fn[1]); //取出盘符
Struct.fFixedDisk := Ord(Drive <> 'A'); //判断是否是硬盘
for I := 1 to Length(Fn) do
Struct.szPathName[I - 1] := Fn[I];
Struct.szPathName[I] := Chr(0); //填充文件名
Hdl := OpenFile(Pchar(Fn), Struct, Style);
if Hdl = HFILE_ERROR then
begin
Result := True; //锁定文件
Showmessage(SysErrorMessage(GetLastError)); //显示错误原因
end
else
Result := False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -