我的是这样设置:   S.X := StrToInt(Sx.Text); //开始点X
  S.Y := StrToInt(Sy.Text); //开始点Y  
  E.X := StrToInt(ex.Text); //终始点X
  E.Y := StrToInt(EY.Text); //终始点Y
  //定义一个TRect的LineR 
  LineR.Left := S.X;
  LineR.Top := S.Y;
  LineR.Right := E.X;
  LineR.Bottom := E.Y;
  //然后画一根直线
  DrawLine(Canvas.Handle, S, E);在窗口的MouesDown  下面写入
 if PtInRect(LineR, Point(X, Y)) then ShowMessage('OK');
但是在终点的上下,也就是说点击并不在直线上夜显示"OK", 这样有点不准确定位是否在直线上的点

解决方案 »

  1.   

    那得看你点击的谁了,这和鼠标点击的对象 有关系,你的liner不一定是这个对象上的区域,所以你要注意屏幕坐标和客户区域坐标之分
      

  2.   

    这是PtInRect的VCL源码
    Result := (P.X >= Rect.Left) and (P.X < Rect.Right) and (P.Y >= Rect.Top)
        and (P.Y < Rect.Bottom);
    它在终点使用的是小于,不包括等于
      

  3.   

    //定义一个TRect的LineR  
      LineR.Left := S.X;
      LineR.Top := S.Y;
      LineR.Right := E.X;
      LineR.Bottom := E.Y;
    我高清楚了,我定义的区域的问题,不能定义开始点到终点的坐标为一个区域的
    所以区域是: e.x - s.x, e.y - s.y 的范围内。如果要确定是否在直线上估计要写上一个函数才可以! 谢谢楼上的热心回答! 
      

  4.   

    Result := (P.X >= Rect.Left) and (P.X < Rect.Right) and (P.Y >= Rect.Top)
      and (P.Y < Rect.Bottom);这个是对的,MSDN 就是这么说的。 PtInRect 这个是window 的API函数的。