Topic : Windows Programming Intro
Author : Mark Klarer
Page : 1

Windows Programming Tutorial: Intro


So, you are interested in programming a windows application. As I mentioned in the C tutorial, windows is huge. In C you have functions like "scanf", "printf", "getc", math functions, input/output functions, etc. You could make a lot of useful programs with functions like that. What if you want to write the same program for Microsoft Windows? Well, in the windows world, you are dealing with lots of new inventions - all of which are programmed in C. One of the most important of the new things are dialogs. Dialogs are the windows with text fields, buttons, drop down lists, tabs, radio buttons, spin controls and lots of other controls. You have to understand how to program them to make windows applications. In windows, you also deal with the Windows Operating System. The operating system has its own state variables, resources (like pens and brushes, the swap file, etc), multitasking.

In fact windows has so much STUFF that I couldn't possibly give you a paragraph or two about it that would be very useful. So lets just jump right into a little demo windows program. The demo program will be a good base for other projects (like Directx projects). Trying to learn windows all in one gulp will surely choke a mortal, so I will feed you tiny pieces. I am no windows programming expert, but I will try the best I can to explain the program. In order to compile this program, you will need to use the Visual C++ compiler. Create a new project, make it a Win32 application. After you create it, add a C source file to the project. Call it "simple" or whatever you want. Copy the source code below into the new source file. Execute it! It should make a small, little window that does nothing but "just sit there". Actually, it does not do anything useful, but it will run in the windows environment, functioning and interacting with windows just as a windows program should.

//simple windows program
#include <windows.h>
#include <stdio.h>



// the windows program callback function declaration
LRESULT CALLBACK WndProcCallBack(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam);

// start point for program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; // main window handle
MSG message; // message structure
WNDCLASSEX WC; // window class structure

// general attributes for the window
WC.cbSize = sizeof(WC);
WC.style = CS_HREDRAW | CS_VREDRAW;
WC.lpfnWndProc = WndProcCallBack;
WC.cbClsExtra = 0;
WC.cbWndExtra = 0;
WC.hInstance = hInstance;
WC.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WC.hCursor = LoadCursor(NULL, IDC_ARROW);
WC.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WC.lpszMenuName = NULL;
WC.lpszClassName = "simple window";
WC.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// register window
RegisterClassEx(&WC);

// create window
hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,
"simple window",
"simple window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, //use default x placement
CW_USEDEFAULT, //use default y placement
256, //set the window x size
128, //set the window y size
NULL,
NULL,
hInstance,
NULL);

// show window
ShowWindow(hWnd, nCmdShow);

// the program's message loop
while(GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}

// exit the application with the error code
return message.wParam;
}

LRESULT CALLBACK WndProcCallBack(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam)
{
switch(nMsg)
{
case WM_CREATE:
SetTimer(hWnd, 1, 50, NULL);
break;
case WM_LBUTTONDOWN:
{
}
break;
case WM_DESTROY:
KillTimer(hWnd, 1);
PostQuitMessage(5);
break;
case WM_TIMER:
break;
}

return DefWindowProc(hWnd, nMsg, wParam, lParam);
}



Now for an explanation.

For those of you who have never seen a windows program source code will say "what?", "what the hell is this??". Yeah, thats what I said too. Actually, this program's simplicity is a pure blessing. There really are only three major parts to it: the include section (a couple of include statements, and a function prototype), the WinMain function, and a callback function. You already know what the include functions do. They add more functions into the compiler. In this case, we have "windows.h", which adds a lot of new functions, like "winmain". The WinMain is directly analogous to C's "main" function - easy. The callback function is the new kid. The callback function is exaclty that - your Windows operating system CALLS it when things happen - you press the mouse button, you hit a drop down list, you exit the application whatever. Check out the callback function, you will be amused. In fact, you will find that windows is callbacks. The program is "event driven". Events happen, your program responds based on the event. That is how windows programs are designed. I have seen programmers start programming in windows and watch how they adopt the concept in steps. Its interesting. The one key thing you have to keep in mind when learning windows programming is "event-driven". Once you see your application in that view, the design will fall into place.

Now for some more details. At the very top, we have two include statements and a CALLBACK function prototype (defined at the program's bottom). The callback prototype shows some variables that windows will send to it: hWnd, nMsg, etc. hWnd is the handle to the window that appears. It is like a pointer to the window object. The next variable is the message windows is sending (like WM_DESTROY). The next two variables wParam and lParam are extra information that windows might send (like mouse position for example).

The WinMain function makes a Windows window object and displays it. It sets up the parameters in the class, creates an object of the class, and displays it. After the object is created, the program will message-loop while it runs, then exit.

The callback function is where the applications behavior exists. Here is where you would write some code on what the program should do during different events, like clicking the mouse, typing in text, etc.

Like I said, this is a very simple program that does not do anything useful by itself.

Page : 1