Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >扩展ToolBarManager、ListView和Grid控件以实现气球式的ToolTip

扩展ToolBarManager、ListView和Grid控件以实现气球式的ToolTip

作者头像
蒋金楠
发布于 2018-01-16 07:58:18
发布于 2018-01-16 07:58:18
1.3K0
举报
文章被收录于专栏:大内老A大内老A

infragistics是全球领先的UI工具和用户体验的专家,Infragistics开发了一系列的炫目的Windows、Web、WPF和Silverlight控件,相信很多人在使用它们。我们现在的项目就在使用Infragistics的Windows Form控件集。虽然这些控件功能强大,也不可能满足你所有的需求,尤其是那些比较苛刻的最终用户的需求。比如,我们最近就接收到这样一个变态的需求:让所以菜单项、工具栏按钮、网格单元的ToolTip以气球式的样式显示。最终,我不得不通过对现有控件的扩展实现这个要求。

一、扩展UltraGrid

首先介绍对UltraGrid的扩展,先来看看显示的效果:当鼠标移到到每一个单元格(或者列头)的时候,会出现如下一个气球式的ToolTip,其文字的内容为单元格中的文本。ToolTip的样式,包括背景、字体等均可以通过Infragistics控件本身支持的风格文件定义。

下面是扩展控件ExtendedUltraGrid的定义,逻辑比较简单:直接继承自UltraGrid,重写两个方法:OnMouseEnterElement和OnMouseLeaveElement。当鼠标移入和移出相应元素的时候,这两个方法会被调用。通过重写OnMouseEnterElement方法,手工创建UltraToolTipInfo对象,并通过UltraToolTipManager对象(UltraToolTipManager在对象构建的时候被初始化)以ToolTip的形式显示出来;手工创建的ToolTip在OnMouseLeaveElement被执行的时候被移除。此外,由于UltraGrid的单元格和列头本身具有自己的ToolTip,你需要通过DisplayLayout.Override.TipStyleCell和DisplayLayout.Override.TipStyleHeader这两个属性抑制它们的显示。

代码语言:js
AI代码解释
复制
   1: using Infragistics.Win;
   2: using Infragistics.Win.UltraWinGrid;
   3: using Infragistics.Win.UltraWinToolTip;
   4:  
   5: namespace Artech.ExtendedControls4ToolTip
   6: {
   7:     
   8:     public class ExtendedUltraGrid : UltraGrid    {     
   9:       
  10:         private UltraToolTipManager toolTipManager = new UltraToolTipManager();
  11:         protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
  12:         {
  13:             base.OnPaint(pe);
  14:             this.DisplayLayout.Override.TipStyleCell = TipStyle.Hide;
  15:             this.DisplayLayout.Override.TipStyleHeader = TipStyle.Hide;
  16:         }
  17:        
  18:         protected override void OnMouseEnterElement(UIElementEventArgs e)
  19:         {
  20:             UltraGridCell enteredCell = e.Element.GetContext(typeof(UltraGridCell)) as UltraGridCell;
  21:             if (enteredCell != null)
  22:             {
  23:                 if (enteredCell.Column.DataType == typeof(bool))
  24:                 {
  25:                     return;
  26:                 }
  27:  
  28:                 UltraToolTipInfo gridToolTip = new UltraToolTipInfo(enteredCell.Text, ToolTipImage.Default, null, DefaultableBoolean.Default);
  29:                 this.toolTipManager.SetUltraToolTip(this, gridToolTip);
  30:                 this.toolTipManager.ShowToolTip(this);
  31:                 return;
  32:             }
  33:  
  34:             ColumnHeader enteredHeader = e.Element.GetContext(typeof(ColumnHeader)) as ColumnHeader;
  35:             if (enteredHeader != null)
  36:             {
  37:                 enteredHeader.ToolTipText = string.Empty;
  38:                 UltraToolTipInfo gridToolTip = new UltraToolTipInfo(enteredHeader.Caption, ToolTipImage.Default, null, DefaultableBoolean.Default);
  39:                 this.toolTipManager.SetUltraToolTip(this, gridToolTip);
  40:                 this.toolTipManager.ShowToolTip(this);
  41:             }
  42:         }
  43:         
  44:         protected override void OnMouseLeaveElement(UIElementEventArgs e)
  45:         {
  46:             base.OnMouseLeaveElement(e);
  47:             UltraGridCell enteredCell = e.Element.GetContext(typeof(UltraGridCell)) as UltraGridCell;
  48:             ColumnHeader enteredHeader = e.Element.GetContext(typeof(ColumnHeader)) as ColumnHeader;
  49:             if (null != enteredCell || null != enteredHeader)
  50:             {
  51:                 this.toolTipManager.HideToolTip();
  52:             }
  53:         }
  54:     }
  55: }

