変化する
Color color = (e.State & TreeNodeStates.Selected) != 0 ?
SystemColors.HighlightText : SystemColors.WindowText;
に
Color color = (e.State & TreeNodeStates.Focused) != 0 ?
SystemColors.HighlightText : SystemColors.WindowText;
これは、.Net 3.5 を使用する Vista x64 および VS 2008 で機能しました。それがあなたのために働くかどうか私に知らせてください。
デフォルトのウィンドウの動作を見ているときに観察したことは、ノードが選択されてフォーカスされるまで、テキストとハイライトが描画されないことでした。ということで、文字色を変更するために合焦状態をチェックしてみました。ただし、これは、マウスが離されるまで新しい色が使用されない Widows の動作を正確に模倣するものではありません。所有者描画モードとウィンドウの描画モードで青いハイライトステータスの変化を描画することを選択したときのポイントが表示されます...確かに混乱しています。
編集 ただし、独自の派生ツリービューを作成すると、すべてがいつ描画されるかを完全に制御できます。
public class MyTreeView : TreeView
{
bool isLeftMouseDown = false;
bool isRightMouseDown = false;
public MyTreeView()
{
DrawMode = TreeViewDrawMode.OwnerDrawText;
}
protected override void OnMouseDown(MouseEventArgs e)
{
TrackMouseButtons(e);
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
TrackMouseButtons(e);
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
TrackMouseButtons(e);
base.OnMouseMove(e);
}
private void TrackMouseButtons(MouseEventArgs e)
{
isLeftMouseDown = e.Button == MouseButtons.Left;
isRightMouseDown = e.Button == MouseButtons.Right;
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
// don't call the base or it will goof up your display!
// capture the selected/focused states
bool isFocused = (e.State & TreeNodeStates.Focused) != 0;
bool isSelected = (e.State & TreeNodeStates.Selected) != 0;
// set up default colors.
Color color = SystemColors.WindowText;
Color backColor = BackColor;
if (isFocused && isRightMouseDown)
{
// right clicking on a
color = SystemColors.HighlightText;
backColor = SystemColors.Highlight;
}
else if (isSelected && !isRightMouseDown)
{
// if the node is selected and we're not right clicking on another node.
color = SystemColors.HighlightText;
backColor = SystemColors.Highlight;
}
using (Brush sb = new SolidBrush(backColor))
e.Graphics.FillRectangle(sb,e.Bounds);
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine |
TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis;
TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, backColor, flags);
}
}