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

📄 commondialogs.cpp

📁 C++ Source code from a tutorial
💻 CPP
字号:
#include "stdafx.h"

#using <mscorlib.dll>
#using <System.DLL>
#using <System.Windows.Forms.DLL>
#using <System.Drawing.DLL>

#include <tchar.h>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::IO;

__gc class MyFormClass : public Form {
public:
    Button *fontbtn;
    RichTextBox *rich;
    void AddControls() {
        Width = 400;
        Height = 400;

        Button *colorbtn = new Button;
        colorbtn->Text = "Color";
        colorbtn->Left = 25;
        colorbtn->Top = 25;
        colorbtn->Click += new EventHandler(
            this, MyFormClass::ColorClick);
        fontbtn = new Button;
        fontbtn->Text = "Font";
        fontbtn->Left = 25;
        fontbtn->Top = 60;
        fontbtn->Click += new EventHandler(
            this, MyFormClass::FontClick);

        Button *openbtn = new Button;
        openbtn->Text = "Open";
        openbtn->Left = 25;
        openbtn->Top = 95;
        openbtn->Click += new EventHandler(
            this, MyFormClass::OpenClick);

        Button *savebtn = new Button;
        savebtn->Text = "Save";
        savebtn->Left = 25;
        savebtn->Top = 130;
        savebtn->Click += new EventHandler(
            this, MyFormClass::SaveClick);

        rich = new RichTextBox();
        rich->Left = 110;
        rich->Top = 25;
        rich->Width = 220;
        rich->Height = 250;

        Control* controls[] = {colorbtn, fontbtn,
            openbtn, savebtn, rich};
        this->Controls->AddRange(controls);

    }
    void ColorClick(Object* sender, EventArgs* e)
    {
        ColorDialog *cd = new ColorDialog();
        cd->Color = this->BackColor;
        if (cd->ShowDialog() == DialogResult::OK) {
            this->BackColor = cd->Color;
        }
    }
    void FontClick(Object* sender, EventArgs* e) {
        FontDialog *fd = new FontDialog();
        fd->Font = fontbtn->Font;
        if (fd->ShowDialog() == DialogResult::OK) {
            fontbtn->Font = fd->Font;
        }
    }
    void OpenClick(Object* sender, EventArgs* e) {
        OpenFileDialog *od = new OpenFileDialog();
        od->DefaultExt = "*.txt";
        od->Filter = "Text files(*.txt)|"
            "*.txt|RTF Files(*.rtf)|"
            "*.rtf|All files (*.*)|*.*";
        if (od->ShowDialog() == DialogResult::OK) {
            String *ext = Path::GetExtension(od->FileName);
            ext = ext->ToUpper();
            if (ext->CompareTo(".RTF") == 0) {
                rich->LoadFile(od->FileName, 
                    RichTextBoxStreamType::RichText);
            }
            else {
                rich->LoadFile(od->FileName, 
                    RichTextBoxStreamType::PlainText);
            }
        }
    }
    void SaveClick(Object* sender, EventArgs* e) {
        SaveFileDialog *sd = new SaveFileDialog();
        
        sd->DefaultExt = "*.txt";
        sd->Filter = "Text files(*.txt)|"
            "*.txt|RTF Files(*.rtf)|"
            "*.rtf|All files (*.*)|*.*";
        if (sd->ShowDialog() == DialogResult::OK) {
            String *ext = Path::GetExtension(sd->FileName);
            ext = ext->ToUpper();
            if (ext->CompareTo(".RTF") == 0) {
                rich->SaveFile(sd->FileName, 
                    RichTextBoxStreamType::RichText);
            }
            else {
                rich->SaveFile(sd->FileName, 
                    RichTextBoxStreamType::PlainText);
            }
        }
    }
};
int _tmain(void) {
    MyFormClass *MyForm = new MyFormClass();
    MyForm->AddControls();
    MyForm->ShowDialog();
    return 0;
}

⌨️ 快捷键说明

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