Alterar cor da fonte do item do treeview

Delphi

31/07/2009

Galera tenho o seguinte código de montagem de um treeview recursivo que esta funcionando perfeitamente:
procedure TForm07708.prPopulateTreeView;
var
_rintNivelRecAnt : Integer;
_rintNivelRecAtual : Integer;
begin
_pRoot := nil;
_pintIndice := -1;
_rintNivelRecAnt := 0;
SetLength(_pArrayNodes, 50);// Setando o tamanho do array
tvw_Estrutura.Items.Clear;
tvw_Estrutura.Items.BeginUpdate;
if cmbVigencias.Text = ´´ then
begin
fnMsg(msgCampoObrigatorio, ´Vigência´);
cmbVigencias.SetFocus;
Exit;
end;
// Trazendo a vigência baseada na descrição
Lib001.Query1.Close;
Lib001.Query1.SQL.Text :=
´ SELECT ´ +
´ VigenciaID ´ +
´ FROM ´ +
´ T417_Vigencias ´ +
´ WHERE ´ +
´ Situacao = ´´A´´ ´ +
´ AND Descricao = ´ + QuotedStr(cmbVigencias.Text) +
´ ORDER BY ´ +
´ Descricao ´;
Lib001.Query1.Open;
Q_Estrutura.Close;
Q_Estrutura.ParamByName(´P_Vigencia´).AsInteger := Lib001.Query1.FieldByName(´VigenciaID´).AsInteger;
Q_Estrutura.Open;
Q_Estrutura.First;
while not(Q_Estrutura.Eof) do
begin
// Estrutura recursiva para montar a árvore mercadológica
if (Q_EstruturaEstruturaIDx.AsString <> ´´) and ((Trim(Q_EstruturaDescricao.AsString )) <> ´´) then
begin
_rintNivelRecAtual := Q_EstruturaNivelRec.AsInteger;
if (_rintNivelRecAtual <> _rintNivelRecAnt) then
begin
if (_rintNivelRecAnt > _rintNivelRecAtual) then
begin
_pintIndice := _pintIndice - (_rintNivelRecAnt - _rintNivelRecAtual);
end
else
begin
Inc(_pintIndice);
end;
end;
if (_pintIndice <= 0 ) then
begin
_pRoot := tvw_Estrutura.Items.AddChild(nil, FormatFloat(´00000´, Q_EstruturaEstruturaIDx.AsInteger) + ´ - ´ + Trim(Q_EstruturaDescricao.AsString));
if Q_Estrutura.FieldByName(´Produto´).Value = 1 then
begin
_pRoot.TreeView.Canvas.Font.Color := clYellow;
_pRoot.TreeView.Repaint;
end
else
begin
_pRoot.TreeView.Canvas.Font.Color := clSilver;
_pRoot.TreeView.Repaint;
end;
end
else
begin
if _pintIndice = 0 then
begin
_pRoot := tvw_Estrutura.Items.AddChild(TTreeNode(_pArrayNodes[_pintIndice]), FormatFloat(´00000´, Q_EstruturaEstruturaIDx.AsInteger) + ´ - ´ + Trim(Q_EstruturaDescricao.AsString));
if Q_Estrutura.FieldByName(´Produto´).Value = 1 then
begin
_pRoot.TreeView.Canvas.Font.Color := clYellow;
_pRoot.TreeView.Repaint;
end
else
begin
_pRoot.TreeView.Canvas.Font.Color := clSilver;
_pRoot.TreeView.Repaint;
end;
end
else
begin
_pRoot := tvw_Estrutura.Items.AddChild(TTreeNode(_pArrayNodes[_pintIndice - 1]), FormatFloat(´00000´, Q_EstruturaEstruturaIDx.AsInteger) + ´ - ´ + Trim(Q_EstruturaDescricao.AsString));
if Q_Estrutura.FieldByName(´Produto´).Value = 1 then
begin
_pRoot.TreeView.Canvas.Font.Color := clYellow;
_pRoot.TreeView.Repaint;
end
else
begin
_pRoot.TreeView.Canvas.Font.Color := clSilver;
_pRoot.TreeView.Repaint;
end;
end;
end;
if _pintIndice >= 0 then
begin
_pArrayNodes[_pintIndice] := _pRoot;
end;
_rintNivelRecAnt := _rintNivelRecAtual;
end;
Q_Estrutura.Next;
end;
_pbolSelecionado := False;
Q_Estrutura.Close;
tvw_Estrutura.Items.EndUpdate;
pnlExpandirRecolher.Caption := ´EXPANDIR ESTRUTURA´;
end;

