You are on page 1of 19

In partial fulfillment of the requirement for the award of the Bachelor of technology (B-Tech) of B.P.U.T.

Submitted to :
Dept. Of C.S.E

Project Guide:
Mr. Debasis Mohapatra (Lecturer)

Submitted By:
Sujit Kumar Singh Barun Kumar Mishra Tapan Kumar Sethi

CONTENTS
Sl.no. 1. 2. 3. 4. Certificate Acknowledgement Abstract Introduction
What is WIN32? What is API DLL files

Heading

Page no. 3 4 5 6

5. 6. 7.

Project goals Project core Modules


WIN 32 resources Message and events Characteristics of a Window Management of different windows GDI (Graphical Device Interface) GDI tools Bitmaps

7 7 8

8. 9.

System and Hardware Requirements Conclusion

18 19

CERTIFICATE
This is to certify that the project work titled WIN32 API Programming was carried out by Sujit Kumar Singh (0901109230) , Barun Kumar Mishra (0901109222) , Tapan Kumar Sethi (0901109108) in partial fulfillment of the requirements for the award of the degree Bachelor of Technology in COMPUTER SCIENCE & ENGINEERING by Parala Maharaja Engineering College,(B.P.U.T.) during the period 2009-2013 under my supervision.

Signature Of Guide :

ACKNOWLEDGEMENT

We would like to express our gratitude to all those who gave us the possibility to complete this project. We want to thank the Department of Computer Science & Engineering for giving us permission to commence this project in the first instance, to do the necessary research work and to use departmental data. We are deeply indebted to our supervisor Mr. Debasis Mohapatra from P.M.E.C. whose help, stimulating suggestions and encouragement helped us in all the time of research for and writing of this thesis. Our friends from the Department of CSE who supported us in our research work. We want to thank them for all their help, support, interest and valuable hints..

ABSTRACT
4

To a programmer, an operating system is defined by its API. An API encompasses all the function calls that an application program can make of an operating system, as well as definitions of associated data types and structures. The API documentation is organized in a tree-structured hierarchy. In Windows, the API also implies a particular program architecture. APIs are generally being implemented in 32-bit or above Operating systems.

Windows Win32 API programming mainly covers the GUI (Graphical User Interface), user events/commands and other process/applications linking, message passing and other activities within an Operating system. Win32 programming uses various system defined and user defined functions and classes to create a GUI environment to support various actions and commands of user(s). It also controls various actions and executions method of an application, thread control for multitasking of CPU and kernel of a 32-bit or above operating system. Various system and user events are controlled by these API programs like user I/O actions, memory management for an application or a program. API controls GDI (Graphics Device Interface) events for controlling graphics operations like text formatting, picture display, animations and video display. User enjoys facilities of various menus, popup-menus, dialog boxes and clipboard utilities. API automatically detects the connected hardware and produces commands and signals accordingly, programmer need not to worry about the type of hardware connected to the system.

Signature of Guide

INTRODUCTION
5

What is WIN32? WIN32 is a family of Windows Programming Interface. It is standard programming interface for Windows 95 and Windows NT. The WIN32 application programming interface uses 32bit address space rather than 26bit values used in Windows 3.x and earlier version of windows. WIN32 includes many new functions than 16 bit. The Kernel allows to create powerful programs by using the capability like multithreading.

User Interface : Interface for the direct interaction with the user. Display Terminals/CGA (Color Graphics Adapter), EGA (Enhanced Graphics Adapter), VGA (Video Graphics Adapter) different video graphics display. Multitasking (cooperative multitasking vs. preemptive multitasking) Windows uses GUI which helps on making the programmer more interactive and user friendly.

What is an API? An application programming interface (API) is a source code interface that a computer system or program library provides in order to support requests for services to be made of it by a computer program. An API differs from an Application Binary Interface in that it is specified in terms of a programming language that can be compiled when an application is built, rather than an explicit low level description of how data is laid out in memory.

DLL (Dynamic Link Library) Files Dynamic-link library (DLL), also known as dynamic link library (without the hyphen), is Microsoft's implementation of the shared library concept in the Microsoft Windows operating systems. These libraries usually have the file extension DLL, OCX (for libraries containing ActiveX controls), or DRV (for legacy system drivers). Dynamic Link Library, a library of executable functions or data that can be used by a Windows application. Typically, a DLL provides one or more particular functions and a program accesses the functions by creating either a static or dynamic link to the DLL. A static link remains constant during program execution while a dynamic link is created by the program as needed. DLLs can also contain just data. DLL files usually end with the extension .dll,.exe., drv, or .fon.

Project Goals : Realization and introduction to various WIN32 APIs and their functions. Implementations of basic APIs using C++ coding. Introduction to *.dll (dynamic linking library) files and their importance.

