CS1253 VISUAL PROGRAMMING

48
1 CS1253 VISUAL PROGRAMMING UNIT I WINDOWS PROGRAMMING Windows environment – a simple windows program – windows and messages – creating the window – displaying the window – message loop – the window procedure – message processing – text output – painting and repainting – introduction to GDI – device context – basic drawing – child window controls

description

CS1253 VISUAL PROGRAMMING. UNIT I WINDOWS PROGRAMMING - PowerPoint PPT Presentation

Transcript of CS1253 VISUAL PROGRAMMING

Page 1: CS1253                                VISUAL PROGRAMMING

1

CS1253 VISUAL PROGRAMMING

UNIT I WINDOWS PROGRAMMINGWindows environment – a simple windows program – windows and messages – creating the window – displaying the window – message loop – the window procedure – message processing – text output – painting and repainting – introduction to GDI – device context – basic drawing – child window controls

Page 2: CS1253                                VISUAL PROGRAMMING

2

CS1253 VISUAL PROGRAMMING

UNIT II VISUAL C++ PROGRAMMING – INTRODUCTION

Application Framework – MFC library – Visual C++ Components – Event Handling – Mapping modes – colors – fonts – modal and modeless dialog – windows common controls – bitmaps

Page 3: CS1253                                VISUAL PROGRAMMING

3

CS1253 VISUAL PROGRAMMING

UNIT III THE DOCUMENT AND VIEW ARCHITECTURE

Menus – Keyboard accelerators – rich edit control – toolbars – status bars – reusable frame window base class – separating document from its view – reading and writing SDI and MDI documents – splitter window and multiple views – creating DLLs – dialog based applications

Page 4: CS1253                                VISUAL PROGRAMMING

4

CS1253 VISUAL PROGRAMMING

UNIT IV ACTIVEX AND OBJECT LINKING AND EMBEDDING (OLE)

ActiveX controls Vs. Ordinary Windows Controls – Installing ActiveX controls – Calendar Control – ActiveX control container programming – create ActiveX control at runtime – Component Object Model (COM) – containment and aggregation Vs. inheritance – OLE drag and drop – OLE embedded component and containers – sample applications

Page 5: CS1253                                VISUAL PROGRAMMING

5

CS1253 VISUAL PROGRAMMING

UNIT-V ADVANCED CONCEPTS

Database Management with Microsoft ODBC – Structured Query Language – MFC ODBC classes – sample database applications – filter and sort strings – DAO concepts – displaying database records in scrolling view – Threading – VC++ Networking issues – Winsock – WinInet – building a web client – Internet Information Server – ISAPI server extension – chat application – playing and multimedia (sound and video) files

Page 6: CS1253                                VISUAL PROGRAMMING

6

CS1253 VISUAL PROGRAMMING

TEXT BOOKS

1.Charles Petzold, “Windows Programming”, Microsoft press, 1996 (Unit I – Chapter 1-9)

2.David J.Kruglinski, George Shepherd and Scot Wingo, “Programming Visual C++”, Microsoft press, 1999 (Unit II – V)

REFERENCES

1.Steve Holtzner, “Visual C++ 6 Programming”, Wiley Dreamtech India Pvt. Ltd., 2003.

Page 7: CS1253                                VISUAL PROGRAMMING

7

UNIT – I

Windows Programming

Text Book :

Charles Petzold, “Windows Programming”, Microsoft press, 1996

(Unit I – Chapter 1-9)

Page 8: CS1253                                VISUAL PROGRAMMING

8

Windows ProgrammingHistory of Windows:

IBM PC – 1981

OS – MSDOS ( Microsoft Disk Operating System)

– MS DOS was minimal operating system.

– For the user, it provided a command-line interface to

commands such as DIR and TYPE and loaded

application programs into memory for execution.

Page 9: CS1253                                VISUAL PROGRAMMING

9

Windows ProgrammingHistory of Windows:

• For the application programmer, MS-DOS offered little more

than a set of function calls for doing file input/output (I/O).

• For other tasks—in particular, writing text and sometimes

graphics to the video display—applications accessed the

hardware of the PC directly.

Page 10: CS1253                                VISUAL PROGRAMMING

10

Windows ProgrammingHistory of Windows:

Apple Computer – Jan 1983

• OS - Lisa

• set a standard for graphical environments with

Macintosh in Jan 1984.

Windows

• announced by Microsoft corporation in Nov 1983 (post-Lisa but

pre-Macintosh)

Page 11: CS1253                                VISUAL PROGRAMMING

11

Windows ProgrammingHistory of Windows:

Windows1.0

• Nov 1985.

• with several updates to support the international market.

• with additional drivers for additional video displays and

