Fórum Alterar cor da fonte do item do treeview #372973

31/07/2009

0

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

Responder

Posts

31/07/2009

Emerson Nascimento

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


Responder

Gostei + 0

31/07/2009

Developer1978

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


Responder

Gostei + 0

31/07/2009

Emerson Nascimento

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.


Responder

Gostei + 0

03/08/2009

Developer1978

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


Responder

Gostei + 0

03/08/2009

Emerson Nascimento

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.


Responder

Gostei + 0

03/08/2009

Developer1978

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.


Responder

Gostei + 0

03/08/2009

Emerson Nascimento

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


Responder

Gostei + 0

03/08/2009

Developer1978

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


Já enviei o form. Um abraço.


Responder

Gostei + 0

03/08/2009

Emerson Nascimento

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.


Responder

Gostei + 0

03/08/2009

Emerson Nascimento

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.


Responder

Gostei + 0

04/08/2009

Developer1978

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.


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar