Checkboxes in a Tree Control
2010-09-14 14:34
1656 查看
Checkboxes in a Tree Control
2009-07-29 13:15
2009-07-29 13:15
(转自http://www.cppblog.com/mydriverc/articles/28298.html)Abstract"Checkboxes in a tree control" shows how to add normal and three-state checkboxes to a tree control, and how to get notified when the checkbox changes. The sample project uses MFC, but the technique can easily be adapted to a Win32 or an ATL/WTL application.OverviewThe TreeView common control supports checkboxes since version 4.70 (distributed originally with Internet Explorer 3.0), and enabling this is supposedly easy- choose one of three:use the Checkboxes option in dialog resource editor specify the TVS_CHECKBOXES window style bit when creating the tree Use treeCtrl.SetWindowStyle(0, TVS_CHECKBOXES) in MFC applications, or SetWindowLong(hwndTree, GWL_STYLE, GetWindowLong(hwndTree, GWL_STYLE) | TVS_CHECKBOXES); InvalidateRect(hwndTree, NULL, true) for Win32 applications. This must be done before the tree control is filled with items. However, there are a few problems asociated with this: How do I detect when the checkbox is changed How (and when!) do I retrieve the current check state How to use Three-State Checkboxes. Tree Control ImagesThe tree control uses the state image list to implement the checkboxes. When the TVS_CHECKBOXES style is set, the tree control creates a custom state image list with the two state image buttons - unchecked & checked. When clicking on the state image, the tree control cycles through the state images.
Retrieving and Setting State Images
Three-State Check boxesFor three-state checkboxes, we just need to replace the default created state image list with our custom image list, windows will figure out the number of check states from the length of the image list. Remember that the first image (index 0) is unused. The sample uses the following Bitmap resource (IDB_STATEIMG in the sample):Add a member variable, CImageList m_ilState to the dialog (the image list must not be destroyed as long as the tree control is displayed) In OnInitDialog, add the following: m_ilState.Create(IDB_STATEIMG /* resID of bitmap */, 16 /* width of single image */, 0 /* 0: the image list won't grow */, RGB(255,255,255) /* transparent color */); m_treeCtrl.SetImageList(&m_ilState, TVSIL_STATE); // set state image list The size of the bitmap affects the number of checkbox 'states'. To retrieve the current state, use TreeCtrl_GetStateImage(...) - 1. As with a checkbox, this value is 0 for unchecked, 1 for checked, and 2 for indeterminate. This way, you could create a 'checkbox' with up to 15 states - which, on the other hand, ounds notlike fun for your users. More promising is the idea that you are not limited to checkboxes! Being Notified when Checkbox changesWe want to know when the checkmark is clicked,so we can do something. The TreeView control does not send a separate notification, when item state changes, so we must manage both NM_CLICK wnd TVN_KEYDOWN message separately.It's even trickier: When you get these notifications, the item state bits still indicates the old state. This wouldn't be as bad itself, but Microsoft might be tempted to change this behavior in the future. You can get the correct image (and thus, the current check state) when the current "Click" or "KeyDown" message is completely handled. To do this, we can post a message to outselves, using PostMessage(some_unique_message_id, ..), and evaluating the new style there. With the message, I send the tree control ID in WPARAM (in case there are multiple tree controls in the dialog), and the item which has changed in LPARAM.
|
相关文章推荐
- XHtmlTree - Tree control with support for HTML, XML, Smart Checkboxes, and Drag & Drop
- Preventing specific items from being selected in a Flex Tree control
- How to retrieve checkboxes values in jQuery
- Selecting multiple checkboxes inside a GridView control
- 启动oracle数据库报错ORA-00205: error in identifying control file, check alert log for more info
- how to make form:checkboxes in JSP
- Create Hierarchical Tree To Control Records In Oracle Forms
- Handling tree control check box selection/de-selection in Win32/MFC
- Handling Checkboxes, Radio Buttons and Select Options in jQuery [转]
- Selecting multiple checkboxes inside a GridView control - From CodeProject.com
- ORACLE11g 实例启动报错 ORA-00205: error in identifying control file, check alert log for more info
- Using the itemDoubleClick event to open nodes in a Flex Tree control
- ORA-00205: error in identifying control file, check alert log for more info
- Checking All CheckBoxes in a GridView
- Checking All CheckBoxes in a GridView
- 【原】Save TreeView Nodes Expansion/Collapse & Checkboxes State In Session
- (在做报文编辑器时右键菜单老不出来)This article explains how to display a context menu for a tree control in MFC
- ORA-00205: error in identifying control file, check alert log for more info
- How to determine which checkboxes are checked in postback (sqlstr="delete spprod where id in ("&request("sid")&")")
- 解决案例:ORA-00205: error in identifying control file, check alert log for more info