我是使用Microsoft C ++ Microsoft Visual Studio的新手,我正在尝试弄清楚如何在Windows窗体中创建一个全局对象,以便稍后在几个函数中引用它。对象类型称为图片。下面是我到目前为止的代码。 注意:项目名称为Testing_ver2
GUIInterface.h
#pragma once
namespace Testing_ver2 {
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 GUIInterface
/// </summary>
public ref class GUIInterface : public System::Windows::Forms::Form
{
public:
GUIInterface(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~GUIInterface()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
//This Is were I'm trying to implement the picture
#include "Picture.h"
Picture pic1;
#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)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(611, 201);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(100, 50);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// GUIInterface
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(926, 546);
this->Controls->Add(this->pictureBox1);
this->Name = L"GUIInterface";
this->Text = L"GUIInterface";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
};
}
Picture.h
//This is used to hold the name of the picture, the path to it, and if it is picked in the viewer
#include <string>
class Picture
{
public:
Picture(){}; //default constructor
Picture(std::string newName, std::string newPath, bool newPicked)
{
strName = newName;
strPath = newPath;
boolPicked = newPicked;
}
void setName(std::string newName)
{
strName = newName;
}
void setPath(std::string newPath)
{
strPath = newPath;
}
void setPicked(bool newPicked)
{
boolPicked = newPicked;
}
std::string getName()
{
return strName;
}
std::string getPath()
{
return strPath;
}
bool getPicked()
{
return boolPicked;
}
private:
std::string strName;
std::string strPath;
bool boolPicked;
};
GUIInterface.cpp
#include "GUIInterface.h"
using namespace System;
using namespace System ::Windows::Forms;
[STAThread]
void main(array<String^>^ arg) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Testing_ver2::GUIInterface form;
Application::Run(%form);
}
上述代码的当前错误之一是托管类的成员不能是非托管类类型,这是由于&#34;图片pic1;&#34;在GUIInterface.h中,我试图创建一个对象调用pic1。
非常感谢任何帮助。