二、扩展UltraListView

下面是扩展后的UltrlListView(在Detail模式)的ToolTip显示的样式,ExtendedUltraGrid差不多:当鼠标移到相应的ListViewItem上面,将相应的内容以气球式的ToolTip实现出来。

扩展后的ExtendedUltrlListView的实现与ExtendedUltraGrid基本上完全一样,通过是对OnMouseEnterElement和OnMouseLeaveElement这两个方法的重写来实现,为了抑制UltrlListView自身的ToolTip的显示,需要将ViewSettingsDetails.SubItemTipStyle和ItemSettings.TipStyle两个属性设置为ItemTipStyle.Hide。下面是具体的代码定义:

代码语言:js
AI代码解释
复制
   1: using System.Windows.Forms;
   2: using Infragistics.Win;
   3: using Infragistics.Win.UltraWinListView;
   4: using Infragistics.Win.UltraWinToolTip;
   5:  
   6: namespace Artech.ExtendedControls4ToolTip
   7: {
   8:   
   9:     public class ExtendedUltraListView : UltraListView
  10:     {
  11:         private UltraToolTipManager toolTipManager = new UltraToolTipManager();       
  12:        
  13:         protected override void OnPaint(PaintEventArgs eventArgs)
  14:         {
  15:             base.OnPaint(eventArgs);
  16:             this.ViewSettingsDetails.SubItemTipStyle = SubItemTipStyle.Hide;
  17:             this.ItemSettings.TipStyle = ItemTipStyle.Hide;
  18:         }
  19:        
  20:         protected override void OnMouseEnterElement(UIElementEventArgs e)
  21:         {
  22:             base.OnMouseEnterElement(e);
  23:             UltraListViewItem enteredItem = e.Element.GetContext(typeof(UltraListViewItem)) as UltraListViewItem;
  24:             if (enteredItem != null)
  25:             {
  26:                 UltraToolTipInfo toolTip = new UltraToolTipInfo(enteredItem.Text, ToolTipImage.Default, null, DefaultableBoolean.Default);
  27:                 this.toolTipManager.SetUltraToolTip(this, toolTip);
  28:                 this.toolTipManager.ShowToolTip(this);
  29:             }
  30:         }
  31:        
  32:         protected override void OnMouseLeaveElement(UIElementEventArgs e)
  33:         {
  34:             base.OnMouseLeaveElement(e);
  35:             UltraListViewItem enteredItem = e.Element.GetContext(typeof(UltraListViewItem)) as UltraListViewItem;
  36:             if (null != enteredItem)
  37:             {
  38:                 this.toolTipManager.HideToolTip();
  39:             }
  40:         }
  41:     }
  42: }

三、扩展UltraToolbarsManager

右图是应用了扩展后的UltraToolbarsManager,工具栏ToolTip显示的样式,实际上当通过鼠标选择某个菜单项的时候,也具有相同样式的Tooltip相识。如果仔细看的话,你还会发现ToolTip的背景和上面默认的颜色不一样,这是因为在程序初始化后设置了样式。ToolTip的样式也随之发生了变化,以与整个风格相匹配。

