Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1253 Visual Programming Unit I Windows Environment

Similar presentations


Presentation on theme: "CS 1253 Visual Programming Unit I Windows Environment"— Presentation transcript:

1 CS 1253 Visual Programming Unit I Windows Environment

2 Visual Programming It allows programming with visual expressions, spatial arrangement of text and grahical symbols. It allows the users to manipulate the programming elements graphically WIN 32 Application Application Framework MFC AppWizard(exe) 17-Feb-19 Shyamala devi.M

3 Overview of syllabus Windows Programming
Visual C++ Programming – Introduction Document and View Architecture ActiveX Controls and Object Linking and Embedding Advanced Concepts in VC++ 17-Feb-19

4 Unit I - Presentation Outline
Windows environment Windows and messages Creating the window Displaying the window Message loop Window procedure Message processing Text output Introduction to GDI Device context Basic drawing Child window controls 17-Feb-19 Shyamala devi.M

5 Windows environment History of windows Aspects of windows
Dynamic linking 17-Feb-19

6 History of windows Windows was announced by Microsoft Corporation in November 1983 (post-Lisa, pre-Macintosh) – drivers for video display and printers Windows 2.0 (1987) – keyboard and mouse interface, running in real mode and access 1MB of memory. Windows 3.0 (1990) – protected mode and access 16 MB and has GPM and provides GUI Windows version 3.1 (1992) – TTFT, OLE , Multimedia Windows NT, introduced in Networking Windows 95 (1995) –lacks security and portability Windows 98 (1998) – H/W support and integrated to Internet Windows 98 and Windows NT are 32-bit preemptive multitasking. 17-Feb-19

7 Aspects of windows Earlier versions of Windows are nonpreemptive - no timeslicer Graphical user interface (GUI) - "visual interface" or "graphical windowing environment.“ WYSIWYG (what you see is what you get). In GUI - the video display itself becomes a source of user input. Interaction between the user and a program becomes more intimate. All applications have the same fundamental look and feel. Window - usually a rectangular area on the screen. Programs running in Windows can share routines that are located in other files called "dynamic-link libraries." 17-Feb-19 Shyamala devi.M

8 Dynamic linking Exported functions are implemented in dynamic-link libraries, DLLs. When we run a Windows program, it interfaces to Windows through a process called "dynamic 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, which are also loaded into memory. When we link a Windows program to produce an executable file, we must link with special "import libraries". These import libraries contain the dynamic-link library names and reference information for all the Windows function calls. 17-Feb-19

9 Windows and messages A window is a rectangular area on the screen that receives user input and displays output in the form of text and graphics. The window receives the user input in the form of "messages" to the window. TWO PARTS Window class Window procedure 17-Feb-19 Shyamala devi.M

10 Window class Window is always created based on the features called "window class." The window class sends the messages to the window procedure. Multiple windows on the same window class use same window procedure. Main subsystem of windows Kernel - HInstance User - Hwnd GDI - Hdc 17-Feb-19

11 Window procedure Every window has an associated window procedure.
It is a function that could be either in the program itself or in a dynamic-link library. Window procedure process the message given by the window class. Two types of messages Queued messages Non queued messages 17-Feb-19

12 Window Flow Diagram Messages Window class Message loop
Window procedure Processed message 17-Feb-19

13 Hungarian Notation Hungarian notation system is the process of prefacing the variable name with a short prefix that indicates the data type of the variable. For example, the sz prefix in szCmdLine stands for "string terminated by zero." The h prefix in hInstance and hPrevInstance stands for "handle;" The i prefix in iCmdShow stands for "integer." 17-Feb-19

14 For example, in the WinMain function, the msg variable is a structure of the MSG type; wndclass is a structure of the WNDCLASS type. typedef struct tagMSG { HWND hwnd ; UINT message ; WPARAM wParam ; LPARAM lParam ; DWORD time ; POINT pt ; } 17-Feb-19

15 Creating the window 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 17-Feb-19

16 Creating the Window –Overlapped window
#define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | \ WS_CAPTION | \ WS_SYSMENU | \ WS_THICKFRAME | \ WS_MINIMIZEBOX | \ WS_MAXIMIZEBOX) In WINUSER.H, this style is a combination of several bit flag The CreateWindow call returns a handle to the created window. 17-Feb-19

17 Registering the window class
if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } 17-Feb-19

