Getting to know Windows – Part 9
Volume – Windows User Interface
Introduction
This is part 9 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, you create your first window.
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.
Entry Point
The following is a C++ program for the console:
#include
using namespace std;
int main()
{
cout << “Hello World!”;
return 0;
}
The beginning of the main function is the entry point for the application; that is where the application starts. To compile this application with the g++ compiler you would type something like:
g++ hello.cpp -o hello.exe
A windows application uses a different entry point function and not main. A windows application uses the entry function called, WinMain. The prototype is:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
The operating system normally supplies all the arguments for this WinMain function. hInstance is the window class instance handle we talked about in the previous parts of the series. The operating system supplies the actual value for you. The last statement in the WinMain is not “return 0″. It is,
return msg.wParam;
where msg is the identifier of the message struct. Do not worry about this return statement for now.
Assume that the name of your windows application in C++ is, firstwin.cpp. For the g++ compiler, you would type the following command to compile the windows application:
g++ firstwin.cpp -mwindows -o firstwin.exe
Note the use and position of the switch, -mwindows. This switch prevents the command prompt window from appearing when the application window appears.
Your First Window Application
I will give you code for your first window application. You will try it. I will explain the code in general terms below it. You will know the detail explanation in other series (divisions). Here is the code:
#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);
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;
}
Type the application code in a text editor and save it as a file called, firstwin.cpp, in the MinGW directory. Open your command prompt window and go to the MinGW directory. Execute the following command:
g++ firstwin.cpp -mwindows -o firstwin.exe
The compiled file should be saved as, firstwin.exe, in the MinGW directory. Open the MinGW directory now with Windows. Double-click the file, firstwin.exe. You should see a window (whose client area is dark blue).
Generalized Code Explanation
You need to include the header file, windows.h. This file contains the headers for your windows. In the code you have the window class procedure, whose name has been given by me. The block of this procedure does nothing other than call the default procedure. You then have the main function. It begins with the declaration of an identifier for the window class struct. The name of the procedure is assigned to one of the members of the class struct. The name of the class has been given by me as “MainWClass”.
The value of the class instance handle is hinstance, got from the first parameter of the WinMain function. As I said, the operating system supplies the actual value through the WinMain function when it calls the WinMain function. It is the operating system that calls the WinMain function. Do not worry what the other members of the window class WNDCLASSEX struct are doing for now.
Next, the window is created, shown and updated. Then you have the while loop to remove messages from the queue and send to the window class procedure. You will get the detail explanation in other series.
The WinMain has the duty to register the class, create the main window and house the message WHILE Loop.
After reading this tutorial, you can begin the next series called, Window Classes. Just search my blog with the title, Window Classes, to arrive at the series.
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.
Well, let us take a break here and 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
Related Windows 7 Articles