Hello!
I have three newbies question about painting on a control.
For the educational purposes I'm programming a mine sweeper game.
1st question: OnPaint
When I click on a start buttion I show a mine field and fill it with squares. Then I click on the mine field and show a mine. But the mine field fully repaint and my squares is gone. Why?
2nd question: ImageList
ImageList is very usefull control but I found one strange thing: when I'm drawing an same image with ImageList and with Canvas.Draw it gives me different results. Why?
ImageList1.Resolution[24].Draw(Bevel1.Canvas, x, y, 0, True);
MineField.Canvas.Draw(x, y, Picture_Loaded_From_TImage_Placed_On_The_Form);
I attached screenshot to show the difference: left, brighter mine field — ImageList, right, darker — Canvas.Draw
3rd question: program logic
First click on the start button works well but the second doesn't work at all. Why?
Here's the minimal proof-of-concept code:
unit test_unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Bevel1: TBevel;
Button1: TButton;
ImageList1: TImageList;
procedure Bevel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Bevel1Paint(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
BoxSize: Integer = 24;
FieldRow: integer = 10;
FieldCol: integer = 10;
Button1Clicked: Boolean = False;
DrawABomb: Boolean = False;
CoordX, CoordY: Integer;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1Clicked := True;
Bevel1.Width := FieldCol * BoxSize;
Bevel1.Height := FieldRow * BoxSize;
Bevel1.Show;
end;
procedure TForm1.Bevel1Paint(Sender: TObject);
var
x, y: Integer;
begin
if Button1Clicked then begin
for x := 0 to FieldCol - 1 do begin
for y := 0 to FieldRow - 1 do begin
ImageList1.Resolution[24].Draw(Bevel1.Canvas,
(x * BoxSize), (y * BoxSize), 0, True);
end;
end;
Button1Clicked := False;
end;
if DrawABomb then begin
ImageList1.Resolution[24].Draw(Bevel1.Canvas, CoordX, CoordY, 1, True);
DrawABomb := False;
end;
end;
procedure TForm1.Bevel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then begin
DrawABomb := True;
CoordX := X; CoordY := Y;
Bevel1.Repaint;
end;
end;
end.