原文鏈接:https://blog.csdn.net/qq_30722721/article/details/102561571
//基本概念:一個Node節點代表一行
//***部分命令請注意順序關系***
//插入列,注意插入列僅initialize_cb()/dialogShown_cb()設置一次,update()時會報錯
for (int i = 0; i < columnCount; ++i)
{
tree->InsertColumn(i, "", columnWidth);
tree->SetColumnResizePolicy(i, BlockStyler::Tree::ColumnResizePolicyConstantWidth);
tree->SetColumnSortable(i, false);
}
//創建
Node *node = tree->CreateNode("label");
tree->InsertNode(node, NULL, NULL, Tree::NodeInsertOptionLast);
tree->InsertNode(node, parentNode, NULL, Tree::NodeInsertOptionLast);//父節點
//遍歷子節點
void GetAllNode(Node* inputNode, vector<Node*> &nodes)
{
if (inputNode == NULL)
{
return;
}
Node* node = inputNode->FirstChildNode();
while (node != NULL)
{
nodes.push_back(node);
NXCommon::GetAllNode(node, nodes);
node = node->NextSiblingNode();
}
}
//得到樹控件所有節點
vector<Node*> GetAllNodes(Tree* tree)
{
vector<Node*> res;
Node *nd = tree->RootNode();
while (nd != NULL)
{
res.push_back(nd);
nd = nd->NextSiblingNode();
}
return res;
}
/得到樹控件所有節點 含子節點
std::vector<Node*> GetAllNodesContainSon(Tree* tree)
{
Node *nd = tree->RootNode();
vector<Node*> res;
while (nd != NULL)
{
res.push_back(nd);
GetAllNode(nd, res);
nd = nd->NextSiblingNode();
}
return res;
}
//清空樹節點
void ClearTree(Tree* tree)
{
vector<BlockStyler::Node*> nodes = GetAllNodes(tree);
for (int i = 0; i < nodes.size(); i++)
{
tree->DeleteNode(nodes[i]);
}
}
//節點選擇狀態
tree->SelectNode(node, false, false); //不選中
node->IsSelected(); //節點是否選中
//顯示文本
node->SetColumnDisplayText(columnID, "Text");
//節點添加屬性
node->GetNodeData()->AddInteger("XX",int);
node->GetNodeData()->SetInteger("XX",int); //先調用AddInteger才有效,否則報錯
//展開節點
node->Expand(NXOpen::BlockStyler::Node::ExpandOption::ExpandOptionExpand);
//注意:OnEditOptionSelectedCallback()不支持節點增刪改
NXOpen::BlockStyler::Tree::EditControlOption OnEditOptionSelectedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID, int selectedOptionID, NXString selectedOptionText, NXOpen::BlockStyler::Tree::ControlType type)
{
if (tree == NULL || node == NULL)
return Tree::EditControlOptionReject;
//如果要檢查輸入值是否為double,必須強制檢查
std::string originText = selectedOptionText.GetLocaleText(); //重要 得到輸入的值
std::string text = NXCommon::DoubleToStr(NXCommon::StrToDouble(originText));
if (originText != text)
{
return Tree::EditControlOptionReject;
}
return Tree::EditControlOptionAccept;
}
//編輯Node某列
NXOpen::BlockStyler::Tree::ControlType AskEditControlCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
if (tree == NULL || node == NULL)
{
return Tree::ControlTypeNone;
}
//只有枚舉樣式,設置一個值時,效果等同字符串控件
std::vector<NXString> tmpContents;
tmpContents.push_back("A");
tmpContents.push_back("B");
tree->SetEditOptions(tmpContents, 0);
return Tree::ControlTypeComboBox;
}
//Node 右鍵菜單
void OnMenuCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID )
{
TreeListMenu *menu = tree->CreateMenu();
menu->AddMenuItem(0, "創建");
menu->AddMenuItem(1, "刪除");
tree->SetMenu(menu);
delete menu;
menu = NULL;
}
//Node 拖動
//更新樹控件時,Tree最好只更新一次,
//否則代碼中未執行完的代碼可能報錯,如:node[i]->GetColumnDisplayText(0).GetLocaleText();
目前NX樹控件不支持直接拖動,可以通過
(1) 重繪樹控件
(2) 將拖動節點備份,重新插入目標節點(個人覺得比較麻煩)
(3) 其他....
NXOpen::BlockStyler::Node::DragType IsDragAllowedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
if (tree == NULL || node == NULL)
return BlockStyler::Node::DragTypeNone;
return BlockStyler::Node::DragTypeAll;//允許節點拖動,但沒有拖動效果
}
//Node 某列排序 注意:代碼放置在數據填充之后生效
tree->SetSortRootNodes(true);
tree->SetColumnSortable(columnID, true);
tree->SetColumnSortOption(columnID, Tree::ColumnSortOption::ColumnSortOptionDescending);
//排序回調函數處理
int ColumnSortCallback(NXOpen::BlockStyler::Tree *tree, int columnID, NXOpen::BlockStyler::Node *node1, NXOpen::BlockStyler::Node *node2)
{
//返回值為0、正負值;分別表示兩個節點相同,第一個節點大於第二個,第一個節點小於第二個
int res = 0;
//注意:node1是樹控件上后一個節點,node2是前一個節點 !!!
std::string content1 = node1->GetColumnDisplayText(columnID).GetLocaleText();
std::string content2 = node2->GetColumnDisplayText(columnID).GetLocaleText();
if (IsDoubleString(content1))
{
if (IsGT(StrToDouble(content1), StrToDouble(content2)))
{
res = 1;
}
else
{
res = -1;
}
}
else
{
if (content1 > content2)
{
res = 1;
}
else
{
res = -1;
}
}
return res;
}
//Node 某列顯示/隱藏
tree->SetColumnVisible(columnID,false);
//Node 第一列前:打勾+圖片
node->SetState(1); //1 = 不打勾,2 = 打勾
node->SetDisplayIcon(bmpPath);
node->SetSelectedIcon(bmpPath);
//Node 設置顯示文本顏色
node->SetForegroundColor(186);
