0% found this document useful (0 votes)
214 views

Tutorial 1 How To Make A Window For Our Application

first tutorial for cg

Uploaded by

api-3717576
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

Tutorial 1 How To Make A Window For Our Application

first tutorial for cg

Uploaded by

api-3717576
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Graphics tutorials by Jawad ul Hassan

Introduction to Graphics Programming with and without windows.h

How to make a window for our application with/without using window


library “Windows.h” in c++:

First of all make this thing clear that using windows.h is not a bad idea if you want your self
remain for windows platform but what happened when you also want to cover the domains of
other OS respectively so in this tutorial I will explain you both the methods to make something
good and platform dependent and also platform independent.

I assume that you are familiar with the terms “platform dependent” and “platform
independent” So I will make an introduction to windows.h header file which links to
windows library. This library was designed by Microsoft Corporation for windows OS
and still it is being used by programmers who are interested to work with some depth
level. There are also some good alternatives provides by Microsoft and other
organizations e.g. Microsoft family product Visual Studio for both Console and GUI
applications and JBuilder for java applications. So now lets move on to point.

These two code sections are only meant for memorizing or better to be copied because
you will use them frequently in your every Computer Graphics application.

So first we discuss our window library method here I am just pasting the Code from the
book “Programming Windows 5th ed by Charles Petzold” one of my favorite writer

So we named this section as our first window using windows.h.

You can view this code on the above mentioned book as “The HELLOWIN Program”.

So here we make a start but this code is little bit modified to meet our needs if you need actual
code then read the book for more knowledge

HELLOWIN.C
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows XP!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/

#include <windows.h>

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,


PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin");
HWND hwnd ;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;


wndclass.lpfnWndProc = WndProc ;

http:\\www.yahoogroups.com\cgmania
Computer Graphics tutorials by Jawad ul Hassan
Introduction to Graphics Programming with and without windows.h
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class
name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x
position
CW_USEDEFAULT, // initial y
position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window
handle
NULL, // window menu
handle
hInstance, // program
instance handle
NULL) ; // creation
parameters

ShowWindow (hwnd, iCmdShow) ;


UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))


{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,


LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{

case WM_PAINT:

http:\\www.yahoogroups.com\cgmania
Computer Graphics tutorials by Jawad ul Hassan
Introduction to Graphics Programming with and without windows.h
hdc = BeginPaint (hwnd, &ps);

GetClientRect (hwnd, &rect) ;

DrawText (hdc, TEXT ("Hello, Windows XP!"), -1, &rect,


DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hwnd, &ps);
return 0;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Now if you are familiar with c or C++ syntax and environment you will surely understand every
thing or you can view the book which I will surely upload on the group in some time but trust me
there is no need to waste your time on this section the only thing I recommend to you is that you
should just only copy this piece of code and run it using visual studio or any c compiler on
windows platform. The file windows.h is by default available in windows OS. You will see a
window box.

This is not a complete work for making any graphic application so just relax and observe I will
slightly make some changes in this code to make it workable with any graphic library which is in
our case is opengl.

Now for our second section which is “Creating window application without using
windows.h”.

// these lines are must for every applications


// memorize them we use glut library for our window creation

#include<gl/glut.h>

void myinit(void)
{
// for making screen color

glClearColor(1.0,1.0,1.0,0.0);
// assigning color to our painter

// for simple plane starting 0,0 point from lower left


gluOrtho2D(0.0,320,0.0,480.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

void mydisplay(void)

http:\\www.yahoogroups.com\cgmania
Computer Graphics tutorials by Jawad ul Hassan
Introduction to Graphics Programming with and without windows.h
{
glClear(GL_COLOR_BUFFER_BIT);
// make your drawing between these two statements

glFlush();
}
int main() // or int main(int argc, char** argv)
{
// always use these lines

// this one is optional but you can use if you want to otherwise
//skipping it will not make any bad effect
//if you want to use the line below then you should make the main
function with arguments
//which is "int main(int argc, char** argv)"

// glutInit(&argc,argv); //Initialize GLUT

// making our diplay buffer which stores the positions of pixels.


glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

//making our window size you will surely find it much easier than the
above method
glutInitWindowSize(300,300);

// what is the location of our window on the screen start from upper
left corner
glutInitWindowPosition(0,0);

//we make our window with a string in quotes for window name which is
displayed on //the top of our window which is in that case is CG MANIA
FIRST LESSON.
glutCreateWindow("CG MANIA FIRST LESSON ");

// we will discuss it later you just assume that it is our application


backbone.
glutDisplayFunc(mydisplay);

// this function is called only for once and used for initialization we
will also discuss it //later
myinit();

// this is much like main function as the name specify makes an


infinite loop and calls every function in the main which is starting
from “glut” but the difference is that it is a main in a main function
yet if you don’t call it you will not be able to run your graphical
application.
glutMainLoop();
// return type
return 0;
}

// enjoy your time

http:\\www.yahoogroups.com\cgmania

You might also like