您的位置:首页 > 其它

玩无限消乐的设计示例(基础篇)DataGridView控件添加拖放事件方法和委托

2016-07-09 08:58 549 查看
给DataGridView控件添加拖放事件方法,预备交换动画元素。

        private void 玩无限消乐_DragDrop(object sender, DragEventArgs e)
        {
            Point 平面二维 = 玩无限消乐.PointToClient(new Point(e.X, e.Y));
            玩无限消除.Rows[玩无限消乐.HitTest(平面二维.X, 平面二维.Y).RowIndex].Cells[玩无限消乐.HitTest(平面二维.X, 平面二维.Y).ColumnIndex].Value = (System.Drawing.Bitmap)e.Data.GetData(typeof(System.Drawing.Bitmap));/*System.String*/
        }

        private void 玩无限消乐_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void 玩无限消乐_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            玩无限消乐.DoDragDrop(玩无限消乐.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, DragDropEffects.All);
        }

        private void 玩无限消乐_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            玩无限消乐.DoDragDrop(玩无限消乐.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, DragDropEffects.All);
        }

为了方便把DataGridView控件改为全局声明,否则的话就要找到控件才能使用。查找代码: DataGridView 表格控件 = (DataGridView)this.Controls["玩无限消乐"];

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;
using System.Drawing.Drawing2D;
using System.Threading;
using System.IO;

namespace Windows屏幕截图
{
public partial class Form1 : Form
{
DataGridView 玩无限消乐 = new DataGridView();
}
}

添加拖放事件方法委托

private void Form1_Load(object sender, EventArgs e)
{
玩无限消乐.Name = "玩无限消乐";
玩无限消乐.AllowDrop = true;
玩无限消乐.AllowUserToAddRows = false;
玩无限消乐.AllowUserToOrderColumns = false;
玩无限消乐.AllowUserToResizeColumns = false;
玩无限消乐.AllowUserToResizeRows = false;
玩无限消乐.ReadOnly = true;
玩无限消乐.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
玩无限消乐.Location = new Point(0, 全屏截取.Height);
玩无限消乐.BackgroundColor = System.Drawing.Color.Linen;
玩无限消乐.ColumnHeadersVisible = false;
玩无限消乐.MultiSelect = false;
玩无限消乐.RowHeadersVisible = false;
玩无限消乐.BorderStyle = BorderStyle.None;
玩无限消乐.ScrollBars = ScrollBars.None;
玩无限消乐.Capture = true;
玩无限消乐.AutoSize = true;
玩无限消乐.CellBorderStyle = DataGridViewCellBorderStyle.None;
玩无限消乐.DefaultCellStyle.SelectionBackColor = Color.LightYellow;
//玩无限消乐.ColumnCount = 14;
//System.Threading.Thread.Sleep(2000);
int 列数 = 0, 数量 = 22;
while (列数++ < 数量)
{
DataGridViewImageColumn 新图列 = new DataGridViewImageColumn();
玩无限消乐.Columns.Add(新图列);
}
玩无限消乐.RowCount = 数量;
玩无限消乐.AutoResizeColumns();
//玩无限消乐.AutoResizeRows();
玩无限消乐.Parent = this;
玩无限消乐.DragDrop += new DragEventHandler(玩无限消乐_DragDrop);
玩无限消乐.DragEnter += new DragEventHandler(玩无限消乐_DragEnter);
玩无限消乐.CellMouseDown += new DataGridViewCellMouseEventHandler(玩无限消乐_CellMouseDown);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: