在WPF对话框(使用C ++ / CLI)上显示(和更改)图像资源的最简单的方法是什么? [英] What is the simplest way to display (and change) an image resource on a WPF dialog (using C++/CLI)?

查看:86
本文介绍了在WPF对话框(使用C ++ / CLI)上显示(和更改)图像资源的最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ / CLI GUI应用程序,我想显示一个图像作为一个视觉帮助,用户看到他们在一个过程中的步骤。每次用户选择新步骤时,都需要更改此图片。



目前我正在使用图片框,并且在运行时从磁盘加载图片。所以这里有一些我需要知道的东西:


  1. 图片框是最好用的东西,


  2. 如何在可执行文件中嵌入图片,并从中加载图片,而不是存在于磁盘上的文件。如何加载新图片(如果我可以破解第2点,我猜想这将是相当愚蠢的)?

已经看到了几个答案涉及到C#,但我没有看到任何东西,它看起来像翻译为在C / C / CLI应用程序做事情。任何建议都是非常欢迎的。

解决方案

这可能不是最好的解决方案,

创建一个新的 Windows窗体应用程序



将这些库添加到链接器设置( Project Proerties - > Link - > Input - > Additional Dependencies ):

  User32.lib Gdi32.lib 

添加以下标题:

  #include< windows.h> 
#includeresource.h

添加以下命名空间:

 使用命名空间System :: Reflection; 
using namespace System :: Runtime :: InteropServices;

向您的资源添加一对位图,并将其调用 IDB_BITMAP1 IDB_BITMAP2



添加一个名为 m_pictureBox1



添加一个按钮并双击该按钮添加一个点击处理程序:

  System :: Void button1_Click(System :: Object ^ sender,System :: EventArgs ^ e)
{
//删除任何先前存储的图像
if(m_pictureBox1-> Image!= nullptr)
{
delete m_pictureBox1-> Image;
}

//选择一个新的位图
static int resource = IDB_BITMAP1;
if(resource == IDB_BITMAP2)
{
resource = IDB_BITMAP1;
}
else
{
resource = IDB_BITMAP2;
}

//获取主模块
模块^ mod = Assembly :: GetExecutingAssembly() - > GetModules()[0]

//获取实例句柄
IntPtr hinst = Marshal :: GetHINSTANCE(mod);

//将位图获取为非托管
HANDLE hbi = LoadImage((HINSTANCE)hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR);

//将非管理位图导入到管理端
Bitmap ^ bi = Bitmap :: FromHbitmap(IntPtr(hbi));

//将位图插入到图片框中
m_pictureBox1-> Image = bi;

//释放非托管位图
DeleteObject(hbi);

//释放实例和模块
delete hinst;
delete mod;
}

.. et voila的位图被整齐地存储在你的应用程序,单击图像将交换的按钮。


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. 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:

  1. Is a picture box the best thing to use for this purpose or is there another control that would better suit?
  2. How do embed the images in the executable and load them from there instead of a file that exists on disk.
  3. How do I load a new image (I'm guessing that this will be fairly obvois if I can crack point 2)?

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. Any suggestions would be very welcome.

解决方案

Well it may not be the best solution, but the following works.

Create a new Windows Forms Application

Add these libraries to your linker settings (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.

Add a picture box called 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.

这篇关于在WPF对话框(使用C ++ / CLI)上显示(和更改)图像资源的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