PROJECT CORE
1. WIN 32 resources 2. Message and events 3. Characteristics of a Window 4. Management of different windows 5. GDI (Graphical Device Interface) 6. GDI tools 7. Bitmaps

MODULES
7

1. WIN 32 resources :
Resources are menus, icons, cursors, dialog boxes, sounds, etc having *.res extension nand for font type it is *.rc. For example, some resources are completely text-based, such is the case for the String Table or the Accelerator Table (Ctrl+C, Ctrl+V, Ctrl+S etc). Some resources can be created like icons, cursors and some having high graphics complexity are imported. Windows program uses BeginUpdateResource function creates an update handle to the executable file whose resources are to be changed. The UpdateResource function uses this handle to add, delete, or replace a resource in the executable file. The EndUpdateResource function closes the handle. Before using a resource, an application must load it into memory. The FindResource and FindResourceEx functions find a resource in a module and return a handle to the binary resource data. Resources like menus allow users to interact with an application. To use one of the commands, the user accesses the list and clicks the desired item. Some example of resources : Caret - A caret is a blinking line, block, or bitmap in the client area of a window. The caret typically indicates the place at which text or graphics will be inserted. Cursors - A cursor is a small picture whose location on the screen is controlled by a pointing device, such as a mouse, pen, or trackball. In the remainder of this overview, the term mouse refers to any pointing device. Icon - An icon is a picture that consists of a bitmap image combined with a mask to create transparent areas in the picture.

2. Message and events :


The computer is a machine that only follows instructions. It almost doesn't know anything. Because of this, the computer cannot predict what a user wants to do with the computer. In fact, a great deal of the responsibility is left to the programmer who must decide what can and cannot or should not be done on an application. To help the users with computer interaction, the operating system provides a series of objects called Windows controls. The programmer decides what objects are necessary for a given application. A message to Windows must provide four pieces of information:
WHO sent the message? The operating system needs this name to identify every object, for any reason. An object in Microsoft Windows is identified as a Handle. For Windows controls, the handle is called HWND. WHAT message? To make matters a little easier, each message is a constant positive natural number (unsigned integer) identified with a particular name. Therefore, the message identifier is passed as UINT. Accompanying items: Because there are so many types of messages, you must provide two additional pieces of information to help process the message. These two items depend on the type of message and could be anything. The first accompanying item is a 32-bit type (unsigned int) called WPARAM (stands for WORD Parameter; in other words, it is a WORD (unsigned int) argument). The second accompanying item is a 32-

bit type of value (long) calle LPARAM (stands for LONG Parameter; in other words, it is a LONG (long in C/C++) argument). Remember that these two can be different things for different messages.

To manage the messages sent to Windows, they are communicated through a function pointer called a Windows procedure. The name of the function is not important but it must return a 32-bit integer, in fact a C/C++ long or Win32 LONG. Therefore, it is declared as LRESULT (LONG Result). Because this is a function pointer, it must be declared and defined as CALLBACK. The messages can be carried in a function defined as follows:
LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

WM_CREATE: Once you have decided to create a message, you send a message to Windows. Actually, when you are creating a window, a message called WM_CREATE is sent to Windows. This is the favorite message you can use to perform any early processing that you want to make sure happens before most other things show up. For example, you can use this message to initialize anything in your application. Therefore, this message is the first sent to the operating system. WM_SHOWWINDOW: After a window has been created, it needs to be displayed, that is, the window needs to be shown. Also, if the window was previously hidden, you can decide to show it. On the other hand, if a window is displaying, you may want to hide it, for any reason you judge necessary. To take any of these actions, that is, to show or hide a window, you must send the WM_SHOWWINDOW message. WM_ACTIVATE: When two or more windows are running on the computer, only one can receive input from the user, that is, only one can actually be directly used at one particular time. Such a window has a title bar with the color identified in Control Panel as Active Window. The other window(s), if any, display(s) its/their title bar with a color called Inactive Window.

Windows message event

Keyboard Message passing

Message passing using Threads


10

3. Characteristics of a Window :
For the user to use an application, it must display the window that can be located on the screen. A window is primarily distinguishable from the rest of the screen by its being surrounded by borders. To create a window that has borders, add the WS_BORDER flag to the dwStyle of the CreateWindow() or the CreateWindowEx() functions. When we create a window using the CreateWindow() or the CreateWindowEx() functions, we can specify the caption by passing a string to the lpWindowName argument. This will provide a name to that window created. When a window comes up, it may start on top with a long bar called the title bar. If you want a window to be equipped with a title bar, add the WS_CAPTION flag to the dwStyle of the CreateWindow() or the CreateWindowEx() functions. The title bar itself is divided in three sections. On the left side, there may be a small picture we call an icon and it is primarily managed as a Windows resource, through a name. To display this icon, the window must be created with the WS_SYSMENU flag to its list of styles. To display the Maximize amd Minimize button, when creating the window, add the WS_MAXIMIZEBOX and WS_MINIMIZEBOX flag to the dwStyle of the CreateWindow() or the CreateWindowEx() functions.

