Topic : How to create a window with Win32
Author : Vivek Mohan
Page : 1 Next >>
Go to page :


  

HOW TO CREATE A WINDOW


Language : C++
Compiled in : Borland C++ 5.5 , Mingw , ( Should compile in MS Visual C++ )
Platform : MS-Windows
I am : Vivek Mohan n my homepage is @ http://compgurux.tripod.com/
u can mail me with suggestions to : mailvivek27@sify.com
The Code only file : win_code.cpp (included in the zip file)

INTRODUCTION
============
Hi ! So you wanna know how to create a window in C++. Ahem ! Well if you have not  misunderstood we are talking about the windows API (Application Programming Interface) , so all Windows haters please take the back seat ! I must tell you I too am an amateur when it comes to Windows programming , and this is a result of what I jotted down while I was self studying programming for windows. So if you are one of those expert programmers , please let me know if there are any  mistakes in this code or its explanation. If you have any comments or questions at all please mail me at the above mentioned address.



THE LIBRARY
===========

# include <windows.h>

A line you must never miss while programming for windows
The win32 API library

THE CLASS NAME
==============

const char g_szClassName [] = "My Window Class";

A name for our windows class. Wait ! Aha ! If I'm not mistaken you are probably thinking that the windows class is a C++ class. No it is not. This is completely different. It is just like what the word means. A particular class of windows with a certain , set of characteristics and behaviour.
You'll understand what I mean...eventually.

But why const ?
---------------
The class name is stored as a const char array because we won't be using  this variable for long and are not going to modify it.

But why the weird name , g_szClassName ?
----------------------------------------
I don't know if this question does pop up into your mind , but
atleast it did in mine. Well firstly I must tell you that it is not compulsory that you keep the same name , but these names are kept in accordance to certain conventions. These conventions are specific to win programming. Using these conventions makes sure that other programmers can read your code without any difficulty.
Moreover as you move on you'll see that these words can be easily identified for what they represent by a careful analysis.

PROTOTYPE FOR WndProc
=====================

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

We'll discuss about the function when we actually
define it.

MAIN
====

int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
            LPSTR lpCmdLine , int nCmdShow )


if you have programmed in dos or unix , you may be familiar with the main() function which is the entry point for code execution. Similarily for windows programs WinMain is the entry point. But unlike the command line arguments in dos or linux "int main(int argc,char *argv[])" , WinMain(..) has a different set of arguments explained below.

HINSTANCE hInstance
-------------------
This is the [h]andle to the [Instance] of the windows class in the memory. It is a handle to the executable module in the memory and plays an important role when it comes to manipulating independent modules and stuff.(Dont worry about
it now..!).

HINSTANCE hPrevInstance
-----------------------
This is the [h]andle to the [Prev]ious [Instance] of the windows application.
This is not at all important and is null for 32 bit windows programs.

LPSTR lpCmdLine
---------------
If you are wondering what LPSTR stands for , it is nothing but [L]ong [P]ointer  to [STR]ing or char*. The long part is not important any more as far as win32 is concerned. lpCmdLine stands for [l]ong [p]ointer to [C]o[m]man[d] [Line] arguments. Unlike what you'd expect (as in dos or unix) , this is a single string containing all of the command line arguments and does not include the  program name.

int nCmdShow
------------
This argument , as you'll see later is useful in specifying the state of the window when drawn (like Minimized , Maximized ..etc). This is the parameter used in shortcuts to your applications in Windows when you want it to be in Maximized mode or minimized or maybe just normal.

{


REGISTERING A WINDOW CLASS
==========================
A Window class holds the information about the type of window. This  class needs to be registered with a particular set of attributes such as icon, background colors etc which are defined in WNDCLASSEX struct. The window class also holds a pointer to the Procedure(or function) which controls the behaviour of the windows. Once registered , any number of windows can be created with the same attribs without specifying the same all over again.

   WNDCLASSEX wc;

A window class wc Setting the different attributes of the window class
   
   wc.cbSize = sizeof(WNDCLASSEX);

cbSize holds the size of the struct WNDCLASSEX

   wc.style  = 0;

style stores the class style and can be usually set to 0

   wc.lpfnWndProc = WndProc ;

lpfnWndProc = [l]ong [p]ointer to [f]u[n]ction [WndProc]. This is a pointer to the function WndProc which is the window procedure and controls the behaviour of the window. Remember the function WndProc we prototyped a few pages back !


   wc.cbClsExtra  = 0;

cbClsExtra stores the amount of extra memory to be allocated
for this class of windows.This is also usually 0;

   wc.cbWndExtra  = 0;

cbWndExtra stores the amount of extra memory to be allocated for each window of this class. This is also usually set to 0.

   wc.hInstance   = hInstance;

Handle to the instance of the application
   
   wc.hIcon       = LoadIcon  (NULL,IDI_APPLICATION);

This is the icon (32x32) shown when the user presses alt+tab to switch between applications. Don't worry about the function LoadIcon (...) now.

   wc.hCursor     = LoadCursor(NULL,IDC_ARROW);

The cursor that'l' be displayed over the window

   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

Background brush to set the color of the window

   wc.lpszMenuName  = NULL;

Name of a menu resource to use with this class , since we don't need no menus it is set to NULL.

   wc.lpszClassName = g_szClassName;

The name of the class to be identified with , if you remeber
we had declared a const char array g_szClassName which stores' our window class name

     wc.hIconSm       = LoadIcon(NULL,IDI_APPLICATION);

This is the handle to the icon displayed on the top left corner of the window or on the taskbar.

REGISTERING THE WINDOW
======================

   if(!RegisterClassEx(&wc))
   {

The RegisterClassEx(WNDCLASSEX&) function which takes the address of  a window class struct , attempts to register it and returns a non-zero value if it is able to do so. Else it returns 0.

           MessageBox(NULL , "Failed to register window class" ,
                        "Error" , MB_ICONEXCLAMATION | MB_OK );


If the registration fails , A message is displayed using the MessageBox function. After running the program you'll understand how the MessageBox(...)  works so it doesn't need much explanation.

           return 0;

Then return 0.

   }

CREATING A WINDOW
=================
Now comes the part where we have to create the window

     HWND hwnd;

A handle to the window we create. The function CreateWindowEx(..) creates a  a window and returns the handle

     hwnd = CreateWindowEx(

The returned handle is stored in hwnd

                   WS_EX_CLIENTEDGE ,

Argument 1 : Is the style of the window , I have set it to 0, you can use other values and see what happens.

                   g_szClassName    ,

Argument 2 : Is the name of the registered class , so that the creator knows what class of window has to be created.

                   "Title Of Me Window !" ,

Argument 3 : Is the title of the window

                   WS_OVERLAPPEDWINDOW    ,

Argument 4 : Is a window style parameter. You can substitute integer values for this and see what happens

        

Page : 1 Next >>