Q: How to use 'GetItemData()' and 'SetItemData()'?
A: A tree control is just a visual representation of some hierarchical data structure. You use 'SetItemData()' and 'GetItemData()' to link each tree item to a node if this data structure.
Firstly you have to define a class or a structure that holds the data for each node. For example if your tree is supposed to show a file system, the structure you define will reflect all the properties of a file, like name, size, timestamp, whether it is a directory or not, access rights and so on. The tree will display only the name. Each of the trees items will be linked to an instance of such a structure.
'SetItemData()' allows you to attach a 'DWORD' to each item and 'GetItemData()' allows you to get that 'DWORD' back. A plain 'DWORD' isn't of much use, but luckily under Windows a 'DWORD' and a pointer have the same size, so you can cast forth and back between them.
When you add an item to the tree, you attach a pointer to a 'node_data' structure to that item:
When you handle an operation on some item of the tree (for example selection) you retrieve the node using 'GetItemData()':
Note: Most of the material in this article is taken from codeguru.com
Q: How to disable an item?
A: Tree items do not have enabled/disabled states. That means that you can stop searching for a function called 'SetItemEnabled()', because it does not exist. You have to simulate this by your own. Let's outline what you need to do in order to 'disable' an item:
Note: Most of the material in this article is taken from codeguru.com
Q: How to correctly delete items from a CListCtrl?
A: Unlike the 'HTREEITEM' values returned from 'CTreeCtrl::InsertItem()', the integer values returned from 'CListCtrl::InsertItem()' aren't stable. In short, this means that you shouldn't store them anywhere. If you need to find an entry again, it's probably best to simply put something useful in 'LVITEM::lParam', and search for it using 'CListCtrl::GetNextItem()'.
Having said that, one can use the following piece of code to correctly delete items from a CListCtrl:
Note: Most of the material in this article is taken from codeguru.com
Q: How to start your dialog application in hidden mode?
A: If you put the function 'ShowWindow(SW_HIDE)' in your 'OnInitDialog()', it won't have any effect, because 'OnInitDialog()' always finishes with calling 'ShowWindow(SW_SHOW)'. But there is a workaround for that. Create a 'BOOL' member variable into your dialog class and set it to 'FALSE' in the constructor.
Now override the 'WM_WINDOWPOSCHANGING' message handler. Your code should look something like this to hide the dialog:
To make the dialog again visible, use the following code:
Note: Most of the material in this article is taken from codeguru.com
Q: How to enable/disable the 'Close' button of your dialog at run-time?
A:
Note: Most of the material in this article is taken from codeguru.com
Q: How to add a minimize/maximize button into your dialog?
A:
Q: How to modify the default 'Open' dialog?
A:
Note: Most of the material in this article is taken from codeguru.com
Q: How to process command line arguments in a MFC application?
A:
Note: Most of the material in this article is taken from codeguru.com
Q: How to change frame and caption window styles at run-time?
I want to remove at run-time the window caption (title bar). I called GetWindowLong to get the old style, removed the WS_CAPTION flag, then called SetWindowLong to set the new style. But this seems to have no effect. What must be done?
A: After 'SetWindowLong()' to set the new style you must call 'SetWindowPos()' with 'SWP_DRAWFRAME' or 'SWP_FRAMECHANGED' flag set.
When using MFC, an easier way to modify window styles is to call 'CWnd::ModifyStyle()' or 'CWnd::ModifyStyleEx()' respectively.
Unfortunately, the first time programmers use these functions, most of them ignore the last parameter ('nFlags'). The solution is to pass 'SWP_DRAWFRAME' or 'SWP_FRAMECHANGED'.
If 'nFlags' is not zero, 'CWnd::ModifyStyle()'/'CWnd::ModifyStyleEx()' combine it with 'SWP_NOZORDER', 'SWP_NOMOVE', 'SWP_NOSIZE', and 'SWP_NOACTIVATE' flags, then call 'SetWindowPos()'.
The example below, shows/hides the control-menu box from title bar (adds/removes 'WS_SYSMENU' style).
Note: Most of the material in this article is taken from codeguru.com
Q: How to prevent a resizable window to be smaller than...?
A: Handle 'WM_GETMINMAXINFO' message:
Q: My resizable window is a dialog and I do not see 'WM_GETMINMAXINFO' in the messages list of ClassWizard. What can I do?
A: Because 'WM_GETMINMAXINFO' is by default filtered by class wizard for a 'CDialog' derived class, you can add the 'ON_WM_GETMINMAXINFO()' macro and the corresponding handler function by hand, or:
Q: My application is SDI/MDI. The required minimum size is given by the view. I handled 'WM_GETMINMAXINFO' in my CView-derived class but does not work. What I'm doing wrong?
A: A view is not responsible for sizing. Handle 'WM_GETMINMAXINFO' in the corresponing frame window:
Q: My application is SDI/MDI. I don't want to be resizable at all.
A: Set also the maximum track size:
An alternative solution is to simply remove 'WS_THICKFRAME' style in 'PreCreateWindow()':
Note: Most of the material in this article is taken from codeguru.com