Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TDBCtrlGrid changing color on OnPaintPanel causes infinite loop



Permlink Replies: 3 - Last Post: Oct 28, 2016 12:46 PM Last Post By: Remy Lebeau (Te...
Valence Crearer

Posts: 64
Registered: 11/27/99
TDBCtrlGrid changing color on OnPaintPanel causes infinite loop
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2016 6:57 AM
void __fastcall TForm2::dbcgDocumentationPaintPanel(TDBCtrlGrid *DBCtrlGrid, int Index) {
DBCtrlGrid->Color = (Index%2)?clGreen:clBlue;
}

The above code causes an infinite loop. It seems to cause an invalidate, which causes a redraw, which causes OnPaintPanel to be called again.

This is not desirable. How would one stop the iteration?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TDBCtrlGrid changing color on OnPaintPanel causes infinite loop
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2016 9:53 AM   in response to: Valence Crearer in response to: Valence Crearer
Valence wrote:

The above code causes an infinite loop.

As it should be. You are using a painting event to do something that is
not related to painting, thus triggering a repaint.

It seems to cause an invalidate, which causes a redraw,
which causes OnPaintPanel to be called again.

Exactly.

How would one stop the iteration?

A painting event should ONLY draw, nothing else. NEVER do something that
needs to trigger a new repaint.

There is no reason whatsoever to change the grid's Color property from inside
of an OnPaint event. If you need to change the Color, do it from outside
the event.

The whole pupose of the OnPaintPanel event is to custom-draw a panel inside
the grid. Per the documentation:

http://docwiki.embarcadero.com/Libraries/en/Vcl.DBCGrids.TDBCtrlGrid.OnPaintPanel

Occurs when TDBCtrlGrid needs to draw a panel in the grid.

Write an OnPaintPanel event handler to customize the drawing of a panel in
the TDBCtrlGrid object. TDBCtrlGrid paints the background using the Color
or SelectedColor property, including the raised border if it is specified
by the PanelBorder property. Then the OnPaintPanel event occurs to allow
an event handler to add to the image of the panel.

The DBCtrlGrid parameter is the grid that contains the panel. The Index parameter
is the index of the panel being painted, where 0 is the first panel, 1 is
the second panel, and so on. Use the PanelIndex property to determine whether
the panel being drawn is selected.

Use the Canvas property to paint the panel. The point (0,0) on the canvas
is the upper left corner of the panel, and the point (PanelWidth, PanelHeight)
is the lower right corner.

Controls placed on the panel are drawn separately and do not need to be drawn
in an OnPaintPanel event handler.

To change the color of the panels being drawn, do something more like this
instead:

void __fastcall TForm2::dbcgDocumentationPaintPanel(TDBCtrlGrid *DBCtrlGrid, 
int Index)
{
    if (Index != DBCtrlGrid->PanelIndex)
    {
        TRect r = Rect(0, 0, DBCtrlGrid->PanelWidth, DBCtrlGrid->PanelHeight);
        DBCtrlGrid->Canvas->Brush->Color = (Index % 2) ? clGreen : clBlue;
        DBCtrlGrid->Canvas->FillRect(r);
    }
 
    // draw whatever else you need...
}


--
Remy Lebeau (TeamB)
Valence Crearer

Posts: 64
Registered: 11/27/99
Re: TDBCtrlGrid changing color on OnPaintPanel causes infinite loop
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2016 11:56 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
void __fastcall TForm2::dbcgDocumentationPaintPanel(TDBCtrlGrid *DBCtrlGrid, 
int Index)
{
    if (Index != DBCtrlGrid->PanelIndex)
    {
        TRect r = Rect(0, 0, DBCtrlGrid->PanelWidth, DBCtrlGrid->PanelHeight);
        DBCtrlGrid->Canvas->Brush->Color = (Index % 2) ? clGreen : clBlue;
        DBCtrlGrid->Canvas->FillRect(r);
    }
 
    // draw whatever else you need...
}


--
Remy Lebeau (TeamB)

Thanks.. Makes perfect sense, I guess, except that the components still draw themselves, so is everything that we draw going "behind" the components on the Grid?

Thanks again.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TDBCtrlGrid changing color on OnPaintPanel causes infinite loop
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2016 12:46 PM   in response to: Valence Crearer in response to: Valence Crearer
Valence wrote:

Thanks.. Makes perfect sense, I guess, except that the components
still draw themselves

Yes, and that is documented behavior:

http://docwiki.embarcadero.com/Libraries/Berlin/en/Vcl.DBCGrids.TDBCtrlGrid.OnPaintPanel

TDBCtrlGrid paints the background using the Color or SelectedColor property,
including the raised border if it is specified by the PanelBorder property.
**Then the OnPaintPanel event occurs** to allow an event handler to add to
the image of the panel.
...
Controls placed on the panel are drawn separately and do not need to be drawn
in an OnPaintPanel event handler.

so is everything that we draw going "behind" the components
on the Grid?

Yes, because you are drawing on the grid itself, while the controls are on
top of the grid.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02