Thread: Windows Logic

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Windows Logic

    I'm just lightly delving into windows programming.
    Not exactly sure what the difference is between the windows aPI and MFC (Though I hear the MFC is quite old and it sounds to me like another layer on top of the WinAPI).
    Anyway the problem is this I am testing for the result of a message box and executing a conditional statement depending on the result, problem is the condition always seems to result to true. Anyway here's the portion of code that does that. Is there anything wrong with the logic??
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    int answer; //stores the result of the message box
        switch(msg)
        {
            case WM_MBUTTONDOWN:
            case WM_LBUTTONDOWN:
            {
                char szFileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);
                GetModuleFileName(hInstance, szFileName, MAX_PATH);
                FlashWindow(hwnd, TRUE);
                answer = MessageBox(hwnd, "Exit Applications Test Zone??", "Quit", MB_YESNO | MB_ICONINFORMATION);
                if (answer = IDYES) DestroyWindow(hwnd); //Always seems to result to IDYES.... Why??           
            };  
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    if (answer = IDYES)
    Probably the most common programming mistake. Replace = with ==
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Honestly if there's one thing I hate about programming it the minor thngs that hinder such as this.... Arghh
    Thanx Magos
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I read in a book once that there's a simple way to avoid that mistake, just test the condition backwards:

    Code:
    if ( IDYES == answer )
    If you accidentally use '=' instead of '==', the compiler won't compile the following:

    Code:
    if ( IDYES = answer )
    Since IDYES is a numeric constant, you can't assign stuff to it.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The latest versions (.NET) of MSVC pick the single = sign error up.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM