DBGrid1.OnDrawColumnCell goes into infinite loop



Hi,
I use the following code in OnDrawColumnCell to embed DBCheckboxes into a
DBGrid. Everything works fine until I move (using mouse drag) a column with
a checkbox to another column location. The drawing of the active column
Checkbox going into infinite loop and causes the screen to freeze. There is
no code in OnColumnMoved method.

Any suggestion will be greatly appreciated.

procedure TfmEmailFolderMgr.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;

bitmap : TBitmap;
fixRect : TRect;
bmpWidth : integer;

imgIndex : integer;
begin
fixRect := Rect;

// customizing the 'SaveAttachment' field
if Column.Field = DBGrid1.DataSource.DataSet.FieldByName('Attachment')
then
begin
//adding some logic to grab the required image
if DBGrid1.DataSource.DataSet.FieldByName('Attachment').AsString = '1'
then
imgIndex := 1
else
imgIndex := 0;


bitmap := TBitmap.Create;
try
//grab the image from the ImageList
//(using the "Salary" field's value)
ImageList2.GetBitmap(imgIndex,bitmap);
//Fix the bitmap dimensions
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
finally
bitmap.Free;
end;

// reset the output rectangle,
// add space for the graphics
fixRect := Rect;
fixRect.Left := fixRect.Left + bmpWidth;
end;

// customizing the 'MsgTextFormat' field
if Column.Field = DBGrid1.DataSource.DataSet.FieldByName('MsgTextFormat')
then
begin
//adding some logic to grab the required image
if DBGrid1.DataSource.DataSet.FieldByName('MsgTextFormat').AsString =
'HTML' then
imgIndex := 2
else
if DBGrid1.DataSource.DataSet.FieldByName('MsgTextFormat').AsString =
'RTF' then
imgIndex := 3
else
imgIndex := 0;


bitmap := TBitmap.Create;
try
//grab the image from the ImageList
ImageList2.GetBitmap(imgIndex,bitmap);
//Fix the bitmap dimensions
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Right; //Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
finally
bitmap.Free;
end;

// reset the output rectangle,
// add space for the graphics
fixRect := Rect;
fixRect.Left := fixRect.Left + bmpWidth;
end;

// for drawing checkbox in 'SaveAttachment' Column
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBCheckBoxSaveAttachment.DataField) then
begin
//DBCheckBoxSaveAttachment.Left := Rect.Right -
DBCheckBoxSaveAttachment.Width; //Rect.Left + DBGrid1.Left + 2;
DBCheckBoxSaveAttachment.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBoxSaveAttachment.Width := 17; //Rect.Right - Rect.Left;
DBCheckBoxSaveAttachment.Left := Round((Rect.Left + Rect.Right -
DBCheckBoxSaveAttachment.Width)/2) + 5; // the CheckBox "floats" on top of
the DBGrid, measured from the same Left starting point
DBCheckBoxSaveAttachment.Height := Rect.Bottom - Rect.Top;

DBCheckBoxSaveAttachment.Visible := True;
end
end
else
begin
if (Column.Field.FieldName = DBCheckBoxSaveAttachment.DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);

DrawState := ISChecked[Column.Field.AsBoolean];

DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
DBCheckBoxSaveAttachment.Visible := False;
end;
end;

// for drawing checkbox in 'File' Column
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBCheckBoxFiled.DataField) then
begin
//DBCheckBoxFiled.Left := Rect.Right + DBCheckBoxFiled.Width;
//Rect.Left + DBGrid1.Left + 2;
//DBCheckBoxFiled.Left := 68; //Rect.Left + DBGrid1.Left +
DBCheckBoxFiled.Width;//Rect.Right + 6;
DBCheckBoxFiled.Left := Round((Rect.Left + Rect.Right -
DBCheckBoxFiled.Width)/2) + 5; // the CheckBox "floats" on top of the
DBGrid, measured from the same Left starting point
DBCheckBoxFiled.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBoxFiled.Width := 17; //Rect.Right - Rect.Left;
DBCheckBoxFiled.Height := Rect.Bottom - Rect.Top;

DBCheckBoxFiled.Visible := True;
end
end
else
begin
if (Column.Field.FieldName = DBCheckBoxFiled.DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);

DrawState := ISChecked[Column.Field.AsBoolean];

DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
DBCheckBoxFiled.Visible := False;
end;
end;
end;

Thanks.

Chris


.


Quantcast