943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 803
  • C++ RSS
Aug 9th, 2009
0

loading bitmaps

Expand Post »
I'm trying to load a bit map but the picture won't come up. It compiles just fine. I'm not sure if this makes a difference either but I am trying to load a bitmap into a dialog box. Here's the code:

The .cpp file:
C++ Syntax (Toggle Plain Text)
  1. #include "windows.h"
  2. #include "resource.h"
  3.  
  4. HBITMAP g_hbmHang = NULL;
  5.  
  6. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  7. {
  8. switch(Message)
  9. {
  10. case WM_CREATE:
  11. g_hbmHang = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_HANG));
  12. if(g_hbmHang == NULL)
  13. MessageBox(hwnd, "Could not load IDB_HANG!", "Error", MB_OK |MB_ICONEXCLAMATION);
  14. break;
  15. case WM_CLOSE:
  16. DestroyWindow(hwnd);
  17. break;
  18. case WM_PAINT:
  19. {
  20.  
  21. BITMAP bm;
  22. PAINTSTRUCT ps;
  23.  
  24. HDC hdc = BeginPaint(hwnd, &ps);
  25.  
  26. HDC hdcMem = CreateCompatibleDC(hdc);
  27. HBITMAP hbmOld = (HBITMAP)SelectObject (hdcMem, g_hbmHang);
  28.  
  29. GetObject(g_hbmHang, sizeof(bm), &bm);
  30.  
  31. BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  32.  
  33. SelectObject(hdcMem, hbmOld);
  34. DeleteDC(hdcMem);
  35.  
  36. EndPaint(hwnd, &ps);
  37. }
  38. break;
  39. case WM_DESTROY:
  40. DeleteObject(g_hbmHang);
  41. PostQuitMessage(0);
  42. break;
  43. default:
  44. return FALSE;
  45. }
  46. return TRUE;
  47. }
  48.  
  49. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  50. {
  51. return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
  52. }
The resource file:
C++ Syntax (Toggle Plain Text)
  1. //Microsoft Developer Studio generated resource script.
  2. //
  3. #include "resource.h"
  4.  
  5. #define APSTUDIO_READONLY_SYMBOLS
  6. /////////////////////////////////////////////////////////////////////////////
  7. //
  8. // Generated from the TEXTINCLUDE 2 resource.
  9. //
  10. #ifndef __BORLANDC__
  11. #include "winres.h"
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. #undef APSTUDIO_READONLY_SYMBOLS
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // English (Canada) resources
  19.  
  20. #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENC)
  21. #ifdef _WIN32
  22. LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_CAN
  23. #pragma code_page(1252)
  24. #endif //_WIN32
  25.  
  26. #ifdef APSTUDIO_INVOKED
  27. /////////////////////////////////////////////////////////////////////////////
  28. //
  29. // TEXTINCLUDE
  30. //
  31.  
  32. 1 TEXTINCLUDE DISCARDABLE
  33. BEGIN
  34. "resource.h\0"
  35. END
  36.  
  37. 2 TEXTINCLUDE DISCARDABLE
  38. BEGIN
  39. "#ifndef __BORLANDC__\r\n"
  40. "#include ""winres.h""\r\n"
  41. "#endif\r\n"
  42. "\0"
  43. END
  44.  
  45. 3 TEXTINCLUDE DISCARDABLE
  46. BEGIN
  47. "\r\n"
  48. "\0"
  49. END
  50.  
  51. #endif // APSTUDIO_INVOKED
  52.  
  53.  
  54.  
  55. IDD_MAIN DIALOG DISCARDABLE 0, 0, 207, 156
  56. STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
  57. CAPTION "Controls One"
  58. FONT 8, "MS Sans Serif"
  59. BEGIN
  60. LTEXT "Add",IDC_STATIC,7,10,14,8
  61. END
  62.  
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. //
  66. // Bitmap
  67. //
  68.  
  69. IDB_HANG BITMAP DISCARDABLE "hang.bmp"
  70. #endif // English (Canada) resources
  71. /////////////////////////////////////////////////////////////////////////////
  72.  
  73.  
  74.  
  75. #ifndef APSTUDIO_INVOKED
  76. /////////////////////////////////////////////////////////////////////////////
  77. //
  78. // Generated from the TEXTINCLUDE 3 resource.
  79. //
  80.  
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. #endif // not APSTUDIO_INVOKED
The resource.h file:
#define IDD_MAIN 100
#define IDB_HANG 101
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
[/code]
Similar Threads
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Aug 10th, 2009
0

Re: loading bitmaps

Download code from http://winprog.org/tutorial/ and compare it with your code.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 10th, 2009
0

Re: loading bitmaps

Hi,
why you've called LoadBitmap(...) in WM_CREATE message? I am sure your problem will be resolved if you change WM_CREATE to WM_INITDIALOG.
further you should learn and understand the difference between WM_CREATE & WM_INITDIALOG.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 10th, 2009
0

Re: loading bitmaps

Thanks that worked perfect.
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Aug 18th, 2009
0

Re: loading bitmaps

Hi goody11,

I'm trying to write a similar program where I can display an image in a dialogbox. I've posted a thread on how to do it....but no response from anyone.
http://www.daniweb.com/forums/thread210539.html


Now i'm trying to use your program, to see if I can extend it for my purpose. I'm getting a compile error "cannot open include file winres.h" Where does this file come from?

Thanks,
Amar
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amarucla is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: loading bitmaps

I downloaded winres.h. If you look in the resource file, and see where I include it, you can replace it with afxres.h instead.
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: static_cast on member void* pVar but how?
Next Thread in C++ Forum Timeline: Compile a Win32 program through CL compiler?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC