| | |
GDI - hwnd and OOP
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi Guys,
I'm trying to write a simple game of drawing rectangles and circles in a window. I've created a different class for each shape: RectClass and CircClass. Each of these classes has it own DrawShape() function which suppose to draw the shapes in the window with given coordinates argument.
As far as I can tell drawing with GDI is accomplished using the InvalidateRect() function which sends the WM_PAINT message. And when handling the message, depend on some global parameter, you call a specific drawing function. Something like this:
However if I want to implement my idea like this, I need to use the hwnd handler within the class member functions (as shown in the code above). But the handler is created in the main program file and I do not have access to it in my classes.
Is there another way to draw using GDI with OOP?
Is there a way to get the HWND handle into the classes? I couldn't find a place in the main program to copy a HWND handle to the classes.
Thank you in advance,
GadiK
I'm trying to write a simple game of drawing rectangles and circles in a window. I've created a different class for each shape: RectClass and CircClass. Each of these classes has it own DrawShape() function which suppose to draw the shapes in the window with given coordinates argument.
As far as I can tell drawing with GDI is accomplished using the InvalidateRect() function which sends the WM_PAINT message. And when handling the message, depend on some global parameter, you call a specific drawing function. Something like this:
//global parameters
#define DRAW_RECT 1
#define DRAW_CIRC 2
int DrawMode;
//drawing functions of each class - dispatch the WM_PAINT nessage
RectClass::DrawShape(RECT rct)
{
DrawMode = DRAW_RECT;
InvalidateRect(hwnd, &rct, TRUE);
}
CircClass::DrawShape(RECT rct)
{
DrawMode = DRAW_CIRC;
InvalidateRect(hwnd, &rct, TRUE);
}
//in the main file
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
.
.
.
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
switch (DrawMode)
{
case DRAW_RECT:
DrawingARect(hdc, RectClassObj.getCoordinates());
break;
case DRAW_CIRC:
DrawingACircle(hdc, CircClassObj.getCoordinates());
break;
}
EndPaint(hWnd, &ps);
break;
.
.
.
}
}However if I want to implement my idea like this, I need to use the hwnd handler within the class member functions (as shown in the code above). But the handler is created in the main program file and I do not have access to it in my classes.
Is there another way to draw using GDI with OOP?
Is there a way to get the HWND handle into the classes? I couldn't find a place in the main program to copy a HWND handle to the classes.
Thank you in advance,
GadiK
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
pass it as a parameter to DrawingARect() and to the drawing functions in your class.
But I guess what really troubles me is the use of the global parameter DrawMode to distinguish between rectangles an circles. I think what I proposed is not good object oriented programming. I was actually hoping that someone would show me a different way to draw from within the class member functions. A way where each object could take control of the window and draw in it. Maybe there's some other command besides InvalidateRect() ?
You might use a pure virtual base class and a vector of classes. Something like this might work.
C++ Syntax (Toggle Plain Text)
class DrawingObject { ... virtual void Draw() = 0; // implemented by subclass }; class Circle : public DrawingObject { ... virtual Draw() ; // implenentation of base class }; class Rectangle : public DrawingObject { ... virtual Draw() ; // implenentation of base class }; // creation of this vector is not shown here vector<DrawingObject> theList; LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { . . . case WM_PAINT: hdc = BeginPaint(hWnd, &ps); for(int i = 0; i < theList.size(); i++) theList[i].Draw();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: please help me with this
- Next Thread: Help Request writing my first Do While
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






