Pregunta

Tengo una aplicación que se conecta a un servidor remoto y los datos de votación cuando sea necesario. Tiene un TreeView, donde los nodos representan los objetos que están disponibles y el color del texto indican si los datos se han cargado o no; gris-cursiva indica no cargado, se carga negro, texto normal.

Actualmente tengo presente al TreeView ser OwnderDrawText y tienen la función TreeView.DrawNode simplemente dibujar el texto como tal:

private void TreeViewDrawNode(object sender, DrawTreeNodeEventArgs e)
{
    if (!e.Node.IsVisible)
    {
        return;
    }

    bool bLoaded = false;

    if (e.Bounds.Location.X >= 0 && e.Bounds.Location.Y >= 0)
    {
       if(e.Node.Tag != null)
       {
           //...
           // code determining whether data has been loaded is done here
           // setting bLoaded true or false
           //...
       }
       else
       {
           e.DrawDefault = true;
           return;
       }

       Font useFont = null;
       Brush useBrush = null;

       if (bLoaded)
       {
           useFont = e.Node.TreeView.Font;
           useBrush = SystemBrushes.WindowText;
        }
        else
        {
            useFont = m_grayItallicFont;
            useBrush = SystemBrushes.GrayText;
        }
        e.Graphics.DrawString(e.Node.Text, useFont, useBrush, e.Bounds.Location);
    }
}

Me imaginé que sería suficiente, sin embargo, esto ha causado algunos problemas;

  1. Cuando se selecciona un nodo, se centró o no, no envuelve todo el texto, ejemplo (espero imgur está bien).
  2. Cuando el nodo se centra, la línea de puntos no muestra tampoco. Si se compara con este ejemplo . Los nodos con el "log" en el texto están utilizando el e.DefaultDraw = true

I intentado siguiendo el ejemplo dado en esta pregunta . Parecía algo como esto:

private void TreeViewDrawNode(object sender, DrawTreeNodeEventArgs e)
 {
  if (!e.Node.IsVisible)
  {
   return;
  }

  bool bLoaded = false;

  if (e.Bounds.Location.X >= 0 && e.Bounds.Location.Y >= 0)
  {
     if(e.Node.Tag != null)
     {
      //...
      // code determining whether data has been loaded is done here
      // setting bLoaded true or false
      //...
     }
     else
     {
      e.DrawDefault = true;
      return;
     }

   //Select the font and brush depending on whether the property has been loaded
   Font useFont = null;
   Brush useBrush = null;

   if (bLoaded)
   {
    useFont = e.Node.TreeView.Font;
    useBrush = SystemBrushes.WindowText;
   }
   else
   {
    //member variable defined elsewhere
    useFont = m_grayItallicFont;
    useBrush = SystemBrushes.GrayText;
   }

   //Begin drawing of the text

   //Get the rectangle that will be used to draw
   Rectangle itemRect = e.Bounds;
   //Move the rectangle over by 1 so it isn't on top of the check box
   itemRect.X += 1;

   //Figure out the text position
   Point textStartPos = new Point(itemRect.Left, itemRect.Top);
   Point textPos = new Point(textStartPos.X, textStartPos.Y);

   //generate the text rectangle
   Rectangle textRect = new Rectangle(textPos.X, textPos.Y, itemRect.Right - textPos.X, itemRect.Bottom - textPos.Y);

   int textHeight = (int)e.Graphics.MeasureString(e.Node.Text, useFont).Height;
   int textWidth = (int)e.Graphics.MeasureString(e.Node.Text, useFont).Width;

   textRect.Height = textHeight;

   //Draw the highlighted box
   if ((e.State & TreeNodeStates.Selected) != 0)
   {
    //e.Graphics.FillRectangle(SystemBrushes.Highlight, textRect);
    //use pink to see the difference
    e.Graphics.FillRectangle(Brushes.Pink, textRect);
   }
   //widen the rectangle by 3 pixels, otherwise all of the text     won't fit
   textRect.Width = textWidth + 3;

   //actually draw the text
   e.Graphics.DrawString(e.Node.Text, useFont, useBrush, e.Bounds.Location);

   //Draw the box around the focused node
   if ((e.State & TreeNodeStates.Focused) != 0)
   {
    textRect.Width = textWidth;
    Pen focusPen = new Pen(Color.Black);
    focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
    e.Graphics.DrawRectangle(focusPen, textRect);
   }
  }
 }

Sin embargo, los resultados fueron este . (Nota, utiliza rosa para diferenciar los colores). Como se puede ver, el fondo resaltado no se extiende todo el camino hasta donde la línea de puntos es enfocada al. Y también hay otro cuadro que se dibuja también.

Estoy un poco confundido sobre cómo solucionar este problema. Todo lo que quiero es tener el texto en cursiva gris cuando se carga algo. La primera y más simple enfoque funciona no del todo y el segundo método se siente como que estoy haciendo demasiado.

Después de todo eso, ¿alguien tiene alguna sugerencia sobre cómo hacer esto correctamente, ya que tiene que haber una manera más sencilla.

Gracias de antemano.

¿Fue útil?

Solución

Usted tendrá que utilizar TextRenderer.DrawText (). Eso es lo que utiliza TreeView, que representa el texto ligeramente diferente de Graphics.DrawString ().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top