保存并打开文件对话框

Inf*_*ter 1 c++ event-handling visual-c++ winforms visual-studio-2012

我正在使用C++.我目前正在设计一个带窗体的程序.我在编写事件处理程序时遇到问题.特别是用于保存和打开文件的click事件处理程序.我在网上搜索过,找不到关于如何编写这些事件处理程序的正确解释.所以我要求一个定义明确的定义.

请不要将我发送给Microsoft,因为他们不会仅部分提供已完成事件处理程序的示例.

Che*_*Alf 5

由于Visual Studio 2012确实支持C++/CLI Winform应用程序并不明显,即使OP确实知道这一点,对于其他读者来说,这就是我创建一个只是为了回答这个问题的方法:

  1. 在Visual Studio 2012中,在"新建项目"对话框中,我选择了[Visual C++> CLR> CLR空项目].

  2. 在新项目中,我添加了一个普通的C++ main函数和一个Windows窗体.

  3. 在链接器设置中,我将其从控制台子系统更改为GUI子系统(并且由于Microsoft的链接器的非标准行为,将入口点调整为mainCRTStartup).

也就是说,下面显示了生成的Winform标题,只显示了事件处理程序代码,它触发了一个手动添加的"打开文件"对话框.

  • 长注释行------显示如何连接事件处理程序.

  • 底部的代码显示了如何编写事件处理程序以及如何运行打开文件对话框.

#pragma once

namespace CppWindowsFormsApplication {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MainWindow
    /// </summary>
    public ref class MainWindow : public System::Windows::Forms::Form
    {
    public:
        MainWindow(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MainWindow()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(149, 208);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"&Open file";
            this->button1->UseVisualStyleBackColor = true;

            // ---------------------------------------------------- !Adds event handler -------------------
            this->button1->Click += gcnew System::EventHandler(this, &MainWindow::button1_Click);
            // 
            // MainWindow
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->button1);
            this->Name = L"MainWindow";
            this->Text = L"MainWindow";
            this->ResumeLayout(false);

        }
#pragma endregion
    private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
        {
            OpenFileDialog^ sfd = gcnew OpenFileDialog();
            sfd->Filter = "Text Files|*.txt|All Files|*.*";
            if( sfd->ShowDialog() != System::Windows::Forms::DialogResult::OK )
            {
                return;
            }
            //MessageBox::Show( sfd->FileName );
            MessageBox::Show( "OK" );
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我没有修复缩进等,因为我没有时间; 几乎所有这些代码都是由Visual Studio 2012的RAD(Rabid应用程序开发)功能生成的.


与此问题的历史相关,现已删除:

我没有时间回答这个问题.我宁愿让其他人回答它(并从中获得一些声誉).这是一个如此简单的问题,我敢肯定,如果没有这么快就关闭,任何数量的读者,比我更多的时间,都会回答它.

但回答它,在其他地方张贴代码,并放弃其他一些事项,是我看到重新打开它的唯一方法.

我诚恳地请每位读者请不要投票支持你不理解的事情.

只有在您彻底了解主题时才投票才能结束问题,并且可以向您自己和其他人证明社区在关闭问题时会更好.

不理解问题并没有一定意味着问题是解精.