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

📄 edit2f.cpp

📁 C++Builder编程实例详解,用具体的例子阐明C++的一些基本操作,所有程序均在BC++上编译过.可靠,建议下载
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Edit2f.h"
#include "Edit2ff.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AboutClick(TObject *Sender)
{
    MessageBox(Form1->Handle, "文本编辑器 1.0 版本", "关于 Edit...",
            MB_OK | MB_ICONINFORMATION); // MB_ICONINFORMATION:显示信息提示图标
                                         // MB_ICONWARNING:显示警告图标
                                         // MB_ICONERROR:显示错误图标
                                         // MB_OK:显示“确定”按钮
                                         // 关于其它参数详见“Win32 用户手册”
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitClick(TObject *Sender)
{
    Close(); // 等价于:form1->Close();
             // 不要使用 exit() 函数,
             // 因为该将直接结束程序运行(不处理任何事件)。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    CanClose = CheckFileSave(); // 检查文本是否需要保存
                                // 当使用 Close 方法关闭窗体时,触发该事件
                                // 如果设置 CanClose 参数(用引用传递)为“false”
                                // 将阻止窗体关闭(放弃 Close 方法的操作)。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewClick(TObject *Sender)
{
    TForm2 *pf = new TForm2((TComponent*)Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenClick(TObject *Sender)
{
    if(!CheckFileSave())
    { // 放弃打开文件操作
        return;
    }

    if(OpenDialog1->Execute())
    { // 打开文件,修改标志位
        TForm2 *pf = new TForm2((TComponent*)Sender);
        pf->RichEdit1->ReadOnly = OpenDialog1->Options.Contains(ofReadOnly);
        pf->RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
        pf->RichEdit1->SetFocus();
        pf->RichEdit1->Modified = False;
        pf->FileName = OpenDialog1->FileName; // 保存文件名
        pf->FilterIndex = OpenDialog1->FilterIndex; // 保存文件类型索引
    }
}
//---------------------------------------------------------------------------
bool TForm1::CheckFileSave()
{
    if(!ActiveMDIChild)
        // 无子窗体
        return(true);

    if(((TForm2 *)ActiveMDIChild)->RichEdit1->Modified)
    { // 需要保存文本
        switch(MessageBox(Form1->Handle, "是否保存修改?", "提示...",
                MB_YESNOCANCEL | MB_ICONQUESTION))
        {
            case ID_YES: // 选择保存文件
                SaveDialog1->FileName = ((TForm2*)ActiveMDIChild)->FileName; // 设置文件名
                SaveDialog1->FilterIndex = ((TForm2*)ActiveMDIChild)->FilterIndex; // 设置文件类型
                if(SaveDialog1->Execute())  // 打开“保存”对话框保存文件
                {                           // 并判断保存情况
                    ((TForm2*)ActiveMDIChild)->RichEdit1->Modified = false;
                    ((TForm2*)ActiveMDIChild)->RichEdit1->Lines->SaveToFile(SaveDialog1->FileName);
                }
                else
                    return(false);
                break;
            case ID_CANCEL: // 取消操作
                return(false); // 放弃操作
        }
    }
    return(true); // 继续
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    Application->OnHint = &DisplayHint;
    //FilterIndex = 1; // 初始化文件类型索引
    //FileName = "Untitled"; // 设置缺省文件名
    GetFontNames();
}

void __fastcall TForm1::DisplayHint(TObject* Owner)
{
    StatusBar1->SimpleText = Application->Hint;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SaveClick(TObject *Sender)
{
    SaveDialog1->FileName = ((TForm2*)ActiveMDIChild)->FileName; // 设置文件名
    SaveDialog1->FilterIndex = ((TForm2*)ActiveMDIChild)->FilterIndex; // 设置文件类型
    if(SaveDialog1->Execute()) // 打开“保存”对话框保存文件
    { // 用户选择“保存”按钮。
        if(SaveDialog1->FilterIndex == 1)
        {
            ((TForm2*)ActiveMDIChild)->RichEdit1->PlainText = false;
            ((TForm2*)ActiveMDIChild)->FileName = SaveDialog1->FileName + ".rtf";
        }
        else if(SaveDialog1->FilterIndex == 2)
        {
            ((TForm2*)ActiveMDIChild)->RichEdit1->PlainText = true;
            ((TForm2*)ActiveMDIChild)->FileName = SaveDialog1->FileName + ".txt";
        }
        else
        {
            ((TForm2*)ActiveMDIChild)->RichEdit1->PlainText = true;
            ((TForm2*)ActiveMDIChild)->FileName = SaveDialog1->FileName;
        }
        ((TForm2*)ActiveMDIChild)->RichEdit1->Lines->SaveToFile(
                            ((TForm2*)ActiveMDIChild)->FileName);
        ((TForm2*)ActiveMDIChild)->Caption = ((TForm2*)ActiveMDIChild)->FileName;
        ((TForm2*)ActiveMDIChild)->RichEdit1->Modified = false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PopupMenu1Popup(TObject *Sender)
{
    if(((TForm2*)ActiveMDIChild)->RichEdit1->SelLength)
    {
        Cut1->Enabled = true;
        Copy1->Enabled = true;
    }
    else
    {
        Cut1->Enabled = false;
        Copy1->Enabled = false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CutClick(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->CutToClipboard(); // 剪切文本(RichEdit1->SelText)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PastClick(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->PasteFromClipboard(); // (从剪裁版中)粘贴文本
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CopyClick(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->CopyToClipboard(); // 复制文本到剪裁版
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FindClick(TObject *Sender)
{
    FindDialog1->FindText = ((TForm2*)ActiveMDIChild)->RichEdit1->SelText; // 设置查找文本
    FindDialog1->Options << frHideUpDown; // 隐藏向前、向后选项
    FindDialog1->Execute(); // 显示“查找”对话框
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FindDialog1Find(TObject *Sender)
{
    TSearchTypes Options;

    if(FindDialog1->Options.Contains(frWholeWord))
    { // 用户选择了全字匹配( WholeWord )
        Options << stWholeWord;
    }
    else
    {
        Options >> stWholeWord;
    }

    if(FindDialog1->Options.Contains(frMatchCase))
    { // 用户选择了区分大小写
        Options << stMatchCase;
    }
    else
    {
        Options >> stMatchCase;
    }

    ((TForm2*)ActiveMDIChild)->RichEdit1->FindText(FindDialog1->FindText,
            ((TForm2*)ActiveMDIChild)->RichEdit1->SelStart + 1,
            ((TForm2*)ActiveMDIChild)->RichEdit1->Text.Length(), Options);

    ((TForm2*)ActiveMDIChild)->RichEdit1->SetFocus();
}
//---------------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                //----------------------------------------------------------------------------
// 处理字体列表项函数,在TForm1::GetFontNames函数中使用,并由系统调用。
int __stdcall EnumFontsProc(TLogFontA &LogFont, TTextMetricA &TextMetric,
                                int FontType, Pointer Data)
{   ((TStrings *)Data)->Add((AnsiString)LogFont.lfFaceName);
     return 1;
}
//----------------------------------------------------------------------------
void __fastcall TForm1::GetFontNames(void)
{    HDC hDC = GetDC(0);
     void * cTmp = (void *)FontName->Items;
     EnumFonts(hDC, NULL, (FONTENUMPROC) EnumFontsProc, (long) cTmp );
         // EnumFonts 为系统(Windows API)函数,
         // 用于获得系统可用的字体列表。
         // EnumFontsProc 为自定义函数,作为系统回调函数,用于处理字体列表项
         // 当系统返回列表项时,调用该函数并返回字体名。
         // cTmp 为用户参数(此处为指向复合列表框FontName->Items的指针),
         // 当系统调用 EnumFontsProc 函数时,返回该参数。
     ReleaseDC(0,hDC);
     FontName->Sorted = True;
}
//----------------------------------------------------------------------------
// 当用户改变文本选择或改变光标位置时,触发该事件。
void __fastcall TForm1::sBoldClick(TObject *Sender)
{
    if(sBold->Down)
    {  // sBold 按钮按下
        ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style << fsBold;
        // Style 属性的各二进制位分别代表不同的类型,
        // 该类型的变量可以用“<<”操作付设置,用“>>”取消设置
    }
    else
    {
         ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                 ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style >> fsBold;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sItalicClick(TObject *Sender)
{
    if(sItalic->Down)
    {
        ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style << fsItalic;
    }
    else
    {
         ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                 ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style >> fsItalic;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sUnderlineClick(TObject *Sender)
{
    if(sUnderline->Down)
    {
        ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style << fsUnderline;
    }
    else
    {
         ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style =
                 ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Style >> fsUnderline;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontNameChange(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Name =
            FontName->Items->Strings[FontName->ItemIndex];
    ((TForm2*)ActiveMDIChild)->RichEdit1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontSizeChange(TObject *Sender)
{
     int fontsize = atoi(FontSize->Text.c_str());

     if (fontsize)
     {
         if (fontsize < 1)
         {
             ShowMessage("The number must be between 1 and 1638.");
             FontSize->Text = 1;
         }
         else if (fontsize > 1638)
         {
             ShowMessage("The number must be between 1 and 1638.");
             FontSize->Text = 1638;
         }
         ((TForm2*)ActiveMDIChild)->RichEdit1->SelAttributes->Size =
                         atoi(FontSize->Text.c_str());
         // SelAttributes 对象用于设置选择文本的字体、颜色、等
     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AlignButton(TObject *Sender)
{
    TControl *cTmp = (TControl*)(Sender);

    ((TForm2*)ActiveMDIChild)->RichEdit1->Paragraph->Alignment =
                    (TAlignment)cTmp->Tag;
    // Paragraph 对象用于设置段落属性
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BulletsButtonClick(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->Paragraph->Numbering =
                    (TNumberingStyle)BulletsButton->Down;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    // 初始化字体
    // 该设置不能在 FormCreate 事件中进行,
    // 因为是当窗体创建时触发 FormCreate 事件,而此时尚未创建 RichEdit1 控件。
    FontName->Text = "宋体";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PrintSetupClick(TObject *Sender)
{
    PrinterSetupDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sPrintClick(TObject *Sender)
{
    ((TForm2*)ActiveMDIChild)->RichEdit1->Print(((TForm2*)ActiveMDIChild)->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontNameKeyPress(TObject *Sender, char &Key)
{
    Key = '\n';
    ((TForm2*)ActiveMDIChild)->RichEdit1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FontSizeKeyPress(TObject *Sender, char &Key)
{
    if(Key == '\r')
    { // 用户按回车键
        ((TForm2*)ActiveMDIChild)->RichEdit1->SetFocus();
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PrintClick(TObject *Sender)
{
    if(PrintDialog1->Execute())
    { // 打印
        ((TForm2*)ActiveMDIChild)->RichEdit1->Print(((TForm2*)ActiveMDIChild)->FileName);
    }
}

//---------------------------------------------------------------------

⌨️ 快捷键说明

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