图片提示

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 测试图片
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
          
        }
        private ToolTip m_ToolTip = new ToolTip();
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable tblDatas = new DataTable("Datas");
            DataColumn dc = null;

            //赋值给dc,是便于对每一个datacolumn的操作
            dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
            dc.AutoIncrement = true;//自动增加
            dc.AutoIncrementSeed = 1;//起始为1
            dc.AutoIncrementStep = 1;//步长为1
            dc.AllowDBNull = false;//

            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
            for (int i = 0; i < 100; i++)
            {
                DataRow newRow;
                newRow = tblDatas.NewRow();
                newRow["Product"] = "大话西游";
                newRow["Version"] = "2.0";
                newRow["Description"] = "我很喜欢";
                tblDatas.Rows.Add(newRow);

                newRow = tblDatas.NewRow();
                newRow["Product"] = "梦幻西游";
                newRow["Version"] = "3.0";
                newRow["Description"] = "比大话更幼稚";
                tblDatas.Rows.Add(newRow);
            }
            this.dataGridView1.DataSource = tblDatas;
        }
        private PictureBox _pedDelete;
        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
            if (info.ColumnIndex == 2)
            {
                if (info.ColumnIndex < 0 || info.RowIndex < 0)
                    return;

                DataGridViewCell cell = dataGridView1[info.ColumnIndex, info.RowIndex];
                if (cell.Value == null)
                    return;
             
                if (_pedDelete == null)
                {

                    _pedDelete = new PictureBox(); //实例删除图片按钮
                    _pedDelete.Click += new EventHandler(Img_Click); //注册删除事件   具体实践如下
                   
                }
                ((System.ComponentModel.ISupportInitialize)(this._pedDelete)).BeginInit();
                _pedDelete.Image = Properties.Resources.Drafts_16x16;
             
                // dataGridView1.Controls.Add(_pedDelete);

                

                Point p = new Point(info.ColumnX+40,info.RowY);
                this._pedDelete.Location = p;
                //Rectangle r = new Rectangle(cell.ContentBounds.Right - 21, cell.ContentBounds.Bottom - 20, 20, 20); //调整显示图片位置
                //_pedDelete.Bounds = r; // 设定位置

                this._pedDelete.Name = "pp";
                this._pedDelete.Size = new System.Drawing.Size(16, 16);

                //_pedDelete.Tag = info;

                _pedDelete.Tag = info.RowIndex;

                this._pedDelete.Image = Properties.Resources.Drafts_16x16;
                dataGridView1.Controls.Add(_pedDelete);
                _pedDelete.Visible = true;
                ((System.ComponentModel.ISupportInitialize)(this._pedDelete)).EndInit();

              
      
            
               
               // m_ToolTip.Show(cell.Value.ToString(), this, p.X, p.Y + 50);
               // _pedDelete.Show() ; // 设定位置
               
               // _pedDelete.PointToScreen(p);
              //  _pedDelete.Tag = htInfo.RowHandle;
                //http://s.yanghao.org/program/viewdetail.php?i=413910
              //  _pedDelete.Visible = true;
            }
            else
            {
                if (_pedDelete != null)
                    _pedDelete.Visible = false;

            }
            //if (info.ColumnIndex < 0 || info.RowIndex < 0)
            //    return;
            //DataGridViewCell cell = dataGridView1[info.ColumnIndex, info.RowIndex];
            //if (cell.Value == null)
            //    return;
            //Point p = new Point(dataGridView1.Location.X + e.Location.X, dataGridView1.Location.Y + e.Location.Y);
            //m_ToolTip.Show(cell.Value.ToString(), this, p.X, p.Y + 50);
        }

        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
          //  _pedDelete.Hide();
          //  this.Refresh();
        }
        private void Img_Click(object sender, EventArgs e)
        {
            int rowhandle = (int)(sender as PictureBox).Tag;

            string Key = dataGridView1.Rows[rowhandle].Cells[0].Value.ToString();
            MessageBox.Show(Key);
        }
    }
}

 

posted on 2013-05-29 11:04  HOT SUMMER  阅读(177)  评论(0)    收藏  举报

导航