简体   繁体   English

在WPF对话框(使用C ++ / CLI)上显示(和更改)图像资源的最简单方法是什么?

[英]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: 因此,在这里我需要了解几件事:

  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)? 如何加载新图像(我猜想如果可以破解第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. 我已经看到了一些与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_BITMAP1IDB_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM