Thread: Button Problem Please Help

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Unhappy Button Problem Please Help

    I want one of my buttons to toggle between On and Off, i mean when i click on On it must turn Off when i click on Off it must turn on, how can i do that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe state which OS/Compiler/GUI Toolkit you're using?

    Or maybe post the code you have to draw the button in the first place.

    http://www.catb.org/~esr/faqs/smart-questions.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Talking

    Dev C++, Windows

    I want it to toggle between "Durdur" and "Calistir"
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK PencereProseduru (HWND, UINT, WPARAM, LPARAM);
    
    
    char YapiSinifAdi[ ] = "WindowsApp";
    HWND DugmeTutmaci;
    HMENU Yavru1;
    int WINAPI WinMain (HINSTANCE AnaFonksiyonTutmaci, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    
    {
        HWND AnaPencereTutmaci;           
        MSG Mesajlar;                     
        WNDCLASSEX PencereYapisi;   
    
        PencereYapisi.hInstance = AnaFonksiyonTutmaci;
        PencereYapisi.lpszClassName = YapiSinifAdi;
        PencereYapisi.lpfnWndProc = PencereProseduru;      
        PencereYapisi.style = CS_DBLCLKS;                 
        PencereYapisi.cbSize = sizeof (WNDCLASSEX);
    
        PencereYapisi.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        PencereYapisi.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        PencereYapisi.hCursor = LoadCursor (NULL, IDC_ARROW);
        PencereYapisi.lpszMenuName = NULL;             
        PencereYapisi.cbClsExtra = 0;                
        PencereYapisi.cbWndExtra = 0;            
        
        PencereYapisi.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&PencereYapisi))
            return 0;
    
        AnaPencereTutmaci = CreateWindowEx (
               0,                   
               YapiSinifAdi,       
               "Windows App",       
               WS_OVERLAPPEDWINDOW, 
               CW_USEDEFAULT,      
               CW_USEDEFAULT,      
               544,                 
               375,                 
               HWND_DESKTOP,        
               NULL,                
               AnaFonksiyonTutmaci, 
               NULL               
               );
        DugmeTutmaci = CreateWindowEx (
               0,
               "button",
               "Durdur",
               WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
               1,      
               1,       
               170,      
               30,   
               AnaPencereTutmaci,
               (HMENU)1,
               AnaFonksiyonTutmaci,
               NULL
               );
               
        ShowWindow (AnaPencereTutmaci, nFunsterStil);
    
        while (GetMessage (&Mesajlar, NULL, 0, 0))
        {
            TranslateMessage(&Mesajlar);
    
            DispatchMessage(&Mesajlar);
        }
    
        return Mesajlar.wParam;
    }
    
    LRESULT CALLBACK PencereProseduru (HWND AnaPencereTutmaci, UINT message, WPARAM wParam, LPARAM lParam)
    {   
            LPSTR Icerik;
        switch (message)         
        {   case WM_COMMAND:
                 if(LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED)
                        {
                           GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+1);
                           if (Icerik == "Durdur")
                           {SetWindowText (DugmeTutmaci,"Calistir");
                           break;}
                           else
                           {SetWindowText (DugmeTutmaci, "Durdur");
                           break;}
                        }
                        break;                              
            case WM_DESTROY:
                PostQuitMessage (0);  
                break;
            default:  
                return DefWindowProc (AnaPencereTutmaci, message, wParam, lParam);
        }
    
        return 0;
    }
    Last edited by hileci555; 07-10-2008 at 01:17 PM. Reason: added code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > LPSTR Icerik;
    This needs to be a STR (specifically an array of them), not merely an uninitialised pointer.

    > GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+1);
    Be honest about the size of things you're referring to. Adding 1 is unlikely to be a good idea.

    > if (Icerik == "Durdur")
    Use strcmp() to compare strings.
    Unless of course you've compiled your code with UNICODE enabled (I don't think dev-c++ defaults to unicode at the moment), so you may be OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Unhappy

    Thanks for the reply here is the working code:
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK PencereProseduru (HWND, UINT, WPARAM, LPARAM);
    
    
    char YapiSinifAdi[ ] = "WindowsApp";
    HWND DugmeTutmaci;
    HMENU Yavru1;
    int WINAPI WinMain (HINSTANCE AnaFonksiyonTutmaci, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    
    {
        HWND AnaPencereTutmaci;           
        MSG Mesajlar;                     
        WNDCLASSEX PencereYapisi;   
    
        PencereYapisi.hInstance = AnaFonksiyonTutmaci;
        PencereYapisi.lpszClassName = YapiSinifAdi;
        PencereYapisi.lpfnWndProc = PencereProseduru;      
        PencereYapisi.style = CS_DBLCLKS;                 
        PencereYapisi.cbSize = sizeof (WNDCLASSEX);
    
        PencereYapisi.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        PencereYapisi.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        PencereYapisi.hCursor = LoadCursor (NULL, IDC_ARROW);
        PencereYapisi.lpszMenuName = NULL;             
        PencereYapisi.cbClsExtra = 0;                
        PencereYapisi.cbWndExtra = 0;            
        
        PencereYapisi.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&PencereYapisi))
            return 0;
    
        AnaPencereTutmaci = CreateWindowEx (
               0,                   
               YapiSinifAdi,       
               "Windows App",       
               WS_OVERLAPPEDWINDOW, 
               CW_USEDEFAULT,      
               CW_USEDEFAULT,      
               544,                 
               375,                 
               HWND_DESKTOP,        
               NULL,                
               AnaFonksiyonTutmaci, 
               NULL               
               );
        DugmeTutmaci = CreateWindowEx (
               0,
               "button",
               "Durdur",
               WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
               1,      
               1,       
               170,      
               30,   
               AnaPencereTutmaci,
               (HMENU)1,
               AnaFonksiyonTutmaci,
               NULL
               );
               
        ShowWindow (AnaPencereTutmaci, nFunsterStil);
    
        while (GetMessage (&Mesajlar, NULL, 0, 0))
        {
            TranslateMessage(&Mesajlar);
    
            DispatchMessage(&Mesajlar);
        }
    
        return Mesajlar.wParam;
    }
    
    LRESULT CALLBACK PencereProseduru (HWND AnaPencereTutmaci, UINT message, WPARAM wParam, LPARAM lParam)
    {   
            char Icerik[256];
        switch (message)         
        {   case WM_COMMAND:
                 if(LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED)
                        {
                           GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+11);
                           if (strcmp(Icerik, "Durdur")== 0) 
                           {SetWindowText (DugmeTutmaci,"Calistir");
                           break;}
                           else
                           {SetWindowText (DugmeTutmaci, "Durdur");
                           break;}
                        }
                        break;                              
            case WM_DESTROY:
                PostQuitMessage (0);  
                break;
            default:  
                return DefWindowProc (AnaPencereTutmaci, message, wParam, lParam);
        }
    
        return 0;
    }
    Last edited by hileci555; 07-10-2008 at 04:50 PM. Reason: It is liviiiing

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sizeof(Icerik)+11
    I said DELETE the addition, not make it bigger?

    What if you really have more chars than will fit, and you lie about the buffer size?
    Trashed stack, random crashing and "Huh?" posted on message boards is what awaits you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    > sizeof(Icerik)+11
    I said DELETE the addition, not make it bigger?

    What if you really have more chars than will fit, and you lie about the buffer size?
    Trashed stack, random crashing and "Huh?" posted on message boards is what awaits you.
    Now you've done it, Salem. He'll make it +21, next time.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it's a good example of how "working" != "bug free"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Cool

    Quote Originally Posted by Salem View Post
    Well it's a good example of how "working" != "bug free"
    Hehe, that's a good one.

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. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 02:15 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Pressing a button works sometimes
    By johny145 in forum Windows Programming
    Replies: 14
    Last Post: 05-18-2005, 11:53 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