CS-360, Practice Exam 1 Key
1 (A) All Windows applications use the "event-driven" programming paradigm.
Explain briefly what this is and how it differs from the traditional
"sequential" programming paradigm.
Sequential: Program does something (solicits input from user) and user
responds, program is in control
Event-driven: User does something and program responds, user in control
(B) All Win32 API applications implement the "event-driven" paradigm with
something called the message loop. What Win32 API function is usually used to
retrieve the next message in the program's message loop? What information is
contained in its parameters? What message should cause the loop to terminate?
GetMessage()
A pointer to a MSG structure that contains a handle to the window where the
message occurred, what the message was, and data passed in from the event
WM_QUIT
2. For each of the following Windows messages give one user action will
cause the message to be sent:
(A) WM_PAINT -- User causes the window to be exposed
(B) WM_COMMAND -- User clicks on a menu item
(C) WM_CHAR -- User types a keyboard/combination that represents a printable
character
(D) WM_MOUSEMOVE -- User moves mouse over client area of window
(E) WM_DESTROY -- User does something that causes the window to be destroyed
by the Window OS
(F) How should a Win32 API application respond to the WM_DESTROY to assure
normal termination of the program?
It should post a WM_QUIT message to the message queue so that the GetMessage()
loop will terminate, thus terminating the application
3. (A) In a Win32 API application, what happens after the Windows operating
system sends a message back to the application's message loop?
If it's not a WM_QUIT message, the program must dispatch it back to Windows
using the DispatchMessage (&msg) function.
(B) What function does the programmer have to write and what should that
function do?
The Window Procedure (WndProc) which will ultimately receive the message. It
should determine if the message is one "of interest" to the application and
take appropriate action.
4. Write some Visual C++ Win32 API code for an application's WndProc( ) that
will draw a red rectangle whose upper left-hand corner is at x=100, y=200 on
the window's client area and that has a width of 90 pixels and a height of 60
pixels whenever the right mouse button goes down. Assume the message that is
sent to the window when the button goes down is wMsg and that the window's
handle is hWnd.
switch(wMsg)
{
case(WM_RBUTTONDOWN)
HDC hDC = GetDC(hWnd);
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));
HPEN hOldPen = (HPEN)SelectObject(hDC,hPen);
Rectangle(hDC,100,200,90,60);
SelectObject(hDC, hOldPen);
DeleteObject(hPen);
ReleaseDC(hWnd, hDC);
break;
}
5. (A) What is the MFC method of handling user events and messages?
Message mapping macros map events to handler functions in CWnd that need to be
overridden or extended in order to be handled by the application.
(B) In an MFC document/view SDI application generated by Visual Studio's
AppWizard, there are always four classes. What are they (i.e., from what MFC
classes are they derived)?
Windows class -- CFrameWnd or CWnd
Document class -- CDocument
View class -- CView
Application class -- CWinapp
6. The following Visual C++ code is placed the view class of an MFC doc/view
application:
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
CDC* pD = GetDC();
CPen pPen(PS_SOLID,1,RGB(255,255,0));
CPen *pPenOld = pD->SelectObject(&pPen);
pD->Ellipse(point.x,point.y,point.x+20,point.y+20);
pD->SelectObject(pPenOld);
CView::OnMouseMove(nFlags, point);
}
(A) When will this code execute?
Whenever the user moves the mouse over the window's client area
(B) Exactly what will appear on the screen if the user glides the mouse
horizontally over the window's client area?
A circle with a one-pixel-wide, yellow border will be drawn inside a rectangular
border whose diagonally opposite corners are (x,y) and (x+20,y+20), where (x,y)
are the coordinates when the mouse moved.
7A. See notes
7B.
void CTextView::OnMakeChoice()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
CBrush b;
b.CreateSolidBrush(RGB(255,0,0));
pDC->SelectObject(&b);
CRect r;
GetClientRect(&r);
CChoice cdlg;
cdlg.DoModal();
if(cdlg.m_choice=="Choice 1")
pDC->Rectangle(&r);
if(cdlg.m_choice=="Choice 2")
pDC->Ellipdr(&r);
}
8. A NET Frmework C# class called Circ is supposed to encapsulate some
functionality associated with a circle. The class has a constructor, a private
integer field called radius, and a public property called Rad that provides
controlled access to the radius field. It also has a public method called
Circumference() that is supposed to compute and return the circumference
(length of the arc of the circle) of an instance of Circ. The following is
skeleton C# code for the Circumference() method:
public int Circumference()
{
// determines the circumference of a Circ object
// use 2*pi*r, where pi is the constant 3.14159, r the radius of the circle
return (2*3.1459*radius)
};
The constructor of the class is supposed to instantiate a circle whose radius
is assigned the value given in the parameter. The following is skeleton code
for the class constructor:
public void Circ(int r)
{
// constructs a circle with a radius r
radius = r;
}
(A) Write some C# code (not more than two lines) that will complete the bodies
of the constructor and of the Circumference( ) methods given above. Write your
code in the spaces after the comments.
(B) Write some C# code that will use this Circ class in a window form in the
following way: (1) create a new instance of the Circ class called myCirc that
has a radius of 50; (2) obtain and display the value of myCirc's radius in the
upper lefthand corner of the client area of a .NET window's form; (3) change
myCirc's radius to 100; (4) compute the circumference of myCirc and store it
in an integer variable called c. The two lines to be displayed on the window's
client area should look something like the following:
The circle had a radius of 50
The circle now has a radious of 100 and a circumference of 628
Circ myCirc = new Circ(50);
Graphics g = this.CreateGraphics();
g.DrawString("The circle has a radius of" + myCirc.Rad.toString(), this.Font,
Brushes.Black, 0,0);
myCirc.Rad = 100;
int c = myCircle.Circumference(100);
g.DrawString("The circle now has a radius of" + myCirc.Rad.toString() +
" and a circumference of " + c.toString() , this.Font, Brushes.Black, 0,30);
9. Assume the Form class of a .NET application has an event called SomeEv. The
delegate for that event has been defined as follows:
public delegate void SomeEvHandler(object objSender, EventArgs someEvArgs);
(A) Write a line of C# code that will add an event handler called MySomeEvHandler
to the form's SomeEv Event.
this.SomeEv += new SomeEvHandler(MySomeEvHandler);
(B) Add some lines of C# code to the skeleton MySomeEvHandler( ) code given below
that will cause the image stored in the file myPic.jpg to be displayed in the
client area of the application's window. The upper lefthand corner of the image
should be 100 pixels in from the window's lefthand border and 20 pixels down from
the top of the window's client area. (You may assume that the file myPic.jpg is
in the same directory as the program's executable file.)
private void MySomeEvHandler(object s, EventArgs e)
{
Image img = Image.FromFile("myPic.jpg");
Graphics g = this.CreateGraphics();
g.DrawImage(img, 100, 20);
}
10. In a .NET framework Windows Forms application the following is the skeleton
event handler for the form's MouseDown event. Add some C# code that will change
the title of the window to "A Mouse Button was Pressed" and show a message box
with the text "Left Mouse Button" if it was the left mouse button that was pressed
and a message box with the text "Right Mouse Button" if it was the right button
that was pressed.
void MyMDown(object sender, MouseEventArgs e)
{
this.Text = "A Button Button was Pressed";
if (e.Button == MouseButtons.Left)
MessageBox.Show("Left Mouse Button");
if (e.Button == MouseButtons.Right)
MessageBox.Show("Right Mouse Button");
}
11. (A) Write some Visual C# code that will create a Windows timer that begins
running and times out once every 5 seconds.
Timer t = new Timer();
t.Interval = 5000;
t.Start(); // or t.Enabled;
B. Assume the handler function for the timer event is called timer1_tick(...).
Write some C# code for this handler that will display your name at some random
location between (x=0, y=0) and (x=200, y=100) on the client area of the window
every time the timer times out.
void timer1_tick(object obj, EventArgs ea)
{
Random r = new Random();
Graphics g = this.CreateGraphics();
g.DrawString("My Name", this.Font, Brushes.Red, r.Next(0,100), r.Next(0,100));
}