XtraTabPage右键菜单(关闭当前页、关闭其它页、所有关闭的实现)
实现的需求:
用户习惯是一个不可忽略的东西,默认这版的dx的tab也木有右键操作,但用户习惯操作如浏览器都有右键关闭功能,故这里实现先dx的该功能
技术实现:
(1)在winform的相应控件内,拖入以下dx的控件
(2)初始化右键的菜单项
1 private void InitPopMenu() 2 { 3 BarButtonItem barItemCurrent = new BarButtonItem 4 { 5 Name = "barItemCurrent", 6 Caption = "关闭当前" 7 }; 8 barItemCurrent.ItemClick -= BarItemCloseCurrentClick; 9 barItemCurrent.ItemClick += BarItemCloseCurrentClick; 10 BarButtonItem barItemOther = new BarButtonItem 11 { 12 Name = "barItemOther", 13 Caption = "关闭其他" 14 }; 15 barItemOther.ItemClick -= BarItemCloseOtherClick; 16 barItemOther.ItemClick += BarItemCloseOtherClick; 17 BarButtonItem barItemAll = new BarButtonItem 18 { 19 Name = "barItemAll", 20 Caption = "关闭全部" 21 }; 22 barItemAll.ItemClick -= BarItemCloseAllClick; 23 barItemAll.ItemClick += BarItemCloseAllClick; 24 popupMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(barItemCurrent), new LinkPersistInfo(barItemOther), new LinkPersistInfo(barItemAll) }); 25 }
(3)绑定tab页的右键
private void XtraTabControlMouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { XtraTabControl tabCtrl = sender as XtraTabControl; var pt = e.Location; if (tabCtrl != null) { XtraTabHitInfo info = tabCtrl.CalcHitInfo(pt); if (info.HitTest == XtraTabHitTest.PageHeader) { popupMenu.ShowPopup(MousePosition); } } } }
(4).效果图如下:
ok,到此右键菜单完成,建议封装一个继承与XtraTabControl的用户自定义控件,在此用户控件内封装好右键菜单。
下一次,讲一下tab页的拖拽,这里透漏一下,实际项目里遇到了dx里tab内关闭按钮的小叉叉跟拖拽的鼠标左键拖拽代码冲突,就是说点击小叉叉不能关闭了。好在后来找到了解决方法~