【发布时间】:2019-10-05 22:51:40
【问题描述】:
我需要在 file.h 中更改 PictureBox 的图像,我可以这样写:
mass[oldcord.first - 1, oldcord.second + 1]->Image = Image::FromFile("EMPTY.png");
但是如果我不是从文件夹中运行我的项目,我会得到一个错误,因为如果找不到图像,因为我不是从项目文件夹中执行它。我怎样才能使用不是来自我的表单,而是来自其他文件的“资源”:
this->MainField->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"Field")));
我希望我的应用程序从任何地方执行,而不仅仅是从我的项目文件夹中执行。
我尝试通过构造函数传递表单资源,但失败了
加法:
#pragma once
#include "MainForm.h"
#include <string>
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
enum TYPE { EMPTY, BLACK, WHITE };
ref class Figure
{
public:
Figure()
{
color = EMPTY;
active = 0;
king = 0;
}
Figure(int x, int y, TYPE color)
{
this->color = color;
this->color = color;
active = 0;
king = 0;
}
void MoveTo(pair<int,int> newcord, System::Windows::Forms::PictureBox^ current, TYPE turn) // ставит на место, меняет статус, цвет
{
current->Location = System::Drawing::Point(37 + newcord.second * 90, 37 + newcord.first * 90);
if (color == EMPTY)
color = turn;
else
color = EMPTY;
}
void make_king(System::Windows::Forms::PictureBox^% current, int i)
{
if (i == 0 && color == WHITE)
{
current->Image = Image::FromFile("WhiteKing.png");
king = true;
}
else if (i == 7 && color == BLACK)
{
current->Image = Image::FromFile("BlackKing.png");
king = true;
}
}
bool is_active()
{
return active;
}
void make_active(bool active)
{
this->active = active;
}
TYPE getColor()
{
return color;
}
void setColor(TYPE color)
{
this->color = color;
}
bool is_king()
{
return king;
}
private:
Graphics^ picture;
TYPE color;
bool active;
bool king;
};
【问题讨论】:
-
恐怕您已经编辑了您的问题并提供了更多信息!
L"Field"是什么意思?它看起来不像一个有效的表达式!但话说回来,也许我错了! -
“Field”是游戏场的图片,存储在资源中,“Empty.png”是存储在项目文件夹中的图片。而且我想使用资源中的所有图像或其他东西,但只能从任何地方运行我的项目,因为如果我从桌面运行它,我会收到错误,它找不到我的图像像“空”一样连接(使用图像 fromfile )
-
this->MainField->Image = gcnew Bitmap(Path::Combine(AppDomain::CurrentDomain->BaseDirectory, "EMPTY.png"));。位图必须在您的.exe路径中(例如,\x64\Debug)。对于资源对象,请参见例如:Accessing embedded resources in C++/CLI。 -
您能详细解释一下吗?例如“cell”类,在make_king函数中,我改变了图片。我已经另外发过了。
-
C++ IDE 从来没有让这件事变得容易,与 C# 和 VB.NET IDE 不同,基本假设是您将使用非托管资源,就像您通常对 C++ 应用程序所做的那样。如果您的 VS 版本足够旧,那么您仍然可以使用 Project > Properties > Add New Item > Resource > "Assembly Resource File"。如果你有一种令人毛骨悚然的感觉,那就是这比应该做的要难,而且你可能根本不应该这样做,那么你是对的。仅将 C++/CLI 用于与现有 C++ 代码的互操作。