我无法在程序中设置CTreeCtrl中的复选框。 [英] I can't set checkboxes in CTreeCtrl from program.

查看:76
本文介绍了我无法在程序中设置CTreeCtrl中的复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
这是一个MFC noob问题。

我有一个设置了TVS_CHECKBOXES的CTreeCtrl。

在OnInitDialog中,我使用一组分组到包和组中的规则填充树,我希望预设一个选择。树已填充好,但未检查任何项目。我可以检查GUI中的项目并读取该状态确定,但我的预设已经消失。



这是代码(好吧,部分内容):

Hi Here is an MFC noob question.
I have a CTreeCtrl with TVS_CHECKBOXES set.
In OnInitDialog I populate the tree with a set of rules grouped into packages and groups, and I wish to preset a selection. The tree is populated ok but with no items checked. I can check items in the GUI and read that state ok, but my pre-sets are gone.

This is the code (well, parts of it):

OnInitDialog()
{
  __super::OnInitDialog();
  PopulateTree();
  return TRUE; 
}

PopulateTree ()
{
  mRulesTree.DeleteAllItems();

  // Aquire current info
  ParseRulesFile();

  // TBD: Do I need to do something like this? It is mentioned on the webb...
  mRulesTree.ModifyStyle( 0, TVS_CHECKBOXES );

  // Create the first nodes
  std::string package = mRulesList[0]->package;
  std::string group   = mRulesList[0]->group;
  HTREEITEM currPackage = mRulesTree.InsertItem( package.c_str(), NULL );
  HTREEITEM currGroup   = mRulesTree.InsertItem( group.c_str(), currPackage );
  HTREEITEM currRule;
  for ( size_t i = 0; i < mRulesList.size(); ++i )
  {
    if ( mRulesList[i]->package != package )
    {
      // Create new package node
      package = mRulesList[i]->package;
      currPackage = mRulesTree.InsertItem( package.c_str(), NULL );
    }
    if ( mRulesList[i]->group != group )
    {
      // Create new group node
      group = mRulesList[i]->group;
      currGroup = mRulesTree.InsertItem( group.c_str(), currPackage );
    }
    std::string ruleDesc = mRulesList[i]->name + " - " + mRulesList[i]->synopsis;
    currRule = mRulesTree.InsertItem( mRulesList[i]->name.c_str(), currGroup );
    if ( mSelectedRules.size() != 0 )
    {
      // We have a set of rules from the project settings.
      // If the current rule is in the list, check the checkbox.
      if ( std::find( mSelectedRules.begin(), 
                      mSelectedRules.end(), 
                      mRulesList[i]->name ) != 
             mSelectedRules.end() )
      {
        // TBD: DOESN'T WORK!
        mRulesTree.SetCheck( currRule, true );
      }
    }
    else
    {
      // We have no previous settings. Use default. TBD: DOESN'T WORK!
      mRulesTree.SetCheck( currRule, mRulesList[i]->default_on );
    }
  }
}





这是来自资源文件:



and this is from the resource file:

IDD_RULES_SETTINGS_DLG DIALOGEX 0, 0, 300, 300
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Rules Settings"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    LTEXT           "RULES",IDC_STATIC,7,8,32,8
    CONTROL         "",IDC_RULES_SETTINGS_TREE,"SysTreeView32",TVS_HASLINES | TVS_HASBUTTONS | TVS_CHECKBOXES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | WS_BORDER | WS_HSCROLL | WS_TABSTOP,14,20,273,245
    DEFPUSHBUTTON   "OK",IDOK,189,280,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,243,280,50,14
END





问候

Robert



Regards
Robert

推荐答案

这有点棘手。 Microsoft在其文档中写道:如果你想要使用此样式,您必须在创建树视图控件之后,在填充树之前使用SetWindowLong设置TVS_CHECKBOXES样式。否则,复选框可能会取消选中,具体取决于计时问题。



最好在SetWindowLong中使用它。
it is some tricky. Microsoft writes in its documenation: "If you want to use this style, you must set the TVS_CHECKBOXES style with SetWindowLong after you create the treeview control, and before you populate the tree. Otherwise, the checkboxes might appear unchecked, depending on timing issues."

You better use it with SetWindowLong.


借助KarstenK的输入,一些网页搜索给了我这个愚蠢的优雅(?)解决方案(! )问题:



在填充PopulateTree函数中的树形控件之前,我执行以下调用:

With help of the input from KarstenK, some web searching gave me this elegant(?) solution of this stupid(!) problem:

Before populating the tree control in the PopulateTree function I do the following calls:
// First remove the checkbox style
mRulesTree.ModifyStyle( TVS_CHECKBOXES, 0 );
// Then explicitly set it
mRulesTree.ModifyStyle( 0, TVS_CHECKBOXES );





在我原来的解决方案中,我只是做了后一个电话,这显然是不够的。



再次感谢!

Robert



In my original solution I just did the latter call, and that was obviously not enough.

Thanks again!
Robert


这篇关于我无法在程序中设置CTreeCtrl中的复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