Q: Use keyboard to toggle CTreeCtrl items
wang shao yu -- wsy@pub.zjpta.net.cn Monday, December 23, 1996 Hi all friends, Merry Christmas! I am using a CTreeView object in my application. I'd like my CTreeView's parent items can be toggled (expanded or collapsed) by pressing [Enter]. The problem is whenever I press [enter] the items do toggled but with a beep! I want to get rid of the annoying beep but without success. I have tried: + Override the OnKeyDown() virtual method in my CTreeView object like this: void CMyTreeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { CTreeCtrl& treeCtrl = GetTreeCtrl(); HTREEITEM hItem; if (nChar == VK_RETURN) {//if item has child hItem = treeCtrl.GetSelectedItem(); if (hItem && treeCtrl.ItemHasChildren(hItem)) treeCtrl.Expand(hItem, TVE_TOGGLE); } else CTreeView::OnKeyDown(nChar, nRepCnt, nFlags); }//OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) + Override the OnKeyUp() virtual method in my CTreeView object like this: void CMyTreeView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { CTreeCtrl& treeCtrl = GetTreeCtrl(); HTREEITEM hItem; if (nChar == VK_RETURN) {//if item has child hItem = treeCtrl.GetSelectedItem(); if (hItem && treeCtrl.ItemHasChildren(hItem)) treeCtrl.Expand(hItem, TVE_TOGGLE); } else CTreeView::OnKeyUp(nChar, nRepCnt, nFlags); }//OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) + Override both OnKeyDown and OnKeyUp in my CTreeView object. In the OnKeyDown method, when nChar is VK_RETURN do nothing. The OnKeyUp method is the same as the above. But in all circumstance, it still beeps. What should I do to eliminate the beeps? Thanks in advance. -- Truly yours, Yong Xu, wsy@pub.zjpta.net.cn, Hangzhou, P.R.C
Mike Blaszczak -- mikeblas@nwlink.com Sunday, December 29, 1996 At 15:20 12/23/96 +0800, Yong Xu wrote: >Hi all friends, Merry Christmas! Happy Kwanzaa. >I am using a CTreeView object in my application. I'd like my CTreeView's >parent items can be toggled (expanded or collapsed) by pressing [Enter]. >The problem is whenever I press [enter] the items do toggled but with >a beep! I want to get rid of the annoying beep but without success. You don't mention what environment you're using; you must've given Dave a kickback. I'll assume that you're using MFC 4.2b under Windows 95, just like you assumed everyone celebrated Christmas. You _do_ say that you try handling OnKeyDown() and OnKeyUp(). You shouldn't handle those messages. Since MFC's message map calls TranslateMessage(), your application will get WM_CHAR messages as the user types. If you wanted to put this code into your app in reaction to WM_KEYDOWN or WM_KEYUP, you should do it in a PreTranslateMessage() override so that you can prevent MFC from calling TranslateMessage(). It's the call to TranslateMessage() against a WM_KEYDOWN posted to the message queue that ends up causing a WM_CHAR to be sent. When the control gets your WM_CHAR with nChar == VK_RETURN, it beeps. Books like Petzold and Richter explain all this in some detail. It would be easiest to just correctly handle the WM_CHAR message, I think. You could get started with something like this: void CFix2View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { CTreeCtrl& treeCtrl = GetTreeCtrl(); HTREEITEM hItem; if (nChar == VK_RETURN) { // only toggle if we have an odd number of repeats if (nRepCnt & 1) { hItem = treeCtrl.GetSelectedItem(); if (hItem && treeCtrl.ItemHasChildren(hItem)) treeCtrl.Expand(hItem, TVE_TOGGLE); } } else CTreeView::OnChar(nChar, nRepCnt, nFlags); } I can't remember if testing nRepCnt is necessary. nRepCnt is only rarely not equal to one in Win32, and I can't remember the exception. Maybe there's something on the MSDN Library CD about it, or in the KB. >Thanks in advance. Please buy me some asprin. Thanks in advance. .B ekiM http://www.nwlink.com/~mikeblas/ I'm afraid I've become some sort of speed freak. These words are my own. I do not speak on behalf of Microsoft.
Dong Chen -- d_chen@ix.netcom.com Thursday, January 02, 1997 Try this: function declared as: void OnKeyDown(NMHDR *pNotifyStruct,LRESULT *result); put this line in your message map: ON_NOTIFY_REFLECT(TVN_KEYDOWN,OnKeyDown) this is the handler: void CMyTreeView::OnKeyDown(NMHDR *pNotifyStruct,LRESULT *result) { TV_KEYDOWN* pKeyDown = (TV_KEYDOWN *) pNotifyStruct; if (pKeyDown->wVKey == VK_RETURN) { m_ItemSel.Expand(); } *result = 0; } -- Dong d_chen@ix.netcom.com
| Вернуться в корень Архива |