printers.

Page 12: CS1253                                VISUAL PROGRAMMING

12

Windows ProgrammingHistory of Windows:

Windows2.0

• Nov 1987.

• with several changes to the user interface.

• also enhancements to the keyboard and mouse interface,

particularly for menus and dialog boxes.

Page 13: CS1253                                VISUAL PROGRAMMING

13

Windows ProgrammingHistory of Windows:

Windows2.0

• requires only Intel 8086 or 8088 microprocessor running in

"real mode" to access 1 megabyte (MB) of memory.

Windows/386

• released shortly after Windows 2.0

• used the "virtual 86" mode of the Intel 386 microprocessor to

window

• multitask many DOS programs that directly accessed

hardware.

Page 14: CS1253                                VISUAL PROGRAMMING

14

Windows ProgrammingHistory of Windows:

Windows/286

• Windows 2.1 was renamed Windows/286.

Windows3.0

• May 22, 1990.

• Windows/286 and Windows/386 versions were merged into one

product with this release.

Page 15: CS1253                                VISUAL PROGRAMMING

15

Windows ProgrammingHistory of Windows:

Windows3.1 – April 1992

• several significant features included the TrueType font

technology, multimedia (sound and music), Object Linking and

Embedding (OLE), and standardized common dialog boxes.

• ran only in protected mode.

• required a 286 or 386 processor with at least 1 MB of memory.

Page 16: CS1253                                VISUAL PROGRAMMING

16

Windows ProgrammingHistory of Windows:

WindowsNT – July 1993

• was the first version of Windows to support the 32-bit mode of

the Intel 386, 486, and Pentium microprocessors.

• Programs that run under Windows NT have access to a 32-bit

flat address space and use a 32-bit instruction set.

• required a 286 or 386 processor with at least 1 MB of was also

designed to be portable to non-Intel processors.

• it runs on several RISC-based workstations.

Page 17: CS1253                                VISUAL PROGRAMMING

17

Windows ProgrammingHistory of Windows:

Windows95 – Aug 1995

• also supported the 32-bit programming mode of the Intel 386

and later microprocessors.

• Adv - requiring fewer hardware resources.

• Dis adv - lacked some of the features of Windows NT, such as

high security and portability to RISC machines.

Page 18: CS1253                                VISUAL PROGRAMMING

18

Windows ProgrammingHistory of Windows:

Windows95 – June 1998

• has a number of enhancements such as,

• performance improvements. • better hardware support.

• a closer integration with the Internet and the World Wide

Web.

Page 19: CS1253                                VISUAL PROGRAMMING

19

Windows ProgrammingDynamic Linking:

Windows provides function calls that an application needs to

implement its user interface and display text and graphics on the

video display.

These functions are implemented in dynamic-link libraries, or

DLLs. These are files with the extension .DLL or sometimes .EXE.

They are located in

Win98 - \WINDOWS\SYSTEM

WinNT - \WINNT\SYSTEM and \WINNT\SYSTEM32

Page 20: CS1253                                VISUAL PROGRAMMING

20

Windows ProgrammingDynamic Linking:

In the early days, Windows was implemented in just three

dynamic-link libraries.

These represented the three main subsystems of Windows, which

were referred to as Kernel, User, and GDI.

• Kernel - handles memory management, file I/O and tasking.

• User - refers to the user interface, and implements all the

windowing logic.

• GDI - is the Graphics Device Interface, which allows a

program to display text and graphics on the screen

and printer.

Page 21: CS1253                                VISUAL PROGRAMMING

21

Windows ProgrammingDynamic Linking:

Windows98 supports several thousand function calls that

applications can use.

Each function has a descriptive name, such as CreateWindow.

• This function creates a window for the program.

• All the Windows functions that an application may use are declared in

header files.

Page 22: CS1253                                VISUAL PROGRAMMING

22

Windows ProgrammingDynamic Linking:

In Windows program, we use the Windows function calls same as

C library functions like strlen.

Difference –

• The machine code for C library functions is linked into program

code.

• whereas the code for Windows functions is located outside of

the program in the DLLs.

Page 23: CS1253                                VISUAL PROGRAMMING

23

Windows ProgrammingDynamic Linking:

When you run a Windows program,

• it interfaces to Windows through a process called "dynamic

linking“.

• A Windows .EXE file contains references to the various

dynamic-link libraries.

Page 24: CS1253                                VISUAL PROGRAMMING

24

Windows ProgrammingDynamic Linking:

When a Windows program is loaded into memory,

• the calls in the program are resolved to point to the entries of

the DLL functions.

• DLL functions are also loaded into memory if not already there.