18 Displaying the window After the CreateWindow call returns, the window has been created internally in Windows. Windows has allocated a block of memory to hold all the information about the window. Two calls are needed to make the window for dispaly. ShowWindow (hwnd, iCmdShow) ;  The ShowWindow function puts the window on the display. UpdateWindow (hwnd) ;  causes the client area to be painted this by sending the window procedure a WM_PAINT message. 17-Feb-19 Shyamala devi.M

19 Message Loop while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg) ; DispatchMessage (&msg) ; } Windows maintains a "message queue" for each Windows. When an input event occurs, Windows translates the event into a "message" and it is placed in the program's message queue. A program retrieves these messages from the message queue by executing a block of code known as the "message loop“ TranslateMessage (&msg) ; passes the msg structure back to Windows for some keyboard translation. DispatchMessage (&msg) ; again passes the msg structure back to Windows. Windows then sends the message to the appropriate window procedure for processing 17-Feb-19 Shyamala devi.M

20 Window procedure The window procedure determines what the window displays in its client area and how the window responds to user input. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) Window procedure gets the messages from the window class and processes it. It identifies the messages by its message id. 17-Feb-19 Shyamala devi.M

21 Message processing Every message that a window procedure receives is identified by a number, which is the message parameter to the window procedure. The Windows header file WINUSER.H defines identifiers beginning with the prefix WM ("window message") for each type of message. When a window procedure processes a message, it should return 0 from the window procedure. All unprocessed messages must be passed to a Windows function DefWindowProc. 17-Feb-19 Shyamala devi.M

22 [process WM_CREATE message] return 0 ; case WM_PAINT :
switch (iMsg) { case WM_CREATE : [process WM_CREATE message] return 0 ; case WM_PAINT : [process WM_PAINT message] case WM_DESTROY : [process WM_DESTROY message] } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; 17-Feb-19 Shyamala devi.M

23 17-Feb-19 Shyamala devi.M

24 Program Entry Point int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) HEADER FILES : 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. PROGRAM MESSAGE BOX FUNCTION: MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ; Body of the message box Caption bar 17-Feb-19

25 Overall window creation
Window Main Variable declarations Window class registration Checking the registration Creating the window Displaying the window Message loop Window procedure WM_CREATE WM_PAINT WM_DESTROY DefWindowProc Program 17-Feb-19

26 Text output Painting and repainting WM_PAINT
Valid and In-valid rectangles 17-Feb-19 Shyamala devi.M

27 Painting and Repainting
When a program displays text or graphics in its client area, it is said to be "painting" its client area. Windows informs a window procedure that the client area needs painting by sending a WM_PAINT message. WM_PAINT Message When the Windows call the function UpdateWindow during initialization in WinMain, it calls WM_PAINT message. This message informs the window procedure that the client area must be painted. Then the window procedure should be ready at any time to process additional WM_PAINT messages and even to repaint the entire client area. 17-Feb-19

28 Previously hidden area of the window is brought into view
A window procedure receives a WM_PAINT message whenever one of the following events occurs: Previously hidden area of the window is brought into view when a user moves a window or uncovers a window. The user resizes the window. The program uses the ScrollWindow or ScrollDC function to scroll part of its client area. The program uses the InvalidateRect or InvalidateRgn function to explicitly generate a WM_PAINT message. Windows removes a dialog box or message box that was overlaying part of the window. A menu is pulled down and then released. A tool tip is displayed. The mouse cursor is moved across the client area. An icon is dragged across the client area. 17-Feb-19

29 Valid and Invalid rectangles
Window procedure should be prepared to update the entire client area whenever it receives a WM_PAINT message It needs to update only a smaller area, most often a rectangular area within the client area. Repainting is required only for the rectangular area uncovered when the dialog box is removed. That area is known as an "invalid region" or "update region.“ Windows internally maintains a "paint information structure" for each window. This structure contains the coordinates of the smallest rectangle that encompasses the invalid region. This is known as the "invalid rectangle.“ 17-Feb-19 Shyamala devi.M

30 Windows does not place multiple WM_PAINT messages in the message queue.
A window procedure can invalidate a rectangle in its own client area by calling InvalidateRect. A program can also validate any rectangular area within the client area by calling the ValidateRect function. A window procedure can obtain the coordinates of the invalid rectangle when it receives a WM_PAINT message by calling GetUpdateRect. 17-Feb-19

31 INTRODUCTION TO GDI DEVICE CONTEXT
Getting the device context – method one Paint information structure Getting the device context – method two Textmetric details 17-Feb-19

