• 一个带有工具提示的列表框


    介绍 我在做一个项目,我需要一个列表框来显示 不适合于列表框宽度的项目的工具提示。 一开始我以为会有一个。net BCL类会有这个 设施。过去我有过一些让我忘记的经历,在那里我浪费了很多时间 写一些现成的东西。但这次我什么也没发现 符合我的要求。所以我写了我自己的列表框类派生自。net System.Windows.Forms。ListBox类并调用它 ToolTipListBox。 它所做的 中的项 ToolTipListBox列表框超过了列表框的宽度 控件时,工具提示将悬浮在项目上。工具提示只浮动 用于不适合列表框宽度的项目。适合的物品 在,工具提示将不会显示。 使用它 属性的成员即可声明列表框对象 ToolTipListBox类,而不是标准的ListBox类。 这是所有。你还必须包括 ToolTipListBox类源文件。或者你 甚至可以构建一个库并引用它。 类源代码清单 隐藏,收缩,复制Code

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;
    
    
    public class ToolTipListBox : System.Windows.Forms.ListBox
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct SIZE
        {
            public int cx;
            public int cy;
        }
        [DllImport("gdi32.dll")]
        public static extern int GetTextExtentPoint32(IntPtr hdc, 
            String str, int len, ref SIZE size);
    
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);
    
        [DllImport("user32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd,IntPtr hdc);
    
    
        public ToolTipListBox()
        {           
            tp.InitialDelay = 500;
            tp.ReshowDelay = 500;
            tp.AutoPopDelay = 3000;         
            tp.Active = true;           
        }   
    
    
        protected override void OnMouseMove(
            System.Windows.Forms.MouseEventArgs e)
        {               
            /* Get the index of the mouse-hovered item */
            int index = IndexFromPoint(e.X,e.Y);
    
            /* Ensure that there is an item */
            if(index != ListBox.NoMatches )
            {               
                /* 
                  Check if the mouse has moved enough
                  distance for a new index 
                */
                if( LastIndex != index )
                {
                    string s = Items[index].ToString(); 
    
                    /* Get the text extent */
                    IntPtr hdc = GetDC(this.Handle);
                    SIZE size;
                    size.cx = 0;
                    size.cy = 0;
                    GetTextExtentPoint32(hdc,s,s.Length,ref size);
                    ReleaseDC(this.Handle,hdc);
    
                    /* If it won't fit show tool-tip */
                    if(this.Width < size.cx)                    
                        tp.SetToolTip(this,s);
    
                    LastIndex = index;              
                }                           
            }
        }
    
        private ToolTip tp = new ToolTip();
        private int LastIndex = -1;
    
    }

    本文转载于:http://www.diyabc.com/frontweb/news344.html

  • 相关阅读:
    [TC11326]ImpossibleGame
    [CC-FNCS]Chef and Churu
    [JZOJ4786]小a的强迫症
    [USACO08NOV]Time Management
    ARC070F HonestOrUnkind
    LOJ2799 「CCC 2016」生命之环
    Luogu P5824 十二重计数法(小球盒子计数)
    Luogu P4249 [WC2007]剪刀石头布
    Kattis heapsoffun Heaps of Fun
    Kattis xorsequences XOR Sequences
  • 原文地址:https://www.cnblogs.com/Dincat/p/13437984.html
Copyright © 2020-2023  润新知