如何使用基于CLR的GUI从.cpp文件调用函数



我使用OpenCV和ITK库开发了一个C++程序,并使用cmake构建了该程序。该程序(xxx.cpp文件(有3个类和一个主函数(用于获取输入和显示输出(。

之后,我创建了一个windows表单应用程序,从GUI中获取输入。它生成两个文件MainForm.h和MainForm.cpp.

我是C++的初学者,从未用C++创建过GUI应用程序。我想在GUI中包含我的外部.cpp文件,这样点击按钮就可以从(xxx.cpp(文件中调用函数。我遵循了此链接中描述的步骤:https://mcn-www.jwu.ac.jp/~ yokamoto/openwww/vsg/OpenCV/

问题是我找不到从外部xxx.cpp文件调用函数的解决方案。我试图将代码复制到MainForm.cpp,但出现了错误。请指导我遵循正确的程序。谢谢

MainForm.h文件:

#pragma once
#include "MainForm.cpp"
namespace GUIProj {
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 MainForm
/// </summary>
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^  button1;
protected:
protected:
protected:
private: System::Windows::Forms::Button^  button2;
private: AxWMPLib::AxWindowsMediaPlayer^  axWindowsMediaPlayer1;
private: AxWMPLib::AxWindowsMediaPlayer^  axWindowsMediaPlayer2;
private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
private: System::Windows::Forms::OpenFileDialog^  openFileDialog2;
private: System::Windows::Forms::PictureBox^  pictureBox1;
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)
{
System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainForm::typeid));
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->openFileDialog2 = (gcnew System::Windows::Forms::OpenFileDialog());
this->axWindowsMediaPlayer2 = (gcnew AxWMPLib::AxWindowsMediaPlayer());
this->axWindowsMediaPlayer1 = (gcnew AxWMPLib::AxWindowsMediaPlayer());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->axWindowsMediaPlayer2))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->axWindowsMediaPlayer1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
this->SuspendLayout();
// 
// button1
// 
this->button1->Location = System::Drawing::Point(105, 257);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Video 1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
// 
// button2
// 
this->button2->Location = System::Drawing::Point(381, 257);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Video 2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MainForm::button2_Click);
// 
// openFileDialog1
// 
this->openFileDialog1->FileName = L"openFileDialog1";
// 
// openFileDialog2
// 
this->openFileDialog2->FileName = L"openFileDialog2";
// 
// axWindowsMediaPlayer2
// 
this->axWindowsMediaPlayer2->Enabled = true;
this->axWindowsMediaPlayer2->Location = System::Drawing::Point(287, 29);
this->axWindowsMediaPlayer2->Name = L"axWindowsMediaPlayer2";
this->axWindowsMediaPlayer2->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^>(resources->GetObject(L"axWindowsMediaPlayer2.OcxState")));
this->axWindowsMediaPlayer2->Size = System::Drawing::Size(265, 201);
this->axWindowsMediaPlayer2->TabIndex = 3;
// 
// axWindowsMediaPlayer1
// 
this->axWindowsMediaPlayer1->Enabled = true;
this->axWindowsMediaPlayer1->Location = System::Drawing::Point(12, 29);
this->axWindowsMediaPlayer1->Name = L"axWindowsMediaPlayer1";
this->axWindowsMediaPlayer1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^>(resources->GetObject(L"axWindowsMediaPlayer1.OcxState")));
this->axWindowsMediaPlayer1->Size = System::Drawing::Size(257, 201);
this->axWindowsMediaPlayer1->TabIndex = 2;
// 
// pictureBox1
// 
this->pictureBox1->Location = System::Drawing::Point(649, 29);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(219, 409);
this->pictureBox1->TabIndex = 4;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew System::EventHandler(this, &MainForm::pictureBox1_Click);
// 
// MainForm
// 
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(928, 419);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->axWindowsMediaPlayer2);
this->Controls->Add(this->axWindowsMediaPlayer1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"MainForm";
this->Text = L"MainForm";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->axWindowsMediaPlayer2))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->axWindowsMediaPlayer1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
openFileDialog1->ShowDialog();
axWindowsMediaPlayer1->URL = openFileDialog1->FileName;
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
openFileDialog2->ShowDialog();
axWindowsMediaPlayer2->URL = openFileDialog1->FileName;
}
private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {       
}
};
}

MainForm.cpp文件:

#include "MainForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]

void Main(array<String^>^ args)
{
Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); GUIProj::MainForm form; Application::Run(%form);
}

xxx.cpp文件:

class A{
// methods
}
class B{
//methods
}
class C{
//methods
}
int main{
// take inputs and call methods of above classes
}

您需要将xxx.cpp拆分为定义.h和实现.cpp文件。关于如何做到这一点的一些材料:

  • https://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/
  • http://www.math.uaa.alaska.edu/~afkjm/csce211/讲义/SeparateCompilation.pdf

一旦您有了一个单独的xxx.h文件,#include就在MainForm.h中,并在pictureBox1_Click中从xxx调用方法。这样可以避免重复的定义错误。

最新更新