Thread: button in function

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    button in function

    i've made a basic programme with buttons that does stuff. If you compile the below it will have no errors but when i run it and click on credits it doesn't go to the right function. It is supposed to go to the function startscrn() but it goes to dogname().

    Code:
    #include <windows.h>
    #include <stdlib.h>
    
    
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    HINSTANCE hInstGlobal;
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        hInstGlobal = hInstance;
        WNDCLASSEX  WndCls;
        static char szAppName[] = "Kazalio Studios";
        MSG         Msg;
    
        WndCls.cbSize        = sizeof(WndCls);
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
        WndCls.lpfnWndProc   = WindProcedure;
        WndCls.cbClsExtra    = 0;
        WndCls.cbWndExtra    = 0;
        WndCls.hInstance     = hInstance;
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndCls.lpszMenuName  = NULL;
        WndCls.lpszClassName = szAppName;
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
        RegisterClassEx(&WndCls);
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                       szAppName, "Dog Simulator 2",
                       WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                       CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
                       NULL, NULL, hInstance, NULL);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage( &Msg);
        }
    
        return static_cast<int>(Msg.wParam);
    }
    
    
    void dogname(HWND hWnd, WPARAM wParam, LPARAM lParam);
    void credits(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    void startscrn(HWND hWnd, WPARAM wParam, LPARAM lParam);
    
    
    
    
         
    
    
    
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
                                   WPARAM wParam, LPARAM lParam)
    {
        HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
    
        switch(Msg)
        {
                   
        case WM_CREATE:
             
             
             
             
    
             
             
             HWND hContinue, hExit, hCredits, hBack;
             hContinue = CreateWindow ( "Button", "Continue",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        10, 320, 140, 20,
                                        hWnd, (HMENU) 1,
                                        hInstGlobal, NULL );
             hExit = CreateWindow     ( "Button", "Exit",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        437, 320, 140, 20,
                                        hWnd, (HMENU) 2,
                                        hInstGlobal, NULL);
             hCredits = CreateWindow  ( "Button", "Credits",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        225, 320, 140, 20,
                                        hWnd, (HMENU) 3,
                                        hInstGlobal, NULL);
             
             return 0;
        case WM_COMMAND:
             if (HIWORD(wParam) == BN_CLICKED)
             {
                if(LOWORD(wParam) == 1)
                {
                                  DestroyWindow(hContinue);
                dogname(hWnd, wParam, lParam);
    	
    }
                 
                if(LOWORD(wParam) == 2)
                {
                   SendMessage (GetParent((HWND)lParam),
                   WM_DESTROY, 0, 0);
                   }
                if(LOWORD(wParam) == 3)
                {
                credits(hWnd, Msg, wParam, lParam);
                }
                }
                if(HIWORD(credits) == BN_CLICKED)
                {
                
                if(LOWORD(credits) == 4)
                {
                startscrn(hWnd, wParam, lParam);
                UpdateWindow(hWnd);
                }
                   }
                return 0;
                                
        case WM_PAINT:
             
             
             
    	hDC = BeginPaint(hWnd, &Ps);
    		
             font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
            
            font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "Welcome to Dog Simulator 2 the sequel to Dog Simulator 1 from Kazalio Studios.", 78);
            TextOut(hDC, 13, 135, "There are a few differences between dog Simulator 1 and Dog Simulator 2, the ", 77);
            TextOut(hDC, 13, 155, "main one being that Dog Simulator 2 is now in a window and has fonts and ",72);
            TextOut(hDC, 13, 175, "graphics to make it look better. There are also more things to do with your", 75);
            TextOut(hDC, 13, 195, "dog.", 4);
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    	break;
        case WM_DESTROY:
    	PostQuitMessage(WM_QUIT);
    	break;
        default:
    	return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }
    
    void startscrn (HWND hWnd, WPARAM wParam, LPARAM lParam)
    {
    HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
         
         font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
    		
             font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "First you must ghhfg a name for your dog.", 42);
            TextOut(hDC, 13, 135, "Names:", 6);
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    }
    
    void credits(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
        
         
         HDC hDC;
         PAINTSTRUCT Ps;
         HFONT font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
         hDC = GetDC (GetParent((HWND) lParam));
    		
             font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            
         
         font = CreateFont(18, 0, 0, 0,
                           FW_NORMAL, FALSE, FALSE, FALSE,
                           ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                           CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                           DEFAULT_PITCH | FF_ROMAN,
                           "Arial");
         
         SelectObject(hDC, font);
         TextOut(hDC, 13, 115, "Dog Simulator 1 and 2 was created by Kazalio Studios. The creators were:", 72);
         TextOut(hDC, 13, 135, "Programming: Alex Pajak", 23);
         TextOut(hDC, 13, 155, "Ideas/Design: Kasper Wilkosz", 28);
         
         DeleteObject(font);
         EndPaint(hWnd, &Ps);
         
         hWnd = (GetParent((HWND) lParam));
         HWND hBack;
         hBack = CreateWindow ( "Button", "Back",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        10, 320, 140, 20,
                                        hWnd, (HMENU) 4,
                                        hInstGlobal, NULL );
         switch(Msg)
         {
                                        
                                        
         case WM_COMMAND:
                                        
            if (HIWORD(credits) == BN_CLICKED)
             {                            
         if(LOWORD(credits) == 4)
                {
                startscrn(hWnd, wParam, lParam);
                UpdateWindow(hWnd);
                }
                }
                
                }
    }
    
    void dogname(HWND hWnd, WPARAM wParam, LPARAM lParam)
    {
         HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
         
         font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
    		
             font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "First you must choose a name for your dog.", 42);
            TextOut(hDC, 13, 135, "Name:", 5);
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    }
    I started out with nothing and I still have most of it left.

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    If you compile the below it will have no errors but when i run it and click on credits it doesn't go to the right function. It is supposed to go to the function startscrn() but it goes to dogname().
    I think your question should have been:

    "When I click on credits, it shows me the credits. However after that, when I click on the back button, it doesn't go to the right function. It is supposed to go to the function startscrn() but it goes to dogname()."

    I see a bunch of problems with your design. Firstly, the reason why it calls dogname() instead of startscrn() is because you put the back button at exact same position as the continue button. However, since the continue button was created first, the mouse will click on it instead of back.

    Secondly, if you minimize or move another window above your application, you'll see that everything goes back to the start screen.

    Thirdly, some of your code makes no sense at all. In the code below, credits is a function. Why you're getting the higher-order word of the function's address and comparing it to BN_CLICKED is something I don't understand.

    Code:
                if(HIWORD(credits) == BN_CLICKED)
                {
                
                if(LOWORD(credits) == 4)
                {
                startscrn(hWnd, wParam, lParam);
                UpdateWindow(hWnd);
                }
                   }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM