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

📄 txtrtf.cpp

📁 C++ Builder 6 编程实例教程-北京希望电子出版社出版
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "TxtRtf.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->OpenDialog1->Title="请选择一个文本文件:";
//设置对话框标题
Form1->OpenDialog1->InitialDir="c:\\pwin98";
//设置缺省路径
Form1->OpenDialog1->Filter="纯文本文件(*.txt)|*.txt|RTF文本文件(*.rtf)|*.rtf";
//设置对话框过滤器
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Form1->OpenDialog1->Execute())
   //显示对话框
   {
   Form1->RichEdit1->Lines->LoadFromFile(Form1->OpenDialog1->FileName);
   //打开文本文件
   if (UpperCase(ExtractFileExt(Form1->OpenDialog1->FileName))==".TXT")
      //如果文件扩展名是TXT
      {
      Form1->Button2->Caption="转换为RTF文件";
      //设置按钮状态
      Form1->SaveDialog1->Filter="RTF文本文件(*.rtf)|*.rtf";
      //设置对话框过滤器
      Form1->SaveDialog1->DefaultExt="rtf";
      //设置缺省扩展名
      Form1->SaveDialog1->Title="请输入一个RTF文件名:";
      //设置对话框标题
      }
   else if (UpperCase(ExtractFileExt(Form1->OpenDialog1->FileName))==".RTF")
      //如果文件扩展名是RTF
      {
      Form1->Button2->Caption="转换为TXT文件";
      //设置按钮状态
      Form1->SaveDialog1->Filter="纯文本文件(*.txt)|*.txt";
      //设置对话框过滤器
      Form1->SaveDialog1->DefaultExt="txt";
      //设置缺省扩展名
      Form1->SaveDialog1->Title="请输入一个TXT文件名:";
      //设置对话框标题
      }
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (Form1->SaveDialog1->Execute())
   //显示对话框
   {
   Form1->RichEdit1->Lines->SaveToFile(Form1->SaveDialog1->FileName);
   //存储文件
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (Form1->RichEdit1->Modified)
   //判断文件是否被改变
   {
   if (MessageDlg("文件已经被修改过,存储修改后的结果吗?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
       //提示用户是否保存修改结果
       {
       if (Form1->SaveDialog1->Execute())
          //显示对话框
          {
          Form1->RichEdit1->Lines->SaveToFile(Form1->SaveDialog1->FileName);
          //存储文件
          }
       }
   }
}
//---------------------------------------------------------------------------

 

⌨️ 快捷键说明

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