首先ctreectrl的checkbox属性需要用代码去设置
m_tree.ModifyStyle(0, TVS_CHECKBOXES);
checkbox改变事件也就是点击事件。
1 重载treectrl的OnNotify()
2 自定义一个消息,在这个消息的处理函数中,去做checkbox改变的响应。
OnNotify内容:
- #define UM_CHECKSTATECHANGE (WM_USER + 19)
- #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
- #define GET_Y_LPARAM(lp) ((lp & 0xFFFF0000)>>16)
- BOOL SomeDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
- {
- // TODO: Add your specialized code here and/or call the base class
- LPNMHDR lpnmh = (LPNMHDR) lParam;
- TVHITTESTINFO ht = {0};
-
- if((lpnmh->code == NM_CLICK) && (lpnmh->idFrom == IDC_TR_TBS))
- {
- DWORD dwpos = GetMessagePos();
-
- // include <windowsx.h> and <windows.h> header files
- ht.pt.x = GET_X_LPARAM(dwpos);
- ht.pt.y = GET_Y_LPARAM(dwpos);
- ::MapWindowPoints(HWND_DESKTOP, lpnmh->hwndFrom, &ht.pt, 1);
-
- TreeView_HitTest(lpnmh->hwndFrom, &ht);
-
- if(TVHT_ONITEMSTATEICON & ht.flags)
- {
- ::PostMessage(m_hWnd, UM_CHECKSTATECHANGE, 0, (LPARAM)ht.hItem);
- }
- }
- return CDialog::OnNotify(wParam, lParam, pResult);
- }
处理的时候取得状态
LRESULT SomeDlg::OnMyMessage(WPARAM wParam, LPARAM lParam){
- HTREEITEM hItemChanged = (HTREEITEM)lParam;
- if(NULL == hItemChanged){ return -1;}
- BOOL checked = m_tbs.GetCheck(hItemChanged);
- ......
- }
阅读(4663) | 评论(0) | 转发(0) |