Problem in displaying two bitmaps

Hi everyone,
I have coded a program to display two bitmaps on a window.One is displayed successfully but the other is not can you help me with this?

Here is my code:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HBITMAP logo;
logo = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
HDC logoB;
logoB = CreateCompatibleDC(hdc);
SelectObject(logoB, logo);
BitBlt(hdc, 0, 0, 4100, 4100, logoB, 10, 10, SRCCOPY);
DeleteDC(logoB);
DeleteObject(logo);
EndPaint(hWnd, &ps);
HBITMAP design;
design = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
HDC designb;
PAINTSTRUCT pas;
HDC hfc = BeginPaint(hWnd, &pas);
designb = CreateCompatibleDC(hfc);
SelectObject(designb, design);
BitBlt(hfc, 0, 0, 4100, 4100, designb, 100, 10, SRCCOPY);
DeleteDC(designb);
DeleteObject(design);
EndPaint(hWnd, &pas);
break;
//---------------------------
}
Try to paint both bitmaps in the same BeginPaint EndPaint block.
Here is a little example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include "Resource.h"

TCHAR szClsName[] = _T("BasicApp");
TCHAR szWndName[] = _T("BasicApp");

static HINSTANCE g_hInstance; 

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
ATOM RegisterMyClass();

void DrawBitmap(HDC hDC, HBITMAP hBitmap, int left, int top, int width, int height);


ATOM RegisterMyClass()
{
  WNDCLASSEX WndClsEx;

	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = WndProcedure;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.lpszMenuName  = NULL;
	WndClsEx.lpszClassName = _T(szClsName);
	WndClsEx.hInstance     = g_hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

  return RegisterClassEx(&WndClsEx);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  MSG        Msg;
	HWND       hWnd;
  
  g_hInstance = hInstance;
  
	if (!RegisterMyClass())
	{
	  MessageBox(0, _T("RegisterClassEx failed"), _T("Error"), MB_OK);
    return FALSE;
  }

	hWnd = CreateWindow(szClsName, szWndName, WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT, 1000, 
                      700, 0, 0, g_hInstance, 0);

	if (!hWnd)
	{
	  MessageBox(0, _T("CreateWindow failed"), _T("Error"), MB_OK);
    return FALSE;
  }
  
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	while (GetMessage(&Msg, NULL, 0, 0))
	{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
	}

	return (int)Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  HBITMAP hBitmap;
  HDC hDC;
  PAINTSTRUCT ps;
  
  switch (Msg)
  {
    case WM_COMMAND:
      MessageBox(hWnd, "Hello World", "Hello", MB_OK);
      break;
    case WM_PAINT:
      hDC = BeginPaint(hWnd, &ps);
      // first bitmap
      hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
      _ASSERTE(hBitmap != NULL);
      DrawBitmap(hDC, hBitmap, 0, 0, 480, 640);
      DeleteObject(hBitmap);
      // second bitmap
      hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
      _ASSERTE(hBitmap != NULL);
      DrawBitmap(hDC, hBitmap, 500, 0, 480, 640);
      EndPaint(hWnd, &ps);
      DeleteObject(hBitmap);
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
  }
  return DefWindowProc(hWnd, Msg, wParam, lParam);
}

void DrawBitmap(HDC hDC, HBITMAP hBitmap, int left, int top, int width, int height)
{
  HDC hMemDC; 
  
  _ASSERTE(hDC != NULL);
  _ASSERTE(hBitmap != NULL);
  _ASSERTE(width > 0);
  _ASSERTE(height > 0);
  
  hMemDC = CreateCompatibleDC(hDC);
  SelectObject(hMemDC, hBitmap);
  BitBlt(hDC, left, top, width, height, hMemDC, 0, 0, SRCCOPY);
  DeleteDC(hMemDC);
}
Last edited on
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

HBITMAP logo;
logo = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
HDC logoB;
logoB = CreateCompatibleDC(hdc);
SelectObject(logoB, logo);
BitBlt(hdc, 0, 0, 4100, 4100, logoB, 10, 10, SRCCOPY);
DeleteDC(logoB);
DeleteObject(logo);
EndPaint(hWnd, &ps);
logo = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
logoB = CreateCompatibleDC(hdc);
SelectObject(logoB, logo);
BitBlt(hdc, 1000, 1000, 4100, 4100, logoB, 100, 100, SRCCOPY);
DeleteDC(logoB);
DeleteObject(logo);
EndPaint(hWnd, &ps);
break;
//---------------------------
}

Tried like this but does not displays the other bitmap.Can anyone help?

Thank you
closed account (E0p9LyTq)
You still have TWO EndPaint() functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case WM_PAINT:
{
   PAINTSTRUCT ps;
   HDC hdc = BeginPaint(hWnd, &ps);

   HBITMAP logo;
   logo = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
   HDC logoB;
   logoB = CreateCompatibleDC(hdc);
   SelectObject(logoB, logo);
   BitBlt(hdc, 0, 0, 4100, 4100, logoB, 10, 10, SRCCOPY);
   DeleteDC(logoB);
   DeleteObject(logo);
   EndPaint(hWnd, &ps); /// DELETE THIS ONE!
   logo = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
   logoB = CreateCompatibleDC(hdc);
   SelectObject(logoB, logo);
   BitBlt(hdc, 1000, 1000, 4100, 4100, logoB, 100, 100, SRCCOPY);
   DeleteDC(logoB);
   DeleteObject(logo);
   EndPaint(hWnd, &ps);
   break;
   //---------------------------
}


PLEASE learn to use code tags so reading your code is easier..
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.