扩展后的ExtendedUltraToolbarsManager的实现与上面的方式类似,同样是通过重写OnMouseEnterElement和OnMouseLeaveElement这两个方法。不过有一点不同的是:用于显示ToolTip的ToolTipManager的ShowToolTip接收参数的类型为Control,但是UltraToolbarsManager本身却并不是从Control类型派生。在这里采用了一个变通的方式:定义了一个Control类型的属性ContainerToBindToolTip,通过该属性从外部注定一个绑定ToolTip的控件。ExtendedUltraToolbarsManager定义如下:

代码语言:js
AI代码解释
复制
   1: using System.ComponentModel;
   2: using System.Windows.Forms;
   3: using Infragistics.Win;
   4: using Infragistics.Win.UltraWinToolbars;
   5: using Infragistics.Win.UltraWinToolTip;
   6:  
   7: namespace Artech.ExtendedControls4ToolTip
   8: { 
   9:     public class ExtendedUltraToolbarsManager : UltraToolbarsManager
  10:     {
  11:         
  12:         private UltraToolTipManager toolTipManager = new UltraToolTipManager();
  13:       
  14:         public Control ContainerToBindToolTip
  15:         { get; set; }      
  16:  
  17:     
  18:         public ExtendedUltraToolbarsManager(IContainer container)
  19:             : base(container)
  20:         {
  21:             this.toolTipManager = new UltraToolTipManager();
  22:         }
  23:        
  24:         protected override void OnMouseEnterElement(UIElementEventArgs e)
  25:         {
  26:             base.OnMouseEnterElement(e);
  27:             if (this.ShowToolTips)
  28:             {
  29:                 this.ShowToolTips = false;
  30:             }
  31:  
  32:             ToolBase tool = e.Element.GetContext(typeof(ToolBase)) as ToolBase;
  33:             if (null != tool)
  34:             {
  35:                 UltraToolTipInfo toolTip = new UltraToolTipInfo(tool.CaptionResolved.Replace("&", string.Empty), ToolTipImage.Default, null, DefaultableBoolean.Default);
  36:                 if (null != this.ContainerToBindToolTip)
  37:                 {
  38:                     this.toolTipManager.SetUltraToolTip(ContainerToBindToolTip, toolTip);
  39:                     this.toolTipManager.ShowToolTip(ContainerToBindToolTip);
  40:                 }
  41:             }
  42:         }
  43:         
  44:         protected override void OnMouseLeaveElement(UIElementEventArgs e)
  45:         {
  46:             base.OnMouseLeaveElement(e);
  47:             ToolBase tool = e.Element.GetContext(typeof(ToolBase)) as ToolBase;
  48:             if (null != tool)
  49:             {
  50:                 this.toolTipManager.HideToolTip();
  51:             }
  52:         }
  53:     }
  54: }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009-12-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
