Menu

Help me with Switchs and Cases...

2002-12-11
2012-09-26
  • Nobody/Anonymous

    I have a bunch of very lame questions that i couldnt some yet with all my readings, so please, dont be cruel :)

    As i create new controls and functions on my program, i create new messages to deal with:

    For example, to deal with the buttons, i have to...

    case WM_COMMAND:
      switch(LOWORD(wParam)) {
        case ID_BUTTON01:
       
        case ID_BUTTON02:
     
      } 

    To deal with timers i have to...

    case WM_TIMER:
      switch(LOWORD(wParam)) {
        case IDT_TIMER1:         

        case IDT_TIMER2:

    }

    There are lots of cases...To deal lots of things. Ok enough with the preambule, the question are:

    a) Why do i see in lots of places people adding return 0; to the end of each case? Its not like weve declared an int function, so i return an integer?

    b) I also see codes where people use break; But break; will take us out of the switch statement if i could learn anything about standard C... If our message match a case, it will run the commands in that case and once our progrgam compares that message with all the other cases, if theres no match, nothing would happen anyway right? Or the message would be handled by defwindowproc, which is what would have to happen anyway. So why do i see people using this break; ? Do i have to use it for each case or just in some special case?

    c) That one is hard to exaplin using my poor english writting skills, so ill demonstrate with a example code:

    case WM_CREATE:
    create stuff;

    case WM_TIMER:
       switch(LOWORD(wParam)) {
          case IDT_TIMER1:                         
        do stuff;
            return 0;
         
          case IDT_TIMER2:                        
            do stuff;         
            return 0;    
         
           default:         
              return DefWindowProc (mainWindow, messages, wParam, lParam);
              return 0;         
    }

    default: 
       return DefWindowProc (mainWindow, messages, wParam, lParam);
       return 0;     

    So i mean, do i have to have a default case inside each switch to deal with the messages i have not specified actions? Or that would go to the last default case anyway?

    This whole message proc stuff is making me insane... since i added a tab control to my program, one of my listboxes (which was working perfect before that) started behaving strtange: It receives text from some procedures... when it  becomes full, normally, a Vscroll used to show up and so i could scroll it normally... Now when the scrollbar shows, it shows kind of defective, bad painted, the two arrows stay kind of invisible until i try to click it, then they show, its very weird. I know its something into all this switches and cases cause that the only place i changed in the code before that started to happen.

    Well Thanks, any help would be much appreciated.
    /Luke

     
    • Derek Baker

      Derek Baker - 2002-12-11

      a. We return 0 to indicate that the window procedure has sucessfully handled the message.

      b. We use break when we want to do something that applies to all cases within the switch statement.

      c. You only have to have one default case, but you can use default within one or more switches.

      Derek

       
      • Nobody/Anonymous

        Im sorry, i really could not understand the answer b.

        Let me try to guess: When our message is beeing compared to each case, if it matches one, that commands are executed and then our message is compared to the other cases after that one right? And if i add a break, the message will not be compared to the other cases because we are out of the switch. Is it this? 

         
    • Derek Baker

      Derek Baker - 2002-12-11

      Close. But even if what you are comparing does NOT match any following case, the code in them WILL still be executed, that is why we need break.

      Sorry, my original explanation was not complete enough. If you break, and there is code after the end of the switch, it will be executed

      Derek

       
      • Nobody/Anonymous

        Thanks Derek, that helped me a lot, things working better here now.

        Let me bore you again, and ask your opinion on something: Im still having that ListBox problem i was talking about in my first msg. I can only thing that for some reason, when windows feel the need to add a ScrollBar to this ListBox i am somehow intercepting this message in one of my cases and messing the whole thing. Do you agree that might be the reason or i better start searching for some other thing?

         
    • Derek Baker

      Derek Baker - 2002-12-11

      Sorry, but I'm learning Windows myself, and haven't got that far in the book!

      Try starting a new thread on that issue.

      Derek

       
    • Nobody/Anonymous

      No need to, i finally found out what the problem was, i was missing a break in the message switch, yes, thats lame :)
      Thanks for all the help Derek !

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.