请问怎么画虚框?(50分)

  • 主题发起人 主题发起人 山药蛋
  • 开始时间 开始时间

山药蛋

Unregistered / Unconfirmed
GUEST, unregistred user!
就象在空桌面上按左键拖动时产生的那个虚框。

应该是用什么api实现的,不会太复杂吧?
 
有一個API DrawFocusRect, 很簡單, 只要傳入Canvas.Handle, 及一個TRect結構指定
方框的位置、大小即可
 
with form1 do
begin
canvas.Pen.Style:=psDot ;
canvas.Rectangle(100,100,200,200);
end;
 
Canvas.DrawFocusRect(Rect);
 
var
fDragging: Boolean;
fRect: TRect;

function NormalizeRect (ARect: TRect): TRect;
var
tmp: Integer;
begin
if ARect.Bottom < ARect.Top then
begin
tmp := ARect.Bottom;
ARect.Bottom := ARect.Top;
ARect.Top := tmp;
end;
if ARect.Right < ARect.Left then
begin
tmp := ARect.Right;
ARect.Right := ARect.Left;
ARect.Left := tmp;
end;
Result := ARect;
end;

procedure TShapesForm.Image1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
fDragging := True;
SetCapture (Handle);
fRect.Left := X;
fRect.Top := Y;
fRect.BottomRight := fRect.TopLeft;
Canvas.DrawFocusRect (fRect);
end;
end;

procedure TShapesForm.Image1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
ARect: TRect;
begin
Caption := Format ('ShapeBmp (x=%d, y=%d)', [X, Y]);
if fDragging then
begin
ARect := NormalizeRect (fRect);
Canvas.DrawFocusRect (ARect);
fRect.Right := X;
fRect.Bottom := Y;
ARect := NormalizeRect (fRect);
Canvas.DrawFocusRect (ARect);
end
else
if ssShift in Shift then
Image1.Canvas.Pixels [X, Y] := clRed;
end;

procedure TShapesForm.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if fDragging then
begin
ReleaseCapture;
fDragging := False;
Image1.Canvas.Rectangle (fRect.Left, fRect.Top,
fRect.Right, fRect.Bottom);
end;
end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部