【发布时间】:2014-08-22 04:37:13
【问题描述】:
我有一个问题,很抱歉这个新手问题(只是 2 周的 Visual c++ 编程经验)。我目前正在使用 Visual C++2010 中的 MDI,这是程序的工作原理,一旦您执行应用程序,MDIParent 会以最大化状态加载,然后 MDIParent 具有 MenuStrip 并且其子菜单之一被命名为 noviceToolStripMenuItem。如果使用鼠标按钮单击 noviceToolStripMenuItem,它将打开一个名为 frmNovice 的子窗体,这很好,现在 frmNovice 包含一个简单的游戏,根据它的预期功能运行,一旦游戏结束,一个名为 frmRetry 的新窗体将被显示,但这次 frmRetry 不是子窗体,frmNovice 将关闭。 frmRetry 有两个按钮,是和否,如果用户单击否,frmRetry 将关闭,然后 MDIParent 将再次保持应用程序的焦点,但如果用户单击是按钮,应用程序应执行单击事件/功能noviceToolStripMenuItem,再次显示 frmNovice,但这是问题所在,我无法完成此操作。 frmRetry(不是子窗体)调用或调用MDIParent 的noviceToolStripMenuItem 的点击事件。有没有办法做到这一点,或者其他解决方案?提前致谢。
这是代码(有些被删除以最小化空间):
**FILE: mdiMain.h**
#pragma once
#include "frmNovice.h"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ noviceToolStripMenuItem;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->selectLevelToolStripMenuItem = (gcnew
System::Windows::Forms::ToolStripMenuItem());
this->noviceToolStripMenuItem = (gcnew
this->selectLevelToolStripMenuItem->Name = L"selectLevelToolStripMenuItem";
this->selectLevelToolStripMenuItem->Size = System::Drawing::Size(70, 20);
this->selectLevelToolStripMenuItem->Text = L"New &Game";
//
// noviceToolStripMenuItem
//
}
#pragma endregion
private: System::Void noviceToolStripMenuItem_Click(System::Object^ sen....) {
//OPENING NEW FORM AS MDI CHILD
frmNovice^ newMDIChild = gcnew frmNovice();
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
};
}
文件:mdiMain.cpp
#include "stdafx.h"
#include "mdiMain.h"
using namespace MemoryGame;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
**FILE: frmNovice.h**
#pragma once
#include "frmRetry.h"
#include "algorithm"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmNovice : public System::Windows::Forms::Form
{
public:
frmNovice(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~frmNovice()
{
if (components)
{
delete components;
}
}
//codes for the UI of the game goes here.....
#pragma region Windows Form Designer generated code
//MY USER DEFINED FUNCTION START
void gamefunction() //this function is where calls the frmRetry to show
{
frmRetry^ form = gcnew frmRetry();
form->Show(); //This time not a CHILD FORM
this->Close();
}
void InitializeComponent(void)
{
//initialize UI components GOES HERE
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//DIFFERENT PROCEDURE FUNCTIONS GOES HERE Also invokes/calls the game function that
//show the frmRetry (this is working properly)
};
}
文件:frmNovice.cpp
#include "StdAfx.h"
#include "frmNovice.h"
//there are variables used for gameplay
int Choice1;
int Choice2;
int Mistakes;
int TimeRecord;
int AllItems;
最后 文件:frmRetry.h
#pragma once
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmRetry : public System::Windows::Forms::Form
{
`enter code here`public:
frmRetry(void)
{
InitializeComponent();
}
protected:
~frmRetry()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ btnYes;
private: System::Windows::Forms::Button^ btnNo;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
CODES for UI inialization goes here
}
#pragma endregion
private: System::Void btnNo_Click(System::Object^ .....e) {
this->Close();
}
private: System::Void btnYes_Click(System::Object.....^ e) {
**THIS IS THE PART IM STuck WITH, i CANT GET THIS DONE**
**EVErything is working fine, except for this part where i have to
call again the frmNovice as an child form of the mdiParent**
THE following are my attempts but didnt work.
// frmRetry^ form = gcnew frmRetry();
// frmNovice^ frmNovice=gcnew frmNovice();
// noviceToolStripMenuItem->PerformClick();
//frmNovice->Show();
}
};
}
文件:frmRetry.cpp
#include "StdAfx.h"
#include "frmRetry.h"
提前致谢
【问题讨论】:
-
使用 ShowDialog() 代替。
-
我已经做了 dat,但它与 Show() 的工作原理相同,并且它们具有相同的行为
标签: c++ visual-studio visual-c++ c++-cli