4. Management of different windows :


A window is referred to as parent when it can be used to host, hold, or carry other As mentioned already, to get its handle, you can call the GetDesktopWindow() function. For examples, when the computer starts, it draws its main screen, also called the desktop, which occupies the widest area that the monitor screen can offer. This primary window becomes the host of all other window that will display as long as the computer is own. This desktop is also a complete window in its own right.
11

If an application has already been created, to get its instance, you can call the GetWindowLong() function. Its syntax is: LONG GetWindowLong(HWND hWnd, int nIndex);

There are two types of windows or object you will deal with in your applications. The type referred to here is defined by the relationship a window has with regards to other windows that are part of an application: Parent: A window is referred to as a parent when there are, or there can be, other windows that depend on it. For example, the toolbar of your browser is equipped with some buttons. The toolbar is a parent to the buttons. When a parent is created, it "gives life" to other windows that can depend on it. Child: A window is referred to as child when its existence and especially its visibility depend on another window called its parent.

After creating the main window, you can use it as a parent for other windows. To specify that a window is a child of another window, when creating it with either the CreateWindow() or the CreateWindowEx() function, pass the handle of the parent as the hWndParent argument. If a window is a child of another window, to get a handle to its parent, you can call the GetParent() function.

12

5. GDI (Graphical Device Interface) :


The Graphics Device Interface (GDI) is a Microsoft Windows application programming interface and core operating system component responsible for representing graphical objects and transmitting them to output devices such as monitors and printers. GDI is responsible for tasks such as drawing lines and curves, rendering fonts and handling palettes. It is not directly responsible for drawing windows, menus, etc.; that task is reserved for the user subsystem, which resides in user32.dll and is built atop GDI. HDC: This is the most fundamental class to draw in your applications. It provides all of the primary functions used to perform the basic drawing steps. In order to use this class, first declare a variable from it. Then call the BeginPaint() function to initialize the variable using the PAINSTRUCT class. Once the variable has been initialized, you can use it to draw. After using the device context call the EndPaint() function to terminate the drawing. After declaring this variable, you must prepare the application to paint by initializing it with a call to the BeginPaint() function. The syntax of the BeginPaint() function is: HDC BeginPaint(HWND hWnd, LPPAINTSTRUCT lpPaint);

Using GDI properties and functions we can use various functions to create line, square, rectangle, circle, ellipse, polygons and other fundamental diagrams. The functions are Line(), Rectangle(), Circle(), Polygon(), PolyPolygon(), Point(), etc.

The color is one the most fundamental objects that enhances the aesthetic appearance of an object. The color is a non-spatial object that is added to an object to modify some of its visual aspects. The MFC library, combined with the Win32 API, provides various actions you can use to take advantage of the various aspects of colors. Three numeric values are used to create a color. Each one of these values is 8 bits. The first number is called red. The second is called green. The third is called blue.

Converted to decimal, each one of these numbers would produce:

13

Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. These three numbers are combined to produce a single number as follows:

Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available. Microsoft Windows considers that a color is a 32-bit numeric value. Therefore, a color is actually a combination of 32 bits:

The bits of the most significant byte (the left byte) are reserved for the operating system's internal use and must be set to 0. Based on this, each color is characterized by its combination of a red, a green, and a blue values.

GDI interface to Kernel

GUI sequence diagram

14

6. GDI tools :
Fonts : A font is a list of symbols that can be drawn on a device context to produce a symbol. A font is designed by an artist but usually follows a specific pattern. For example a font designed to produce symbols readable in the English language must be designed by a set of predetermined and agreed upon symbols. These English symbols are grouped in an entity called the English alphabet. When designing such a font, the symbols created must conform to that language. This also implies that one font can be significantly different from another and a font is not necessarily a series of readable symbols. Before using a font to draw a symbol on a device, the font must have been installed. Microsoft Windows installs many fonts during setup. To handle its various assignments, the operating system uses a particular font known as the System Font. This is the font used to display the menu items and other labels for resources in applications. If you want to use a different font to draw text in your application, you must select it. A font in Microsoft Windows is stored as an HFONT value. To Create a font, you can use the CreateFont() function.

