Broad Network


Button Types in Windows

Windows Predefined Controls – Part 8

Volume - Windows User Interface

Forward: In this part of the series, we look at button types in windows.

By: Chrysanthus Date Published: 29 Aug 2012

Introduction

This is part 8 of my series, Windows Predefined Controls. In order to understand this tutorial, you most have read all the previous tutorials of the series. In this part of the series, we look at button types in windows.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Button
A button is a control that the user can click to provide input to an application. Today the windows API UI has eight buttons. We shall look at the meaning of the eight buttons in this tutorial.

Push Button
A Push Button is the usual rectangular button. Try the following code to see what a push button looks like:

#include <windows.h>
using namespace std;


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch (uMsg)
    {

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wcx;

    wcx.cbSize = sizeof(wcx);
    wcx.style = CS_HREDRAW | CS_VREDRAW;                  
    wcx.lpfnWndProc = MainWndProc;   
    wcx.cbClsExtra = 0;              
    wcx.cbWndExtra = 0;              
    wcx.hInstance = hinstance;       
    wcx.hIcon = NULL;
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                
    wcx.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);              
    wcx.lpszMenuName =  NULL;
    wcx.lpszClassName = "MainWClass";
    wcx.hIconSm = NULL;


    RegisterClassEx(&wcx);


    HWND hwndMain;

    hwndMain = CreateWindowEx(0, "MainWClass", "Main Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, NULL);                  

    if (!hwndMain)
        return FALSE;


    ShowWindow(hwndMain, SW_SHOW);
    UpdateWindow(hwndMain);


    HWND hwndPush;
    hwndEdit1 = CreateWindowEx(0, "BUTTON", "Push Button", WS_CHILD| BS_PUSHBUTTON, 100, 100, 90, 25, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndPush, SW_SHOW);
    UpdateWindow(hwndPush);


    MSG msg;
    BOOL bRet;


    while( (bRet = GetMessage( &msg, hwndMain, 0, 0 )) != 0)
        {
            if (bRet == -1)
            {
                // handle the error and possibly exit the application
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }


    return msg.wParam;
}

You can use the following command at the command prompt to compile the code:

    g++ winpush.cpp -mwindows -o winpush.exe

You create a button control in a similar way that you create an edit control. With the button control, the class is BUTTON. The name of the button (window) becomes the title of the class. The style has WS_CHILD because the button is a child window. The rest of the arguments for the CreateWindowEx function are the same as for the Edit control that we saw in one of the previous tutorials.

Default Push Button
The Default Push Button is like the push button but it behaves slightly differently. The default push button is the type of button that is used for an OK button. You can have many buttons in a window, and among them, one would be the default button. If you press the Enter Key, even if the default button does not have a focus, it will be activated as if it were clicked. The other buttons (push buttons) have to be selected before they can be activated. If you want to have a default push button for the above code, you would have to replace the BS_PUSHBUTTON style with the BS_DEFPUSHBUTTON style. The BS_DEFPUSHBUTTON style is for the default push button, while the BS_PUSHBUTTON style is for the ordinary push button.

Check Box
A check box is a small square box that can be On or Off. When it is on it has a tick; when it is off it does not have a tick. In the above code, you have the code segment:

    HWND hwndPush;
    hwndEdit1 = CreateWindowEx(0, "BUTTON", "Push Button", WS_CHILD| BS_PUSHBUTTON, 100, 100, 90, 25, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndPush, SW_SHOW);
    UpdateWindow(hwndPush);

This code segment is for the push button. To see what a check box looks like, replace this code segment with the following code and try the resulting code. You can compile the file with a different (.exe) name.

    HWND hwndChk;
    hwndChk = CreateWindowEx(0, "Button", NULL, WS_CHILD|BS_AUTOCHECKBOX, 100, 100, 12, 12, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndChk, SW_SHOW);
    UpdateWindow(hwndChk);

Note the use of the BS_AUTOCHECKBOX style for the check box.

Three State Check Box
A normal check box is either ON or Off. There are situations when it is not clear if the box is on or off. This state is called an indeterminate state, and it occurs for a special kind of check box called the three State Check Box. You need the BS_AUTO3STATE style for a three state check box. Replace the above code segment with the one below to know what the three state check box looks like:

    HWND hwnd3St;
    hwnd3St = CreateWindowEx(0, "Button", NULL, WS_CHILD|BS_AUTO3STATE, 100, 100, 12, 12, hwndMain, (HMENU)1, hinstance,

NULL);
    ShowWindow(hwnd3St, SW_SHOW);
    UpdateWindow(hwnd3St);

Click the check box 3 times to discover the presentation of the third state. Note the use of the BS_AUTO3STATE style for the three state check box.

Split Button
A Split Button is two-buttons combined in one: a push button and an arrow button. When the user clicks the arrow part of the button, a menu drops down. This button works with the Windows Vista operating system. Consult some other documentation for its details.

Radio Button
A radio button, also called an option button is a round button. When it is On it has a dot inside it. When it is Off, it does not have a dot. The following code segment will create a radio button:

    HWND hwndRad;
    hwndRad = CreateWindowEx(0, "Button", "Radio Button", WS_CHILD|BS_AUTORADIOBUTTON, 100, 100, 14, 14, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndRad, SW_SHOW);
    UpdateWindow(hwndRad);

Note the use of the BS_AUTORADIOBUTTON style.

Command Link
A command Link is a push button, but looks very different from a push button. A command link typically displays an arrow icon, a line of text, and additional text in a smaller font. This was introduced in Windows Vista. Consult some other documentation for the details.

Group Box
A group box is not really a button; but it is considered as a button. It is a rectangle that is used to enclose a number of controls of your choice. If you have a name for the group of controls, this name will go to the upper-left corner of the rectangle. The following code would produce a group box with two radio buttons.

    HWND hwndG;
    hwndG = CreateWindowEx(0, "Button", "Group of Controls", WS_CHILD|BS_GROUPBOX, 100, 100, 300, 100, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndG, SW_SHOW);
    UpdateWindow(hwndG);

    HWND hwndRad1;
    hwndRad1 = CreateWindowEx(0, "Button", NULL, WS_CHILD|BS_AUTORADIOBUTTON, 170, 140, 13, 13, hwndMain, (HMENU)2, hinstance, NULL);
    ShowWindow(hwndRad1, SW_SHOW);
    UpdateWindow(hwndRad1);

    HWND hwndRad2;
    hwndRad2 = CreateWindowEx(0, "Button", NULL, WS_CHILD|BS_AUTORADIOBUTTON, 250, 140, 13, 13, hwndMain, (HMENU)3, hinstance, NULL);
    ShowWindow(hwndRad2, SW_SHOW);
    UpdateWindow(hwndRad2);

Note the use of the BS_GROUPBOX style for the group box. A button is a child window, so all buttons should have the WS_CHILD style.

We have seen the eight types of buttons. Remember, a group box is considered as a button even though its purpose is to group controls.

Let us stop here. We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message