c++ - 从 Form2 显示 Form1

标签 c++ visual-c++ command-line-interface

我一直在尝试使 Form1Form2 中可见。在我的程序中发生的情况是,您按 form1 中的按钮隐藏 form1,然后 Form2 会弹出......但是当计时器在 Form2 上耗尽,它应该隐藏自己,而 Form1 应该弹出。 但是当我尝试运行时我得到了这个:

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form2.h(122): error C2039: 'Form1' : is not a member of 'Retaliation::Form2'

(Ps。报复是该计划的名称)。

这是我的代码(顶部有 #include "Form1.h")

   #pragma once
   #include "stdAfx.h"
   #include "Form1.h"

   namespace Retaliation {

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


/// <summary>
/// Summary for Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
public:
    int MoviePlay;

public: 
    int TickL;
    Form2(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form2()
    {
        if (components)
        {
            delete components;
        }
    }
private: AxWMPLib::AxWindowsMediaPlayer^  MoviePlayer1;
private: System::Windows::Forms::Timer^  timer1;
private: System::ComponentModel::IContainer^  components;
protected: 

protected: 

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>


   #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->components = (gcnew System::ComponentModel::Container());
        System::ComponentModel::ComponentResourceManager^  resources =  (gcnew System::ComponentModel::ComponentResourceManager(Form2::typeid));
        this->MoviePlayer1 = (gcnew AxWMPLib::AxWindowsMediaPlayer());
        this->timer1 = (gcnew System::Windows::Forms::Timer(this- >components));
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >   (this->MoviePlayer1))->BeginInit();
        this->SuspendLayout();
        // 
        // MoviePlayer1
        // 
        this->MoviePlayer1->Enabled = true;
        this->MoviePlayer1->Location = System::Drawing::Point(0, -1);
        this->MoviePlayer1->Name = L"MoviePlayer1";
        this->MoviePlayer1->OcxState =  (cli::safe_cast<System::Windows::Forms::AxHost::State^  >(resources- >GetObject(L"MoviePlayer1.OcxState")));
        this->MoviePlayer1->Size = System::Drawing::Size(860, 547);
        this->MoviePlayer1->TabIndex = 0;
        this->MoviePlayer1->Enter += gcnew System::EventHandler(this, &Form2::axWindowsMediaPlayer1_Enter);
        // 
        // timer1
        // 
        this->timer1->Interval = 1000;
        this->timer1->Tick += gcnew System::EventHandler(this, &Form2::timer1_Tick);
        // 
        // Form2
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(859, 495);
        this->Controls->Add(this->MoviePlayer1);
        this->Name = L"Form2";
        this->Text = L"Form2";
        this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->MoviePlayer1))->EndInit();
        this->ResumeLayout(false);

    }
   #pragma endregion
private: System::Void axWindowsMediaPlayer1_Enter(System::Object^  sender,     System::EventArgs^  e) {
         }
private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) {
             String^ Movie = File::ReadAllText("Movie.txt");
             if (Movie == "1")
             {

             MoviePlayer1 -> Ctlcontrols -> play();
             }

         }
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
             TickL += 1;
             if (Movie == "1")
             {
             if (TickL == 47)
             {
                 this -> timer1 -> Stop();
                 TickL = 0;
                 this -> Hide();
                 this -> Form1 -> Show();

             }
             }
         }
};
   }

最佳答案

您需要做的是在创建并显示 Form2 时传递对 Form1 的引用。然后,当 Form2 关闭时,它可以使用该引用再次显示 Form1

因此,将您的 Form2 更改为具有 private 成员变量来引用 Form1 并在构造函数中设置它

public ref class Form2 : public System::Windows::Forms::Form
{
private:
    Form1 ^m_pMainForm;

public: 
    Form2(Form1 ^pMainForm)
    {
        InitializeComponent();

        // save reference to main form
        m_pMainForm = pMainForm;
    }
    // ... etc.

然后更改 Form1 中创建 Form2 的代码以传递对其自身的引用

public SomeMethodInForm1()
{
    // create and show Form2
    Form2 ^pFormTwo = gcnew Form2(this);  // pass a reference to Form1
    pFormTwo->Show();
}

最后,在Form2中,您可以使用保存的引用重新显示Form1

public SomeMethodInForm2()
{
    // redisplay Form1
    m_pMainForm->Show();

    // ... close Form2 or whatever you want to do
}

根据类声明的顺序,您可能需要为其中一种表单添加前向声明,或者可能在 header 和定义之间适本地拆分代码,但这只是一个小细节。如果您仍然遇到困难,请发布另一个问题。

关于c++ - 从 Form2 显示 Form1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755850/

相关文章:

c# - 通过 CLI 从 Unity 生成 Visual Studio 项目

python - 当自定义命名空间为对象时,未设置默认 argparse 参数值

C++ weak_ptr创建性能

c++ - 是否允许标准迭代器操作抛出?

c++ - Blob 内数据的对齐和填充

node.js - 如何向 Vorpal.js 提供多词参数

C++ 日志记录类实例标识符

c++ - 使用 cvWarpPerspective 变形图像导致图像的某些部分超出可视区域

c++ - Visual Studio 2013 Ultimate - 缺少 C++ 项目模板

c++ - 我们可以创建一个 VC++ 可执行文件,它可以在 32 位和 64 位 Windows 上本地运行吗?