Thread: respond to button

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

    respond to button

    i'm trying to create a window that writes something different on the click of a button, but the problem is the window doesn't do anything.

    Code:
    #include <windows.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);
    }
    
    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;
             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);
             return 0;
        case WM_COMMAND:
             if (HIWORD(wParam) == BN_CLICKED)
             {
                if(LOWORD(wParam) == 1)
                {
                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, 100, "Dog Simulator 3", 15);
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    	
    }
                 
                if(LOWORD(wParam) == 2)
                {
                   SendMessage (GetParent((HWND)lParam),
                   WM_DESTROY, 0, 0);
                   }
                   }
                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);
    
    	EndPaint(hWnd, &Ps);
    	break;
        case WM_DESTROY:
    	PostQuitMessage(WM_QUIT);
    	break;
        default:
    	return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }
    I started out with nothing and I still have most of it left.

  2. #2
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    it's ok, i've done it
    I started out with nothing and I still have most of it left.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Ummmmmmmm, not meaning to be picky..........

    I hope you got all these errors.....

    If you want the variables in the callback to hold their values make them global or static. In C WIN32 I use an array/linked list of structures to hold these values.

    This is old..
    while(GetMessage(&Msg, NULL, 0, 0))

    Should now be
    while(GetMessage(&Msg, NULL, 0, 0)>0)// -ve return is now an error


    Don't call BeginPaint() except within the WM_PAINT handler, use this to call for a paint

    Code:
    RECT   Rect;
    GetClientRect(hWnd,&Rect); //get area to paint
    InvalidateRect(hWnd,&Rect,FALSE); //send paint msg, no background erase
    UpdateWindow(hWnd); //post directly to app msg que not OS msg que
    If not using a .NET version of MSVC (as .NET GDI objects are temp by default)
    A GDI object will/may not delete if selected into a DC. The DC may also not delete. These are memory leaks, bad ones.

    Test the handles for NULL before creating new objects in any code that may get called more than once.

    Try..
    Code:
    static HFONT   hFont=NULL;
    
    
    //create
    if(!hFont)
        //create font
    //when you select a GDI object into a DC the current GDI of that type is the return value
    HFONT   hOldFont = SelectObject(hDC, hFont);// catch current font
    
    //use DC
    
    SelectObject(hDC,hOldFont);// return DC to original state
    if(!DeleteObject(hFont))// clean up
       //error call GetLastError()
    else //good
       hFont=NULL;//reset handle

    Use FillRect() to erase any current text on the DC
    Last edited by novacain; 03-19-2005 at 10:31 PM.
    "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. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  2. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 02:15 AM
  3. Pressing a button works sometimes
    By johny145 in forum Windows Programming
    Replies: 14
    Last Post: 05-18-2005, 11:53 AM
  4. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  5. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM