Making Buttons

This is a discussion on Making Buttons within the Windows Programming forums, part of the Platform Specific Boards category; I have a main menu for a game, but I can't find anywhere that says how to make a Button. ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    57

    Making Buttons

    I have a main menu for a game, but I can't find anywhere that says how to make a Button. I just want to place a few buttons on the screen for the player to click on and go to that. How would I make them?


    By the way I am coding it in C++.
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  2. #2
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Umm, if your making a game i dunno if you using directx, but heres how you do it just for normal window proggies.

    #define ID_BUTTON 4563 //define ID at the top of ur code


    //create the button in winmain
    HWND hWndButton = CreateWindow("BUTTON", "Click Me", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
    10, 10, 65, 15, hWnd, (HMENU)ID_BUTTON, hInstance, 0);


    Hope that helps,
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    Yea, just regular windows. A couple questions on that. That would go in this part of my code right:
    Code:
    hwnd = CreateWindow ("Window Class 1",
    		     
    			 "TiloSite by Tazar",
    			 WS_OVERLAPPEDWINDOW,
    			 10,
    			 10,
    			 700,
    			 700,
    			 NULL,
    			 NULL,
    			 hInstance,
    			 NULL);
    how exactly would i put that in there then? And what is the (HMENU) for? Thanks
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Nope. Just define it at the top, and then do the other stuff that
    ( TNT ) said.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And just for clarities sake Tazar, each and every button is a "window", so you would'nt have just one CreateWindowEx() call, but several: one for the main window, and one for EACH button. BTW all Windows controls are separate "window"s, and each recieves it's own message loop, etc....
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    yay! The button is working. So now I have 2 more questions. My bitmap for the main menu has fancy text for where the player clicks to go to the option, so if i lined up the button exactly where the Text were, and took off the WS_VISIBLE, the player could click the text and go to the action of the button? That is pretty wordy... and one more question.

    How do i detect if the player hit the button? So I would detect if they hit it, and then do the code for that option right? Thanks again
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  7. #7
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    to detect if it is pressed it will send an ID_BUTTON (from first example) in your wndproc as wParam of a WM_COMMAND message
    hope that helps
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    I got the reading it done. But taking off WS_VISIBLE doesn't work... Anyway i could make that text there and still go to that function?
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  9. #9
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    you coud try ShowWindow(button_window,SW_HIDE);
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    I put that under the button's code, and it said button_window undeclared identifier, and if i changed it to BUTTON1 or button1 it still didn't work. What now?
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  11. #11
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    when you are making your CreateWindow for the button what are you putting "here"=CreateWindow(...
    put that in the 1st parameter of ShowWindow
    ex
    HWND hWndButton = CreateWindow("BUTTON", "Click Me", BS_PUSHBUTTON | WS_CHILD,
    10, 10, 65, 15, hWnd, (HMENU)ID_BUTTON, hInstance, 0);
    ShowWindow(hWndButton,SW_HIDE);
    hope that helps
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    Stubborn game! It hides it, but you cant click on the text. I'll be darned...
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    In a word: no. If you hide the button then you no longer can recieve a click signal from it. Here's one solution, though: forget the button! That is, instead of relying on the button for input, simply find the coordinates of that area, store it in a RECT structure, then after testing for a mouseclick, (say, WM_LBUTTONUP) take the LOWORD of lParam, which will contain the x( or maybe its y) coord of the click, and the HIWORD of lparam, which will contain the other coord, and place those two in a POINT structure. Then it's as simple as saying "if(PtInRect(ptClick, rctArea))...blah blah blah...(again, the params may be backwords...)

    Happy Coding
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    57
    Stubborn game! It hides it, but you cant click on the text. I'll be darned...
    Language: C++
    Compiler: Visual C++ 6.0
    Currently Working On: Simple OpenGL aspects

  15. #15
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Just make a bitmap, load it in, and do all of the stuff to make it work*hope that works*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2009, 02:31 AM
  2. Ownerdraw buttons (2)
    By maes in forum Windows Programming
    Replies: 7
    Last Post: 09-11-2003, 05:50 AM
  3. Radio Buttons in Visual C++ 6
    By Ripper1 in forum Windows Programming
    Replies: 22
    Last Post: 05-16-2003, 07:54 AM
  4. Site on Making Common Controls like BUttons
    By knight543 in forum Windows Programming
    Replies: 3
    Last Post: 03-02-2002, 05:10 PM
  5. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21