TListBox OnDrawItem causes AV in COMCTL32.dll

89 Views Asked by At

I have narrowed down the cause of my app generating access violations from COMCTL32.dll to TListBox::OnDrawItem. The TListBox.Style is lbOwnerDrawFixed.

App is built with C++ Builder XE4, running on Win7-64 and Win8-64. I can make it happen on Win7, but only from within the IDE, and only sporadically; the release build on Win7 does not raise the error, but on Win8 it does, every time.

This occurs only when the form is first created. The form includes buttons to move to next/previous record, which calls all of the control-populating code, but there is never a problem with those calls; only with this first time after form construction.

The form constructor sets a TTimer to pause 100ms. The TTimer::OnTimer calls a function that loads the data into the controls. After the TListBox is loaded, the OnDrawItem event is called.

If I set TListBox.Style to lbStandard and remove the custom OnDrawItem, there is no problem.

Here is a sample of a OnDrawItem handler:

void __fastcall TFieldForm::EditsListDrawItem(TWinControl *Control, 
      int Index, TRect &Rect, TOwnerDrawState State)
{
    TListBox* t_box = (TListBox*)Control;
    TCanvas* t_canvas = t_box->Canvas;

    int t_offset = 2; // default text offset width

    t_canvas->Brush->Color = (State.Contains(odSelected) ? 
                              clHighlight : Panel->Color);
    t_canvas->Font->Color = (State.Contains(odSelected) ? 
                             clHighlightText : clWindowText);
    t_canvas->Font->Style = TFontStyles();

    t_box->ItemHeight = t_canvas->TextHeight(FieldNameLabel->Caption);
    t_canvas->FillRect(Rect); // clear the rectangle
    t_canvas->TextOut(Rect.Left + t_offset, Rect.Top, 
                       t_box->Items->Strings[Index]);

} 

I have tested to be sure TListBox.Canvas is not NULL, but it made no difference; apparently it is always not-NULL.

I don't know how to proceed from here, and welcome suggestions.

Thanks, Kathleen

0

There are 0 best solutions below