Agora eu tenho que alterar a cor da fonte do indice que é adicionado de acordo com a instrução:
if Q_Estrutura.FieldByName(´Produto´).Value = 1 then
begin
_pRoot.TreeView.Canvas.Font.Color := clYellow;
_pRoot.TreeView.Repaint;
end
else
begin
_pRoot.TreeView.Canvas.Font.Color := clSilver;
_pRoot.TreeView.Repaint;
end;

Mas a cor não se altera, permanece sempre a mesma, como se o código não estivesse sendo executado.

Se alguém puder me ajudar agradeço


Developer1978

Developer1978

Curtidas 0

Respostas

Emerson Nascimento

Emerson Nascimento

31/07/2009

pelo que eu pude avaliar, a manipulação da ´pintura´ que você precisa deve ser feita no evento OnCustomDrawItem.


GOSTEI 0
Developer1978

Developer1978

31/07/2009

pelo que eu pude avaliar, a manipulação da ´pintura´ que você precisa deve ser feita no evento OnCustomDrawItem.

Já tentei também e não deu certo


GOSTEI 0
Emerson Nascimento

Emerson Nascimento

31/07/2009

quando você precisar publicar uma rotina Delphi ou SQL, coloque-a entre [ code ] e [ /code ]

tentei dar uma simplificada no seu procedimento principal:
procedure TForm07708.prPopulateTreeView;
var
  _rintNivelRecAnt: Integer;
  _rintNivelRecAtual: Integer;
  Noh: TTreeNode;
begin
  _pRoot := nil;
  _pintIndice := -1;
  _rintNivelRecAnt := 0;

  if cmbVigencias.Text = ´´ then
  begin
    fnMsg(msgCampoObrigatorio, ´Vigência´);
    cmbVigencias.SetFocus;
    Exit;
  end;

  SetLength(_pArrayNodes, 50);// Setando o tamanho do array
  tvw_Estrutura.Items.Clear;
  tvw_Estrutura.Items.BeginUpdate;

  // Trazendo a vigência baseada na descrição
  Lib001.Query1.Close;
  Lib001.Query1.SQL.Text :=
  ´ SELECT ´ +
  ´ VigenciaID ´ +
  ´ FROM ´ +
  ´ T417_Vigencias ´ +
  ´ WHERE ´ +
  ´ Situacao = ´´A´´ ´ +
  ´ AND Descricao = ´ + QuotedStr(cmbVigencias.Text) +
  ´ ORDER BY ´ +
  ´ Descricao ´;
  Lib001.Query1.Open;
  Q_Estrutura.Close;
  Q_Estrutura.ParamByName(´P_Vigencia´).AsInteger := Lib001.Query1.FieldByName(´VigenciaID´).AsInteger;
  Q_Estrutura.Open;
  Q_Estrutura.First;

  while not(Q_Estrutura.Eof) do
  begin
    // Estrutura recursiva para montar a árvore mercadológica
    if (Q_EstruturaEstruturaIDx.AsString <> ´´) and ((Trim(Q_EstruturaDescricao.AsString )) <> ´´) then
    begin
      _rintNivelRecAtual := Q_EstruturaNivelRec.AsInteger;
      if (_rintNivelRecAtual <> _rintNivelRecAnt) then
      begin
        if (_rintNivelRecAnt > _rintNivelRecAtual) then
        begin
          _pintIndice := _pintIndice - (_rintNivelRecAnt - _rintNivelRecAtual);
        end
        else
        begin
          Inc(_pintIndice);
        end;
      end;

      if (_pintIndice <= 0 ) then
        Noh := nil
      else
      if _pintIndice = 0 then
        Noh := _pArrayNodes[_pintIndice]
      else
        Noh := _pArrayNodes[_pintIndice - 1];

      _pRoot := tvw_Estrutura.Items.AddChildObject(
          Noh,
          FormatFloat(´00000´, Q_EstruturaEstruturaIDx.AsInteger) + ´ - ´ + Trim(Q_EstruturaDescricao.AsString),
          Q_Estrutura.GetBookmark
      );

      if _pintIndice >= 0 then
        _pArrayNodes[_pintIndice] := _pRoot;

      _rintNivelRecAnt := _rintNivelRecAtual;
    end;

    Q_Estrutura.Next;
  end;

  _pbolSelecionado := False;
  Q_Estrutura.Close;
  tvw_Estrutura.Items.EndUpdate;

  pnlExpandirRecolher.Caption := ´EXPANDIR ESTRUTURA´;
