在WPF对话框(使用C++/CLI)上显示(和更改)图像资源的最简单方法是什么?
我有一个C++/CLI GUI应用程序,我想显示一个图像作为视觉辅助,让用户看到他们在执行过程中的哪一步。每次用户选择新步骤时,都需要更改此图像 目前我正在使用一个图片框,并在运行时从磁盘加载了一个图像。所以这里有几件事我需要知道: 图片框是用于此目的的最佳工具还是有其他更适合的控件? 如何将图像嵌入到可执行文件中并从那里加载它们,而不是从磁盘上的文件加载它们。 我如何加载一个新的图像我猜如果我能破解点2,这将是相当直观的?在WPF对话框(使用C++/CLI)上显示(和更改)图像资源的最简单方法是什么?,wpf,image,dynamic,c++-cli,load,Wpf,Image,Dynamic,C++ Cli,Load,我有一个C++/CLI GUI应用程序,我想显示一个图像作为视觉辅助,让用户看到他们在执行过程中的哪一步。每次用户选择新步骤时,都需要更改此图像 目前我正在使用一个图片框,并在运行时从磁盘加载了一个图像。所以这里有几件事我需要知道: 图片框是用于此目的的最佳工具还是有其他更适合的控件? 如何将图像嵌入到可执行文件中并从那里加载它们,而不是从磁盘上的文件加载它们。 我如何加载一个新的图像我猜如果我能破解点2,这将是相当直观的? 我已经看到了一些与C相关的答案,但我还没有看到任何类似于在C++/CL
我已经看到了一些与C相关的答案,但我还没有看到任何类似于在C++/CLI应用程序中执行操作的答案。任何建议都是非常受欢迎的。好吧,这可能不是最好的解决方案,但下面的方法很有效 创建新的Windows窗体应用程序 将这些库添加到链接器设置项目属性->链接->输入->其他依赖项: 添加以下标题:
#include <windows.h>
#include "resource.h"
向资源中添加一对位图,并将其称为IDB_位图1和IDB_位图2
添加一个名为m_pictureBox1的图片框
添加按钮并双击该按钮以添加点击处理程序:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Remove any previously stored images
if(m_pictureBox1->Image != nullptr)
{
delete m_pictureBox1->Image;
}
// Pick a new bitmap
static int resource = IDB_BITMAP1;
if( resource == IDB_BITMAP2)
{
resource = IDB_BITMAP1;
}
else
{
resource = IDB_BITMAP2;
}
// Get the primary module
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
// Get the instance handle
IntPtr hinst = Marshal::GetHINSTANCE(mod);
// Get the bitmap as unmanaged
HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR);
// import the unmanaged bitmap into the managed side
Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));
// insert the bitmap into the picture box
m_pictureBox1->Image = bi;
// Free up the unmanaged bitmap
DeleteObject(hbi);
// Free up the instance and module
delete hinst;
delete mod;
}
位图在你的应用程序中被整齐地存储,每次点击按钮,图像就会交换。< /P>对不起乔恩,但是我是C++的新手,我想知道如何将文件添加到资源中,有没有我应该写的代码?在资源窗口上,右键单击并选择Addio> Realth->位图,然后点击导入…选择要导入的文件。
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Remove any previously stored images
if(m_pictureBox1->Image != nullptr)
{
delete m_pictureBox1->Image;
}
// Pick a new bitmap
static int resource = IDB_BITMAP1;
if( resource == IDB_BITMAP2)
{
resource = IDB_BITMAP1;
}
else
{
resource = IDB_BITMAP2;
}
// Get the primary module
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
// Get the instance handle
IntPtr hinst = Marshal::GetHINSTANCE(mod);
// Get the bitmap as unmanaged
HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR);
// import the unmanaged bitmap into the managed side
Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));
// insert the bitmap into the picture box
m_pictureBox1->Image = bi;
// Free up the unmanaged bitmap
DeleteObject(hbi);
// Free up the instance and module
delete hinst;
delete mod;
}