最近做一个Case就是遇到这样的事,用一个PopupMenu为Listview的子项使用。下面是代码;写的有点乱,不过能用;
第一步先在你的你的Form的OnCretae事件中添加代码;
procedure TForm1.FormCreate(Sender: TObject); var i:Integer; begin for i:=0 to PopupMenu1.Items.Count -1 do begin PopupMenu1.Items[i].Visible :=false; end; end;
//这个循环的作用是初始化PopupMenu的所有子项为不可见;
procedure TForm1.ListView1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); var i, j, s, a:Integer; begin ListView1.Update; for i:=0 to ListView1.Items.Count - 1 do begin if ListView1.Items.Count >0 then begin for a:=0 to ListView1.Items.Count +1 do begin if ListView1.SelCount =0 then begin for j :=0 to PopupMenu1.Items.Count -1 do begin PopupMenu1.Items[j].Visible :=False; end; end else begin for s:=0 to PopupMenu1.Items.count -1 do begin PopupMenu1.Items[s].Visible :=True; end; end; end ; // Exit; end; end; end;
//上面这段代码作用是判断用户选择哪一个ListView的子项,如果是第N个的话当在第N个上面右击鼠标时就会出现PopupMenu菜单,
//当然选择一个空子项这个PopupMenu菜单是不会显示的;