2016-08-01 3 views
0

Ich versuche eine DataGridViews-Zeile als Bitmap zu erhalten, um sie als Cursorsymbol zu verwenden. Leider hat das DataGridViewRow-Objekt keine DrawToBitmap-Methode.Wie bekomme ich eine DataGridView-Zeile als Bitmap für ein Cursorsymbol?

Ich schaffte es, die Grenze der Zeile (RowRect) zu bekommen und eine Bitmap des gesamten DataGridView (BMP) zu bekommen. Ich denke, dass ich als nächstes die Reihe von der Bitmap schneiden muss, aber ich habe keine Idee, wie man das macht.

Hier ist mein Startcode:

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); 
     } 
    } 
} 

Antwort

0

Sie benötigen den ganzen Clientarea Inhalt greifen zuerst (! Bitmap zu klein ist), dann wird das Rechteck Reihe ausgeschnitten. Stellen Sie außerdem sicher, der erstellten Ressourcen zu entsorgen!

Dies sollte funktionieren:

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); 
      } 
     } 
    } 
} 
Verwandte Themen