Pergunta

Eu tenho um aplicativo conectado a um servidor remoto e dados de pesquisa quando necessário. Possui uma TreeView em que os nós representam os objetos disponíveis e a cor do texto indica se os dados foram carregados ou não; O texto regular não carregado, o texto preto, preto, é carregado.

Atualmente, configurei o TreeView como OwnDerDRAWTEXT e tenho a função TreeView.DRAWNODE Basta desenhar o texto como assim:

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

Achei que isso seria suficiente, no entanto, isso tem causado alguns problemas;

  1. Quando um nó é selecionado, focado ou não, ele não envolve todo o texto, exemplo (Espero que o imgur esteja bem).
  2. Quando o nó é focado, o contorno pontilhado também não mostra. Se você comparar com isso exemplo. Os nós com o "log" no texto estão usando o e.defaultDraw = true

Eu tentei seguir o exemplo dado em isto pergunta. Parecia algo assim:

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

No entanto, os resultados foram isto. (Observe, usado rosa para diferenciar as cores). Como você pode ver, o fundo destacado não se estende até onde está a linha pontilhada focada. E também há outra caixa desenhada também.

Estou um pouco perplexo sobre como consertar isso. Tudo o que eu quero é ter um texto em itálico cinza quando algo é carregado. A primeira e mais simples abordagem não funciona e o segundo método parece que estou fazendo muito.

Depois de tudo isso, alguém tem alguma sugestão sobre como fazer isso corretamente, porque deve haver uma maneira mais simples.

Agradeço antecipadamente.

Foi útil?

Solução

Você precisará usar o textrender.drawText (). É isso que o TreeView usa, ele torna o texto um pouco diferente dos gráficos.DrawString ().

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top