Saturday, December 16

Windows Static Control

Google+ Pinterest LinkedIn Tumblr +

Windows Predefined Controls – Part 13
Volume – Windows User Interface

Introduction
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 forchatrans@yahoo.com. 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:

#include
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 hwndSt;
    hwndSt = CreateWindowEx(0, “STATIC”, “First Name”, WS_CHILD, 100, 100, 100, 20, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndSt, SW_SHOW);
    UpdateWindow(hwndSt);

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

    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++ 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.

Chrys

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
 

Share.

About Author

Leave A Reply