TreeView::DrawNode Event
2010-11-16 08:49
337 查看
#using <System.dll> #using <System.Windows.Forms.dll> #using <System.Drawing.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; public ref class TreeViewOwnerDraw: public Form { private: TreeView^ myTreeView; // Create a Font object for the node tags. System::Drawing::Font^ tagFont; public: TreeViewOwnerDraw() { tagFont = gcnew System::Drawing::Font( "Helvetica",8,FontStyle::Bold ); // Create and initialize the TreeView control. myTreeView = gcnew TreeView; myTreeView->Dock = DockStyle::Fill; myTreeView->BackColor = Color::Tan; myTreeView->CheckBoxes = true; // Add nodes to the TreeView control. TreeNode^ node; for ( int x = 1; x < 4; ++x ) { // Add a root node to the TreeView control. node = myTreeView->Nodes->Add( String::Format( "Task {0}", x ) ); for ( int y = 1; y < 4; ++y ) { // Add a child node to the root node. node->Nodes->Add( String::Format( "Subtask {0}", y ) ); } } myTreeView->ExpandAll(); // Add tags containing alert messages to a few nodes // and set the node background color to highlight them. myTreeView->Nodes[ 1 ]->Nodes[ 0 ]->Tag = "urgent!"; myTreeView->Nodes[ 1 ]->Nodes[ 0 ]->BackColor = Color::Yellow; myTreeView->SelectedNode = myTreeView->Nodes[ 1 ]->Nodes[ 0 ]; myTreeView->Nodes[ 2 ]->Nodes[ 1 ]->Tag = "urgent!"; myTreeView->Nodes[ 2 ]->Nodes[ 1 ]->BackColor = Color::Yellow; // Configure the TreeView control for owner-draw and add // a handler for the DrawNode event. myTreeView->DrawMode = TreeViewDrawMode::OwnerDrawText; myTreeView->DrawNode += gcnew DrawTreeNodeEventHandler( this, &TreeViewOwnerDraw::myTreeView_DrawNode ); // Add a handler for the MouseDown event so that a node can be // selected by clicking the tag text as well as the node text. myTreeView->MouseDown += gcnew MouseEventHandler( this, &TreeViewOwnerDraw::myTreeView_MouseDown ); // Initialize the form and add the TreeView control to it. this->ClientSize = System::Drawing::Size( 292, 273 ); this->Controls->Add( myTreeView ); } protected: // Clean up any resources being used. ~TreeViewOwnerDraw() { if ( tagFont != nullptr ) { delete tagFont; } } // 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); } } } // Selects a node that is clicked on its label or tag text. void myTreeView_MouseDown( Object^ /*sender*/, MouseEventArgs^ e ) { TreeNode^ clickedNode = myTreeView->GetNodeAt( e->X, e->Y ); if ( NodeBounds( clickedNode ).Contains( e->X, e->Y ) ) { myTreeView->SelectedNode = clickedNode; } } // Returns the bounds of the specified node, including the region // occupied by the node label and any node tag displayed. Rectangle NodeBounds( TreeNode^ node ) { // Set the return value to the normal node bounds. Rectangle bounds = node->Bounds; if ( node->Tag != nullptr ) { // Retrieve a Graphics object from the TreeView handle // and use it to calculate the display width of the tag. Graphics^ g = myTreeView->CreateGraphics(); int tagWidth = (int)g->MeasureString( node->Tag->ToString(), tagFont ).Width + 6; // Adjust the node bounds using the calculated value. bounds.Offset( tagWidth / 2, 0 ); bounds = Rectangle::Inflate( bounds, tagWidth / 2, 0 ); g->~Graphics(); } return bounds; } }; [STAThreadAttribute] int main() { Application::Run( gcnew TreeViewOwnerDraw ); }
相关文章推荐
- TreeView.DrawNode Event 官方指南
- BUG:TreeView: NodeCheck Event Does Not Occur
- Treeview SelectedNodeChanged event not firing
- 解决TreeView的节点更改NodeFont后无法完全显示节点名的问题
- TreeView 控件 TreeNodeCheckChanged事件无法自动回发
- Node.js EventEmitter
- TreeView.AfterCheck和TreeNode.Checked赋值的问题
- 如何让TreeView的TreeNodeCheckChanged事件自动回发到服务器端
- Node.js EventEmitter
- .net C# Treeview的查找node文字项
- WARNING: Subscription for node down event still pending
- 用键盘上下左右键和ctrl键移动treeview的treenode节点
- node.js中的events.EventEmitter.listenerCount方法使用说明
- 海洋工作室——网站建设专家:VS.NET - ASP.NET -ToolTip per node in ASP.NET Treeview control
- CRS-0184 and clssnmLocalJoinEvent: takeover aborted due to ALIVE node on Disk
- 【quick-lua 002】 触摸捕获事件 cc.NODE_TOUCH_CAPTURE_EVENT
- cocos2dx 3.2 渲染(Director::drawScene 在游戏loop里每帧调用。 内部实际通过调用场景内每个node的 visit方法完成渲染)
- 带Checkbox的TreeView(一) 完美兼容IE、Firefox ,在js中添加了getCurrentNode(evt)方法,注册方法变为TreeView1.Attributes.Add("onclick", "CheckEvent
- node EventEmitter 对象
- cocos2d-x 3.0 绘制图形DrawNode与调用周期性回调函数schedule