quarta-feira, 13 de fevereiro de 2019

C++ builder - vetor de struct em StringGrid

Se precisácemos criar grades  manualmente
precisariamos utilizar linhas horizontais
e verticais, mas o StringGrid já vem pronto,
só temos que alterar suas propriedades para
escolhermos o total de linhas e de colunas
que precisamos, para isto não há regras,
não existe um padrão definido,
tudo o que temos que fazer é adaptá-lo as
nessecidades de nosso projeto.
O StringGrid é muito usado para exibir
valores por categorias sem importar o tipo,
que é o nosso caso estamos preenchendo as grades
com os membros de uma struct previamente declarada.
Mas também pode ser usado em tabelas, planilhas,
ou onde separadores verticais e horizontais
forem necessários.
Aqui está mais um exemplo de utilização do StringGrid.





//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
struct Escola {
char *nomes;
int idade;
float notas;
};

//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint(TObject *Sender)
{
  Label1 -> Caption = "C++ BUILDER - VETOR DE STRUCT EM STRINGGRID ";
Canvas -> Pen -> Color = clBlue;
Canvas -> Pen -> Width = 10;
Canvas -> Rectangle ( 05, 05, 590, 295 );

int i, j = 0;
struct Escola Alunos [ 10 ] =
{ { "Ana Celia    ", 19, 10.0},
{ "Eder Costa     ", 21, 9.9 },
{ "Humberto Gomes ", 18, 9.7 },
{ "Dijalma Lacerda", 22, 7.3 },
{ "Caroline Silva ", 18, 9.3 },
{ "Igor Goncalves ", 21, 8.6 },
{ "Bruna Carla    ", 19, 8.9 },
{ "Fabio Quadros  ", 22, 7.7 },
{ "Geany Barros   ", 19, 8.4 },
{ "Jaqueline Vega ", 22, 7.5}};
StringGrid1 -> ColWidths [ 0 ] = 30;
StringGrid1 -> ColWidths [ 1 ] = 25;
StringGrid1 -> ColWidths [ 2 ] = 150;
StringGrid1 -> ColWidths [ 4 ] = 30;
StringGrid1 -> ColWidths [ 6 ] = 30;
for ( i = 0; i < 10 ; i++ ) {
j++;
StringGrid1 -> Cells [ 0 ] [ i ] = j;
StringGrid1 -> Cells [ 2 ] [ i ] = Alunos [ i ].nomes;
StringGrid1 -> Cells [ 4 ] [ i ] = Alunos [ i ].idade;
StringGrid1 -> Cells [ 6 ] [ i ] = Alunos [ i ].notas;
}
Canvas -> Font -> Name = "Verdana";
Canvas -> Font -> Size = 18;
Canvas -> Font -> Style = TFontStyles ( ) << fsBold << fsUnderline
   << fsItalic;
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 200, 260, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 260, 260, "Samuel Lima" );
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Grids.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TStringGrid *StringGrid1;
TLabel *Label1;
TLabel *Label2;
void __fastcall OnPaint(TObject *Sender);

private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

object Form1: TForm1
  Left = 439
  Top = 165
  Caption = 'C++ BUILDER - VETOR DE STRUCT EM STRINGGRID'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clRed
  Font.Height = 0
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 19
  object Label1: TLabel
    Left = 70
    Top = 8
    Width = 6
    Height = 23
    Alignment = taCenter
    Caption = ' '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic, fsUnderline]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 106
    Top = 39
    Width = 376
    Height = 19
    Caption = 'Ordem           Nomes                 Idades     Notas'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = 0
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object StringGrid1: TStringGrid
    Left = 106
    Top = 64
    Width = 399
    Height = 193
    BevelWidth = 3
    Color = clBlack
    ColCount = 8
    DefaultColWidth = 24
    FixedColor = clRed
    FixedCols = 0
    RowCount = 10
    FixedRows = 0
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    GradientEndColor = clBlack
    GradientStartColor = clBlack
    GridLineWidth = 5
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goThumbTracking]
    ParentFont = False
    TabOrder = 0
    ColWidths = (
      24
      24
      24
      24
      24
      24
      24
      24)
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.