Recent

Author Topic: problem: insert PICTURE inside GRID  (Read 2008 times)

guest61740

  • Guest
problem: insert PICTURE inside GRID
« on: November 18, 2017, 09:28:38 pm »
hello!
i'm trying to insert a picture inside a grid, i've tried with some codes that i found on the web (on similar topic) but i made a mess and ruined my code, so i decided to start over again (i'm a beginner).

that's my actual code: (there's basically nothing at the moment):

Quote
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
ExtCtrls,
  Grids;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    StringGrid1: TStringGrid;
  private
{ private declarations }
  public
{ public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

end.

{
  Return TColor value in XXXXXX format
  (X being a hex digit)
}
function
  TColorToHex( Color : TColor )
    : string;
begin
  Result :=
    { red value }
    IntToHex( GetRValue( Color ), 2 ) +
    { green value }              -
    IntToHex( GetGValue( Color ), 2 ) +
    { blue value }
    IntToHex( GetBValue( Color ), 2 );
end;

function RGBToColor(R, G, B: Byte): TColor;



                 
and i would like to have something like the example i've attached below.

any suggestion?


thank you very much!





howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: problem: insert PICTURE inside GRID
« Reply #1 on: November 18, 2017, 11:20:05 pm »
You could try this.
Set the Picture property of your image as desired, and generate an OnPaint handler for the image control (named img in the code below).
Complete the code of your main form unit thus:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Graphics, ExtCtrls;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     img: TImage;
  14.     procedure imgPaint(Sender: TObject);
  15.   end;
  16.  
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21.  
  22. {$R *.lfm}
  23.  
  24. procedure TForm1.imgPaint(Sender: TObject);
  25. const
  26.   gridCount = 25;
  27. var
  28.   r: TRect;
  29.   dim, col, cl, row, rw: Integer;
  30. begin
  31.   img.Canvas.Pen.Color := clGreen; // choose desired color
  32.   r := img.ClientRect;
  33.   dim := r.Width;
  34.   if r.Height > dim then
  35.     dim := r.Height;
  36.   dim := 1 + dim div gridCount;
  37.   for col := 0 to gridCount do begin
  38.     cl := col*dim;
  39.     for row := 0 to gridCount do begin
  40.       rw := row * dim;
  41.       img.Canvas.Line(cl, 0, cl, r.Height);
  42.       img.Canvas.Line(0, rw, r.Width, rw);
  43.     end;
  44.   end;
  45. end;
  46.  
  47. end.
« Last Edit: November 18, 2017, 11:22:28 pm by howardpc »

 

TinyPortal © 2005-2018