[英]What is the simplest way to display (and change) an image resource on a WPF dialog (using C++/CLI)?
I have a C++/CLI GUI application and I want to display an image as a visual aid for the user to see what step in a procedure they're at. 我有一个C ++ / CLI GUI应用程序,我想显示一个图像作为视觉辅助,以便用户查看他们所处程序的哪个步骤。 This image will need to be changed each time the user selects the new step.
每次用户选择新步骤时,都需要更改此图像。
Currently I'm using a picture box and have an image loaded from the disk at run time. 目前,我正在使用图片框,并在运行时从磁盘加载了图像。 So there are a few things I need to know here:
因此,在这里我需要了解几件事:
I've seen a few answers which relate to C# but I've not seen anything which looks like it translates to doing things in a C++/CLI app. 我已经看到了一些与C#相关的答案,但是还没有看到任何看起来像是在C ++ / CLI应用程序中执行操作的答案。 Any suggestions would be very welcome.
任何建议将非常欢迎。
Well it may not be the best solution, but the following works. 好吧,这可能不是最好的解决方案,但是以下方法可行。
Create a new Windows Forms Application
创建一个新的
Windows Forms Application
Add these libraries to your linker settings ( Project Proerties -> Link -> Input -> Additional Dependencies
): 将这些库添加到您的链接器设置(
Project Proerties -> Link -> Input -> Additional Dependencies
):
User32.lib Gdi32.lib
Add these headers: 添加以下标头:
#include <windows.h>
#include "resource.h"
Add these namespaces: 添加以下名称空间:
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
Add a pair of bitmaps to your resources and call them IDB_BITMAP1
and IDB_BITMAP2
. 在资源中添加一对位图,并将其称为
IDB_BITMAP1
和IDB_BITMAP2
。
Add a picture box called m_pictureBox1
. 添加一个名为
m_pictureBox1
的图片框。
Add a button and double-click the button to add an on-click handler: 添加一个按钮并双击该按钮以添加一个单击处理程序:
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;
}
..et voila the bitmaps are stored neatly in you app and each time you click the button the images will swap. ..et瞧,位图整齐地存储在您的应用程序中,每次单击该按钮时,图像将交换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.