就是我知道通过function ItemAtPos(Pos: TPoint; Existing: Boolean): Integer;
可以获得当前选择内容的位置,delphi的帮助文件里面说什么这个函数返回-1表示当前没有选择内容,但是当我在
procedure TForm1.ListBox1DblClick(Sender: TObject);
var
  i,pos:integer;
begin
pos:=listbox1.ItemAtPos(mouse.CursorPos,true);
showmessage(inttostr(pos));
if listbox1.ItemAtPos(mouse.CursorPos,true)=-1 then
 for i:=0 to listbox1.Count-1 do
   if listbox1.Selected[i] then
   showmessage(listbox1.Items.Strings[i]);
end;这个listbox的鼠标双击事件当中却发现,当listbox1.ItemAtPos(mouse.CursorPos,true)=-1的时候才表示当前listbox有内容被选择,于是由于不可以通过这个函数得到listbox的选择内容的索引值,所以我只好遍历这个listbox找寻选择内容阿,对于这个我很奇怪,难道这个函数在不同的事件当中会返回不同的值马,还有,在
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  pos:integer;
begin
pos:=listbox1.ItemAtPos(point(x,y),true);
showmessage(inttostr(pos));
if (button=mbleft) and (listbox1.ItemAtPos(point(x,y),true)<>-1) then
listbox1.BeginDrag(false);
end;
这个listbox的mousedown事件当中这个listbox1.ItemAtPos(point(x,y),true);是工作正常的阿,返回正确的listbox的选择内容的索引值阿,而且返回-1表示没有内容被选择阿!!以上的内容我说的都是事实阿,我对这个问题感到非常奇怪,请各位大侠帮忙解答阿!!

解决方案 »

  1.   

    因为mouse.CursorPos并不是相对于listbox的point吧
      

  2.   

    mouse.CursorPos难道不是mouse得点击位置吗!!不会吧,我想它应该是这个吧!!我们去写个程序试验一下阿!!
      

  3.   

    > mouse.CursorPos难道不是mouse得点击位置吗!!是,但它是Screen坐标,要先转换到listbox的Client坐标:
    procedure TForm1.ListBox1DblClick(Sender: TObject);
    var
      i,pos:integer;
    beginpos:=listbox1.ItemAtPos(listbox1.ScreenToClient(mouse.CursorPos),true);
    showmessage(inttostr(pos));
    if pos <> -1 then
      showmessage(listbox1.Items.Strings[pos]);
    end;
      

  4.   

    我明白了,
    procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      pos:Tpoint;
    begin
    edit1.Text:=inttostr(x)+'   '+inttostr(y);
    pos:=listbox1.ScreenToClient(mouse.CursorPos);
    edit2.Text:=inttostr(pos.X)+'   '+inttostr(pos.Y);
    end;原来mouse.CursorPos得到得是整个屏幕得坐标,需要通过相应空间得设备坐标阿!!通过上面得函数我终于发现道理原来是这样得!1