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

问题描述 投票:0回答:1

我使用cmake构建的OpenCV和ITK库开发了C ++程序。程序(xxx.cpp文件)具有3个类和一个主要功能(用于输入和显示输出)。

之后,我创建了一个Windows窗体应用程序以从GUI接受输入。它产生两个文件MainForm.h和MainForm.cpp。

我是C ++的初学者,从未使用C ++创建GUI应用程序。我希望将我的外部.cpp文件包含在GUI中,以便单击按钮即可调用(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
}
c++ winforms include itk
1个回答
1
投票

您需要将xxx.cpp分为定义.h和实现.cpp文件。有关如何执行此操作的一些材料:

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

© www.soinside.com 2019 - 2024. All rights reserved.