Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 18: Progress-Based Controls Visual C++ and MFC Fundamentals


18.1 Timers............................................................................................................


18.1.1..Overview...............................................................................................


A timer is a non-spatial object that uses recurring lapses of time from a computer or from
your application. To work, every lapse of period, the control sends a message to the
operating system. The message is something to the effect of "I have counted the number
of lapses you asked me to count".

As opposed to the time set on your computer, a timer is partly but greatly under your
control. Users do not see nor use a timer as a control. As a programmer, you decide if,
why, when, and how to use this control.

Practical Learning: Introducing the Timer Control



  1. Start MSVC and create a new project named RandShapes

  2. Create it as a Dialog Based application with no About box

  3. Delete the TODO line, the OK, and the Cancel buttons

  4. Set the Border style to None

  5. In the header file of the dialog box, declare two integer variables named DlgWidth
    and DlgHeight


public:
int DlgWidth;
int DlgHeight;
};


  1. To make the dialog box occupy the whole screen when it comes up, change the
    OnInitDialog() event as follows:


BOOL CRandShapesDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
DlgWidth = GetSystemMetrics(SM_CXSCREEN);
DlgHeight = GetSystemMetrics(SM_CYSCREEN);

SetWindowPos(&wndTopMost, 0, 0, DlgWidth, DlgHeight, SWP_SHOWWINDOW);

return TRUE; // return TRUE unless you set the focus to a control
}


  1. Because you cannot close System Menu-less window, to make sure you can close it
    by moving the mouse, generate the WM_MOUSEMOVE message for the dialog
    box and implement its OnMouseMove() event as follows:

Free download pdf