230 likes | 401 Views
Visual Programming. Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan. What is a Window?. Window is the rectangular area that a particular program occupies on the screen.
E N D
Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
What is a Window? • 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.
Messages • Most messages come from Windows, but a window can also send itself a message. • A window is one program can send a message to a window in another program. • This can be visualized as a type of electronic mail system in which windows can communicate with each other. • These messages are put into a loop called as Message Queue. • Two kinds of message queues are • System Queue • Application Queue
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.
Message loop • 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.
#include<windows.h> Int _stdcall WinMain(HINSTANCE i,HINSTANCEj,char *k,int l) { HWND h; h=CreateWindow(“BUTTON”,”PressMe”,WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,I,0); ShowWindow(h,l); MessageBox(0,”BS-IT 5th”,”Computer”,0); return(0); }
CreateWindow() • 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.
CreateWindow(“BUTTON”,”PressMe”,WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,0);CreateWindow(“BUTTON”,”PressMe”,WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,0); Parameters: • First parameter passed to Create() is the name of the window class. • The Second parameter indicates the text that is going to appear on the button surface. • The third parameter specifies the window style. • The next four parameters specify the window’s initial position and size.(x,y,width,height) • The next three parameters specify the handles to the parent window, the menu and application instance. • The last parameter contains the extra informations.
Displaying the Window • 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.
WINDOW CLASS • 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.
#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; }
Components of WNDCLASS • The WNDCLASS has 10 fields • Wndclass.style = CS_VREDRAW | CS_HREDRAW Specifies the style of the window. • Wndclass.lpfnWndProc = (WNDPROC); Specifies the message handling function of the window. • Wndclass.cbClsExtra = 0; reserve the some extra space at the end of the copy of the window class structure that the window stores. • Wndclass.cbWndExtra = 0; reserves some extra space at the end of all data structure for windows based on this class. • Wndclass.hInstance=hInstance; is the instance handle of the program. This is passed to the program as a parameter to WinMain.
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. • Wndclass.hIcon=LoadIcon(Null, IDI_APPLICATION0); Specifies the icon associated with a window. The icon will be displayed when the window is minimized. • Wndclass.lpszMenuName = Null; is the name of the menu associated with our field. • Wndclass.hbrBackground= GetStockObject(WHITE_BRUSH); spcifies the brush to be used when a window needs to be painted.The program specifies a White Brush. • Wndclass.lpszClassName=“keybuttclass” contains the name of the window class.
RegisterClass() • After filling up the WNDCLASS struct, register the class with RegisterClass(&wndclass);
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().
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.
ASSIGNMENT: • Draw a flowchart of Windows 3.1 Architecture and Windows 95 Applications. • Draw a simple flowchart of Windows program. • List out the Windows Type Styles that uses in CreateWindow( ) function.(atleast 15) • Explain in brief API. • Explain the various steps to create and display a window. • Write a program to display “Hello” at any place where I click with the left mouse button in the window?
THANKS Dated: 20-10-10