Page 25: CS1253                                VISUAL PROGRAMMING

25

Windows ProgrammingDynamic Linking:

When link a Windows program to produce an executable file,

• must link with special "import libraries" provided with

programming environment.

• These import libraries contain the dynamic-link library names

and reference information for all the Windows function calls.

• The linker uses this information to construct the table in

the .EXE file that Windows uses to resolve calls to Windows

functions when loading the program.

Page 26: CS1253                                VISUAL PROGRAMMING

26

Simple Windows ProgramFirst Windows Program:

The Windows program has exactly the same components as the

character-mode version.

It has

• an include statement,

• a program entry point,

• a function call, and

• a return statement.

Page 27: CS1253                                VISUAL PROGRAMMING

27

Simple Windows ProgramFirst Windows Program:

For Example,

To display "Hello Welcome!!!” in a message box.

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE

hPrevInstance, PSTR szCmdLine, int iCmdShow)

{

MessageBox (NULL, TEXT ("Hello Welcome!!!"), TEXT

("HelloMsg"), 0) ;

return 0 ;

}

Page 28: CS1253                                VISUAL PROGRAMMING

28

Simple Windows ProgramFirst Windows Program:

The header file STDIO.H has been replaced with WINDOWS.H.

The entry point main has been replaced with WinMain.

The C run-time library function printf has been replaced with the

Windows API function MessageBox.

However, there is much in the program that is new, including

several strange-looking uppercase identifiers.

Page 29: CS1253                                VISUAL PROGRAMMING

29

Simple Windows ProgramThe Header Files:

#include <windows.h>

WINDOWS.H is a master include file that includes other

Windows header files.

The most important and most basic of these header files are:

• WINDEF.H -   Basic type definitions. • WINNT.H -   Type definitions for Unicode support. • WINBASE.H -  Kernel functions. • WINUSER.H   - User interface functions. • WINGDI.H   - Graphics device interface functions.

Page 30: CS1253                                VISUAL PROGRAMMING

30

Simple Windows ProgramThe Header Files:

These header files define all the Windows data types, function

calls, data structures, and constant identifiers.

They are an important part of Windows documentation.

It is convenient to use the Find In Files option from the Edit menu

in the Visual C++ Developer Studio to search through these

header files.

It can also open the header files in the Developer Studio and

examine them directly.

Page 31: CS1253                                VISUAL PROGRAMMING

31

Simple Windows ProgramProgram Entry Point:

Just as the entry point to a C program is the function main, the

entry point to a Windows program is WinMain, which always

appears like this:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE

hPrevInstance, PSTR szCmdLine, int iCmdShow)

This entry point is documented in,

/Platform SDK/User Interface

Services/Windowing/Windows/Window Reference/Window

Functions.

Page 32: CS1253                                VISUAL PROGRAMMING

32

Simple Windows ProgramProgram Entry Point:

It is declared in WINBASE.H like so (line breaks and all):

int

WINAPI

WinMain(

HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nShowCmd

); LPSTR & PSTR are two data types defined in WINNT.H as pointers

to character strings. The LP prefix stands for "long pointer“.

Page 33: CS1253                                VISUAL PROGRAMMING

33

Simple Windows ProgramPrefix Data Typeb BOOLc or ch charclr COLORREFcx, cy Horizontal or vertical distancedw DWORDh Handlel LONGn intp Pointersz Zero-terminated stringw WORDwnd CWndstr CStringm_ class member variable

Hungarian Notation

Note:Prefixes can be combined:

pszNamem_nAge

Page 34: CS1253                                VISUAL PROGRAMMING

34

Simple Windows ProgramProgram Entry Point:

WinMain Parameters:

First parameter -

• It is something called an "instance handle“.

• In Windows programming, a handle is simply a number that an

application uses to identify something.

• In this case, the handle uniquely identifies the program.

Page 35: CS1253                                VISUAL PROGRAMMING

35

Simple Windows ProgramProgram Entry Point:

WinMain Parameters:

Second parameter -

• A program could determine if other instances of itself were

running by checking the hPrevInstance parameter.

• It could then skip certain chores and move some data from the

previous instance into its own data area.

• In the 32-bit versions of Windows, this concept has been abandoned. The second parameter to WinMain is always NULL (defined as 0).

Page 36: CS1253                                VISUAL PROGRAMMING

36

Simple Windows ProgramProgram Entry Point:

WinMain Parameters:

Third parameter -

• is the command line used to run the program.

• Some Windows applications use this to load a file into memory

when the program is started.

Fourth parameter –

• indicates how the program should be initially displayed—either

normally or maximized to fill the window, or minimized to be

displayed in the task list bar.

Page 37: CS1253                                VISUAL PROGRAMMING

