簡體   English   中英

如何使用基於 CLR 的 GUI 從.cpp 文件調用函數

[英]How to call functions from .cpp file using CLR based GUI

我使用 OpenCV 和 ITK 庫開發了 C++ 程序,這些庫是我使用 cmake 構建的。 該程序(xxx.cpp 文件)有 3 個類和一個主 function(接受輸入並顯示輸出)。

之后,我創建了一個 windows 表單應用程序以從 GUI 獲取輸入。 它生成兩個文件 MainForm.h 和 MainForm.cpp。

我是 C++ 的初學者,從未在 C++ 中創建 GUI 應用程序。 我想在 GUI 中包含我的 external.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文件。 關於如何做到這一點的一些材料:

一旦你有一個單獨的xxx.h文件, #include它在你的MainForm.h中並從pictureBox1_Click中的xxx調用方法。 這樣可以避免重復的定義錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM