简体   繁体   English

保存并打开文件对话框

[英]Save and Open file Dialogs

I AM USING C++. 我正在使用C ++。 I am currently designing a program with windows forms. 我目前正在设计一个带有Windows窗体的程序。 I am having a problem writing event handlers. 我在编写事件处理程序时遇到问题。 especially the click event handler for save and open file. 特别是用于保存和打开文件的click事件处理程序。 I have searched the net and can find no decent explanation on how to write these event handlers. 我在网上搜索,找不到关于如何编写这些事件处理程序的正当解释。 So i am asking for a well defined definition. 因此,我要求一个定义明确的定义。

Please do not send me to Microsoft as they do not provide a sample of completed event handlers only partially. 请不要将我发送给Microsoft,因为他们不会仅部分提供完整的事件处理程序示例。

Since it's not obvious at a glance that Visual Studio 2012 does support C++/CLI Winform applications, even though the OP certainly knows this, for other readers, here's how I created one just to answer this question: 由于一眼就看不到Visual Studio 2012是否支持C ++ / CLI Winform应用程序,即使OP确实知道这一点,对于其他读者来说,我还是通过以下方式创建了该应用程序来回答此问题:

  1. In Visual Studio 2012, in the New Project dialog I selected [Visual C++ > CLR > CLR Empty Project]. 在Visual Studio 2012中,在“新建项目”对话框中,选择了[Visual C ++> CLR> CLR空项目]。

  2. In the new project I added an ordinary C++ main function and a Windows Form. 在新项目中,我添加了一个普通的C ++ main函数和一个Windows窗体。

  3. In the linker settings I changed it from console subsystem to GUI subsystem (and due to the non-standard behavior of Microsoft's linker, adjusted the entry point to mainCRTStartup ). 在链接器设置中,我将其从控制台子系统更改为GUI子系统(并且由于Microsoft链接器的非标准行为,因此将入口点调整为mainCRTStartup )。

That said, below is shown the generated Winform header, with just the event handler code, which fires up an Open File dialog, added manually. 就是说,下面显示了生成的Winform标头,其中仅包含事件处理程序代码,它会触发手动打开的“打开文件”对话框。

  • The long comment line ------ shows how to connect up an event handler. 长长的注释行------显示了如何连接事件处理程序。

  • The code at the bottom shows both how to code up an event handler, and how to run the open file dialog. 底部的代码显示了如何编写事件处理程序以及如何运行打开文件对话框。

#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" );
        }
    };
}

I didn't fix the indenting etc., since I didn't have the time; 我没有修复缩进等,因为我没有时间。 nearly all of this code was generated by Visual Studio 2012's RAD ( Rabid Application Development ) feature. 几乎所有这些代码都是由Visual Studio 2012的RAD( 狂暴应用程序开发 )功能生成的。


Related to this question's history, now removed: 与此问题的历史相关,现已删除:

I didn't really have the time to answer this question. 我真的没有时间回答这个问题。 I would much rather have let someone else answer it (and reap some reputation from it). 我宁愿让其他人回答它(并从中获得一些声誉)。 It is such a simple question that I'm sure if it wasn't closed so fast, any number of readers, with more time than I had, would have answered it. 这个问题很简单,我可以确定它的关闭速度是否如此之快,因此,任何读者,比我有更多的时间,都会回答这个问题。

But answering it, with code posted elsewhere, and forsaking some other matters, was the only way I saw to have it reopened. 但是回答它,在其他地方发布代码,放弃其他事情,是我看到重新打开它的唯一方法。

I sincerely ask every reader to please don't vote to close what you don't understand . 我真诚地要求每个读者不要投票来关闭你不理解的东西

Only vote to close a question when you thoroughly understand the subject matter and can prove to yourself and others that the community is better off with the question closed. 仅当您完全理解主题并可以向自己和他人证明关闭问题后社区会变得更好时,才投票关闭问题。

Failure to understand a question does not necessarily mean that the question is ungood. 不理解问题并不一定意味着问题是解精。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨平台原生打开/保存文件对话框 - cross platform native open/save file dialogs 跟踪开放的儿童对话 - Keeping track of open child dialogs 模态对话框不会作为最上面的窗口打开 - Modal Dialogs do not open as topmost window 用C ++打开/解码Zork保存文件(Quetzal格式) - Open/decode Zork save file (Quetzal format) in C++ 在 C++ 中打开/保存到二进制文件间歇性失败 - open/save to binary file in c++ fails intermittently MFC C++:如何将某个文件实际保存到系统,而不仅仅是打开另存为对话框 - MFC C++: How to actually save a certain file to the system, not just open a save as dialog box 如何在Windows通用文件对话框中显示API填充的虚拟文件夹 - How to display an API populated virtual folder in Windows Common File Dialogs 使用静态版本的Qt编译时,文件对话框崩溃 - File dialogs crash when compiling with static version of Qt 在VS 2010,C ++中使用“打开”时保存文件的默认路径 - The default path to save a file when you use “open” in VS 2010,C++ 如何使用FFMPEG打开文件\\实时流使用openCV编辑每个帧并保存为使用FFMPEGfile \\ live stream编码? - How to open file\live stream with FFMPEG edit each frame with openCV and save that as encoded with FFMPEGfile\live stream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM