Paint and Invalidate

Introduction

When a window is changed either by an internal or external agent (call to one of its methods or movement over its surface by another window), the area altered must be issued with an Invalidate command. This is a simplified version of the Windows InvalidateRectangle command. This command is forwarded as a WM_PAINT message to all windows that overlap the window issuing the Invalidate command.

An Example

In the payroll problem , when you change employee information, you want the listbox in the window to be redrawn to reflect the results. This listbox should also be redrawn whenever another window erases part of it by moving over it. You will create this message handler.

The Message Handler.

In this case you could create the function first. Make sure it is public and has a TObject* as its parameter. The following is the handler for the payroll problem.

void __fastcall TSDIPayrollForm::paint(TObject *Sender)
{
    ArrayIterator< EmployeePtr> next(_employees);
    char buff[32000];
    ostrstream out(buff,32000);
    while (next)
    {
        next()->print(out);
        out << endl;
        next++;
    }
    out << '\0';
    EmployeeList->Items->Text = buff;
}

This function is very similar to the print functions in your console programs.

Make A Handler

The paint function you created can be made into the message handler for the WM_PAINT message by selecting it as the handler of the OnPaint Event in the object inspector.

Using Invalidate

For an example of how Invalidate is called from a method, see the AddEmployee1Click in the payroll discussion.