37

Simple Windows ProgramMessageBox Function:

It is designed to display short messages.

The little window that MessageBox displays is actually considered

to be a dialog box, although not one with a lot of versatility.

For example,

MessageBox (NULL, TEXT ("Hello, Windows 98!"),

TEXT("HelloMsg"), 0) ;

Page 38: CS1253                                VISUAL PROGRAMMING

38

Simple Windows ProgramMessageBox Function:

Parameters:

First Parameter - is normally a window handle.

Second Parameter - is the text string that appears in the body

of the message box.

Third Parameter - is the text string that appears in the caption bar of the message box.

Page 39: CS1253                                VISUAL PROGRAMMING

39

Simple Windows ProgramMessageBox Function:

Parameters:

Fourth Parameter - can be a combination of constants beginning with the prefix MB_ that are defined in WINUSER.H.

• can pick one constant from the first set to indicate what buttons you

wish to appear in the dialog box:

Page 40: CS1253                                VISUAL PROGRAMMING

40

Simple Windows ProgramMessageBox Function:

Buttons:

#define MB_OK 0x00000000L

#define MB_OKCANCEL 0x00000001L

#define MB_ABORTRETRYIGNORE 0x00000002L

#define MB_YESNOCANCEL 0x00000003L

#define MB_YESNO 0x00000004L

#define MB_RETRYCANCEL 0x00000005L

Note: When set the fourth argument to 0, only the OK button

appears in the message box.

Page 41: CS1253                                VISUAL PROGRAMMING

41

Simple Windows ProgramMessageBox Function:

Buttons:

can use the OR (|) operator to combine one of the constants

shown above with a constant that indicates which of the buttons is

the default:

#define MB_DEFBUTTON1 0x00000000L

#define MB_DEFBUTTON2 0x00000100L

#define MB_DEFBUTTON3 0x00000200L

#define MB_DEFBUTTON4 0x00000300L

Page 42: CS1253                                VISUAL PROGRAMMING

42

Simple Windows ProgramMessageBox Function:

Buttons:

can also use a constant that indicates the appearance of an icon in the message box:

#define MB_ICONHAND 0x00000010L

#define MB_ICONQUESTION 0x00000020L

#define MB_ICONEXCLAMATION 0x00000030L

#define MB_ICONASTERISK 0x00000040L

Page 43: CS1253                                VISUAL PROGRAMMING

43

Simple Windows ProgramMessageBox Function:

Buttons:

Some of these icons have alternate names:

#define MB_ICONWARNING MB_ICONEXCLAMATION #define MB_ICONERROR MB_ICONHAND #define MB_ICONINFORMATION MB_ICONASTERISK #define MB_ICONSTOP MB_ICONHAND

Page 44: CS1253                                VISUAL PROGRAMMING

44

Simple Windows ProgramCompile, Link and Run:

When ready to compile HELLOMSG, select Build Hellomsg.exe

from the Build menu, or press F7, or select the Build icon from the

Build toolbar.

Alternatively, select Execute Hellomsg.exe from the Build menu,

or press Ctrl+F5, or click the Execute Program icon from the Build

toolbar.

Will get a message box asking you if you want to build the program.

Page 45: CS1253                                VISUAL PROGRAMMING

45

Simple Windows ProgramCompile, Link and Run:

During the compile stage,

• the compiler generates an .OBJ (object) file from the C source

code file.

During the link stage,

• the linker combines the .OBJ file with .LIB (library) files to

create the .EXE (executable) file.

• can see a list of these library files by selecting Settings from

the Project tab and clicking the Link tab.

Page 46: CS1253                                VISUAL PROGRAMMING

46

Simple Windows ProgramCompile, Link and Run:

Import Libraries:

In particular, KERNEL32.LIB, USER32.LIB, and GDI32.LIB.

These are "import libraries" for the three major Windows

subsystems.

They contain the dynamic-link library names and reference

information that is bound into the .EXE file.

Page 47: CS1253                                VISUAL PROGRAMMING

47

Simple Windows ProgramCompile, Link and Run:

Import Libraries:

Windows uses this information to resolve calls from the program

to functions in the KERNEL32.DLL, USER32.DLL and GDI32.DLL

dynamic-link libraries.

Configuration Files:

In the Visual C++ Developer Studio, compile and link the program

in different configurations.

By default, these are called Debug and Release.

Page 48: CS1253                                VISUAL PROGRAMMING

48

Simple Windows ProgramCompile, Link and Run:

Configuration Files:

The executable files are stored in subdirectories of these names.

In the Debug configuration, information is added to the .EXE file

that assists in debugging the program and in tracing through the

program source code.