segunda-feira, 11 de fevereiro de 2019

C++ builder - matriz de inteiro em StringGrid

Em poucos minutos escrevi este código,
que já rodava na minha cabeça, 
mas a intenção mesmo era de apresentar
este componente poderoso do C++ Builder,
o "StringGrid", que não se resume ao seu nome,
pelo contrário, é totalmente genérico
e aceita qualquer tipo de dados do C/C++.
Declarei um StringGrid de 64 células,
sendo de 8 linhas por 8 colunas que receberão
os 64 números inteiros que foram escritos
num arquivo de texto, este arquivo  criado 
é fechado por fclose (); e em seguida é aberto
para leitura, onde o mesmo preenche as grades
do nosso StringGrid.



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

#include <vcl.h>
#pragma hdrstop
#define tam 8
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate ( TObject *Sender ) {
FILE * arq;
if ( ( arq = fopen ( "C:\\Users\\Embarcadero\\Documents\\"
"Projetos C++ Builder\\OnPaint\\Matriz.txt", "w" ) ) == NULL ) {
ShowMessage ( "Não foi possível criar o arquivo " );
}
Label1 -> Caption = "C++ BUILDER - MATRIZ DE INTEIRO EM STRINGGRID ";
int i = 0, j = 0;
int A [ tam ] [ tam ];
for( i = 1; i <= tam * tam; i++ ) {
fprintf ( arq, "%d ", i );
}
fclose ( arq );
if ( ( arq = fopen ( "C:\\Users\\Embarcadero\\Documents\\"
"Projetos C++ Builder\\OnPaint\\Matriz.txt", "r" ) ) == NULL ) {
ShowMessage ( "Não foi possível abrir o arquivo " );
}
for ( i = 0; i < tam; i++ ) {
for ( j = 0; j < tam; j++ ) {
fscanf ( arq, "%d", &A [ i ] [ j ] );
StringGrid1 -> Cells [ j ] [ i ] = A [ i ] [ j ];
}
}
fclose ( arq );
}

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

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

#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;
TShape *Shape1;
void __fastcall FormCreate(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 - MATRIZ DE INTEIRO 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
  OnCreate = FormCreate
  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 Shape1: TShape
    Left = 0
    Top = 0
    Width = 600
    Height = 300
    Brush.Style = bsClear
    Pen.Color = clAqua
    Pen.Width = 10
    Shape = stRoundRect
  end
  object StringGrid1: TStringGrid
    Left = 200
    Top = 40
    Width = 233
    Height = 233
    BevelWidth = 3
    Color = clBlack
    ColCount = 8
    DefaultColWidth = 24
    FixedCols = 0
    RowCount = 8
    FixedRows = 0
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    GridLineWidth = 5
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goThumbTracking]
    ParentFont = False
    TabOrder = 0
  end

end

Nenhum comentário:

Postar um comentário

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