Windows Static Control
Windows Predefined Controls – Part 13
Volume - Windows User Interface
Forward: In this part of the series, we look at the meaning of Windows Static Control.
By: Chrysanthus Date Published: 29 Aug 2012
Introduction
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 <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 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
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT