Как вызывать функции из файла. cpp с использованием CLR на основе GUI - PullRequest
0 голосов
/ 07 апреля 2020

Я разработал программу на C ++ с использованием библиотек OpenCV и ITK, которую я создал с помощью cmake. Программа (xxx. cpp file) имеет 3 класса и основную функцию (для ввода и отображения вывода).

После этого я создал приложение формы windows, чтобы получать данные от GUI. Он производит два файла MainForm.h и MainForm. cpp.

Я новичок в C ++ и никогда не создавал GUI приложений на C ++. Я хочу включить мой внешний файл. 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);

}

ххх. cpp файл:

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

1 Ответ

1 голос
/ 07 апреля 2020

Вам нужно разделить ваш xxx.cpp на файлы определения .h и .cpp реализации. Некоторые материалы о том, как это сделать:

Если у вас есть отдельный xxx.h файл, #include он в вас MainForm.h и вызов методов из xxx в pictureBox1_Click. Таким образом вы избежите повторяющихся ошибок определений.

...