DrawTreeNodeEventArgs.DrawDefault 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出是否應該由作業系統繪製 TreeNode,而不是由擁有者繪製。
public:
property bool DrawDefault { bool get(); void set(bool value); };
public bool DrawDefault { get; set; }
member this.DrawDefault : bool with get, set
Public Property DrawDefault As Boolean
屬性值
如果應該由作業系統繪製節點,則為 true
;如果要由事件處理常式繪製節點,則為 false
。 預設值是 false
。
範例
下列程式碼範例示範如何使用這個屬性。 在此範例中 TreeView.DrawNode ,事件的處理常式會手動繪製節點標記和所選節點的自訂醒目提示色彩。 未選取的節點不需要自訂。 針對未選取的節點, DrawDefault 屬性會設定為 true
,以便由作業系統繪製它們,而且不會有自訂的外觀。
注意
如果您手動繪製節點,並將 屬性設定 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
備註
針對您不想在處理事件時自訂的 DrawNode 節點,將此屬性 true
設定為 。 例如,當您只想要自訂處於特定狀態的節點時,這會很有用。