那麼它可能不是最好的解決方案,但下面的作品。
創建一個新的Windows Forms Application
添加這些庫的鏈接器設置(Project Proerties -> Link -> Input -> Additional Dependencies
):
User32.lib Gdi32.lib
添加這些標題:
#include <windows.h>
#include "resource.h"
添加這些命名空間:
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
將一對位圖添加到您的資源中,並將它們稱爲IDB_BITMAP1
和IDB_BITMAP2
。
添加一個名爲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;
}
..et瞧的位圖整齊地存放在你的應用程序,並每次單擊該按鈕的圖像將交換。
對不起喬恩,但我是新來的c + +,我想知道如何將文件添加到資源,有沒有我應該寫的代碼? – 2012-10-26 17:07:07
在'Resources'窗口中,右鍵點擊並選擇'Add' - >'Resource' - >'Bitmap',然後點擊'Import ...'來選擇要導入的文件。 – 2012-10-29 10:28:24