Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello,is it possible to add bitmap(image) in dialog box(or GUI) which i created using create window in visual c++ express 2010.if so can you tell,the steps to do.

Actually I added the bitmap file in the resources( By right clicking on resouces -> Add -> Existing Item -> .bmp file) but it doesn't show me an ID for that bitmap file. I think, If I have that ID I can load the bitmap file into GUI programatically.

Thanks in advance
Anil
Posted
Updated 15-Sep-11 22:25pm
v3

You can load images from files too:
C++
  HBITMAP  hbmp = (HBITMAP)LoadImage(0,__T("myimage.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  // use the bitmap handle and finally destroy it
  if(hbmp) DeleteObject(hbmp);

Good luck.
 
Share this answer
 
Comments
Member 9500147 5-Nov-12 2:15am    
The picture control i have created in resource file.I want to add an image on a picture control from a file. And also i want to link two dialog boxes using picture control. I am stuck over here please help me guys ..!
thnx in advance .
mbue 5-Nov-12 7:41am    
Little example to apply the bitmap to the control:


#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// multibyte or unicode
#include <tchar.h>

// needed libraries
#pragma comment (lib,"user32.lib")
#pragma comment (lib,"gdi32.lib")

// 32 vs. 64 bit
#ifdef _WIN64
#define SET_DWL_USER(H,P) SetWindowLongPtr(H,DWLP_USER,(LONG_PTR)P)
#define GET_DWL_USER(H) GetWindowLongPtr(H,DWLP_USER)
#else
#define SET_DWL_USER(H,P) SetWindowLong(H,DWL_USER,(long)P)
#define GET_DWL_USER(H) GetWindowLong(H,DWL_USER)
#endif

/////////
// simple dialog class
class CMyDlg
{
public:
CMyDlg(HINSTANCE h);
~CMyDlg();

int DoModal(const int idd);

private:
static INT_PTR FAR PASCAL __dlg(HWND h,unsigned int m,WPARAM w,LPARAM l);

HINSTANCE _hinst;
HBITMAP _hbmp;
};

// dialog message handler
INT_PTR FAR PASCAL CMyDlg::__dlg(HWND h,unsigned int m,WPARAM w,LPARAM l)
{
CMyDlg* This = (CMyDlg*)GET_DWL_USER(h);
switch(m)
{
case WM_INITDIALOG:
{
// attach my this pointer to the dialog
This = (CMyDlg*)l; SET_DWL_USER(h,This);

// delete if already loaded
if(This->_hbmp) DeleteObject(This->_hbmp);
// load the image from file
This->_hbmp = (HBITMAP)LoadImage(0,__T("myimage.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(This->_hbmp)
{
HWND hctrl;

hctrl = GetDlgItem(h,101);
if(IsWindow(hctrl))
{
// attach image to the control
SendMessage(hctrl,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)This->_hbmp);
}
hctrl = GetDlgItem(h,102);
if(IsWindow(hctrl))
{
// attach image to another control
SendMessage(hctrl,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)This->_hbmp);
}
}
}
break;
case WM_COMMAND:
switch(LOWORD(w))
{
// end the dialog and quit
case IDOK: EndDialog(h,LOWORD(w)); break;
case IDCANCEL: EndDialog(h,LOWORD(w)); break;
}
break;
}
return 0;
}

CMyDlg::CMyDlg(HINSTANCE h)
{
_hinst = h;
_hbmp = 0;
}

CMyDlg::~CMyDlg()
{
// finally delete object
if(_hbmp) DeleteObject(_hbmp);
}

int CMyDlg::DoModal(const int idd)
{
HRSRC hres = FindResource(_hinst,MAKEINTRESOURCE(idd),RT_DIALOG);
HGLOBAL hdlg = hres ? LoadResource(_hinst,hres):0;
// find resource handle and show dialog here
return hdlg?(int)DialogBoxIndirectParam(_hinst,(LPCDLGTEMPLATE)LockResource(hdlg),(HWND)0,__dlg,(LONG_PTR)this):0;
}

/////////
// program starts here
int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
CMyDlg dlg(h);
return dlg.DoModal(100);
}
CBitmap bmpHello;
bmpHello.LoadBitmap( IDB_HELLO );

// Calculate bitmap size using a BITMAP structure.
BITMAP bm;
bmpHello.GetObject( sizeof(BITMAP), &bm );

// Create a memory DC, select the bitmap into the
// memory DC, and BitBlt it into the view.
CDC dcMem;
dcMem.CreateCompatibleDC( pDC );
CBitmap* pbmpOld = dcMem.SelectObject( &bmpHello );
pDC->BitBlt( 10,10, bm.bmWidth, bm.bmHeight,
&dcMem, 0,0, SRCCOPY );

// Reselect the original bitmap into the memory DC.
dcMem.SelectObject( pbmpOld );
 
Share this answer
 
Comments
Richard MacCutchan 15-Sep-11 8:02am    
C++ Express does not include MFC.
I assume you are talking about Win32, a traditional Windows program.

In a dialog I would simply add a picture control from the toolbox to the dialog and fill in the image. Look here to get a start with dialogs:

Win32 dialogs
[^]

In a normal Window you must do the painting yourself. For that you will need to process the WM_PAINT Message[^] and then load and paint your Image with help of the GDI (graphics device interface)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900