O
omvm
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ShellApi;
type
TForm1 = class(TForm)
ListView1: TListView;
ListView2: TListView;
procedure ListView1Edited(Sender: TObject; Item: TListItem;
var S: string);
procedure ListView1GetImageIndex(Sender: TObject; Item: TListItem);
procedure ListView2Edited(Sender: TObject; Item: TListItem;
var S: string);
procedure ListView2GetImageIndex(Sender: TObject; Item: TListItem);
private
Is2File: Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function Rename(const OldName, NewName: string): integer;
var
lpFileOp: TSHFileOpStruct;
begin
with lpFileOp do
begin
Wnd := Application.Handle;
wFunc := FO_RENAME;
pFrom := PChar(OldName + #0#0);
pTo := PChar(NewName + #0#0);
fFlags := FOF_ALLOWUNDO;
hNameMappings := nil;
lpszProgressTitle := '';
fAnyOperationsAborted := False;
end;
Result := SHFileOperation(lpFileOp);
end;
procedure TForm1.ListView1Edited(Sender: TObject; Item: TListItem;
var S: string);
var
OldFile, NewFile: string;
begin
OldFile := ExtractFilePath(Application.ExeName) + ListView1.Selected.Caption;
NewFile := ExtractFilePath(Application.ExeName) + S;
if FileExists(NewFile) then
begin
ShowMessage('文件存在,请重命名');
Is2File := True;
end else
begin
Rename(OldFile, NewFile);
end;
end;
procedure TForm1.ListView1GetImageIndex(Sender: TObject; Item: TListItem);
begin
if Is2File then
begin
Is2File := False;
ListView1.Selected.EditCaption;
end;
end;
procedure TForm1.ListView2Edited(Sender: TObject; Item: TListItem;
var S: string);
var
OldFile, NewFile: string;
begin
OldFile := ExtractFilePath(Application.ExeName) + ListView2.Selected.Caption;
NewFile := ExtractFilePath(Application.ExeName) + S;
if Rename(OldFile, NewFile) = 0 then
begin
ShowMessage('重命名成功');
end else
begin
Is2File := True;
end;
end;
procedure TForm1.ListView2GetImageIndex(Sender: TObject; Item: TListItem);
begin
if Is2File then
begin
Is2File := False;
ListView2.Selected.EditCaption;
end;
end;
end.
object Form1: TForm1
Left = 192
Top = 133
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object ListView1: TListView
Left = 8
Top = 88
Width = 250
Height = 150
Columns = <
item
Width = 100
end>
Items.Data = {
3A0000000200000000000000FFFFFFFFFFFFFFFF000000000000000008746573
742E74787400000000FFFFFFFFFFFFFFFF000000000000000000}
TabOrder = 0
ViewStyle = vsReport
OnEdited = ListView1Edited
OnGetImageIndex = ListView1GetImageIndex
end
object ListView2: TListView
Left = 280
Top = 88
Width = 250
Height = 150
Columns = <
item
Width = 100
end>
Items.Data = {
3A0000000200000000000000FFFFFFFFFFFFFFFF000000000000000008746573
742E74787400000000FFFFFFFFFFFFFFFF000000000000000000}
TabOrder = 1
ViewStyle = vsReport
OnEdited = ListView2Edited
OnGetImageIndex = ListView2GetImageIndex
end
end
在程序目录下建两个文件:test.txt和test1.txt。
运行一下,将ListView1和ListView2中的test.txt都改为test1.txt。大家看一下结果,为什么
不一样。原因是什么?
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ShellApi;
type
TForm1 = class(TForm)
ListView1: TListView;
ListView2: TListView;
procedure ListView1Edited(Sender: TObject; Item: TListItem;
var S: string);
procedure ListView1GetImageIndex(Sender: TObject; Item: TListItem);
procedure ListView2Edited(Sender: TObject; Item: TListItem;
var S: string);
procedure ListView2GetImageIndex(Sender: TObject; Item: TListItem);
private
Is2File: Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function Rename(const OldName, NewName: string): integer;
var
lpFileOp: TSHFileOpStruct;
begin
with lpFileOp do
begin
Wnd := Application.Handle;
wFunc := FO_RENAME;
pFrom := PChar(OldName + #0#0);
pTo := PChar(NewName + #0#0);
fFlags := FOF_ALLOWUNDO;
hNameMappings := nil;
lpszProgressTitle := '';
fAnyOperationsAborted := False;
end;
Result := SHFileOperation(lpFileOp);
end;
procedure TForm1.ListView1Edited(Sender: TObject; Item: TListItem;
var S: string);
var
OldFile, NewFile: string;
begin
OldFile := ExtractFilePath(Application.ExeName) + ListView1.Selected.Caption;
NewFile := ExtractFilePath(Application.ExeName) + S;
if FileExists(NewFile) then
begin
ShowMessage('文件存在,请重命名');
Is2File := True;
end else
begin
Rename(OldFile, NewFile);
end;
end;
procedure TForm1.ListView1GetImageIndex(Sender: TObject; Item: TListItem);
begin
if Is2File then
begin
Is2File := False;
ListView1.Selected.EditCaption;
end;
end;
procedure TForm1.ListView2Edited(Sender: TObject; Item: TListItem;
var S: string);
var
OldFile, NewFile: string;
begin
OldFile := ExtractFilePath(Application.ExeName) + ListView2.Selected.Caption;
NewFile := ExtractFilePath(Application.ExeName) + S;
if Rename(OldFile, NewFile) = 0 then
begin
ShowMessage('重命名成功');
end else
begin
Is2File := True;
end;
end;
procedure TForm1.ListView2GetImageIndex(Sender: TObject; Item: TListItem);
begin
if Is2File then
begin
Is2File := False;
ListView2.Selected.EditCaption;
end;
end;
end.
object Form1: TForm1
Left = 192
Top = 133
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object ListView1: TListView
Left = 8
Top = 88
Width = 250
Height = 150
Columns = <
item
Width = 100
end>
Items.Data = {
3A0000000200000000000000FFFFFFFFFFFFFFFF000000000000000008746573
742E74787400000000FFFFFFFFFFFFFFFF000000000000000000}
TabOrder = 0
ViewStyle = vsReport
OnEdited = ListView1Edited
OnGetImageIndex = ListView1GetImageIndex
end
object ListView2: TListView
Left = 280
Top = 88
Width = 250
Height = 150
Columns = <
item
Width = 100
end>
Items.Data = {
3A0000000200000000000000FFFFFFFFFFFFFFFF000000000000000008746573
742E74787400000000FFFFFFFFFFFFFFFF000000000000000000}
TabOrder = 1
ViewStyle = vsReport
OnEdited = ListView2Edited
OnGetImageIndex = ListView2GetImageIndex
end
end
在程序目录下建两个文件:test.txt和test1.txt。
运行一下,将ListView1和ListView2中的test.txt都改为test1.txt。大家看一下结果,为什么
不一样。原因是什么?