Creating Window Basics
Getting to know Windows – Part 7
Volume – Windows User Interface
Introduction
This is part 7 of my series, Getting to know Windows. I assume you have read all the previous tutorials before this one. You should be reading the tutorials in the order given. In this part of the series we look at creating window basics.
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.
Creating a Window
An application typically creates the main window as its first window before it can go on to create other windows. You create a window using the CreateWindowEx function. Before creating a window, you have to register the class of the window first, and then provide a window procedure for the class; then you can create the window.
A class has the characteristics of a window. Some of these characteristics can be given when creating the window in the CreateWindowEx function. If you do not give the characteristic in the CreateWindowEx function, make sure you give it in the class description (see details later), unless the characteristic is optional.
The CreateWindowEx Function
There is a predefined function called CreateWindowEx, which is used to create a window. Two other functions are normally used with the CreateWindowEx function. The functions are ShowWindow and UpdateWindow. Example code of the use of these functions is:
HINSTANCE hinst;
HWND hwndMain;
hwndMain = CreateWindowEx(0, “MainWClass”, “Main Window”, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL, (HMENU) NULL, hinst, NULL);
if (!hwndMain)
return FALSE;
ShowWindow(hwndMain, SW_SHOWDEFAULT);
UpdateWindow(hwndMain);
The first thing in the code is the declaration of an identifier that will hold the instance handle of the class. Next you have the declaration of the identifier that will hold the handle of the window created. The CreateWindowEx function returns the handle of the window it has created. This will be held by hwndMain.
The CreateWindowEx function has many arguments. The second argument is the name of the class; it is a string in double quotes. The third argument is the name of the window, which will be displayed in the title bar of the window. It is a string in double quotes and it can have spaces. One of the arguments is the identifier for the class instance handle.
There is an if-statement. This if-statement checks if the window was successfully created. If it was not successfully created, the if-statement returns FALSE to the calling environment (operating system). The condition, (!hwndMain) will result in False if the window was not created (successfully). Note the NOT (!) inside the condition.
When the window is created by the CreateWindowEx function it is not displayed. The ShowWindow predefined function displays the window. It first argument is the handle of the window. We shall talk about its second argument later. As the window is just displayed, it does not have a color. The UpdateWindow predefined function paints the window’s client area. Its argument is the window handle. The uses of the last two functions do not look logical; but that is how things are with Windows.
Destroying a Window
Closing a window does not destroy the window. Destroying a window means the window has to save its system data and then free the memory that the window was using and free any resources that were allocated to the window, so that other applications can use the resources (and memory). We shall see more about this, as we go along.
The Windows API Volumes
Those of us who write (publish) for the Internet, write for money. We get our earnings through the advertisements you see on our web pages like this one. So please, do click the advertisements on my pages to know what my partners are advertising. In that way they pay me on your behalf, for advertising their products. If you do not click the advertisements of the Internet articles, they will not pay us. I know you are getting the stuff free, but do click the advertisements to enable us continue to write. Thanks.
We can stop here. We continue in the next part of the series.
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):
Getting to know Windows
What is a Microsoft Window?
Basics of Window Classes
Window Procedure Basics
Message Basics for Window Class Procedure
Basics of Message Handling in Windows
Creating Window Basics
Basic Coding of Window Class Procedure
Your first Window
Written by Chrys