Homework Title No

6
 Homework title no. : -1 Course code:-CAP625T Course instructor: - Miss.Nisha Rani Course tutor:-NA  D.of.allocation:-22/1/12 D.of.submission:12/1/12  Roll no.:- Section:  Declaration:-  I declare that this assignment is my individual work. I have not copied  from any other student work or from any other source expect where due acknowledgement is made explicitly in the text nor has any part being written for me by another person.   Signature Comments:-__________  Marks obtained:-________ ______________ Q1. Windows allows the user to switch between application. How?  Ans Context Switching  So, a program runs and calls API functions. How do other programs get a chance to run, then? Most of the time, programs simply allow the operating system to switch to another program because they are waiting  for something (human input, hard disk). The kernel then performs context  switching to allow another program to run. This is done by: 1. Savi ng the cu rrent pr ogra m's stat e (inclu ding re giste rs), 2. Figu ring out which prog ram t o run next, 3. and resto ring a diff erent progr am's state .  If a program (or thread to be more accurate) runs for more than a certain  period of time (the thread quantum), the operating system will context  switch to another program. This idea is called preemption.

Transcript of Homework Title No

Page 1: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 1/6

  Homework title no. : -1 Course code:-CAP625T 

Course instructor: - Miss.Nisha Rani Course tutor:-NA

  D.of.allocation:-22/1/12 D.of.submission:12/1/12

  Roll no.:- Section:

 Declaration:-

 I declare that this assignment is my individual work. I have not copied 

 from any other student work or from any other source expect where due

acknowledgement is made explicitly in the text nor has any part being 

written for me by another person.

  Signature

Comments:-________________________________________________

 Marks obtained:-___________________________________________

Q1. Windows allows the user to switch between application. How?

 Ans

Context Switching 

 So, a program runs and calls API functions. How do other programs get a

chance to run, then? Most of the time, programs simply allow the

operating system to switch to another program because they are waiting 

 for something (human input, hard disk). The kernel then performs context 

 switching to allow another program to run. This is done by:

1. Saving the current program's state (including registers),

2. Figuring out which program to run next,

3. and restoring a different program's state.

 If a program (or thread to be more accurate) runs for more than a certain

 period of time (the thread quantum), the operating system will context 

 switch to another program. This idea is called preemption.

Page 2: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 2/6

Q2. Windows program code consists of static and dynamic data. Take any

example to show how window program carries static data. Give the

 suitable coding example.

 Ans

 Static data example:-

#include<windows.h>>

  Long FAR PASCAL Windproof (HWND, UINT, WPARAM,

 LPARAM); /*one word and long data passed with the message */ 

into PASCAL Win Main(HANDLE hInstance,HANDLE 

hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)

HWND hWnd;

MSG msg; /*message structure*/ WNDCLASS wndclass; /*window class structure*/ 

if(!hPrevInstance)

wndclass.style =CS_HREDRAW| CS_VREDRAW; /*redraw 

window if horizontal or vertical size(1) changes*/ 

wndclass.lpfnWndProc =WndProc;

wndclass.cbClsExtra=0;

wndclass.cbWndExtra=0;

wndclass.hInstance=hInstance;

wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

 /*loads the idi_app icon*/ 

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); /*loads the

 standard arrow cursor shpe*/ 

wndclass.hbrBackground=GetStockObject(WHITE_BRUSH);

 /*loads the background predefined white brush*/ 

wndclass.lpszMenuName=NULL;

wndclass.lpszClassName="MYCLASS";

if(!RegisterClass (&wndclass))

return(0);

}

hWnd=CreateWindow("TEXT EXAMPLE","Minimal 

3",WS_OVERLAPPEDWINDOW,10,10,200,100,NULL,NULL,hInstance,

 NULL); /*standard window style for program windowP*/ 

ShowWindow(hWnd,nCmdShow);

Page 3: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 3/6

while(GetMessage(&msg,NULL,NULL,NULL))

 DispatchMessage(&msg);

 }

return(msg.wParam);

 }

long FAR PASCAL WndProc(HWND hWnd,UINT 

wMessage,WPARAM wParam,LPARAM lParam)

 Static BOOL bFirsTime = True;

 Static HANDLE hStatic text, hStaticIcon, HstaticRECT;

 HANDLE hInstance;

 switch(wMessage)

{ case WM_DESTROY:

if (bFirsTime)

 BFirsTime = Flase; /*get the instance handle */ 

hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);

hStayicText = createWindow(“STATIC”, THIS IS A TEXT” 

10,10,130,60, NULL,NULL,NULL,NULL);

 ShowWindow(hStaticText, SW_SHOWNORMAL);

 }

 Break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd,wMessage,wParam,lParam);

}

return(0L);}

Q3. Taking an example, show how Windows do default message

 processing?

Page 4: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 4/6

 Ans

 Example:-

#include <windows.h>

int PASCAL WinMain (HANDLE hInstance,HANDLE 

hPrevInstance,LPSTR lpszCmdLine,

int nCmdShow)

  HWND hWnd; /*Creating The program window*/ 

 MSG msg;

hWnd = CreateWindow ("BUTTON", "Hello World.",

 BS_PUSHBUTTON,

 

10,10,100,100,NULL,NULL,hInstance,NULL);

 ShowWindow (hWnd,nCmdShow);while (GetMessage(&msg,NULL,NULL,NULL))

if(msg.message==KEYUP)

break;

else

 

 DefWindowProc(msg.hwnd,msg.message,msg.wParam,msg.lParam);

}

return (0); }

Q4. Create a program window to show the text written by the user.

 Ans

#include<Windows.h>

char szSize[100];

char szTitle[] = "These are the dimensions of your client area:";

HFONT hfOld = SelectObject(hdc, hf);

SetBkColor(hdc, g_rgbBackground);

SetTextColor(hdc, g_rgbText);

if(g_bOpaque)

Page 5: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 5/6

SetBkMode(hdc, OPAQUE);

}

else

SetBkMode(hdc, TRANSPARENT);

}

DrawText(hdc, szTitle, -1, prc, DT_WORDBREAK);

wsprintf(szSize, "{%d, %d, %d, %d}", prc->left, prc->top, prc->right,

 prc->bottom);

DrawText(hdc, szSize, -1, prc, DT_SINGLELINE | DT_CENTER |

 DT_VCENTER);

SelectObject(hdc, hfOld); }

Q5. Identify the function and message designed to make termination of 

window application.

 Ans Function are:-

For keyboard:

 If(msg.message==WM_KEYDOWN)For mouse click:

•  If(msg.message==WM_RBUTTONDOWN)

•  If(msg.message==WM_LBUTTON 

 EXAMPLE:-

#include <windows.h>

int PASCAL WinMain (HANDLE hInstance,HANDLE 

hPrevInstance,LPSTR lpszCmdLine, int nCmdShow)

{   HWND hWnd; /*Creating The program window*/ 

 MSG msg;

hWnd = CreateWindow ("BUTTON", "Hello World.",

 BS_PUSHBUTTON, 10,10,100,100,NULL,NULL,hInstance,NULL);

 ShowWindow (hWnd,nCmdShow);

while (GetMessage(&msg,NULL,NULL,NULL))

Page 6: Homework Title No

8/3/2019 Homework Title No

http://slidepdf.com/reader/full/homework-title-no 6/6

if(msg.message==WM_KEYDOWN)

break;

if else(msg.message==WM_RBUTTONDOWN)

break;

if else(msg.message==WM_RBUTTONDOWN)

break;

else

 DefWindowProc(msg.hwnd,msg.message,msg.wParam,msg.lParam);

}

return (0);

}