Thread: making a standard button in win32

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

    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

    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,071
    ok thanks....but how would i identify between multiple buttons?

  4. #4

    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,071
    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, 11:32 AM
  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, 05: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, 07:57 PM