我试图在主函数而不是表单的标题中实现界面控件。我正在尝试这样做,因为我必须使用多种表单,从另一个表单中调用一个表单,反之,在它们之间传输信息。 例如,我在 MyForm 中有 button1,在 MyForm 的 header 中我通过代码对其进行操作:
私有(private):System::Void button1_Click(System::Object^ 发送者,System::EventArgs^ e) {
//东西
}
我如何在主函数中执行相同的操作?我应该包含什么,要使用什么命名空间,以及代码是什么样子?我的主 .cpp 现在看起来是这样:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int main(array < String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(0);
Project1::MyForm form;
Application::Run(%form);
}
谢谢!
最佳答案
C++/CLI 仍然是 C++ 而不是 C#:您可以在 .cpp 中而不是在类定义中实现表单的方法。这不是向导为您做的事情。
//MyForm.h
namespace Project1
{
class MyForm
{
//...
//Somewhere in the MyForm class definition
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
};
}
在cpp中:
//MyForm.cpp
using namespace System;
namespace Project1
{
Void MyForm::button1_Click(Object^ sender, EventArgs^ e) {
//stuff
}
}//namespace Project1
对于在多个表单之间进行通信的问题,您可以选择更多文档/ View (或模型/ View / Controller )方法,方法是将您的状态放入一个共享对象中,该对象在修改时提供事件,并让您的表单进行监听当状态改变时这些事件会自动更新。
关于winforms - C++/CLI : How to control WinForms from main function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34615920/