Broad Network


Windows Predefined Controls

Windows Predefined Controls – Part 1

Volume - Windows User Interface

Forward: In this part of the series, we learn the meaning of Windows Controls.

By: Chrysanthus Date Published: 29 Aug 2012

Introduction

This is part 1 of my series, Windows Predefined Controls. This is the third division (series) of many series. Each series is called a division. A number of divisions form a volume. There are a number of volumes. All the volumes form my Windows API Tutorial Set. In this part of the series, we learn the meaning of Windows Controls.

The application language used in this division is C++.

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.

Prerequisite
You need to have knowledge in the following before stating this tutorial series:

- Getting to know Windows
- Window Classes

To reach any of the series, just type the title and my name Chrys in the Search Box of this page and click Search.

Position of a Window
The desktop is like the first quadrant of the coordinate axes in math. However, the positive and negative values of the y-axis for the desktop are opposite to those of math. So the left-top corner of the desktop is the origin. Coming down from there, you have increasing positive y-values. Going to the right from there, you have increasing positive x-values.

The initial x and y coordinates of a window in the CreateWindowEx function refer to the left-upper corner of the window relative to the parent window. The position of a window is these initial coordinates, everything being equal.

A Control
A window can have a child window. A control is a child window that can receive input or display output. The input is from the user of the application and it comes from the keyboard or mouse or some other input device.

The initial x and y coordinates of a control are relative to the left-upper corner of the parent window client area and not the desktop.

Classes for Controls
A class is a set of window characteristics. In the article, Window Classes, in the previous series, it was indicated that the operating system provides the following window classes. At the moment you will not know the characteristics of the classes or their meanings. However, by the end of this tutorial, you will appreciate the use of the classes. I will not give you the corresponding WNDCLASSEX struct members of the classes. You do not have to know them before you use the classes; I do not know them myself, since they are operating system classes.

Class Description
Button: The class for a button.
ComboBox: The class for a combo box.
Edit: The class for an edit control.
ListBox: The class for a list box.
MDIClient: The class for an MDI client window.
ScrollBar: The class for a scroll bar.
Static: The class for a static control.

Creating a Control
Some controls have macros (functions) you can use to create them. However, to create any control, you can use the CreateWindowEx function. We saw this in the first series. The syntax of the CreateWindowEx function is:

HWND WINAPI CreateWindowEx(
  __in      DWORD dwExStyle,
  __in_opt  LPCTSTR lpClassName,
  __in_opt  LPCTSTR lpWindowName,
  __in      DWORD dwStyle,
  __in      int x,
  __in      int y,
  __in      int nWidth,
  __in      int nHeight,
  __in_opt  HWND hWndParent,
  __in_opt  HMENU hMenu,
  __in_opt  HINSTANCE hInstance,
  __in_opt  LPVOID lpParam
);

What interest us here are the class of the control window (lpClassName), the name of the control window (lpWindowName), the styles for the control window (dwStyle), the initial horizontal position of the control (x), the initial vertical position of the control (y), the width of the control (nWidth), the height of the control (nHeight) and the handle of the parent window. The instance handle hInstance, will be read from the argument of the WinMain function which is the entry point of the application. The hMenu parameter also interests us (see below). We shall see examples of the use of this function below.

Child Window Identifier
Each child window in a window (parent) needs an identifier. No two child-windows can have the same identifier. The identifier is an integer. It is set for the hMenu argument as in the following example:

    (HMENU)2

where 2 is the identifier (integer). So the integer type is cast (converted) into an HMENU type, by the use of the parentheses around HMENU.

Edit Control
The edit control is a child window that displays text (string) that the user types at the keyboard. The Edit Control is of the EDIT class. The following code will create a window, whose handle is hwndMain and then create an edit control. The edit control is the child window and its parent window handle is, hwndMain.

#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 hwndEdit1;
    hwndEdit1 = CreateWindowEx(0, "EDIT", "Edit Test", WS_CHILD, 100, 100, 150, 15, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndEdit1, SW_SHOW);
    UpdateWindow(hwndEdit1);


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

Save the code as a text file with the name winedit.cpp in the mingw directory. Try the above code to see what the edit control looks like. Try it in the command prompt window under the mingw directory with the following command:

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

After this, double click the icon, winapp.exe in the winapp.exe folder, to see the window and edit control.

The code segment that creates the edit control is,

    HWND hwndEdit1;
    hwndEdit1 = CreateWindowEx(0, "EDIT", "Edit Test", WS_CHILD, 100, 100, 150, 15, hwndMain, (HMENU)1, hinstance, NULL);
    ShowWindow(hwndEdit1, SW_SHOW);
    UpdateWindow(hwndEdit1);

Note that the name, "Test Edit" given to the edit control appears as the initial text in the edit control. That is alright. If you do not want any initial text, then set the name of the child window to NULL. You can select the edit control in the displayed window with the mouse and type anything that you want in it. The positions and dimensions of the edit control are given in device units (see later). The window style is WS_CHILD meaning it is a child window. You have the ShowWindow and UpdateWindow functions for the child window, which use the handle of the child window. These two functions are needed to display and paint the window (see reasons later).

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