//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
#include "Para.h"
#include "About.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Word_2K_SRVR"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
StatusBar->Panels->Items[0]->Width = 0.75*Width ;
//获得系统字体
FontName->Items = Screen->Fonts ;
//新建文件
NewBtn->Click() ;
//获得历史菜单
HistoryList = new TStringList() ;
AnsiString hFileName ;
hFileName = ExtractFilePath(Application->ExeName) + HistoryListName ;
if (FileExists(hFileName))
{
HistoryList->LoadFromFile(hFileName) ;
BuildHistory(File) ;
}
//void GetHistory();
}
//---------------------------------------------------------------------------
//退出事件
void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
if(isNamed)
{
HistoryList->Add(cFileName) ;
BuildHistory(File) ;
}
Action = caFree ;
HistoryList->SaveToFile(ExtractFilePath(Application->ExeName) + HistoryListName) ;
delete HistoryList ;
}
//---------------------------------------------------------------------------
//判断是否关闭窗口
void __fastcall TfrmMain::FormCloseQuery(TObject *Sender, bool &CanClose)
{
String sc = Caption ;
int ExitCode ;
sc.Delete(sc.Length() - 8,9) ;
if(redtMain->Modified)
{
ExitCode = Application->MessageBox(("将改动保存到 " + sc + "?").c_str(),
"写字板", MB_YESNOCANCEL|MB_ICONWARNING) ;
switch(ExitCode)
{
case IDCANCEL:
CanClose = false ; // 不关闭程序
break ;
case IDYES:
SaveBtnClick(Sender) ; //保存文件并退出
case IDNO:
CanClose = true ; // 不保存文件退出
break ;
}
}
}
//---------------------------------------------------------------------------
//自动调整大小
void __fastcall TfrmMain::ResizeClick(TObject *Sender)
{
StatusBar->Panels->Items[0]->Width = 0.75*Width ;
}
//---------------------------------------------------------------------------
//获得历史菜单
void TfrmMain::GetHistory()
{
/* AnsiString hFileName ;
hFileName = ExtractFilePath(Application->ExeName) + HistoryListName ;
if (FileExists(hFileName))
{
HistoryList->LoadFromFile(hFileName) ;
BuildHistory(File) ;
} */
}
//---------------------------------------------------------------------------
//建立历史菜单
void TfrmMain::BuildHistory(TMenuItem *FileMenu)
{
TMenuItem *MenuItem ;
int Pos ;
if(HistoryList->Count > MaxHistoryFile)
for(Pos = MaxHistoryFile; Pos<HistoryList->Count; Pos++)
HistoryList->Delete(0) ;
if(BottomS != NULL)
for(Pos=0; Pos<ShowHistoryFiles; Pos++)
File->Delete(HistoryAddPos) ;
if(HistoryList->Count > 0)
{
for (Pos=0; Pos<HistoryList->Count; Pos++)
{
MenuItem = new TMenuItem(FileMenu) ;
MenuItem->Caption = "&"+IntToStr(Pos+1)+" "
+ HistoryList->Strings[Pos] ;
MenuItem->OnClick = HistoryItemClick ;
FileMenu->Insert(HistoryAddPos+Pos,MenuItem) ;
}
ShowHistoryFiles = HistoryList->Count ;
if (BottomS==NULL)
{
BottomS = new TMenuItem(FileMenu) ;
BottomS->Caption="-" ;
FileMenu->Insert(HistoryAddPos+HistoryList->Count,BottomS) ;
}
}
}
//---------------------------------------------------------------------------
//历史菜单单击事件
void __fastcall TfrmMain::HistoryItemClick(TObject *Sender)
{
int Pos ;
AnsiString ItemName ;
TMenuItem *ParentItem ;
ParentItem=(TMenuItem *)((TMenuItem *)Sender)->Parent ;
Pos = ParentItem->IndexOf((TMenuItem *)Sender)-
ParentItem->IndexOf(TopS)-1 ;
ItemName = HistoryList->Strings[Pos] ;
Caption = ItemName ;
isNamed = false ;
if(FileExists(ItemName))
{
cFileName = ItemName ;
Caption = ExtractFileName(ItemName) ;
redtMain->Lines->LoadFromFile(cFileName) ;
redtMain->SelStart = 0 ;
redtMain->Modified = false ;
isNamed = true ;
}
}
//---------------------------------------------------------------------------
//文件名处理函数
void TfrmMain::FileNameFiltrate(String cFileName)
{
int n = ExtractFileName(cFileName).Length() ;
int t = ExtractFileExt(cFileName).Length() ;
Caption = ExtractFileName(cFileName).Delete(n+1-t,t) + " - 写字板" ;
//Caption = ExtractFileName(cFileName + " - 写字板") ;
Application->Title = Caption ;
}
//---------------------------------------------------------------------------
//保存文档改动
bool TfrmMain::SaveDocChange(String Caption)
{
int ExitCode ;
bool ret ;
Caption.Delete(Caption.Length() - 8,9) ;
if(redtMain->Modified)
{
ExitCode = Application->MessageBox(("将改动保存到 " + Caption + "?").c_str(),
"写字板", MB_YESNOCANCEL|MB_ICONWARNING) ;
switch(ExitCode)
{
case IDCANCEL: ret = true ;
break ;
case IDYES: SaveBtn->Click() ;
case IDNO: ret = false ;
break ;
}
}
return ret ;
}
//---------------------------------------------------------------------------
//新建文件
void __fastcall TfrmMain::NewBtnClick(TObject *Sender)
{
if(redtMain->Modified)
if(SaveDocChange(Caption))
return ;
cFileName = "文档" ;
SaveDialog1->FileName = cFileName ;
Caption = ExtractFileName(cFileName + " - 写字板") ;
Application->Title = Caption ;
FontName->Text = "宋体" ;
FontSize->Text = "10" ;
redtMain->Clear() ;
redtMain->Modified = false ;
isNamed = false ;
}
//---------------------------------------------------------------------------
//打开文件
void __fastcall TfrmMain::OpenBtnClick(TObject *Sender)
{
if(redtMain->Modified)
if(SaveDocChange(Caption))
return ;
if(OpenDialog1->Execute())
{
cFileName = OpenDialog1->FileName;
//文件名处理函数
FileNameFiltrate(cFileName) ;
redtMain->Clear() ;
redtMain->Lines->LoadFromFile(cFileName) ;
redtMain->SelStart = 0 ;
redtMain->Modified = false ;
isNamed = true ;
}
}
//---------------------------------------------------------------------------
//保存
void __fastcall TfrmMain::SaveBtnClick(TObject *Sender)
{
if(isNamed)
{
redtMain->Lines->SaveToFile(cFileName) ;
redtMain->Modified = false ;
}
else
SaveAsBtnClick(Sender) ;
}
//---------------------------------------------------------------------------
//另存文件
void __fastcall TfrmMain::SaveAsBtnClick(TObject *Sender)
{
SaveDialog1->FileName = cFileName ;
if(SaveDialog1->Execute())
{
cFileName = SaveDialog1->FileName ;
switch(SaveDialog1->FilterIndex)
{
case 1: SaveDialog1->DefaultExt = "rtf" ;
break ;
case 2: SaveDialog1->DefaultExt = "txt" ;
break ;
case 3: SaveDialog1->DefaultExt = "doc" ;
break ;
default:SaveDialog1->DefaultExt = "" ;
break ;
}
//文件名处理函数
FileNameFiltrate(cFileName) ;
isNamed = true ;
- 1
- 2
前往页