Creating a More Attractive Tstringgrid

Using TStringGrid.

I have seen,  Delphi Visual component library’s (VCL) programmer, using a good component. But this does not belong to Delphi VCL itself, but made ​​by third party. I tried searching the Internet, most of these are high-priced components. For example, this is VCL grid made by TMS Software:

()

PICTURE 1

I don’t say it’s wrong, but…when we have a business, we have to force our expenses to be low, but high income. With this articles, I will explain to format TStringGrid like PICTURE 1.

we start with original TStringGrid from Delphi (Picture 2):

PICTURE 2

Yes, deal. It’s not interesting. But for a minute, we change it more beauty, looks like PICTURE 1. If we succeed, we can save the cost of manufacturing software, because the grid is our own program.

Preparation Steps:

  1. First thing we do. Change DefaultDraw property to false. So that the display becomes as shown below:

    PICTURE 3

    Wait a minute! But it’s still ugly! Yes, I know. We haven’t finished guys. There’s a second step below

  2. We start by changing the color of Fixed Column to  blue color.  Write the following program code in the event OnDrawCell:

    with stringgrid1 do
    begin                         
        if gdFixed in  State then               
        begin                               
            Canvas.Brush.Color := $00CD934B;                            
            Canvas.Font.Color := $00000;               
        end;              
        Canvas.FillRect(rect); end;

    and the picture becomes like PICTURE 4

    PICTURE 4

  3. OMG! TStringGrid is covered by blue. We want the blue color only in the Fixed Column, not for all. now add this:

    with stringgrid1 do
    begin                   
        if gdFixed in  State then                 
        begin                    
            Canvas.Brush.Color := $00CD934B;                    
            Canvas.Font.Color := $00000;            
       end else         
       begin              
           
           Canvas.Brush.Color := $FFFFFF;              
           
           Canvas.Font.Color := $00000;      
       
       end;      
        
       Canvas.FillRect(rect); end;

    TStringGrid become:

    PICTURE 5

  4. Color problem is solved. But we still have a problem, its fonts are missing. We have to draw foreground, so it can be displayed. Change source code at:

    Canvas.FillRect(rect);   

    become:

    Canvas.FillRect(rect);         
    DrawText(Canvas.Handle, PChar(S), Length(S), Rect, DT_LEFT or DT_VCENTER  or DT_SINGLELINE)

    those source code make our TStringGrid become:

  5. PICTURE 6

  6. There is still a mistake. Fonts in Fixed Column is not center. Change code in the DrawText to be:

       if ARow = 0 then
           DrawText(Canvas.Handle, PChar(S), Length(S),
           Rect, DT_CENTER or DT_VCENTER  or DT_SINGLELINE)
      
       else    
           DrawText(Canvas.Handle, PChar(S), Length(S),
           Rect, DT_LEFT or DT_VCENTER  or DT_SINGLELINE);

  7. All characters is center, but its color is still black, for changing to white like PICTURE 1, let’s go to change a little bit at

    gdFixed in state 

    become:

    if gdFixed in  State then              
    begin                       
          Canvas.Brush.Color := $00CD934B;                       
          Canvas.Font.Color := $FFFFFF;   
              
    end else
        

  8. In Picture 2, there are clear boundaries between each cell, while the grid that we make not like it. Now focus at the boundary line Cell. Put the following line of code at the beginning of the code, after: with stringgrid1 do:

    with Rect do
    begin             
        Top := Top + 1;             
        Left := Left + 1;             
        Bottom := Bottom – 1;             
        Right := Right – 1;         
    end;

    our grid looks like:

    PICTURE 7

  9. We almost finish our jobs. The last problem: The pointer is missing. This little changing make it reveal pointer:

    if gdFixed in  State then              
    begin                       
        Canvas.Brush.Color := $00CD934B;                       
        Canvas.Font.Color := $FFFFFF;        
    end else           
    if gdSelected in State then
           
    begin
               
        Canvas.Brush.Color := $00E0BB8D;
               
        Canvas.Font.Color := $000000;
           
    end else
           
    begin           
        canvas.Brush.Color := $FFFFFF;           
        Canvas.Font.Color := $000000;        
    end;

mission is completed, our grid become:

PICTURE 8

comments powered by Disqus
Loading