RE: Classes in Windows programming?
OOPS, Okay I figured out that I wasn't setting HDC and RECT to something
before using it....
Thankyou all anyways!
:-)
--
Best regards
Robert
"Robby" wrote:
Hello,
Please consider the following code:
=====================================================
class Cat
{
public:
Cat(int initialAge); //Constructor
~Cat(); //Destructor
int getAge();
void setAge(int age);
void Meow(HDC hdc,RECT rect);
private:
int itsAge;
};
//Constructor
Cat::Cat(int initialAge)
{
itsAge = initialAge;
}
//Destructor
Cat::~Cat()
{}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
RECT rect;
case WM_CREATE:
//Instantiate classes
Cat Frisky(11); //Constructor
...Other code
return 0;
//I want my paint handler to fetch these Cat methods
case WM_PAINT:
Frisky.setAge(5);
Frisky.Meow(hdc,rect);
...other code
return 0
===================================================
Based on the code above, I get the following error:
C:\_DTS_PROGRAMMING\_EVC_Examples\eVC_for_PLC\EVC_PLC\WndProc.cpp(168) :
error C2360: initialization of 'Frisky' is skipped by 'case' label
This error basically says:
"The initialization of identifier can be skipped in a switch statement. You
cannot jump past a declaration with an initializer unless the declaration is
enclosed in a block. (Unless it is declared within a block, the variable is
within scope until the end of the switch statement.)"
So if I tried to move the "Cat Frisky(11);" like so:
=================================================
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
RECT rect;
Cat Frisky(11);
================================================
But this gave me the following two errors:
C:\_DTS_PROGRAMMING\_EVC_Examples\eVC_for_PLC\EVC_PLC\WndProc.cpp(181) :
warning C4700: local variable 'rect' used without having been initialized
C:\_DTS_PROGRAMMING\_EVC_Examples\eVC_for_PLC\EVC_PLC\WndProc.cpp(181) :
warning C4700: local variable 'hdc' used without having been initialized
And if I declare RECT and HDC as static, the app would compile however then
Frisky.Meow() would not display appropriately... further... it would not
display at all, here is the method:
void Cat::Meow(HDC hdc,RECT rect)
{
TCHAR szBuffer[2];
wsprintf(szBuffer,TEXT("%d"),itsAge);
DrawText (hdc,szBuffer,-1, &rect,
DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
DrawText (hdc,TEXT ("Message in Method"),-1, &rect,
DT_CENTER | DT_TOP | DT_SINGLELINE);
}
This is my first attempt of combining C++ with Win32 API's (Windows
programming)
I am just trying to underdtand the basic rule of thumb of, where I am
supposed to innitiate my class objects and so that I can use its methods in
my windows handlers?
All suggestions appreciated.....
Thanking you guys in advance!
--
Kind regards
Robert