📄 ufilehistory.cpp
字号:
//---------------------------------------------------------------------------
// This file is part of Dicom Explorer, see http://www.sourceforge.net/projects/dcmsee
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Liu Jie (liucoldstar@yahoo.com)
//
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "uFileHistory.h"
//---------------------------------------------------------------------------
__fastcall TFileHistory::TFileHistory(AnsiString HistoryFileName,int nMaxHistoryItems,
TMenuItem *pFileMenu,void __fastcall (__closure *pOpenFileFunction)(String FileName))
{
TFileStream *pStream;
//Assign defaults from values passed
m_nMaxHistoryItems=nMaxHistoryItems;
m_pFileMenu=pFileMenu;
m_HistoryFileName=HistoryFileName;
m_pOpenFileFunction=pOpenFileFunction;
//Assign values of top seperator to existing seperator
//m_pTopSeperator=pSeperator;
//Bottom seperator doesn't exist yet so set it to NULL
m_pBottomSeperator=NULL;
//Initialize the file history list
m_pHistoryList=new TStringList;
//If the file doesn't exist, create an empty file
if (!FileExists(m_HistoryFileName))
{
pStream=new TFileStream(m_HistoryFileName,fmCreate);
delete pStream;
}
//Attempt to open the file and load history items
//If the attempt fails, ignore the error
try
{
m_pHistoryList->LoadFromFile(m_HistoryFileName);
//if the history file was found set up the menu
RebuildHistory();
}
catch (...)
{
MessageBox(Application->Handle, "Error in reading history file!", "Error",MB_ICONERROR|MB_OK);
}
}
//---------------------------------------------------------------------------
__fastcall TFileHistory::~TFileHistory(void)
{
//Save the history list to the file
//If something goes wrong display an error message
try
{
//Save the history list
m_pHistoryList->SaveToFile(m_HistoryFileName);
}
catch (...)
{
//Display an error message
MessageBox(Application->Handle, "Error in saving history file!", "Error",MB_ICONERROR|MB_OK);
}
//Destroy the history list
if (m_pHistoryList) delete m_pHistoryList;
}
//---------------------------------------------------------------------------
//Clean the history list
void __fastcall TFileHistory::CleanHistory(void)
{
TMenuItem *pMenuItem;
int nPos,nHistoryEnd;
//If BottomSeperator is not NULL then there are history items
if (m_pBottomSeperator)
{
//Use position of BottomSeperator as end of history range
nHistoryEnd=m_pFileMenu->IndexOf(m_pBottomSeperator);
//Delete all history elements including bottom seperator
for(nPos=0;nPos<=nHistoryEnd;nPos++) m_pFileMenu->Delete(0);
}
m_pHistoryList->Clear();
}
//---------------------------------------------------------------------------
//Function actually changes the file menu to show the history items
void __fastcall TFileHistory::RebuildHistory(void)
{
TMenuItem *pMenuItem;
int nPos,nHistoryEnd;
// if BottomSeperator is not NULL then there are history items
if (m_pBottomSeperator)
{
//Use position of BottomSeperator as end of history range
nHistoryEnd=m_pFileMenu->IndexOf(m_pBottomSeperator);
//Delete all history elements including bottom seperator
for(nPos=0;nPos<=nHistoryEnd;nPos++) m_pFileMenu->Delete(0);
}
//Trim list, if required, to maximum items allowed
if (m_pHistoryList->Count>m_nMaxHistoryItems)
{
for(nPos=m_nMaxHistoryItems;nPos<m_pHistoryList->Count;nPos++) m_pHistoryList->Delete(m_nMaxHistoryItems);
}
//If there are history items then build menu items
if (m_pHistoryList->Count>0)
{
//For each file in the history list
for (nPos=0;nPos<m_pHistoryList->Count;nPos++)
{
//Create a new menu item
pMenuItem=new TMenuItem(m_pFileMenu);
//Use position numer as accelerator in front of file name
pMenuItem->Caption="&"+IntToStr(nPos+1)+" "+m_pHistoryList->Strings[nPos];
//Assign event handler
pMenuItem->OnClick=HistoryItemClick;
//Insert into file menu
m_pFileMenu->Insert(nPos,pMenuItem);
}
//Create and insert a seperator following the last item
m_pBottomSeperator=new TMenuItem(m_pFileMenu);
m_pBottomSeperator->Caption= "-";
m_pFileMenu->Insert(m_pHistoryList->Count,m_pBottomSeperator);
}
}
//---------------------------------------------------------------------------
//Event handler for clicks on the history items
void __fastcall TFileHistory::HistoryItemClick(TObject *Sender)
{
int nPos;
AnsiString FileName;
//Calculate position in history list from releative position of selected file name
nPos=m_pFileMenu->IndexOf((TMenuItem *)Sender);
//Get file name from history list
FileName=m_pHistoryList->Strings[nPos];
//Call back to owner form function to open the file
//ShowMessage(FileName);
m_pOpenFileFunction(FileName);
}
//---------------------------------------------------------------------------
//Called by the main application to maintain the history list
void __fastcall TFileHistory::AddToHistory(AnsiString FileName)
{
int nPos;
//Try to find the file name in the history list
nPos=m_pHistoryList->IndexOf(FileName);
//If the file was found,delete it from the history list
if (nPos!= -1) m_pHistoryList->Delete(nPos);
//If a new file is opened, add the file to the top of the history list
m_pHistoryList->Insert(0,FileName);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -