BZUPAGES.COM Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department...

21
BZUPAGES.COM Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.

Transcript of BZUPAGES.COM Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department...

BZUPAGES.COM

Lecture – 2 Miss. SADAF MAJEED SIAL

Computer Science Department Bahauddin Zakariya University Multan.

BZUPAGES.COM

Window is the rectangular area that a particular program occupies on the screen.

Single program can create many windows, usually the program has a single main or top-level window.

From the programmer’s perspective, a window is an object that receives and processes messages.

BZUPAGES.COM

BZUPAGES.COM

System Queue: There will be only one queue available for windows. Whenever an event occurs through a hardware the event name will be put into this queue in the name of a message.

Application Queue: Every application has its own queue. This queue gets messages from the System Queue.

BZUPAGES.COM

Finally the message goes to the message loop. This message loop gets the message from Application Queue and translates it into Windows character message and sends it to the appropriate Windows procedure.

BZUPAGES.COM

BZUPAGES.COM

#include<windows.h>Int _stdcall WinMain(HINSTANCE i,HINSTANCE

j,char *k,int l){HWND h;h=CreateWindow(“BUTTON”,”PressMe”,WS_OV

ERLAPPEDWINDOW,10,10,150,100,0,0,I,0);ShowWindow(h,l);

MessageBox(0,”BS-IT 5th”,”Computer”,0);return(0);

}

BZUPAGES.COM

A window is always created based on a window class.

The window is created using the CreateWindow function.

Here HWND is the handle to the window.HWND: Each window has a handle which is the pointer

to the window code in the memory.

this handle is called HWND.

Whenever a window is created using CreateWindow() function of SDK, it returns this pointer “hwnd” which points to the window code in the memory.

BZUPAGES.COM

CreateWindow(“BUTTON”,”PressMe”,WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,0);

Parameters:1. First parameter passed to Create() is the name of the

window class.

2. The Second parameter indicates the text that is going to appear on the button surface.

3. The third parameter specifies the window style.

4. The next four parameters specify the window’s initial position and size.(x,y,width,height)

5. The next three parameters specify the handles to the parent window, the menu and application instance.

6. The last parameter contains the extra informations.

BZUPAGES.COM

The next function call ShowWindow(hWND,nCmdShow);

hWnd is the handle to the window.

nCmdShow specifies how the window is to be displayed and is passed to WinMain as the argument.

BZUPAGES.COM

How do I get several windows on the screen?

BZUPAGES.COM

To create and display a window the following steps are to be followed.

Set up the window class Register the window class Create the window Display the window Paint the client area of the window.

BZUPAGES.COM

BZUPAGES.COM

#include<windows.h>long _stdcall myfunc(HWND,UINT,UINT,long);WNDCLASS a;int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l){

HWND h;MSG m; a.hInstance=i; a.lpszClassName="sadaf"; a.lpfnWndProc=myfunc; RegisterClass(&a); h=CreateWindow("sadaf","title",WS_OVERLAPPEDWINDOW,20,20,300,200,0,0,i,0); ShowWindow(h,l); while(GetMessage(&m,0,0,0))

DispatchMessage(&m); return 0;

}long _stdcall myfunc(HWND w, UINT x, UINT y, long z){

switch(x){case WM_DESTROY:

PostQuitMessage(0);break; default:

return DefWindowProc(w,x,y,z);}return 0;

}

BZUPAGES.COM

The WNDCLASS has 10 fields1. Wndclass.style = CS_VREDRAW | CS_HREDRAW

Specifies the style of the window.

2. Wndclass.lpfnWndProc = (WNDPROC);Specifies the message handling function of the window.

3. Wndclass.cbClsExtra = 0;reserve the some extra space at the end of the copy of the window class structure that the window stores.

4. Wndclass.cbWndExtra = 0;reserves some extra space at the end of all data structure for windows based on this class.

5. Wndclass.hInstance=hInstance;is the instance handle of the program. This is passed to the program as a parameter to WinMain.

BZUPAGES.COM

6. Wndclass.hCursor = LoadCursor (Null, IDC_ARROW);

specifies the shape the mouse cursor will take when it is positioned over the client area of our window.

7. Wndclass.hIcon=LoadIcon(Null, IDI_APPLICATION0);

Specifies the icon associated with a window. The icon will be displayed when the window is minimized.

8. Wndclass.lpszMenuName = Null;is the name of the menu associated with our field.

9. Wndclass.hbrBackground= GetStockObject(WHITE_BRUSH);

spcifies the brush to be used when a window needs to be painted.The program specifies a White Brush.

10. Wndclass.lpszClassName=“keybuttclass”contains the name of the window class.

BZUPAGES.COM

After filling up the WNDCLASS struct, register the class with

RegisterClass(&wndclass);

BZUPAGES.COM

Once the window class is registered, WinMain() calls the CreateWindow() function to create the application’s window.

Then GetMessage() function extracts messages from the message queue of our applications.

DispatchMessage( ) function dispatches the messages received to our window procedure, myfunc().

BZUPAGES.COM

Messages can be handles by our window procedure. If we don’t want to handle them then they should be passed on the default window procedure by calling the DefWindowProc( ) function.

WM_DESTROY message is generated when we close the window.◦ When we receives this message the loop will be

terminated.

BZUPAGES.COM

1. Draw a flowchart of Windows 3.1 Architecture and Windows 95 Applications.

2. Draw a simple flowchart of Windows program.

3. List out the Windows Type Styles that uses in CreateWindow( ) function.(atleast 15)

4. Explain in brief API.

5. Explain the various steps to create and display a window.

6. Write a program to display “Hello” at any place where I click with the left mouse button in the window?

BZUPAGES.COM

Dated: 20-10-10