一个奇怪的问题!!

ttoth12 2003-11-29 12:58:36
就是我知道通过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表示没有内容被选择阿!!以上的内容我说的都是事实阿,我对这个问题感到非常奇怪,请各位大侠帮忙解答阿!!
...全文
26 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttoth12 2003-11-29
  • 打赏
  • 举报
回复
我明白了,
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
短歌如风 2003-11-29
  • 打赏
  • 举报
回复
> mouse.CursorPos难道不是mouse得点击位置吗!!

是,但它是Screen坐标,要先转换到listbox的Client坐标:
procedure TForm1.ListBox1DblClick(Sender: TObject);
var
i,pos:integer;
begin

pos:=listbox1.ItemAtPos(listbox1.ScreenToClient(mouse.CursorPos),true);
showmessage(inttostr(pos));
if pos <> -1 then
showmessage(listbox1.Items.Strings[pos]);
end;
ttoth12 2003-11-29
  • 打赏
  • 举报
回复
mouse.CursorPos难道不是mouse得点击位置吗!!不会吧,我想它应该是这个吧!!我们去写个程序试验一下阿!!
司码君 2003-11-29
  • 打赏
  • 举报
回复
同意楼上,还有我看你的问题应该没有这么麻烦!
hkbarton 2003-11-29
  • 打赏
  • 举报
回复
因为mouse.CursorPos并不是相对于listbox的point吧
tongki 2003-11-29
  • 打赏
  • 举报
回复
是吗?我了试试去
zhourongbiao 2003-11-29
  • 打赏
  • 举报
回复
确实奇怪,我试了一下,暂时作个记号,有空在看看。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