32 Device context Device Context ("DC") is a data structure maintained internally by GDI. A device context is associated with a particular display device, such as a video display or a printer. The values in the device context are graphics "attributes." These attributes defines how the GDI drawing functions work. When a program needs to paint, it must first obtain a handle to a device context. When we obtain this handle, Windows fills the internal device context structure with default attribute values. 17-Feb-19

33 Getting the device context – method one
case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; [use GDI functions] EndPaint (hwnd, &ps) ; return 0 ; We use DrawText() to display the text on the client area of the window. Paint information structure: typedef struct tagPAINTSTRUCT { HDC hdc ; BOOL fErase ;  0 means already erased RECT rcPaint ;  Structure of type Rect (left, right, top, bottom ) BOOL fRestore ; BOOL fIncUpdate ; BYTE rgbReserved[32] ; } PAINTSTRUCT ; 17-Feb-19

34 Getting the device context – method two
case WM_PAINT: { hdc = GetDC (hwnd) ; [use GDI functions] ReleaseDC (hwnd, hdc) ; } We use TextOut GDI function for displaying text and so we need textmetric details. TextOut (hdc, x, y, psText, iLength) ; 17-Feb-19

35 TEXTMETRIC typedef struct tagTEXTMETRIC { LONG tmHeight ;
LONG tmAscent ; LONG tmDescent ; LONG tmInternalLeading ; LONG tmExternalLeading ; LONG tmAveCharWidth ; LONG tmMaxCharWidth ; [other structure fields] }TEXTMETRIC, * PTEXTMETRIC ; 17-Feb-19

36 Text Metric structure :
17-Feb-19

37 Basic Drawing Structure of GDI Device context Drawing dots and lines
GDI mapping modes Rectangles, regions and clipping 17-Feb-19

38 Structure of GDI The primary goals of GDI is to support device-independent graphics. Windows programs should be able to run without problems on any graphics output device. Two groups of graphics output devices Raster devices Vector devices. Raster devices represent images as a rectangular pattern of dots. (video display adapters, dot-matrix printers, and laser printers.) Vector devices draw images using lines 17-Feb-19

39 GDI Function Calls GDI Primitives
Functions that get (or create) and release (or destroy) a device context (GetDC and ReleaseDC ) Functions that obtain information about the device context (GetTextMetrics) Functions that draw something (TextOut) Functions that set and get attributes of the device context (SetTextColor) Functions that work with GDI objects (CreatePen) GDI Primitives Lines and curved Lines Filled areas Bitmaps Text 17-Feb-19

40 Device context hdc = BeginPaint (hwnd, &ps) ; [other program lines]
EndPaint (hwnd, &ps) ; hdc =GetDC (hwnd) ; ReleaseDC (hwnd, hdc) ; hdc = GetWindowDC (hwnd) ; hdc =CreateDC (pszDriver, pszDevice, pszOutput, pData) ; DeleteDC (hdc) ; 17-Feb-19

41 Drawing Dots and Lines Setting Pixels Straight Lines
Bounding Box Functions Stock Pens Creating, Selecting, and Deleting Pens Filling the Gaps Drawing Mapping Modes 17-Feb-19

42 Setting the pixel GetPixel SetPixel
The GetPixel function returns the color of the pixel at the specified coordinate position: crColor = GetPixel (hdc, x, y) ; The SetPixel function sets the pixel at a specified x- and y-coordinate to a particular color: SetPixel (hdc, x, y, crColor) ; 17-Feb-19

43 Straight lines Windows can draw straight lines, elliptical lines and Bezier splines. Windows 98 supports seven functions that draw lines: LineTo Draws a straight line. Polyline and PolylineTo Draw a series of connected straight lines. PolyPolyline Draws multiple polylines. Arc Draws elliptical lines. PolyBezier and PolyBezierTo Draw Bezier splines. 17-Feb-19

44 Straight lines Five attributes of the device context affect the appearance of lines that we draw using these functions: current pen position (for LineTo, PolylineTo, PolyBezierTo, and ArcTo only), pen background mode background color and Drawing mapping mode. To draw a straight line, we must call two functions. MoveToEx (hdc, xBeg, yBeg, NULL) ; LineTo (hdc, xEnd, yEnd) ; PROGRAM MoveToEx sets the "current position" that specifies the point at which the line begins The LineTo function draws a straight line from the current position to the the end point of the line specified in Lineto function. 17-Feb-19


Download ppt "CS 1253 Visual Programming Unit I Windows Environment"

Similar presentations


Ads by Google