end;


evento para ´pintar´ o item:
procedure TForm07708.tvw_EstruturaAdvancedCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  var PaintImages, DefaultDraw: Boolean);
var
  i: integer;
begin
  i := 0;

  if Node.Data <> nil then
  begin
    Q_Estrutura.GotoBookmark(Node.Data);
    i := Q_Estrutura.FieldByName(´Produto´).AsInteger;
  end;

  if i = 1 then
    Node.TreeView.Canvas.Font.Color := clYellow
  else
    Node.TreeView.Canvas.Font.Color := clSilver;
end;

fiz dessa forma, implementando o evento OnAdvancedCustomDrawItem e funcionou.


GOSTEI 0
Developer1978

Developer1978

31/07/2009

quando você precisar publicar uma rotina Delphi ou SQL, coloque-a entre [ code ] e [ /code ] tentei dar uma simplificada no seu procedimento principal:
procedure TForm07708.prPopulateTreeView;
var
  _rintNivelRecAnt: Integer;
  _rintNivelRecAtual: Integer;
  Noh: TTreeNode;
begin
  _pRoot := nil;
  _pintIndice := -1;
  _rintNivelRecAnt := 0;

  if cmbVigencias.Text = ´´ then
  begin
    fnMsg(msgCampoObrigatorio, ´Vigência´);
    cmbVigencias.SetFocus;
    Exit;
  end;

  SetLength(_pArrayNodes, 50);// Setando o tamanho do array
  tvw_Estrutura.Items.Clear;
  tvw_Estrutura.Items.BeginUpdate;

  // Trazendo a vigência baseada na descrição
  Lib001.Query1.Close;
  Lib001.Query1.SQL.Text :=
  ´ SELECT ´ +
  ´ VigenciaID ´ +
  ´ FROM ´ +
  ´ T417_Vigencias ´ +
  ´ WHERE ´ +
  ´ Situacao = ´´A´´ ´ +
  ´ AND Descricao = ´ + QuotedStr(cmbVigencias.Text) +
  ´ ORDER BY ´ +
  ´ Descricao ´;
  Lib001.Query1.Open;
  Q_Estrutura.Close;
  Q_Estrutura.ParamByName(´P_Vigencia´).AsInteger := Lib001.Query1.FieldByName(´VigenciaID´).AsInteger;
  Q_Estrutura.Open;
  Q_Estrutura.First;

  while not(Q_Estrutura.Eof) do
  begin
    // Estrutura recursiva para montar a árvore mercadológica
    if (Q_EstruturaEstruturaIDx.AsString <> ´´) and ((Trim(Q_EstruturaDescricao.AsString )) <> ´´) then
    begin
      _rintNivelRecAtual := Q_EstruturaNivelRec.AsInteger;
      if (_rintNivelRecAtual <> _rintNivelRecAnt) then
      begin
        if (_rintNivelRecAnt > _rintNivelRecAtual) then
        begin
          _pintIndice := _pintIndice - (_rintNivelRecAnt - _rintNivelRecAtual);
        end
        else
        begin
          Inc(_pintIndice);
        end;
      end;

      if (_pintIndice <= 0 ) then
        Noh := nil
      else
      if _pintIndice = 0 then
        Noh := _pArrayNodes[_pintIndice]
      else
        Noh := _pArrayNodes[_pintIndice - 1];

      _pRoot := tvw_Estrutura.Items.AddChildObject(
          Noh,
          FormatFloat(´00000´, Q_EstruturaEstruturaIDx.AsInteger) + ´ - ´ + Trim(Q_EstruturaDescricao.AsString),
          Q_Estrutura.GetBookmark
      );

      if _pintIndice >= 0 then
        _pArrayNodes[_pintIndice] := _pRoot;

      _rintNivelRecAnt := _rintNivelRecAtual;
    end;

    Q_Estrutura.Next;
  end;

  _pbolSelecionado := False;
  Q_Estrutura.Close;
  tvw_Estrutura.Items.EndUpdate;

  pnlExpandirRecolher.Caption := ´EXPANDIR ESTRUTURA´;
