2010-04-09 16 views
2

Je souhaite afficher une longue chaîne pivotée en arrière-plan d'une de mes lignes dans un DataGridView. Cependant, ceci:Affichage d'une chaîne pivotée - DataGridView.RowPostPaint

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
{ 
    if (e.RowIndex == 0) 
    { 
     ... 
     //Draw the string 
     Graphics g = dataGridView1.CreateGraphics(); 
     g.Clip = new Region(e.RowBounds); 
     g.RotateTransform(-45); 
     g.DrawString(printMe, font, brush, e.RowBounds, format); 
    } 
} 

ne fonctionne pas parce que le texte est clipsé avant il est mis en rotation.

J'ai aussi essayé de peindre sur un Bitmap en premier, mais il semble y avoir un problème pour peindre des bitmaps transparents - le texte est noir pur.

Des idées?

Répondre

0

Je l'ai compris. Le problème était que les bitmaps n'ont apparemment pas de transparence, même lorsque vous utilisez PixelFormat.Format32bppArgb. Dessiner la corde l'a fait dessiner sur un fond noir, c'est pourquoi il faisait si sombre.

La solution consistait à copier la ligne de l'écran sur un bitmap, à dessiner sur le bitmap et à le recopier sur l'écran.

g.CopyFromScreen(absolutePosition, Point.Empty, args.RowBounds.Size); 

//Draw the rotated string here 

args.Graphics.DrawImageUnscaledAndClipped(buffer, args.RowBounds); 

Voici le code complet COTATION de référence:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs args) 
{ 
    if(args.RowIndex == 0) 
    { 
     Font font = new Font("Verdana", 11); 
     Brush brush = new SolidBrush(Color.FromArgb(70, Color.DarkGreen)); 
     StringFormat format = new StringFormat 
     { 
      FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip, 
      Trimming = StringTrimming.None, 
     }; 

     //Setup the string to be printed 
     string printMe = String.Join(" ", Enumerable.Repeat("RUNNING", 10).ToArray()); 
     printMe = String.Join(Environment.NewLine, Enumerable.Repeat(printMe, 50).ToArray()); 

     //Draw string onto a bitmap 
     Bitmap buffer = new Bitmap(args.RowBounds.Width, args.RowBounds.Height); 
     Graphics g = Graphics.FromImage(buffer); 
     Point absolutePosition = dataGridView1.PointToScreen(args.RowBounds.Location); 
     g.CopyFromScreen(absolutePosition, Point.Empty, args.RowBounds.Size); 
     g.RotateTransform(-45, MatrixOrder.Append); 
     g.TranslateTransform(-50, 0, MatrixOrder.Append); //So we don't see the corner of the rotated rectangle 
     g.DrawString(printMe, font, brush, args.RowBounds, format); 

     //Draw the bitmap onto the table 
     args.Graphics.DrawImageUnscaledAndClipped(buffer, args.RowBounds); 
    } 
}