zoukankan      html  css  js  c++  java
  • 扩展ToolBarManager、ListView和Grid控件以实现气球式的ToolTip

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

    一、扩展UltraGrid

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

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

       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

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

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

       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

    image

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

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

       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: }
    作者:Artech
    出处:http://artech.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    nginx之location匹配优先级和安全问题
    nginx--->高并发优化
    高并发处理方案
    检查Linux服务器性能
    浅谈Nginx负载均衡和F5的区别
    大数据不仅仅是海量数据
    自己实现C++的string类
    hihoCoder题目之Magic Box
    vim入门之配色方案(colorscheme)设置
    新开通博客,很是高兴
  • 原文地址:https://www.cnblogs.com/artech/p/1615737.html
Copyright © 2011-2022 走看看