Thread: Working with winAPI in classes (newbie question)

  1. #1
    Registered User
    Join Date
    Nov 2007

    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.

    class visual{
    	int left, top, width, height;
    	int typeID;
    	string typeName;
    	LRESULT CALLBACK create();
    	void destroy();
    LRESULT CALLBACK visual::create()
    	while (width == 0 && height == 0)
    		if (width > 0 && height > 0)
    			TEXT("DEFAULT PUSH BUTTON"),            
    			NULL );   
    	return 0;
    class defbutton: public visual{
    	typeName = "BUTTON";
    LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
    //add visual objects here
    defbutton button1;
    switch (Message)
        case WM_DESTROY:
            return 0;
    	case WM_CREATE:
    		button1.height = 150;
    		button1.width = 300;
            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
    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
    Farncombe, Surrey, England
    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.

    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
    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, 08:23 AM
  2. Another question on classes
    By hpy_gilmore8 in forum C++ Programming
    Replies: 26
    Last Post: 05-24-2003, 09:11 AM
  3. Passing classes question
    By Daggie in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 11:09 AM
  4. buttons (very lame newbie question)
    By myth in forum Windows Programming
    Replies: 1
    Last Post: 04-12-2002, 10:10 AM
  5. Newbie question
    By haus in forum C Programming
    Replies: 7
    Last Post: 02-03-2002, 11:58 AM