我试图将DataGridViews行作为Bitmap将其用作游标图标。 遗憾的是,DataGridViewRow对象没有DrawToBitmap方法。
我设法得到行的界限(RowRect)并获得整个DataGridView(bmp)的Bitmap。我想我接下来需要从位图中删除一行,但我不知道该怎么做。
这是我的开始代码:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
if (e.Button == MouseButtons.Left)
{
rw = dataGridView1.SelectedRows[0];
Rectangle RowRect = dataGridView1.GetRowDisplayRectangle(rw.Index, true);
Bitmap bmp = new Bitmap(RowRect.Width, RowRect.Height);
dataGridView1.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
Cursor cur = new Cursor(bmp.GetHicon());
Cursor.Current = cur;
rowIndexFromMouseDown = dataGridView1.SelectedRows[0].Index;
dataGridView1.DoDragDrop(rw, DragDropEffects.Move);
}
}
}
答案 0 :(得分:0)
您需要首先获取整个 clientarea内容(您的位图太小!),然后剪切行矩形。还要确保处理创建的资源!
这应该有效:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
if (e.Button == MouseButtons.Left)
{
Size dgvSz = dataGridView1.ClientSize;
int rw = dataGridView1.SelectedRows[0].Index;
Rectangle RowRect = dataGridView1.GetRowDisplayRectangle(rw, true);
using (Bitmap bmpDgv = new Bitmap(dgvSz.Width, dgvSz.Height))
using (Bitmap bmpRow = new Bitmap(RowRect.Width, RowRect.Height))
{
dataGridView1.DrawToBitmap(bmpDgv , new Rectangle(Point.Empty, dgvSz));
using ( Graphics G = Graphics.FromImage(bmpRow ))
G.DrawImage(bmpDgv , new Rectangle(Point.Empty,
RowRect.Size), RowRect, GraphicsUnit.Pixel);
Cursor.Current.Dispose(); // not quite sure if this is needed
Cursor cur = new Cursor(bmpRow .GetHicon());
Cursor.Current = cur;
rowIndexFromMouseDown = dataGridView1.SelectedRows[0].Index;
dataGridView1.DoDragDrop(rw, DragDropEffects.Move);
}
}
}
}