![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
#1
|
|||
|
|||
|
На форме есть StringGrid,рисую на нём какой-либо узор клетками(они закрашиваются при нажатии на ЛКМ),нажимаю запись и цвета записываются в c:\2.txt.Вопрос в том,как достать этот рисунок из c:\2.txt и поместить его на StringGrid при нажатии,например,на вторую кнопку?
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tm= array [0..50,0..50] of byte;
var
Form1: TForm1;
m:tm;
implementation
{$R *.dfm}
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Sg : TStringGrid;
Col, Row : Integer;
begin
Sg := Sender as TStringGrid;
Sg.MouseToCell(X, Y, Col, Row);
if Button = mbLeft then begin
Sg.Rows[Row].Objects[Col] := Pointer(1);
M[Row,Col]:=66;
end else if Button = mbRight then begin
Sg.Rows[Row].Objects[Col] := Pointer(0);
M[Row,Col]:=65;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
type
TSave = record
BrColor : TColor;
end;
var
Sg : TStringGrid;
Save : TSave;
Flag : Integer;
begin
Sg := Sender as TStringGrid;
Flag := Integer(Sg.Rows[ARow].Objects[ACol]);
if Flag <> 1 then Exit;
with Sg.Canvas, Save do begin
BrColor := Brush.Color;
Brush.Color := RGB(10, 80, 10);
FillRect(Rect);
Brush.Color := BrColor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var f:integer;
begin
f:=fileCreate('c:\2.txt');
filewrite(f,m,sizeof(m));
fileclose(f);
end;
end.Это близко к истине?(( Код:
procedure TForm1.Button2Click(Sender: TObject); var f:TextFile; begin AssignFile(f,'2.txt'); Reset(f); fileread(f,m,sizeof(m)); end; |
|
#2
|
||||
|
||||
|
Может так попроще будет, без промежуточных массивов. Проверил - работает
Код:
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
i, j : integer;
begin
with Sender as TStringGrid do
begin
MouseToCell(X, Y, i, j);
if Button = mbLeft then
Rows[j].Objects[i]:= Pointer(1)
else
Rows[j].Objects[i]:= Pointer(0);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
BrColor : TColor;
Sg : TStringGrid absolute Sender;
begin
if Integer(Sg.Rows[ARow].Objects[ACol]) <> 1 then Exit;
with Sg.Canvas do
begin
BrColor := Brush.Color;
Brush.Color := RGB(10, 80, 10);
FillRect(Rect);
Brush.Color := BrColor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f: TextFile;
i, j: integer;
begin
AssignFile(f, '2.txt');
Rewrite(f);
with StringGrid1 do
begin
for i := 0 to ColCount - 1 do
for j := 0 to RowCount - 1 do
Writeln(f, Integer(Rows[i].Objects[j]));
end;
CloseFile(f);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
f: TextFile;
i, j, tmp: integer;
begin
AssignFile(f, '2.txt');
Reset(f);
with StringGrid1 do
begin
for i := 0 to ColCount - 1 do
for j := 0 to RowCount - 1 do
begin
Readln(f, tmp);
Rows[i].Objects[j]:= Pointer(tmp);
end;
end;
CloseFile(f);
end;Последний раз редактировалось Alegun, 24.03.2015 в 19:10. |