码迷,mamicode.com
首页 > Windows程序 > 详细

C# ListView 自定义ToolTip 显示

时间:2016-01-13 12:28:46      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoTolltip
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private ToolTip lvTp = new ToolTip();
        private ListViewItem currentItem = new ListViewItem();
        private void Form2_Load(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
            listView1.FullRowSelect = true;
           // listView1.GridLines = true;
            listView1.Columns.Add("Column1");
            listView1.Columns.Add("Column2");
            for (int i = 0; i < 10; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems[0].Text = i.ToString();
                lvi.ImageIndex = i;
                lvi.SubItems.Add("Item" + i.ToString());
                lvi.ToolTipText = "Item" + i.ToString() +"\r\n" +strShowIms;
             
                listView1.Items.Add(lvi);
            }
          //  listView1.ShowItemToolTips = true;
            listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
            lvTp.OwnerDraw = true;
            lvTp.Draw += new DrawToolTipEventHandler(lvTp_Draw);
            lvTp.Popup += new PopupEventHandler(lvTp_Popup);
        }
        string strShowIms = @"sssAAAAAAA
          1  AAAAAAAAAAAAAAAAAAAAA
           2     AAAAAAA
         2     AAAAAAA
         3     AAAAAAA
         4     AAAAAAA
         5     AAAAAAA
         6     AAAAAAA
         7     AAAAAAA
         8     AAAAAAA
         9     AAAAAAA
";
        string strText = "";
        public void lvTp_Popup(object sender, PopupEventArgs e)
        {
            Size s = TextRenderer.MeasureText(strText, f);
            e.ToolTipSize = new Size(s.Width, s.Height + 40);
        }
        Font f = new Font("宋体", 10.0f);
        Font fTitle = new Font("宋体", 12.0f);
        void lvTp_Draw(object sender, DrawToolTipEventArgs e)
        {
            using (e.Graphics)
            {
                int x = e.Bounds.Width;
                int y = e.Bounds.Height;
                e.DrawBackground();

                SolidBrush b = new SolidBrush(Color.FromArgb(233, 240, 245));//矩形背景色
                SolidBrush border = new SolidBrush(Color.FromArgb(190, 219, 249));//边框颜色
                Rectangle recg = new Rectangle(0, 0, x, 30);

                e.Graphics.FillRectangle(b, recg);
                e.Graphics.DrawRectangle(new Pen(border), recg);
                e.Graphics.DrawRectangle(new Pen(border), 0, 0, x - 1, y - 1);
                int tempx = x / 4;
                if (x < 180 && x > 100)
                {
                    tempx = 10;
                }
                else if (x <= 100)
                {
                    tempx = 5;
                }
                e.Graphics.DrawImage(global::DemoTolltip.Properties.Resources.当前床位信息, new PointF(tempx, 5));
                e.Graphics.DrawString("当前信息", fTitle, Brushes.Black, new PointF(tempx + 40, 7));
                // e.Graphics.DrawLine(new Pen(Brushes.Blue),new Point(1,30),new Point(400,30));
                e.Graphics.DrawString(e.ToolTipText, f, Brushes.Black, new PointF(4, 40));     
            }
        }

        void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            ListViewItem lvi = listView1.GetItemAt(e.X, e.Y);
            if (lvi != null && lvi != currentItem)
            {
                strText = lvi.ToolTipText;
                currentItem = lvi;
                lvTp.Show(lvi.ToolTipText, listView1, e.Location);
            }
            else if (lvi == null)
            {
                strText="";
                lvTp.Hide(listView1);
            }
            currentItem = lvi;
        }

  
       
    }
}

  

C# ListView 自定义ToolTip 显示

标签:

原文地址:http://www.cnblogs.com/china-guoch/p/5126570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!