Classes and structures.

Hello,

I have a class with a structure inside it. I use the structure to create objects that contains coordinates and a HBITMAP which is loaded from the resource.

Then in my class I have function that displays the object on the screen:

void DrawIcon(HDC hdc, struct icon* icon);

What it does is that it draws the bitmap from the structure. But it doesn't seem to work.

My structure is defined in private along with the structure variables.

I know my code for displaying bitmaps is working, because I tested it outside the class.

I also tried to debug the class function and I do get all of the data from the pointer (the x- and y-coordinates are valid).

Any suggestions to solve this problem?

Regards,

Simon H.A.
Last edited on
Can we get you to post more code? There's very little to go by here.
Sure thing. Here is my class (the important part):

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
class flow_menu {

private:

	//This structure contains information about an icon object.
	struct icon {

		//Coordinates.
		DWORD X;
		DWORD Y;

		//Size.
		DWORD WIDTH;

		//Bitmap container.
		HBITMAP BMP;

		//Is the icon focused?
		BOOL FOCUS;

		//Is the icon clicked?
		BOOL CLICK;

		//Maximum and minimum size (for resizing).
		DWORD MAX;
		DWORD MIN;

		//Sizing in process?
		BOOL SIZING;

		//Up- or down-size?
		BOOL UPDOWN;

		//Standard Constructor.
		icon() {
			X = 0;
			Y = 0;
			WIDTH = 0;
			BMP = NULL;
			FOCUS = FALSE;
			CLICK = FALSE;
			MAX = 0;
			MIN = 0;
			SIZING = FALSE;
			UPDOWN = FALSE;
		}

		//Constructor that accepts inputs.
		icon (DWORD x_pos, DWORD y_pos, DWORD width, DWORD max_size, DWORD min_size, HBITMAP image) {			
			X = x_pos;
			Y = y_pos;
			WIDTH = width;
			MAX = max_size;
			MIN = min_size;
			BMP = image;
		}

	};

	//Main menu icons.
	icon MUSIC;
	icon VIDEO;
	icon SETTINGS;

public:

	//This function draws an icon object.
	void DrawIcon(HDC hdc, struct icon* icon);
	
	//Updates the flow menu.
	void UpdateMenu(HDC hdc);
};


Here is my function that draws the icons (HDC is from the WM_PAINT message handler):

1
2
3
4
5
6
7
8
//Updates the flow menu.
void flow_menu::UpdateMenu(HDC hdc) {

	DrawIcon(hdc, &MUSIC);
	DrawIcon(hdc, &VIDEO);
	DrawIcon(hdc, &SETTINGS);

}


This is the function that draws the icon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//This function draws an icon object.
void flow_menu::DrawIcon(HDC hdc, struct icon* icon) {

	BITMAP bm;
	HBITMAP hbmold;

	HDC hdcmem = CreateCompatibleDC(hdc);

	hbmold = (HBITMAP)SelectObject(hdcmem, icon->BMP);

	GetObject(icon->BMP, sizeof(bm), &bm);

	SetStretchBltMode(hdc, HALFTONE);

	//Draws bitmap according to the structure passed.
	StretchBlt(hdc, icon->X, icon->Y, icon->WIDTH, icon->WIDTH, hdcmem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);

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




Wow, this problem right here is why Inheritence is a good thing. Your 'icon' struct should be defined outside of your 'flow_control' class, which should then inherit the attributes from 'icon'. If you don't want icon to be an object that can exist outside of that class then you can do stuff like make the constructor for icon private.

Speaking of private data, "MUSIC", "VIDEO" and "SETTINGS" are all private, I do not see their "BMP" data members initialized to anything as 'flow_control' does not appear to have a constructor, and there does not appear to be a public function to change that. I'm wondering why you even have a second constructor for 'icon' since a.) it's in the private scope and b.) 'flow_control' does not have a constructor of its own.

Now that I think about it, I'm getting the impression that you forgot to include another class\struct that you are using to tie all of this together, is that the case?
Last edited on
I didn't include my constructor in the previous post but I do have one and it does initialize the objects with a valid HBITMAP.
I moved the structure outside of the class but it doesn't seem to do any difference.
I solved the problem. I took my structure outside the class and declared it public with the class. Then I had some problems with my constructor also, since I didn't initialize my HBITMAP correctly.
Everything works fine now.


Regards,

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