为什么drawfocusrect对image没有用? (100分)

  • 主题发起人 主题发起人 jbas
  • 开始时间 开始时间
J

jbas

Unregistered / Unconfirmed
GUEST, unregistred user!
我试着画一个虚框在image上,但对form这个可用,
对image,如果不是.bmp就会把image上的图片清除,是.bmp就什么反映都没有。大侠们帮帮忙!

unit Unitdrawfocusrect;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, RxGIF, ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
MyPoint:TPoint;
FDrawing:boolean;
MyDrawRect:TRect;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=true;
MyDrawRect.Left:=X;
MyDrawRect.Top:=Y;
MyDrawRect.BottomRight:=MyDrawRect.TopLeft;
MyPoint:=point(X,Y);
drawfocusrect(image1.Picture.Bitmap.Canvas.Handle,MyDrawRect);
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=false;
drawfocusrect(image1.Picture.Bitmap.Canvas.Handle,MyDrawRect);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDrawing=true then
begin
drawfocusrect(image1.Picture.Bitmap.Canvas.Handle,MyDrawRect);
if X>MyPoint.X then
begin
MyDrawRect.Left:=MyPoint.X;
MyDrawRect.Right:=X;
end
else
begin
MyDrawRect.Left:=X;
MyDrawRect.Right:=MyPoint.X
end;
if Y>MyPoint.Y then
begin
MyDrawRect.Top:=MyPoint.Y;
MyDrawRect.Bottom:=Y;
end
else
begin
MyDrawRect.Top:=Y;
MyDrawRect.Bottom:=MyPoint.Y
end;
Drawfocusrect(image1.Picture.Bitmap.Canvas.Handle,MyDrawrect);
end;
end;
end.
 
可以用 image1.Canvas.DrawFocusRect(MyDrawRect);来代替上面的
Drawfocusrect(image1.Picture.Bitmap.Canvas.Handle,MyDrawrect);
但是这个在image没有图片时适用,但当有图片时就不行了,请指教!
 
高手们是不是都去看世界杯了?
 
版主menxin请帮忙看一下。
 
我只是想实现局部放大的功能,用鼠标来选择放大区域。
 
现在能在image上画出虚框了,不过是用了form.canvas,好象有点欺骗的味道:)
下步就是实现这个区域的放大缩小了,如果有哪位大侠知道怎样实现的话,通知一声.谢了。
unit Unitdrawfocusrect;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, RxGIF, ExtCtrls;

type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
MyPoint:TPoint;
FDrawing:boolean;
MyDrawRect:TRect;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=true;
MyDrawRect.Left:=image1.Left+X;
MyDrawRect.Top:=image1.Top+Y;
MyDrawRect.BottomRight:=MyDrawRect.TopLeft;
MyPoint:=point(image1.Left+x,image1.Top+y);
// image1.Canvas.DrawFocusRect(MyDrawRect);
drawfocusrect(self.Canvas.Handle,MyDrawRect);
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=false;
// image1.Picture.Bitmap.Canvas.DrawFocusRect(MyDrawRect);
drawfocusrect(self.Canvas.Handle,MyDrawRect);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDrawing=true then
begin
// image1.Canvas.DrawFocusRect(MyDrawRect);
drawfocusrect(self.Canvas.Handle,MyDrawRect);
if (image1.Left+x)>MyPoint.X then
begin
MyDrawRect.Left:=MyPoint.X;
MyDrawRect.Right:=image1.Left+x;
end
else
begin
MyDrawRect.Left:=image1.Left+x;
MyDrawRect.Right:=MyPoint.X
end;
if Y>MyPoint.Y then
begin
MyDrawRect.Top:=mypoint.Y;
MyDrawRect.Bottom:=image1.Top+Y;
end
else
begin
MyDrawRect.Top:=image1.Top+y;
MyDrawRect.Bottom:=MyPoint.Y
end;
// image1.Canvas.DrawFocusRect(MyDrawRect);
Drawfocusrect(self.Canvas.Handle,MyDrawrect);
end;
end;
end.
 
1. DrawFocusRect 的问题你考虑得太复杂了,

var
x0, y0, x1, y1: integer;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=true;
x0 := x;
y0 := y;
x1 := x;
y1 := y;
image1.Canvas.drawfocusrect(Rect(x0, y0, x1, y1));
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing:=false;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDrawing then
begin
image1.Canvas.DrawFocusRect(Rect(x0, y0, x1, y1));
image1.Canvas.Drawfocusrect(Rect(x0, y0, x, y));
x1 := x;
y1 := y;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
end;

2. 局部放大用 Canvas.CopyRect 即可实现,你可把以上得到的 (x0, y0) - (x1, y1) 之间的 Rect
Copy 至整个 Image.Canvas.ClipRect
 
>>DrawFocusRect 的问题你考虑得太复杂了,
不复杂呀,你的代码有问题,不能从下望上画,不能从右望左画。我的比较好。:)
>>2. 局部放大用 Canvas.CopyRect
我解决了,用StretchDraw,就可以了。
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Bmp: TBitmap;
begin
FDrawing:=false;
drawfocusrect(self.canvas.handle, MyDrawRect);
Bmp := TBitmap.Create;
Bmp.Width:=MyDrawRect.Right-MyDrawRect.Left;
Bmp.Height:=MyDrawRect.Bottom-MyDrawRect.Top;
Bmp.Canvas.CopyRect(Rect(0, 0, Bmp.Width, Bmp.Height),image1.Picture.Bitmap.Canvas, MyDrawRect);
image1.Picture.Bitmap.Canvas.StretchDraw(Rect(0, 0, image1.Width, image1.Height), Bmp);
Bmp.Free;
end;
 
>> 你的代码有问题,不能从下望上画,不能从右望左画。我的比较好。:)

我的代码你试过吗?你说的上,下或左、右的问题并不存在:)
 
>>我的代码你试过吗?你说的上,下或左、右的问题并不存在:)
当然试过了,你一直按着鼠标,来回拉拉看,就有我说的问题了。
 
不能在image上画框原因很简单: 你的画框引起了image的重绘。
既然你的问题都解决了。 分分吧
 
多人接受答案了。
 
后退
顶部