end;
evento para ´pintar´ o item:
procedure TForm07708.tvw_EstruturaAdvancedCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  var PaintImages, DefaultDraw: Boolean);
var
  i: integer;
begin
  i := 0;

  if Node.Data <> nil then
  begin
    Q_Estrutura.GotoBookmark(Node.Data);
    i := Q_Estrutura.FieldByName(´Produto´).AsInteger;
  end;

  if i = 1 then
    Node.TreeView.Canvas.Font.Color := clYellow
  else
    Node.TreeView.Canvas.Font.Color := clSilver;
end;
fiz dessa forma, implementando o evento OnAdvancedCustomDrawItem e funcionou.


Fiz como voce postou mas o Node.Data sempre esta como nil e nesse caso o i sempre vai ser zero deixando a fonte a fonte de todos os itens do treeview como clSilver


GOSTEI 0
Emerson Nascimento

Emerson Nascimento

31/07/2009

fez axatamente como foi postado?
a tabela Q_Estrutura não pode sofrer qualquer tipo de manutenção, seja troca de ordenação, ou ser fechada.

publique io código, exatamente como está atualmente no seu programa.


GOSTEI 0
Developer1978

Developer1978

31/07/2009

fez axatamente como foi postado? a tabela Q_Estrutura não pode sofrer qualquer tipo de manutenção, seja troca de ordenação, ou ser fechada. publique io código, exatamente como está atualmente no seu programa.

Me passe seu email que mando o .dfm e .pas e depois publicamos no fórum quando estiver funcionando se for possivel.


GOSTEI 0
Emerson Nascimento

Emerson Nascimento

31/07/2009

emerson.en@gmail.com
emerson.en@terra.com.br
emerson.en@ig.com.br


GOSTEI 0
Developer1978

Developer1978

31/07/2009

emerson.en@gmail.com emerson.en@terra.com.br emerson.en@ig.com.br


Já enviei o form. Um abraço.


GOSTEI 0
Emerson Nascimento

Emerson Nascimento

31/07/2009

você alterou o procedimentoprPopulateTreeView da forma como eu sugeri. note que eu não faço uso de AddChild e sim de AddChildObject.
utilize o código que eu te passei.


GOSTEI 0
Emerson Nascimento

Emerson Nascimento

31/07/2009

retificando...

você NÃO alterou o procedimento prPopulateTreeView da forma como eu sugeri. note que eu não faço uso de AddChild e sim de AddChildObject.
utilize o código que eu te passei.


GOSTEI 0
Developer1978

Developer1978

31/07/2009

retificando... você NÃO alterou o procedimento prPopulateTreeView da forma como eu sugeri. note que eu não faço uso de AddChild e sim de AddChildObject. utilize o código que eu te passei.


Funcionou. Muito obrigado e que Deus te abençoe.


GOSTEI 0
POSTAR