CS-360, Practice
Exam I
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.
(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?
2. For each of the
following Windows messages give one user action will cause the message
to be sent:
(A) WM_PAINT
(B) WM_COMMAND
(C) WM_CHAR
<>(D) WM_MOUSEMOVE
(E) WM_DESTROY
(F) How should a Win32
API program respond to the WM_DESTROY to assure normal termination
of the program?
3A. In a Win32 API application, what happens
after the Windows operating system sends a
message back to the application's message loop?
3B. What function does the programmer have to
write and what should that function do?
4. Write some Visual C++ Win32 API code 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.
5A. What is the
MFC method of handling user events and messages?
5B. 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)?
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?
(B) Exactly what will
appear on the screen if the user glides the mouse horizontally over the
window's
client area?
7A. In an MFC Single Document Interface application the Visual Studio
Designer has been used to prepare
the template for a dialog box whose ID is IDD_CHOICE that contains a
Listbox control whose ID is IDC_C
containing the CStrings "Choice 1" and "Choice 2".
Explain how you would use Visual Studio to prepare a dialog box class
called CChoice based on the MFC
CDialog class that will encapsulate the dialog box in Part A.
7B. Assume you have declared a public CString variable called m_choice
in the CChoice class and that you
have used the "Dialog Data Exchange (DDX)" mechanism of associating the
m_choice variable with the
IDC_C Listbox control. The dialog box is to be invoked in response to
the user clicking on a "Make Choice"
menu item on the main window. In the skeleton menu item handler below
write some Visual C++ code that
will start the dialog box modally and display either a filled red
square or circle centered in the middle of the client
area of the parent window, depending whether the user chose
"Choice 1" or "Choice 2" from the its list box
before clicking its "OK" button. ("Choice 1" means a square; "Choice 2"
means a circle.) The radius of the
circle or the length of the side of the square should be 100 pixels.
This handler function is defined in the class
derived from CView.
void CTextView::OnMakeChoice()
{
// TODO: Add your command handler code here
}
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*radius, where pi is the constant 3.14159, and radius is the
radius //of the circle
};
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
}
(A) Write some C# code (not more than three lines each) that completes
the bodies of the constructor and
the Circ( ) methods given above. Write your code in the spaces after
the comments above.<>
(B) Write some C# code that will use this Circ class in a Windows 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 window's form;
(3) change myCirc's radius to 100;
(4) compute the circumference of myCirc and display it and the new
radius just underneath the display
of the original radius. The two lines displayed should look
something
like the following:
The circle had a radius of 50
The circle now has
a radius of 100 and a circumference of 628
// Your code goes here
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 the Form's
SomeEv Event.
(B) Add some 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 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)
{
// Your code goes here
}
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 show a message
box with the text "Left
Mouse Button" when it was the left mouse button that was pressed and
a message box with the text
"Right Mouse Button" when it was the right button that was pressed.
void myMDown(object sender, MouseEventArgs
e)
{
// Your code goes here
11A Write some
Visual C# code that will create a Windows timer that begins running and
times
out once every 5 seconds.
11B. 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.