奇怪,大家来看一下? (200分)

  • 主题发起人 主题发起人 omvm
  • 开始时间 开始时间
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。大家看一下结果,为什么
不一样。原因是什么?
 
没有人愿意试一下吗?
 
我试了,没有什么一样我看不出你的意图

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(S) then
begin
ShowMessage('文件存在,请重命名');
Is2File := True;
end else
begin
Rename(OldFile, NewFile);
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 FileExists(S) then
if Rename(OldFile, NewFile) = 0 then
begin
ShowMessage('重命名成功');
end else
begin
Is2File := True;
end;
end;

 
关键在GetImageIndex事件中下面这句
ListView.Selected.EditCaption;
在ListView2中执行了却没有效果怎么回事?这和ListView1不一样啊。
 
可能是listview1打开后不允许listview2修改这个文件
 
不是的,你还是没有明白这两段程序的区别,ListView1和ListView2只是给大家对照的看两
个程序的区别的,ListView1中重命名后如果目录下有相同文件的话,就会进入编辑状态,
就是ListView上的小方框还在,而ListView2中重命名后如果目录下有相同文件的话,却不会
进入编辑状态,我想知道为什么?
是不是由于Rename这个过程引起的。如果是,为什么啊?我都快昏了,请大家踊跃发言。
 
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;
Abort;//尝试加上实验一下,也许有效,如果不行,使用Items.EditCaption;实验一下
end;
end;
 
还有,在存在重名文件的时候,你要进行阻止重新命名,不要使用Edit时间,使用
ListView1Editing事件,你可以通过设置AllowEdit来决定是否接受命名。
 
谢谢楼上的朋友提前。请大家发言啊!!
我每周只有两天能上网,所以直到现在才又把这个问题提前了。
 
后退
顶部