Pens : In order to draw, two primary objects are needed: a platform and a tool. So far, we were using the platform, called a device context. We introduced the device context class as HDC. The device context is a combination of the platform on which the drawing is performed and the necessary tools to draw on it. A pen is a tool used to draw lines and curves on a device context. In the graphics programming, a pen is also used to draw the borders of a geometric closed shape such as a rectangle or a polygon. To make it an efficient tool, a pen must produce some characteristics on the lines it is asked to draw. These characteristics can range from the width of the line drawn to their colors, from the pattern applied to the level of visibility of the lines. To manage these properties, Microsoft Windows considers two types of pens: cosmetic and geometric. To create a pen, you can call the CreatePen() function A pen is referred to as cosmetic when it can be used to draw only simple lines of a fixed width, less than or equal to 1 pixel. A pen is geometric when it can assume different widths and various ends. Brushes : A brush is a drawing tool used to fill out closed shapes or the interior of lines. Using a brush is like picking up a bucket of paint and pouring it somewhere. In the case of computer graphics, the area where you position the brush is called the brush origin. The color or pattern that the brush holds would be used to fill the whole area until the brush finds a limit set by some rule. A brush can be
15

characterized by its color (if used), its pattern used to fill the area, or a picture (bitmap) used as the brush. A brush is referred to as solid if it is made of a color simply used to fill a closed shape. To create a solid brush, call the CreateSolidBrush() function. Its syntax is: HBRUSH CreateSolidBrush(COLORREF crColor);

GDI Interface

GDI tools

16

7. Bitmaps :
A bitmap is a series of points (bits) arranged like a map so that, when put together, they produce a picture that can be written to, copied from, re-arranged, changed, manipulated, or stored as a a computer file. Bitmaps are used to display pictures on graphical applications, word processors, database files, or audience presentations. To display its product on a device such as a monitor or a printer, a bitmap holds some properties and follows a set of rules. There are various types of bitmaps, based on the number of colors that the bitmap can display. First of all, a bitmap can be monochrome, in which case each pixel corresponds to 1 bit. A bitmap can also be colored. The number of colors that a bitmap can display is equal to 2 raised to the number of pits/pixel. For example, a simple bitmap uses only 4 pits/pixel or 4 bpp can handle only 24 = 16 colors. A more enhanced bitmap that requires 8 bpp can handle 28 = 256 colors. Bitmaps are divided in two categories that control their availability to display on a device. A device-independent bitmap (DIB) is a bitmap that is designed to be loaded on any application or display on any device and produce the same visual effect. To make this possible, such a bitmap contains a table of colors that describes how the colors of the bitmap should be used on pixels when displaying it. The characteristics of a DIB are defined by the BITMAPINFO structure. A device-dependent bitmap (DDB) is a bitmap created from the BITMAP structure using the dimensions of the bitmap. Unlike the other GDI tools, creating a bitmap usually involves more steps. For example, you may want to create a bitmap to display on a window. You may create another bitmap to paint a geometric area, in which case the bitmap would be used as a brush. Before creating a bitmap as a GDI object, you should first have a bitmap. You can do this by defining an array of unsigned hexadecimal numbers. Such a bitmap can be used for a brush. One way you can use a bitmap is to display a picture on a window. To do this, you must first have a picture resource. Although the image editors of both Borland C++ Builder and Microsoft Visual C++ are meant to help with regular application resources, they have some limitations. Nevertheless, once your bitmap is ready, call the LoadBitmap() function. Its syntax is: HBITMAP LoadBitmap(HINSTANCE hInstance, LPCTSTR lpBitmapName); Before selecting the newly created bitmap object, allocate a block of computer memory that would hold the bitmap. You can then copy it to the actual device. This job can be taken care of by the CreateCompatibleDC() function. Its syntax is: HBITMAP CreateCompatibleBitmap(HDC hdc, int nWidth, int nHeight);

17

System and Hardware requirements

Software Requirements : Win 98 (32-bit) or later. Windows Server 2008 R2 recommended. Microsoft DirectX 8.0 or later

Hardware Requirements : 800 MHz minimum ~ 1.2 GHz for graphical operations 256 MB RAM (For 98, 2000 and XP) ~ 512 (Vista and Win7) 500 MB free hard-disk space 128 MB video (simple use) ~ 256 MB video (graphical operations)

Other optional Requirements : Adobe Flash player 8 or higher Adobe Shockwave player 7 or higher

18

CONCLUSION
As we can see that Windows APIs are being used in every field of Windows programming to make different applications. WIN32 API is a huge field and it is widely being used in approximately every application to make it user friendly by using GUI technology of various APIs of Windows. This helps an user to conveniently use GUI based applications. We can create infinite numbers of applications using C++, and integrating it with modern APIs and GUIs even increases its capability and usability. Above discussion partially completes our project on WIN32 API Programming which covers basic functions of WIN32 APIs, its implementations and uses in Windows programming. This project is being extended to implement and integrate various WIN32 API and GUI properties to produce some useful and new applications.

19

You might also like