DrawTreeNodeEventArgs.State 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取要绘制的 TreeNode 的当前状态。
public:
property System::Windows::Forms::TreeNodeStates State { System::Windows::Forms::TreeNodeStates get(); };
public System.Windows.Forms.TreeNodeStates State { get; }
member this.State : System.Windows.Forms.TreeNodeStates
Public ReadOnly Property State As TreeNodeStates
属性值
TreeNodeStates 值的按位组合,指示 TreeNode 的当前状态。
示例
下面的代码示例演示如何使用所有者绘图自定义 TreeView 控件。 TreeView示例中的 控件在标准节点标签旁边显示可选节点标记。 使用 TreeNode.Tag 属性指定节点标记。 控件 TreeView 还使用自定义颜色,包括自定义突出显示颜色。
可以通过设置颜色属性来自定义大多数 TreeView 颜色,但选择突出显示颜色不能作为属性使用。 此外,默认选择突出显示矩形仅围绕节点标签扩展。 必须使用所有者绘图来绘制节点标记,并绘制足够大以包含节点标记的自定义突出显示矩形。
在示例中,事件的处理程序 TreeView.DrawNode 手动绘制节点标记和自定义选择突出显示。 未选择的节点不需要自定义。 对于这些, DrawDefault 属性设置为 true
,以便由操作系统绘制它们。
有关完整示例,请参阅 DrawTreeNodeEventArgs 概述参考主题。
// Draws a node.
private:
void myTreeView_DrawNode( Object^ sender, DrawTreeNodeEventArgs^ e )
{
// Draw the background and node text for a selected node.
if ( (e->State & TreeNodeStates::Selected) != (TreeNodeStates)0 )
{
// Draw the background of the selected node. The NodeBounds
// method makes the highlight rectangle large enough to
// include the text of a node tag, if one is present.
e->Graphics->FillRectangle( Brushes::Green, NodeBounds( e->Node ) );
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
System::Drawing::Font^ nodeFont = e->Node->NodeFont;
if ( nodeFont == nullptr )
nodeFont = (dynamic_cast<TreeView^>(sender))->Font;
// Draw the node text.
e->Graphics->DrawString( e->Node->Text, nodeFont, Brushes::White, Rectangle::Inflate( e->Bounds, 2, 0 ) );
}
// Use the default background and node text.
else
{
e->DrawDefault = true;
}
// If a node tag is present, draw its string representation
// to the right of the label text.
if ( e->Node->Tag != nullptr )
{
e->Graphics->DrawString( e->Node->Tag->ToString(), tagFont, Brushes::Yellow, (float)e->Bounds.Right + 2, (float)e->Bounds.Top );
}
// If the node has focus, draw the focus rectangle large, making
// it large enough to include the text of the node tag, if present.
if ( (e->State & TreeNodeStates::Focused) != (TreeNodeStates)0 )
{
Pen^ focusPen = gcnew Pen( Color::Black );
try
{
focusPen->DashStyle = System::Drawing::Drawing2D::DashStyle::Dot;
Rectangle focusBounds = NodeBounds( e->Node );
focusBounds.Size = System::Drawing::Size( focusBounds.Width - 1, focusBounds.Height - 1 );
e->Graphics->DrawRectangle( focusPen, focusBounds );
}
finally
{
if ( focusPen )
delete safe_cast<IDisposable^>(focusPen);
}
}
}
// Draws a node.
private void myTreeView_DrawNode(
object sender, DrawTreeNodeEventArgs e)
{
// Draw the background and node text for a selected node.
if ((e.State & TreeNodeStates.Selected) != 0)
{
// Draw the background of the selected node. The NodeBounds
// method makes the highlight rectangle large enough to
// include the text of a node tag, if one is present.
e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
// Draw the node text.
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
Rectangle.Inflate(e.Bounds, 2, 0));
}
// Use the default background and node text.
else
{
e.DrawDefault = true;
}
// If a node tag is present, draw its string representation
// to the right of the label text.
if (e.Node.Tag != null)
{
e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont,
Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);
}
// If the node has focus, draw the focus rectangle large, making
// it large enough to include the text of the node tag, if present.
if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = NodeBounds(e.Node);
focusBounds.Size = new Size(focusBounds.Width - 1,
focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
' Draws a node.
Private Sub myTreeView_DrawNode(ByVal sender As Object, _
ByVal e As DrawTreeNodeEventArgs) Handles myTreeView.DrawNode
' Draw the background and node text for a selected node.
If (e.State And TreeNodeStates.Selected) <> 0 Then
' Draw the background of the selected node. The NodeBounds
' method makes the highlight rectangle large enough to
' include the text of a node tag, if one is present.
e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node))
' Retrieve the node font. If the node font has not been set,
' use the TreeView font.
Dim nodeFont As Font = e.Node.NodeFont
If nodeFont Is Nothing Then
nodeFont = CType(sender, TreeView).Font
End If
' Draw the node text.
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, _
e.Bounds.Left - 2, e.Bounds.Top)
' Use the default background and node text.
Else
e.DrawDefault = True
End If
' If a node tag is present, draw its string representation
' to the right of the label text.
If (e.Node.Tag IsNot Nothing) Then
e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont, _
Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top)
End If
' If the node has focus, draw the focus rectangle large, making
' it large enough to include the text of the node tag, if present.
If (e.State And TreeNodeStates.Focused) <> 0 Then
Dim focusPen As New Pen(Color.Black)
Try
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
Dim focusBounds As Rectangle = NodeBounds(e.Node)
focusBounds.Size = New Size(focusBounds.Width - 1, _
focusBounds.Height - 1)
e.Graphics.DrawRectangle(focusPen, focusBounds)
Finally
focusPen.Dispose()
End Try
End If
End Sub
注解
使用此属性可检查要绘制的对象是否TreeNode处于特定状态。 此属性仅提供有关节点的基本状态信息。 例如,可以使用此属性来确定节点是选中、选中还是聚焦。 如果需要了解详细信息,请通过 Node 属性检索节点。 例如,若要确定是否展开节点,必须检索节点并检查 TreeNode.IsExpanded 属性。