unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
B, E: TPoint;
procedure ClearBE;
procedure DrawFocusRect;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
B := Mouse.CursorPos; //定位起始点
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then //左键按下拖动时
begin
DrawFocusRect; //擦除原有焦点框
{ 防止焦点框绘制超出Image1 }
if X > Image1.ClientWidth then X := Image1.ClientWidth;
if Y > Image1.ClientHeight then Y := Image1.ClientHeight;
E := Image1.ClientToScreen(Point(X, Y));
DrawFocusRect; //绘制新焦点框
end;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
DrawFocusRect; //擦除焦点框
ClearBE; //还原起止点坐标
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ClearBE; //初始化起止点坐标
end;
procedure TForm1.ClearBE;
begin
B := Point(0, 0);
E := Point(0, 0);
end;
procedure TForm1.DrawFocusRect;
var
R: TRect;
begin
with R do
begin
{ 设定空矩形 }
Left := 0;
Top := 0;
Right := 0;
Bottom := 0;
{ 如果是向下拖动,则设定所需矩形 }
if (E.X > B.X) and (E.Y > B.Y) then
begin
TopLeft := B;
BottomRight := E;
end;
end;
{ 绘制焦点框,使用的是屏幕的DC }
windows.DrawFocusRect(GetDC(0), R);
end;
end.