简单说一下解决思路,不一定是你要的,可能我理解你要的东西有偏差.不好意思.
----------------------------------------------------------------------
1.FORM 上放上 TPanel 和 TImage 控件.Image 在 Panel 内,Image.AutoSize := True;
Image 内显示你要显示的图片.
2.在窗体建立或者 LOAD 图片时动态建立多个 TPaintBox 控件,Parent 指向 Panel,
大小为你要的小方块大小,OnMouseMove 事件指向自定义的事件进行处理.
下面的代码全部动态建立,新建立一个工程,把整个文件覆盖就可以了
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
procedure MyMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Panel : TPanel;
Image : TImage;
PaintBox : array of TPaintBox;
I, J, K : Integer;
const
SpaceWidth = 20; // 假设的方块的宽和高
SpaceHeight = 20;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Panel := TPanel.Create(Self);
with Panel do
begin
Parent := Self;
Left := 10;
Top := 10;
Width := 400;
Height := 400;
end;
Image := TImage.Create(Panel);
with Image do
begin
Parent := Panel;
Left := 0;
Top := 0;
AutoSize := True;
end;
try
Image.Picture.LoadFromFile('c:/1.BMP')
except
MessageBox(Self.Height, '请确定存在图片 c:/1.BMP;并保证是BMP格式.', '提示', MB_OK);
end;
//-------------确定方块数量,这个看你的要求进行修改
I := Image.Width div SpaceWidth;
if ((Image.Width mod SpaceWidth) > 0) then
Inc(I);
J := Image.Height div SpaceHeight;
if ((Image.Height mod SpaceHeight) > 0) then
Inc(J);
//-------------确定方块数量,这个看你的要求进行修改
SetLength(PaintBox, (I * J));
For K := 0 to (I * J) - 1 do
begin
PaintBox[K] := TPaintBox.Create(Panel);
with PaintBox[K] do
begin
Parent := Panel;
Left := (K mod I) * SpaceWidth;
Top := (K mod J) * SpaceHeight;
Width := SpaceWidth;
Height := SpaceHeight;
Tag := K;
OnMouseMove := MyMouseMove;
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// 释放资源
for K := (I * J) - 1 downto 0 do
begin
PaintBox[K].Free;
PaintBox[K] := Nil;
end;
end;
procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Self.Caption := Format('X = %d; Y = %d',[X, Y]);
// 另一种理解
with (Sender as TPaintBox) do
Self.Caption := Format('X = %d; Y = %d',[Tag mod I, Tag mod J]);
end;
end.