procedure   TForm1.WMMouseMove(var Msg:TWMMouseMove);  //因为是tform1的过程,所以在控件Button1上时StatusBar1.SimpleText不会有坐标变化?
var
  point:TPoint;
  xp,yp:Integer;
  Pos1,pos2:TPoint;
begin
  GetCursorPos(point);
  Pos1.X:= Self.Button1.Left;    //控件在容器的相对x坐标
  Pos1.Y:= Self.Button1.Top;      //控件在容器的相对y坐标
  pos2:= ClientToScreen(Pos1);
  xp:=pos2.X-point.X;
  yp:=pos2.Y-point.Y;
  StatusBar1.SimpleText:='X:'+inttostr(xp)+','+'Y:'+inttostr(Yp);
end;
以上是用控件在屏幕上的坐标减鼠标在屏幕上的坐标的 鼠标在控件内的相对坐标
但是当鼠标移动到button1上的时候StatusBar1并不能显示鼠标在控件内的相对坐标!
请问怎么样操作可以让StatusBar1.SimpleText显示 鼠标在控件内的相对坐标?
望高手帮忙修改一下!谢谢!

解决方案 »

  1.   


    //你是以按纽的左上角为坐标原点吗?
    //只要在下面加入就可以了
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    button1.OnMouseMove:=form1.OnMouseMove;
    end;
      

  2.   

    procedure   TForm1.FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X,
        Y:   Integer);
    begin
        Caption:=   Format( '(%d,   %d)   -   %s ',   [X,   Y,   Sender.ClassName]);
    end;Button1.OnMouseMove   :=   FormMouseMove;
      

  3.   


    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
        point:TPoint;
        xp,yp:Integer;
        Pos1,pos2:TPoint;
    begin
       // GetCursorPos(point);
        point.X:=x;
        point.Y:=y;
        if    ptinrect(button1.ClientRect,point) then
        begin
        pos1:=button1.ClientRect.TopLeft;
        xp:=x-;
        yp:=y-pos1.Y;//这样在按纽里就是相对坐标
        StatusBar1.SimpleText:='X:'+inttostr(xp)+','+'Y:'+inttostr(Yp);
    end;
    //
      

  4.   

    搞错了
    procedure   TForm1.FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X, 
        Y:   Integer); 
    var 
            point:TPoint;
            xp,yp:Integer; 
            Pos1:TPoint; 
    begin 
            point.X:=x; 
            point.Y:=y; 
            pos1:=button1.ClientRect.TopLeft; 
            xp:=x-pos1.x; 
            yp:=y-pos1.Y;//这样在按纽里就是相对坐标 
            StatusBar1.SimpleText:='X:'+inttostr(xp)+','+'Y:'+inttostr(Yp);