frmInfo.pas
上传用户:youjie821
上传日期:2013-01-27
资源大小:459k
文件大小:16k
- unit frmInfo;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, RzButton, ExtCtrls, RzPanel, RzBtnEdt, StdCtrls, Mask, RzEdit,
- RzLabel, ActnList, FileCtrl, ComCtrls, RzTreeVw, RzSplit, RzListVw,
- ImgList, Menus, hxSysUtils, hxFileRes, hxClasses, hxUpdate, RzSpnEdt, ShellApi,
- hxVersion;
- type
- TInfoForm = class(TForm)
- ActionList1: TActionList;
- actOK: TAction;
- actCancel: TAction;
- RzPanel3: TRzPanel;
- RzLabel4: TRzLabel;
- edtProjectName: TRzEdit;
- edtRootDir: TRzButtonEdit;
- RzLabel2: TRzLabel;
- lvFiles: TRzListView;
- RzSizePanel1: TRzSizePanel;
- tvDirectory: TRzTreeView;
- ImageList1: TImageList;
- actSelectRoot: TAction;
- actAddFile: TAction;
- pmnuDirectory: TPopupMenu;
- N1: TMenuItem;
- RzPanel2: TRzPanel;
- RzLabel3: TRzLabel;
- actDeleteDirectory: TAction;
- N3: TMenuItem;
- pmnuFiles: TPopupMenu;
- N4: TMenuItem;
- N2: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- actDeleteFile: TAction;
- actRefreshFiles: TAction;
- actRefreshDirectory: TAction;
- N7: TMenuItem;
- N8: TMenuItem;
- RzPanel1: TRzPanel;
- RzButton1: TRzButton;
- RzButton2: TRzButton;
- dlgOpen: TOpenDialog;
- ImageList2: TImageList;
- actIncVersion: TAction;
- actDecVersion: TAction;
- N9: TMenuItem;
- N10: TMenuItem;
- actAddDirectory: TAction;
- A1: TMenuItem;
- N11: TMenuItem;
- edtDescription: TRzEdit;
- edtFloat: TRzEdit;
- actBrowseFiles: TAction;
- B1: TMenuItem;
- actRefreshVersion: TAction;
- N12: TMenuItem;
- procedure actOKExecute(Sender: TObject);
- procedure actCancelExecute(Sender: TObject);
- procedure lvFilesSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- procedure edtPathKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure edtRootDirButtonClick(Sender: TObject);
- procedure actSelectRootExecute(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure actDeleteDirectoryExecute(Sender: TObject);
- procedure actRefreshFilesExecute(Sender: TObject);
- procedure tvDirectoryChange(Sender: TObject; TreeNode: TTreeNode);
- procedure actRefreshDirectoryExecute(Sender: TObject);
- procedure actDeleteFileExecute(Sender: TObject);
- procedure actDeleteFileUpdate(Sender: TObject);
- procedure actAddFileExecute(Sender: TObject);
- procedure actAddFileUpdate(Sender: TObject);
- procedure edtFloatKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure lvFilesClick(Sender: TObject);
- procedure lvFilesGetImageIndex(Sender: TObject; Item: TListItem);
- procedure actDeleteDirectoryUpdate(Sender: TObject);
- procedure lvFilesColumnClick(Sender: TObject; Column: TListColumn);
- procedure lvFilesCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- procedure actAddDirectoryExecute(Sender: TObject);
- procedure actAddDirectoryUpdate(Sender: TObject);
- procedure actBrowseFilesUpdate(Sender: TObject);
- procedure actBrowseFilesExecute(Sender: TObject);
- procedure actRefreshVersionExecute(Sender: TObject);
- procedure actRefreshVersionUpdate(Sender: TObject);
- private
- FResTree: TResTree;
- FSortIndex: Integer;
- FSortAsc: Integer;
- FDescription: string;
- FProjectName: string;
- FRootDir: string;
- function GetTreeNodeByResNode(ResNode: TResNode): TTreeNode;
- procedure RefreshTreeView(TreeNodes: TTreeNodes);
- procedure AddFileToListView(TreeNode: TTreeNode; FileName: string);
- procedure AddNodeToListView(ResNode: TResNode);
- procedure RefreshListView(ParentNode: TResNode);
- procedure SetDescription(const Value: string);
- procedure SetProjectName(const Value: string);
- procedure SetRootDir(const Value: string);
- function GetDescription: string;
- function GetProjectName: string;
- function GetRootDir: string;
- protected
- procedure DropFiles(var Msg: TMessage); message WM_DROPFILES;
- public
- constructor Create(AOwner: TComponent; AProjectItem: TProjectItem); reintroduce;
- destructor Destroy; override;
- property ProjectName: string read GetProjectName write SetProjectName;
- property RootDir: string read GetRootDir write SetRootDir;
- property Description: string read GetDescription write SetDescription;
- property ResTree: TResTree read FResTree;
- end;
- function ShowInfoForm(var AProjectName, ARootDir, ADescription: string): Boolean;
- implementation
- uses Math;
- {$R *.dfm}
- function ShowInfoForm(var AProjectName, ARootDir, ADescription: string): Boolean;
- var
- Index: Integer;
- ProjectItem: TProjectItem;
- begin
- Index:= GetProjectCollection.IndexOf(AProjectName);
- if Index <> -1 then // 修改
- ProjectItem:= GetProjectCollection.Items[Index]
- else // 添加
- ProjectItem:= GetProjectCollection.Add('', '', '');
- with TInfoForm.Create(nil, ProjectItem) do
- try
- Result:= (ShowModal = mrOk);
- if Result then
- begin
- ProjectItem.RemoveResTree;
- ProjectItem.ProjectName:= ProjectName;
- ProjectItem.RootDir:= RootDir;
- ProjectItem.Description:= Description;
- ProjectItem.ResTree.Assign(ResTree);
- ProjectItem.SaveResTree;
- AProjectName:= ProjectName;
- ARootDir:= RootDir;
- ADescription:= Description;
- end
- else
- begin
- Index:= GetProjectCollection.IndexOf('');
- if Index <> -1 then GetProjectCollection.Delete(Index);
- end;
- finally
- Free;
- end;
- end;
- procedure TInfoForm.actOKExecute(Sender: TObject);
- begin
- ModalResult:= mrOk;
- end;
- procedure TInfoForm.actCancelExecute(Sender: TObject);
- begin
- ModalResult:= mrCancel;
- end;
- procedure TInfoForm.lvFilesSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- begin
- if Selected then
- begin
- with lvFiles do
- begin
- edtFloat.Left:= Left + Column[0].Width + Column[1].Width + Item.Left;
- edtFloat.Top:= lvFiles.Top + Item.Top;
- edtFloat.Width:= lvFiles.Column[2].Width;
- edtFloat.Height:= 16;
- edtFloat.Text:= Item.SubItems[1];
- end;
- edtFloat.Visible:= True;
- end
- else
- edtFloat.Visible:= False;
- end;
- procedure TInfoForm.edtPathKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if Key = VK_RETURN then
- begin
- lvFiles.Selected.SubItems[1]:= edtFloat.Text;
- edtFloat.Visible:= False;
- end;
- end;
- procedure TInfoForm.edtRootDirButtonClick(Sender: TObject);
- begin
- actSelectRoot.Execute;
- end;
- procedure TInfoForm.actSelectRootExecute(Sender: TObject);
- var
- szDir: string;
- begin
- if SelectDirectory('选择目录', '', szDir) then
- begin
- RootDir:= szDir;
- FResTree.LoadFiles(szDir);
- RefreshTreeView(tvDirectory.Items);
- end;
- end;
- procedure TInfoForm.RefreshTreeView(TreeNodes: TTreeNodes);
- procedure TravelTree(ResNode: TResNode; TreeNode: TTreeNode);
- var
- I: Integer;
- begin
- Assert(ResNode <> nil);
- if PResInfo(ResNode.Data)^.ResType = rtDirectory then
- begin
- TreeNode:= TreeNodes.AddChild(TreeNode, ResNode.Text);
- TreeNode.Data:= ResNode;
- end;
- for I:= 0 to ResNode.Count - 1 do
- TravelTree(TResNode(ResNode.Children[I]), TreeNode);
- end;
- var
- I: Integer;
- RootNode: TResNode;
- TreeNode: TTreeNode;
- begin
- TreeNodes.BeginUpdate;
- try
- TreeNodes.Clear;
- RootNode:= TResNode(FResTree.RootNode);
- TreeNode:= TreeNodes.AddChild(nil, RootNode.Text);
- TreeNode.Data:= RootNode;
- for I:= 0 to RootNode.Count - 1 do
- TravelTree(TResNode(RootNode.Children[I]), TreeNode);
- finally
- TreeNodes.EndUpdate;
- end;
- end;
- procedure TInfoForm.actDeleteDirectoryExecute(Sender: TObject);
- begin
- if tvDirectory.Selected <> nil then
- begin
- FResTree.RemoveNode(TResNode(tvDirectory.Selected.Data));
- tvDirectory.Items.Delete(tvDirectory.Selected);
- end;
- end;
- procedure TInfoForm.RefreshListView(ParentNode: TResNode);
- var
- I: Integer;
- ResNode: TResNode;
- begin
- lvFiles.Clear;
- if ParentNode = nil then Exit;
- for I:= 0 to ParentNode.Count - 1 do
- begin
- ResNode:= TResNode(ParentNode.Children[I]);
- if ResNode.Data = nil then Continue;
- AddNodeToListView(ResNode);
- end;
- end;
- procedure TInfoForm.actRefreshFilesExecute(Sender: TObject);
- begin
- RefreshListView(TResNode(tvDirectory.Selected.Data));
- end;
- procedure TInfoForm.tvDirectoryChange(Sender: TObject; TreeNode: TTreeNode);
- begin
- RefreshListView(TResNode(TreeNode.Data));
- end;
- procedure TInfoForm.actRefreshDirectoryExecute(Sender: TObject);
- begin
- RefreshTreeView(tvDirectory.Items);
- end;
- procedure TInfoForm.FormShow(Sender: TObject);
- begin
- RefreshTreeView(tvDirectory.Items);
- RefreshListView(nil);
- end;
- procedure TInfoForm.actDeleteFileExecute(Sender: TObject);
- begin
- FResTree.RemoveNode(TResNode(lvFiles.Selected.Data));
- lvFiles.DeleteSelected;
- end;
- procedure TInfoForm.actDeleteFileUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= lvFiles.Selected <> nil;
- end;
- procedure TInfoForm.actAddFileExecute(Sender: TObject);
- var
- ParentNode: TResNode;
- CurrentDir: string;
- begin
- ParentNode:= TResNode(tvDirectory.Selected.Data);
- dlgOpen.Filter:= '所有文件(*.*)|*.*';
- dlgOpen.InitialDir:= FormatDirectoryName(FRootDir) + PResInfo(ParentNode.Data)^.DownloadURL;
- dlgOpen.FileEditStyle:= fsEdit;
- if dlgOpen.Execute then
- AddFileToListView(tvDirectory.Selected, dlgOpen.FileName);
- end;
- procedure TInfoForm.actAddFileUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= tvDirectory.Selected <> nil;
- end;
- procedure TInfoForm.edtFloatKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if Key = VK_RETURN then
- begin
- lvFiles.Selected.SubItems[1]:= edtFloat.Text;
- edtFloat.Visible:= False;
- with PResInfo(TResNode(lvFiles.Selected.Data).Data)^ do
- begin
- ModifyTime:= StrToDateTime(lvFiles.Selected.SubItems[0]);
- Version:= edtFloat.Text;
- end;
- end;
- end;
- procedure TInfoForm.lvFilesClick(Sender: TObject);
- begin
- if lvFiles.Selected <> nil then
- begin
- edtFloat.Visible:= True;
- edtFloat.SetFocus;
- end;
- end;
- procedure TInfoForm.lvFilesGetImageIndex(Sender: TObject; Item: TListItem);
- var
- szOldTime: string;
- begin
- szOldTime:= DateTimeToStr(PResInfo(TResNode(Item.Data).Data)^.ModifyTime);
- if SameText(Item.SubItems[0], szOldTime) then
- Item.ImageIndex:= 0
- else
- Item.ImageIndex:= -1;
- end;
- procedure TInfoForm.actDeleteDirectoryUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= (tvDirectory.Selected <> nil) and (tvDirectory.Selected.AbsoluteIndex <> 0);
- end;
- procedure TInfoForm.lvFilesColumnClick(Sender: TObject; Column: TListColumn);
- begin
- FSortIndex := Column.Index;
- FSortAsc := FSortAsc * -1;
- (Sender as TCustomListView).AlphaSort;
- end;
- procedure TInfoForm.lvFilesCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- var
- ix: Integer;
- begin
- if FSortIndex = 0 then
- Compare := CompareText(Item1.Caption,Item2.Caption) * FSortAsc
- else begin
- ix := FSortIndex - 1;
- Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]) * FSortAsc;
- end;
- end;
- procedure TInfoForm.actAddDirectoryExecute(Sender: TObject);
- var
- szRoot, szDir: string;
- ResNode: TResNode;
- begin
- if tvDirectory.Selected.AbsoluteIndex = 0 then
- szRoot:= FormatDirectoryName(FRootDir)
- else
- szRoot:= FormatDirectoryName(FRootDir) + PResInfo(TResNode(tvDirectory.Selected.Data).Data)^.DownloadURL;
- if SelectDirectory('选择目录', szRoot, szDir) then
- begin
- ResNode:= FResTree.AddDirectory(TResNode(tvDirectory.Selected.Data), szDir);
- RefreshTreeView(tvDirectory.Items);
- GetTreeNodeByResNode(ResNode).Selected:= True;
- end;
- end;
- procedure TInfoForm.SetDescription(const Value: string);
- begin
- FDescription := Value;
- edtDescription.Text:= FDescription;
- end;
- procedure TInfoForm.SetProjectName(const Value: string);
- begin
- FProjectName := Value;
- edtProjectName.Text:= FProjectName;
- end;
- procedure TInfoForm.SetRootDir(const Value: string);
- begin
- FRootDir := Value;
- edtRootDir.Text:= FRootDir;
- end;
- function TInfoForm.GetDescription: string;
- begin
- Result:= edtDescription.Text;
- end;
- function TInfoForm.GetProjectName: string;
- begin
- Result:= edtProjectName.Text;
- end;
- function TInfoForm.GetRootDir: string;
- begin
- Result:= edtRootDir.Text;
- end;
- procedure TInfoForm.actAddDirectoryUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= tvDirectory.Selected <> nil;
- end;
- procedure TInfoForm.AddNodeToListView(ResNode: TResNode);
- var
- pInfo: PResInfo;
- begin
- pInfo:= PResInfo(ResNode.Data);
- if pInfo^.ResType = rtFile then
- with lvFiles.Items.Add do
- begin
- Caption:= pInfo^.FileName;
- SubItems.Add(FormatDateTime('YYYY-MM-DD HH:MM:SS', FileDateToDateTime(FileAge(FResTree.RootNode.Text +
- pInfo^.DownloadURL))));
- SubItems.Add(pInfo.Version);
- SubItems.Add(pInfo^.DownloadURL);
- Data:= ResNode;
- end;
- end;
- constructor TInfoForm.Create(AOwner: TComponent; AProjectItem: TProjectItem);
- var
- Index: Integer;
- begin
- inherited Create(AOwner);
- // 接受拖放
- DragAcceptFiles(lvFiles.Handle, True);
- FResTree:= TResTree.Create;
- ProjectName:= AProjectItem.ProjectName;
- RootDir:= AProjectItem.RootDir;
- Description:= AProjectItem.Description;
- AProjectItem.LoadResTree;
- FResTree.Assign(AProjectItem.ResTree);
- FSortAsc:= 1;
- RefreshTreeView(tvDirectory.Items);
- RefreshListView(nil);
- end;
- destructor TInfoForm.Destroy;
- begin
- FResTree.Free;
- inherited Destroy;
- end;
- function TInfoForm.GetTreeNodeByResNode(ResNode: TResNode): TTreeNode;
- var
- I: Integer;
- begin
- if ResNode = nil then
- Result:= tvDirectory.Items.GetFirstNode
- else
- for I:= 1 to tvDirectory.Items.Count - 1 do
- if tvDirectory.Items[I].Data = ResNode then
- begin
- Result:= tvDirectory.Items[I];
- Break;
- end;
- end;
- procedure TInfoForm.DropFiles(var Msg: TMessage);
- var
- I, C: Integer;
- Buffer: array[0..1023] of char;
- begin
- inherited;
- // Todo:RzListView截断了WM_DROPFILES消息,以下代码执行不到!!
- C:= DragQueryFile(Msg.WParam, $FFFFFFFF, nil, 0);
- for I:= 0 to C - 1 do
- begin
- FillChar(Buffer, SizeOf(Buffer), #0);
- DragQueryFile(Msg.WParam, I, Buffer, SizeOf(Buffer));
- Caption:= Buffer;
- AddFileToListView(tvDirectory.Selected, Buffer);
- end;
- end;
- procedure TInfoForm.AddFileToListView(TreeNode: TTreeNode; FileName: string);
- var
- ParentNode: TResNode;
- CurrentDir, NewFileName: string;
- begin
- ParentNode:= TResNode(TreeNode.Data);
- CurrentDir:= FormatDirectoryName(FRootDir) + FormatDirectoryName(PResInfo(ParentNode.Data)^.DownloadURL);
- NewFileName:= FileName;
- if not SameText(CurrentDir, ExtractFilePath(FileName)) then
- begin
- if MessageBox(0, '所选文件与不在当前目录,是否将其拷贝到当前目录?', '提示',
- MB_YESNO + MB_ICONINFORMATION) = IDYES then
- begin
- NewFileName:= CurrentDir + ExtractFileName(FileName);
- CopyFile(PAnsiChar(FileName), PAnsiChar(NewFileName), True);
- end
- else
- Exit;
- end;
- AddNodeToListView(FResTree.AddFile(ParentNode, NewFileName));
- end;
- procedure TInfoForm.actBrowseFilesUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= tvDirectory.Selected <> nil;
- end;
- procedure TInfoForm.actBrowseFilesExecute(Sender: TObject);
- var
- CurrentDir: string;
- begin
- if tvDirectory.Selected.AbsoluteIndex = 0 then
- CurrentDir:= FRootDir
- else
- CurrentDir:= FormatDirectoryName(FRootDir) + PResInfo(TResNode(tvDirectory.Selected.Data).Data)^.DownloadURL;
- ShellExecute(0, 'open', PChar(CurrentDir), '', '', SW_NORMAL);
- end;
- procedure TInfoForm.actRefreshVersionExecute(Sender: TObject);
- begin
- lvFiles.Selected.SubItems[1]:= GetFileVersion(FormatDirectoryName(RootDir) +
- PResInfo(TResNode(lvFiles.Selected.Data).Data)^.DownloadURL);
- //edtFloat.Perform(WM_KEYDOWN, VK_RETURN, 0);
- end;
- procedure TInfoForm.actRefreshVersionUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled:= lvFiles.Selected <> nil;
- end;
- end.