Working with winAPI in classes (newbie question)

This is a discussion on Working with winAPI in classes (newbie question) within the Windows Programming forums, part of the Platform Specific Boards category; I'll just give you some of my code for simplicity's sake. I'm creating classes for winAPI controls. Code: class visual{ ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Working with winAPI in classes (newbie question)

    I'll just give you some of my code for simplicity's sake. I'm creating classes for winAPI controls.

    Code:
    class visual{
    public:
    	int left, top, width, height;
    	int typeID;
    	string typeName;
    	LRESULT CALLBACK create();
    	void destroy();
    	visual();
    	~visual();
    };
    
    //
    
    LRESULT CALLBACK visual::create()
    {
    	while (width == 0 && height == 0)
    	{
    		if (width > 0 && height > 0)
    		{
    			CreateWindowEx(0,                                     
    			TEXT(typeName.c_str()),                         
    			TEXT("DEFAULT PUSH BUTTON"),            
    			WS_CHILD|WS_VISIBLE|typeID,   
    			left,                                     
    			top,                                
    			width,                               
    			height,                                     
    			hwnd,                                   
    			NULL,                                   
    			0,                              
    			NULL );   
    		}
    	}
    	return 0;
    }
    
    //
    
    class defbutton: public visual{
    public:
    	defbutton();
    	~defbutton();
    };
    
    //
    
    defbutton::defbutton()
    {
    	typeID = BS_DEFPUSHBUTTON;
    	typeName = "BUTTON";
    }
    
    //
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
    //add visual objects here
    defbutton button1;
    //
    
    switch (Message)
        {
        case WM_DESTROY:
            PostQuitMessage(0); 
            return 0;
    
    	case WM_CREATE:
    		button1.create();
    		button1.height = 150;
    		button1.width = 300;
    		break;
    	
        default:
            return DefWindowProc(hwnd,Message,wParam,lParam);  //let system deal with msg
        }
    }
    For some reason the button will not show up when I run the program, even though button.create() is supposed to create one. Any thoughts?

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Does it freeze/hang? I wouldn't expect visual::create() to return since it will get stuck in an infinite loop waiting for width & height to no longer be 0. I see what the attempt was, but it won't work that way. What you should do instead is pass width, height, etc as parameters to visual::create(). Also, as a side note, for typeName, I would use win32's predefined type names, the one for button is WC_BUTTON (i.e. "typeName = WC_BUTTON")

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    CALLBACK is used to indicate that a particular function is called back from the Windowing subsystem. It should not be used with your create() function.

    as @anthony says, if the width and height is zero, it would probably just start the create function, which enters an endless loop. Since both create() and WndProc() runs in the same thread, it never gets to the width/height setting. And of course, it would still be a terrible race-condition to manage to get into the loop and get inside the if, and not be caught by the while-loop exiting. if you set only one of height and width to something other than zero, the while loop will exit. If you manage to get BOTH the height and width to non-zero JUST in the split between the while-loop and the if-statement, it will enter the if-statement. But chances of this are slim indeed.

    Is there any particular reason you don't want to set the height and width either as you construct the button, or before you call create() in your WM_CREATE?

    I'm afraid to say that it looks like your grasp of programming and teh concept of what happens when and how is a bit vague - perhaps you should spend a little more time programming console applications - I'm not saying this to discourage you from a future career in programming or anything like that, but Windows API is not trivial, and there are many thing that depend on a full understanding of "how things hang together" both when it comes to execution/parrallelism in the code, and how calls affect each other, endless loops and such.

    Edit: By the way, it seems like the width/height is undefined (not intialized in the constructor and the button is a local variable in WndProc() which means that the content of it is undefined), so the application may actually never enter the while loop at all - and it would just fall out of create, then you set the width/height and leave the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    Yea sorry, I'm used to creating console apps and for some reason I was thinking that visual.create() and LRESULT CALLBACK WndProc() were running in parallel to eachother. Problem solved, my bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on use of classes
    By Flyer in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2003, 09:23 AM
  2. Another question on classes
    By hpy_gilmore8 in forum C++ Programming
    Replies: 26
    Last Post: 05-24-2003, 10:11 AM
  3. Passing classes question
    By Daggie in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:09 PM
  4. buttons (very lame newbie question)
    By myth in forum Windows Programming
    Replies: 1
    Last Post: 04-12-2002, 11:10 AM
  5. Newbie question
    By haus in forum C Programming
    Replies: 7
    Last Post: 02-03-2002, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21