⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editunit.cpp

📁 一个文本编辑器
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "EditUnit.h"
#include "MdiUnit.h"
#include "AboutUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CSPIN"
#pragma resource "*.dfm"
TMainForm *MainForm;
int ChildCount = 0;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MyDropFileFunction(TWMDropFiles &Msg)
{
  int NumOfFiles = DragQueryFile((HDROP)Msg.Drop, 0xFFFFFFFF, NULL, 0 );
  for(int i = 0; i < NumOfFiles; ++i )
  {
    //从Msg消息中得到文件路径
    String NameFile;
    NameFile.SetLength(MAX_PATH);
    int Length = DragQueryFile((HDROP)Msg.Drop, i, NameFile.c_str(),NameFile.Length() ) ;
    NameFile.SetLength(Length);
    //从路径中取得文件护展名;
    String NameExt = ExtractFileExt( NameFile.LowerCase() ) ;
    if( NameExt == ".txt" || NameExt == ".hf" )
    {
      TMdiForm* pForm = new TMdiForm(this);
      pForm->Caption = NameFile;
      pForm->newFile = false ;
      pForm->RichEdit1->Lines->LoadFromFile(NameFile);
      pForm->RichEdit1->Modified = false ;
      pForm->Show() ;
    }
  }
  DragFinish((HDROP)Msg.Drop);
}
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  DragAcceptFiles(this->Handle, true);
  FontList->Items = Screen->Fonts;
  FontList->ItemIndex = 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NewNClick(TObject *Sender)
{  //新建
  TMdiForm* pForm = new TMdiForm(this);
  pForm->Caption = "未命名" + String(++ChildCount) + ".hf";
  pForm->newFile = true ;
  pForm->Show();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::OpenNClick(TObject *Sender)
{   //打开
  if( OpenDialog1->Execute() )
  {
    TMdiForm* pForm = new TMdiForm(this);
    pForm->Caption = OpenDialog1->FileName;
    pForm->newFile = false ;
    pForm->RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
    pForm->RichEdit1->Modified = false ;
    pForm->Show() ;
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SaveNClick(TObject *Sender)
{    //保存
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
  if( !pForm->newFile )
          pForm->RichEdit1->Lines->SaveToFile( pForm->Caption );
  else SaveAsNClick(Sender);

}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SaveAsNClick(TObject *Sender)
{    //另存
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
  {
    SaveDialog1->FileName = ExtractFileName(pForm->Caption) ;
    if (SaveDialog1->Execute() )
    {
      AnsiString FileName = SaveDialog1->FileName ;
      if( ExtractFileExt( FileName ) == "" || ExtractFileExt( FileName ) == "." )
        FileName = ChangeFileExt(FileName,".hf");
      if( FileExists( FileName ) )
      {
        String Msg = "文件" + FileName +"已经存在。\r\n要替换它吗?";
        int Check = MessageDlg(Msg, mtWarning	, TMsgDlgButtons()<<mbOK<<mbNo, 0);
        if( Check != mrOk )
        //if( Application->MessageBoxA(Msg.c_str(),"保存", MB_YESNO ) != IDYES )
          return ;
      }
        pForm->Caption = FileName;
        pForm->RichEdit1->Lines->SaveToFile( pForm->Caption );
        pForm->newFile = false ;
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PrintSetNClick(TObject *Sender)
{   //打印设置
  PrinterSetupDialog1->Execute() ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PrintNClick(TObject *Sender)
{   //打印
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
  {
    if( PrintDialog1->Execute() )
    {
      try
      {
          pForm->RichEdit1->Print(pForm->Caption) ;
      }
      catch(...)
      {
        Printer()->EndDoc();
        throw;
      }
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N8Click(TObject *Sender)
{
  for( int i = 0; i < this->MDIChildCount ; ++i )
    this->MDIChildren[i]->Close() ;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::LeftBtClick(TObject *Sender)
{
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
  {
    TSpeedButton* Bt = (TSpeedButton*)Sender;
    switch ( Bt->Tag )
    {
      case 1: pForm->RichEdit1->Paragraph->Alignment = taLeftJustify  ; break;
      case 2: pForm->RichEdit1->Paragraph->Alignment = taCenter       ; break;
      case 3: pForm->RichEdit1->Paragraph->Alignment = taRightJustify ; break;
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FontListChange(TObject *Sender)
{
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
    pForm->RichEdit1->SelAttributes->Name = FontList->Items->Strings[FontList->ItemIndex];
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FontSizeChange(TObject *Sender)
{
  TMdiForm* pForm = dynamic_cast<TMdiForm*>(this->ActiveMDIChild ) ;
  if( pForm )
     pForm->RichEdit1->SelAttributes->Size = FontSize->Value ;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::A1Click(TObject *Sender)
{
  AboutForm->ShowModal();  
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -