making a standard button in win32

This is a discussion on making a standard button in win32 within the Windows Programming forums, part of the Platform Specific Boards category; i know this has probably been asked a million times before..but how do i make a simple 'push button' in ...

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070

    making a standard button in win32

    i know this has probably been asked a million times before..but how do i make a simple 'push button' in win32 programming?

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hello psychopath,

    I know of a few implementation to get the job done:

    Code:
    #include <windows.h>
    
    // Make sure this is setup in your Win32 initialization and stuff...
    HINSTANCE hInstance;
    
    // Default font for button
    void SetDefaultFont( HWND hWnd, int identifier ){
        SendDlgItemMessage(hWnd, identifier, WM_SETFONT,
    		(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
    }
    
    // Push button
    HWND CreateButtonEx( HWND hWnd, int identifier, int x, int y,
    					 int width, int height, char* tempText, unsigned long WS_CUSTOM ) {
        HWND hButtonTemp;
        
        hButtonTemp = CreateWindowEx(0, "BUTTON", tempText, 
    		WS_CHILD | WS_CUSTOM | WS_VISIBLE, x, y, width, height, hWnd, 
    		(HMENU)identifier, hInstance, NULL);
        SetDefaultFont(hWnd, identifier);
        return hButtonTemp;
    }
    Code 1.1: Setting up variables/functions for push button

    Now on WM_CREATE all you have to do is:

    Code:
    #define IDBTN 10
    HWND BTN;
    
    BTN = CreateButtonEx( hWnd, IDBTN, 0, 0, 80, 23, "My Push Btton", WS_VISIBLE );
    Now to explain all of this, I'll sum it up. IDBTN will be a handle to your button in case you cant use the HWND handle BTN. Also, the last argument in CreateButtonEx is there in case you want to add some other Window Styles that arent in the functions. I used WS_VISIBLE even though it was already in the implementation because I didn't need any special styles.

    Now hWnd, is your window (dialog) handle. Which you will see in your Procedure, example:

    LRESULT CALLBACK DlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
    Of course, if your dialog procedure is not hWnd in your code, just change CreateButtonEx's first argument to the corresponding variable name. Now if you'd like, I can set up a whole code to display all of this in use instead of throwing little tid bits here and there.

    In fact here is a link to a code I just wrote: Win32 Button Control

    If you have any further questions, please let me know.

    Hope this helps,
    Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    ok thanks....but how would i identify between multiple buttons?

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Well you would just make more handles, example:

    Code:
    #define IDBTN0 10
    #define IDBTN1 11
    #define IDBTN2 12
    #define IDBTN3 13
    #define IDBTN4 14
    #define IDBTN5 15
    // etc...
    
    HWND buttons[5]; // for 6 buttons
    
    buttons[0] = CreateButtonEx( hWnd, IDBTN0, 0, 0, 80, 23, "My Push Button", WS_VISIBLE );
    
    buttons[1] = CreateButtonEx( hWnd, IDBTN1, 40, 0, 80, 23, "My Second Push Button", WS_VISIBLE );
    
    buttons[2] = CreateButtonEx( hWnd, IDBTN3, 80, 0, 80, 23, "My Third Push Button", WS_VISIBLE );
    
    // etc...
    Hope that helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    ahhh...ok, thanks

    -psychopath

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 12:32 PM
  2. disabling button in another prog
    By timmygax in forum Windows Programming
    Replies: 11
    Last Post: 10-29-2001, 03:40 PM
  3. Button Identifier Problems!
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 06:48 PM
  4. Unselecting a selected button in C++Builder 5
    By Frozen_solid in forum Windows Programming
    Replies: 4
    Last Post: 09-25-2001, 08:57 PM

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