EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Bitmaps

Bitmap is a file format to store image pixels in terms of bits in a file. It is a 2D array of bits (4bit, 8 bit, 16bit, 24bit) to store a 2D image. A graphical application can use a bitmap in tool-bars, buttons, menus and many other places. Image editor applications can create, manipulate (scale, scroll, rotate, and paint), and store images as files on a disk. Win32 or Linux graphical subsystem reads these bits one by one and creates pixels in the screen. Thus the entire picture is formed in the screen.

LoadBitmap

The LoadBitmap function loads the specified bitmap resource from a module's executable file. This function has been superseded by the LoadImage function.

HBITMAP LoadBitmap(
  HINSTANCE hInstance,  /* handle to application instance */
  LPCTSTR lpBitmapName  /* address of bitmap resource name */
);

Parameters

  • hInstance - Handle to the instance of the module whose executable file contains the bitmap to be loaded.
  • lpBitmapName - Pointer to a null-terminated string that contains the name of the bitmap resource to be loaded. Alternatively, this parameter can consist of the resource identifier in the low-order word and zero in the high-order word. The MAKEINTRESOURCE macro can be used to create this value.

Return Values

If the function succeeds, the return value is the handle to the specified bitmap. If the function fails, the return value is NULL.

Loading steps:

  • Call LoadBitmap with the resource file string name
  • Create a new device context which is compatible to current Window DC
  • Select this bitmap to this newly created memory device context
  • Copy bits from source of new device context to window device context
  • Free bitmap resource using DeleteObject
  • Free this new DC by calling DeleteDC

Source Code

#include <windows.h>
#define BMP_WIDTH 100
#define BMP_HEIGHT 40
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
                         WPARAM wParam,
10                           LPARAM lParam);
11 
12  int APIENTRY WinMain(HINSTANCE hInstance,
13                       HINSTANCE hPrevInstance,
14                       LPSTR     lpCmdLine,
15                       int       nCmdShow)
16  {
17    WNDCLASS wc;
18    MSG msg;
19    HWND hWnd;
20    ZeroMemory(&wc, sizeof(WNDCLASS));
21    hInst = hInstance;
22    wc.style           = CS_HREDRAW | CS_VREDRAW;
23    wc.lpfnWndProc     = (WNDPROC)WndProc;
24    wc.hInstance       = hInstance;
25    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
26    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
27    wc.lpszClassName   = (LPCTSTR)"MyWin32Class";
28 
29 
30    RegisterClass(&wc);
31 
32     hWnd = CreateWindow("MyBitmapWnd", "Bitmap loading demo!!", WS_OVERLAPPEDWINDOW,
33        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
34 
35    ShowWindow(hWnd, nCmdShow);
36    UpdateWindow(hWnd);
37    while (GetMessage(&msg, NULL, 0, 0)) 
38    {
39      DispatchMessage(&msg);
40    }
41    return 0;
42  }
43 
44  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
45  {
46    PAINTSTRUCT ps;
47    HDC hdc, hmemdc;
48    HBITMAP hbitmap;
49    switch (message) 
50    {
51      case WM_PAINT:
52      {
53        hdc = BeginPaint(hWnd, &ps);
54        GetClientRect(hWnd, &rt);
55        hbitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAPLOGO));
56        hmemdc = CreateCompatibleDC(hdc);
57        SelectObject(hmemdc, hbitmap);
58        BitBlt(hdc, (rt.right - rt.left)/2  - BMP_WIDTH/2,
59              (rt.bottom - rt.top)/2  - BMP_HEIGHT/2,
60               BMP_WIDTH, BMP_HEIGHT, hmemdc, 0, 0, SRCCOPY);
61        DeleteObject(bitmap);
62        DeleteDC(memdc);
63        EndPaint(hWnd, &ps);
64        break;
65      }
66      case WM_DESTROY:
67        PostQuitMessage(0);
68        break;
69      default:
70        return DefWindowProc(hWnd, message, wParam, lParam);
71     }
72     return 0;
73  }

Output

LoadBitmap example demo application

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 19:25:27(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.