Painting bitmaps in multiple classes

Hello forum,

Once again I'm facing a problem that is very annoying.

I have multiple classes. For example I have made a class for the custom border I've made.
Another one is for some navigation in the window.

My problem is, that I can't draw bitmaps in both classes. If the painting procedure for the first class starts it works just fine, but I get no output from the other class. If I swap those classes the first class will not work, but the other class will work just fine.

Here is drawing procedure which is the same in both classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void window::draw_bitmap(HBITMAP bmp, HDC hdc, int x_pos, int y_pos) { //Draws a bitmap.

	HDC hdcmem;
	BITMAP bm;
	HBITMAP hbmold;

	hdcmem = CreateCompatibleDC(hdc);
	hbmold = (HBITMAP)SelectObject(hdcmem, bmp);
	GetObject(bmp, sizeof(bm), &bm);
	BitBlt(hdc, x_pos, y_pos, bm.bmWidth, bm.bmHeight, hdcmem, 0, 0, SRCCOPY); 

	SelectObject(hdcmem,hbmold);
	DeleteDC(hdcmem);

}


And here is how I do the painting of the different buttons/icons (All in bitmaps):
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
void window::border_paint() { //This function paints the buttons on the border according to the check procedure.

	PAINTSTRUCT paint;
	HDC hdc = BeginPaint(GetActiveWindow(),&paint);

	draw_bitmap(hbm[0], hdc, 0, 0); //Redraws the border. 
	draw_bitmap(hbm[1], hdc, window_x-9, 0);

	if(!buttons[0]) //Green button.
		draw_bitmap(hbm[2], hdc, 1180-diff, 1);
	else
		draw_bitmap(hbm[3], hdc, 1180-diff, 1);

	if(!buttons[1]) //Yellow button.
		draw_bitmap(hbm[4], hdc, 1210-diff, 1);
	else
		draw_bitmap(hbm[5], hdc, 1210-diff, 1);

	if(!buttons[2]) //Red button.
		draw_bitmap(hbm[6], hdc, 1240-diff, 1);
	else
		draw_bitmap(hbm[7], hdc, 1240-diff, 1);

	EndPaint(GetActiveWindow(), &paint);

}


In the class constructor I define the different bitmaps, so they will only be defined once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
window::window() {

	hbm[0] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_BORDER));
	hbm[1] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_BORDER_END));
	hbm[2] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_MINIMIZE_OFF));	
	hbm[3] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_MINIMIZE_ON));	
	hbm[4] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_RESTORE_OFF));	
	hbm[5] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_RESTORE_ON));	
	hbm[6] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_X_OFF));	
	hbm[7] = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ID_X_ON));		
	buttons[0] = 0;
	buttons[1] = 0;
	buttons[2] = 0;
	size = 1;
	diff = 0;
	window_x = 0;
	window_y = 0;
	x = 0;
	y = 0;
}


Regards,

Simon H.A.

Can I assume that the paint sequence function void window::border_paint() is being called in
response to a WM_PAINT message.
I ask this question because BeginPaint and EndPaint functions should only be used in response to a WM_PAINT message.
Yes,
When WM_PAINT message occurs, I call void window::border_paint() for repainting border, and another one which is called void media_player_navigation::paint() to repaint the graphics in the window.

1
2
3
4
5
6
	case WM_PAINT:

		SetFocus(hwnd);
		window.border_paint(); 
		media_navigation.paint();
		break;
Last edited on
Well you will have to make an adjustment, you can't call the BeginPaint and EndPaint functions
more than once in response to WM_PAINT message (well you can call them but as you see, the first call to EndPaint has re-validated the DC and you can't get any further painting done).

You wiil have to change the paint routines to take a HDC parameter - For 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
void window::border_paint(HDC hdc) 
{ //This function paints the buttons on the border according to the check procedure.

	//PAINTSTRUCT paint;
	//HDC hdc = BeginPaint(GetActiveWindow(),&paint);

	draw_bitmap(hbm[0], hdc, 0, 0); //Redraws the border. 
	draw_bitmap(hbm[1], hdc, window_x-9, 0);

	if(!buttons[0]) //Green button.
		draw_bitmap(hbm[2], hdc, 1180-diff, 1);
	else
		draw_bitmap(hbm[3], hdc, 1180-diff, 1);

	if(!buttons[1]) //Yellow button.
		draw_bitmap(hbm[4], hdc, 1210-diff, 1);
	else
		draw_bitmap(hbm[5], hdc, 1210-diff, 1);

	if(!buttons[2]) //Red button.
		draw_bitmap(hbm[6], hdc, 1240-diff, 1);
	else
		draw_bitmap(hbm[7], hdc, 1240-diff, 1);

	//EndPaint(GetActiveWindow(), &paint);
 }


and modify the paint message to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Windows_procedure(......)
{
    //some variables
     HDC hDC;
    PAINTSTRUCT paint;


case WM_PAINT:

    SetFocus(hwnd);
    
    hDC = BeginPaint(hwnd,&paint)

    window.border_paint(hDC); 
    media_navigation.paint(hDC);
    
    EndPaint(hwnd, &paint)
    break


//other stuff 


Last edited on
That worked.

Thanks,

Simon H.A.
Topic archived. No new replies allowed.