首先我的图片下面有一个Panel1。
我看到有些这样的贴子,
是秋风写的如下:
全给你贴上吧:
...
uses Jpeg;type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.CMMouseEnter(var Msg: TMessage);
var
img: TImage;
begin
img := TImage(Msg.LParam);
if (img <> nil) and (img.Name = 'Image1') then
img.Picture.LoadFromFile('d:\a.jpg')
end;procedure TForm1.CMMouseLeave(var Msg: TMessage);
var
img: TImage;
begin
img := TImage(Msg.LParam);
if (img <> nil) and (img.Name = 'Image1') then
img.Picture.LoadFromFile('d:\b.jpg')
end;end.但当我鼠标称动图片上时, img.name是值找到的是Panel1 , 而不是IMage1 , 这是为什么要怎样解决??
我看到有些这样的贴子,
是秋风写的如下:
全给你贴上吧:
...
uses Jpeg;type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.CMMouseEnter(var Msg: TMessage);
var
img: TImage;
begin
img := TImage(Msg.LParam);
if (img <> nil) and (img.Name = 'Image1') then
img.Picture.LoadFromFile('d:\a.jpg')
end;procedure TForm1.CMMouseLeave(var Msg: TMessage);
var
img: TImage;
begin
img := TImage(Msg.LParam);
if (img <> nil) and (img.Name = 'Image1') then
img.Picture.LoadFromFile('d:\b.jpg')
end;end.但当我鼠标称动图片上时, img.name是值找到的是Panel1 , 而不是IMage1 , 这是为什么要怎样解决??
在Image的OnMouseMove中写入
if Image2.Picture.Graphic = nil then
Image2.Picture.LoadFromFile('d:\a.jpg');
然后在其它事件如Panel的OnMouseMove中写入Image2.Picture.Graphic := nil;试一下
function ControlFromPoint(mParent: TWinControl; mPoint: TPoint): TControl;
var
I: Integer;
J: Integer;
begin
Result := nil;
if not Assigned(mParent) then Exit;
for I := mParent.ControlCount - 1 downto 0 do begin
if PtInRect(mParent.Controls[I].BoundsRect, mPoint) then begin
if not mParent.Controls[I].Visible then Continue;
if mParent.Controls[I] is TWinControl then
Result := ControlFromPoint(TWinControl(mParent.Controls[I]),
Point(mPoint.X - mParent.Controls[I].Left,
mPoint.Y - mParent.Controls[I].Top));
if not Assigned(Result) then Result := mParent.Controls[I];
Break;
end;
end;
end; { ControlFromPoint }procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Timer1.Enabled then Exit;
Timer1.Enabled := True;
Image1.Picture.LoadFromFile('d:\a.jpg');
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
if ControlFromPoint(Self, ScreenToClient(Mouse.CursorPos)) <> Image1 then
begin
Timer1.Enabled := False;
Image1.Picture.LoadFromFile('d:\b.jpg')
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 100;
end;