扩展UltraGrid控件实现对所有数据行的全选功能[Source Code下载]
在前面一篇文章中,我通过对三种Infragistics 控件(UltraToolBarManager、UltraGird和UltraListView)进行扩展,以实现对ToolTip样式的定义,今天我
蒋金楠
2018/01/16
1.6K0
扩展UltraGrid控件实现对所有数据行的全选功能[Source Code下载]
c#透明panel
先看下效果 纯透明的pane,然后设置一个半透明的图片,可以看出来显示了父控件的button 看代码 public partial class PanelEx : Panel {
冰封一夏
2019/09/11
2.4K0
(四十)c#Winform自定义控件-开关
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/09
1.6K0
(四十)c#Winform自定义控件-开关
(四十四)c#Winform自定义控件-水波
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/09
3950
(四十四)c#Winform自定义控件-水波
(六十三)c#Winform自定义控件-箭头(工业)
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/29
6100
(六十三)c#Winform自定义控件-箭头(工业)
(三十四)c#Winform自定义控件-Tab页
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1.6K0
(三十四)c#Winform自定义控件-Tab页
(五十九)c#Winform自定义控件-池子(工业)
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/09
3810
(五十九)c#Winform自定义控件-池子(工业)
显示DataGrid序号的一个适用的方法
如果数据量小的话没有问题,一旦数据量大,显示特别慢,还有个缺点就是拖动行高时行号不随行高的变化而变动,出现是几个序号在一个单元格中显示。我自己对他们的算法进行总结,写出一个效果比较不错的带序号的 DataGrid。原理:只显示表格中显示行的序号,并且拖动行,行号一起移动。
Java架构师必看
2020/10/09
6340
【C#】妈妈再也不用担心自定义控件如何给特殊类型的属性添加默认值了,附自定义GroupBox一枚
------------------更新:201411190903------------------
AhDung
2018/09/13
1.5K0
【C#】妈妈再也不用担心自定义控件如何给特殊类型的属性添加默认值了,附自定义GroupBox一枚
自己动手把 VTK 封装成 Windows Forms 控件
虽然 Kitware 提供了 ActiViz 作为 vtk 的 .Net 库,但这是一个收费软件,并且在调试模式下一直存在程序退出时资源无法释放的问题,于是自己动手做了 vtk 的 .Net 封装库。
秦建辉
2024/08/16
3903
【Flutter 组件集录】Tooltip 与 Overlay
今天是八月更文的最后一天,带大家看一下 Tooltip 组件的实现,从而引出 Overlay 组件的使用方式。 Tooltip 组件主要的作用是在鼠标悬浮或长按手势下触发消息提示。它继承自 StatefulWidget ,其中必须传入 String 类型的 message ,还有很多其他的参数用于配置。
张风捷特烈
2022/03/18
1.8K0
【Flutter 组件集录】Tooltip 与 Overlay
【C#】分享一个可灵活设置边框的Panel
---------------------------更新:2014-05-19---------------------------
AhDung
2018/09/13
1K0
【C#】分享一个可灵活设置边框的Panel
(二十九)c#Winform自定义控件-文本框(二)
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
5980
【C#】让工具栏ToolStrip能触发焦点控件的Leave、Validating、DataError等事件以验证数据
----------------更新:2014-04-21---------------
AhDung
2018/09/13
1.3K0
(一)c#Winform自定义控件-基类控件
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1.5K0
(一)c#Winform自定义控件-基类控件
(六十二)c#Winform自定义控件-警灯(工业)
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
5540
(六十二)c#Winform自定义控件-警灯(工业)
【愚公系列】2023年11月 Winform控件专题 ToolTip控件详解
Winform控件是Windows Forms中的用户界面元素,它们可以用于创建Windows应用程序的各种视觉和交互组件,例如按钮、标签、文本框、下拉列表框、复选框、单选框、进度条等。开发人员可以使用Winform控件来构建用户界面并响应用户的操作行为,从而创建功能强大的桌面应用程序。
愚公搬代码
2023/11/30
2.2K0
[UWP 自定义控件]了解模板化控件(5):VisualState
使用TemplatePart实现上篇文章的两个需求(Header为空时隐藏HeaderContentPresenter,鼠标没有放在控件上时HeaderContentPresent半透明),虽然功能已经实现,但这样实现的话基本上也就别想扩展了。譬如开发者做不到通过继承或修改ControlTemplate实现如下功能:
dino.c
2019/01/18
5430
[UWP 自定义控件]了解模板化控件(5):VisualState
发布一个日期选择控件(ASPNET2.0)
The Coolest DHTML Calendar,这是一个在GPL下发布的JS日历程序,具有极高的可配置性,包括外观样式、显示格式、显示内容等等。默认程序是只提供日期选择的,需要设置几个showtime参数才能显示时间选择。下载的程序包(zip)里面已经有详细的说明文档和例子. 这个控件的aspnet 1.1版本的代码在我的donews blog上,代码可以自由修改发布. 代码和示例下载地址:demo using System; using System.Web; using System.Web.U
张善友
2018/01/19
1.9K0
(八十)c#Winform自定义控件-分割线标签
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/10/09
1K0
(八十)c#Winform自定义控件-分割线标签
推荐阅读
相关推荐
扩展UltraGrid控件实现对所有数据行的全选功能[Source Code下载]
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档