0

Edited: I want to draw a vertical centered TIcon graphic and Text on a TJvDBGrid (Project's Jedi descendant of TDBGrid). I tried to disable the DefaultDrawing method of JvDBGrid and override it, but I only could fill the cells with black (I think that my code is incomplete to do the override).

Now I succeeded draw the Icon on Cell and the Text remains the same with Default Drawing. How I can center the Icon (vertical and horizontal) and the Text (just vertical ), like this?

Here is my code:

procedure TFrmXXX.JvDBGridXXXDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Icon: TIcon;
  fixRect: TRect;
  imgWidth: Integer;
begin
  fixRect := Rect;

  if Column.Index = 0  then //always the first one
  begin
    Icon := GetIcon; //Returns TIcon object
    try
      imgWidth := (Rect.Bottom - Rect.Top);
      fixRect.Right := Rect.Left + imgWidth;
      (Sender as TJvDBGrid).Canvas.StretchDraw(fixRect, Icon);
    finally
      Icon.Free;
    end;
    fixRect := Rect;
    fixRect.Left := fixRect.Left + imgWidth;
  end;

  (Sender as TJvDBGrid).DefaultDrawColumnCell(fixRect, DataCol, Column, State);
end;
Mathias Berwig
  • 488
  • 5
  • 13
  • Can you tell us what you have tried so far please. – David Heffernan Mar 16 '15 at 14:15
  • What "errors" did you get? What version of Delphi? What are these "several ways" you've tried? I don't see much effort to provide enough for us to work with. – Jerry Dodge Mar 16 '15 at 14:18
  • In your screenshot, I see the image is centered, but not the text. That contradicts your question giving two different requirements. Also, what do VCL Styles have to do with anything? Your screenshot doesn't appear to use them either. – Jerry Dodge Mar 16 '15 at 14:27
  • @DavidHeffernan I tried to OwnDraw the OnDrawDataCell and OnDrawColumnCell, but on same cases I get black background on the cell. – Mathias Berwig Mar 16 '15 at 14:34
  • @JerryDodge My mistake :( I want to align the image on the center of cell (horizontal and vertical) and the text just on vertical center of cell. – Mathias Berwig Mar 16 '15 at 14:34
  • 4
    @MathiasBerwig There is perhaps some confusion about how questions and answers work here. Posting your software requirements and expecting a solution is not what happens - nobody will write you a custom solution. What you need to do is to [edit] your question to include the code you have tried that did not work - show us what you did, explain what you expected, and describe how the result was different from your expectations. If you received errors, include as much detail about them as possible, including the full text of the exceptions, lines where it occured, and ideally a call stack. – J... Mar 16 '15 at 16:07
  • @J... I'm really sorry about that. Maybe I have expressed myself badly on my question (I'm new on this kind of forum, but I promise I'll improve my interactions). After reading the guidelines, I edited the question and added some information. Hope that it is better now. – Mathias Berwig Mar 16 '15 at 17:31
  • 1
    @MathiasBerwig One way to understand it is that Stack Overflow isn't necessarily a "forum". It's a Question/Answer site for resolving specific issues which are helpful to other readers in the future. – Jerry Dodge Mar 16 '15 at 17:38

1 Answers1

0

After many tests, I found a solution merging different tutorials on internet. On DrawColumnCell event, I wrote something like this:

Canvas.FillRect(Rect); //Fill the cell using current brush. 

And in each specific column case, I used one of these methods:

Canvas.Draw((Rect.Right - Rect.Left - Icon.Width) div 2 + Rect.Left, (Rect.Bottom - Rect.Top - Icon.Height) div 2 + Rect.Top, Icon); //Draw the graphic centered on cell

Canvas.DrawText(Canvas.Handle, PChar(Column.Field.DisplayText), Length(Column.Field.DisplayText), Rect, DT_VCENTER or DT_CENTER or DT_SINGLELINE or DT_NOPREFIX); //Draw vertical and horizontal centered text

Canvas.DrawText(Canvas.Handle, PChar(Column.Field.DisplayText), Length(Column.Field.DisplayText), Rect, DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX);  //Draw vertical centered and horizontal left justified text
Mathias Berwig
  • 488
  • 5
  • 13