Saturday, December 16

Windows Static Control

Google+ Pinterest LinkedIn Tumblr +

Windows Predefined Controls – Part 13
Volume – Windows User Interface

This is part 13 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 the meaning of Windows Static Control.

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

Text Static Control
There are 4 static controls but in this series, we shall talk only about the text static control. A text static control is like an edit control, but it does not receive typed input from the user. A static control cannot be selected and cannot receive the keyboard focus. A static control is normally used as a label for other controls. If you have an edit control for example, you would normally use a static control on the left or above the edit control. This static control is a label and would have text that indicates the purpose of the edit control. The static control is of the system window class, STATIC.

The static control is a child window. The following code displays a static control above an edit control:

using namespace std;

    switch (uMsg)
            return DefWindowProc(hwnd, uMsg, wParam, lParam);

    return 0;

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    wcx.cbSize = sizeof(wcx); = 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;


    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);

    HWND hwndSt;
    hwndSt = CreateWindowEx(0, “STATIC”, “First Name”, WS_CHILD, 100, 100, 100, 20, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndSt, SW_SHOW);

    HWND hwndEd = CreateWindowEx(0, “EDIT”, NULL, WS_CHILD, 100, 122, 100, 20, hwndMain, (HMENU)2, hinstance, NULL);
    ShowWindow(hwndEd, SW_SHOW);

    MSG msg;
    BOOL bRet;

    while( (bRet = GetMessage( &msg, hwndMain, 0, 0 )) != 0)
            if (bRet == -1)
                // handle the error and possibly exit the application

    return msg.wParam;

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

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

There are two controls in the code. Let us look at the static one. The class is, STATIC; this is the second argument of the CreateWindowEx function. The name of the static control is, “First Name”; this is the third argument of the function; this name appears as the content display of the static control. You have the WS_CHILD style indicating that it is a child window. The rest of the arguments to the CreateWindowEx function are like those for the EDIT control. Remember, each control must have a unique integer identifier (at the tenth argument of the CreateWindowEx function).

If you open the application, winst.exe, by double-clicking on it, you would see the 2 controls with the static control above the edit control. The static control has the text “First Name” indicating that the user should type his first name in the edit control.

That is it for this part of the series. We stop here and continue in the next part.


To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

A Window Button Function and Macro
Windows Static Control
Static Control Styles


About Author

Leave A Reply