Fórum Alterar cor da fonte do item do treeview #372973
31/07/2009
0
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
Curtir tópico
+ 0Posts
31/07/2009
Emerson Nascimento
Gostei + 0
31/07/2009
Developer1978
Já tentei também e não deu certo
Gostei + 0
31/07/2009
Emerson Nascimento
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
03/08/2009
Developer1978
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;
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 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
03/08/2009
Emerson Nascimento
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
03/08/2009
Developer1978
Me passe seu email que mando o .dfm e .pas e depois publicamos no fórum quando estiver funcionando se for possivel.
Gostei + 0
03/08/2009
Emerson Nascimento
emerson.en@terra.com.br
emerson.en@ig.com.br
Gostei + 0
03/08/2009
Developer1978
Já enviei o form. Um abraço.
Gostei + 0
03/08/2009
Emerson Nascimento
utilize o código que eu te passei.
Gostei + 0
03/08/2009
Emerson Nascimento
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
04/08/2009
Developer1978
Funcionou. Muito obrigado e que Deus te abençoe.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)