Topic : MFC - Views
Author : Unknown
Page : << Previous 7  
Go to page :


Then, we can add a control variable for the tree control. By doing this, the tree control can be accessed by directly referring to this variable instead of calling function CWnd::GetDlgItem(...). In the sample, the newly generated class is named CDirFormView, the ID of the dialog template is IDD_DIALOG_VIEW, the ID of the tree control is IDC_TREECTRL, and the variable added for the tree control is CDirFormView::m_tcDir.

Implementing New Member Functions

We can just copy all the member functions from CTreeView and implement them in CDirFormView. The only difference is that we must replace all function calls of CTreeView::GetTreeCtrl() by m_tcDir. Also, we can implement a GetTreeCtrl() function in class CDirFormView and let it return the reference of m_tcDir. By doing this, we don't need to change anything else.

The following variables and functions are declared in class CDirFormView, they are implemented exactly the same as in class CDirView:

char CDirFormView::m_szPath[_MAX_PATH];

CString CDirFormView GetDir(HTREEITEM);

void CDirFormView AddDirs(HTREEITEM, BOOL);

void CDirFormView::AddChildrenChildren(HTREEITEM);

void CDirFormView::OnInitialUpdate();

afx_msg void CDirFormView::OnClickTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult);

afx_msg void CDirFormView::OnItemExpandingTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult);


Resizing Tree Control

Since the controls contained in the form view does not get resized or repositioned automatically, we need to move or resize them after receiving message WM_SIZE. This will make the form view better balanced.

In the sample, function CDirFormView::ResizeTreeView() is implemented to resize the tree control. After this function is called, the tree control will be resized so that it just fits within the form view window (A border is left around the tree control). The following is the implementation of this function:

(Code omitted)

Mouse Cursor Coordinates

Because the dimension of the tree control is not the same with that of the form view, when function CTreeCtrl::HitTest(...) is called in response to notification NM_CLICK, we need to convert the coordinates of mouse cursor from form view window to the tree control window. The following code fragment shows how function CTreeCtrl::HitTest(...) is called when mouse's left button is pressed:

(Code omitted)

Replacing CDirView with CDirFormView

Now we can replace class CDirView with CDirFormView when creating the splitter window. This is fairly simple, all we need is to use class CDirFormView to create the left pane of the splitter window in function CMainFrame::OnCreateClient(...):

(Code omitted)

This new version of Explorer behaves exactly the same with the previous one. However, with form view, we can add other common controls such as buttons to the left pane. This will give us more flexibility in improving our application.

Summary

1) A standard text editor can be implemented by class CEditView. This class contains some member functions that can be used to implement a lot of useful commands:

i) Serialization can be implemented by function CEditView::SerializeRaw(...).

ii) Undo, Cut, Copy and Paste commands can be implemented by the following undocumented functions: CEditView::OnEditUndo(), CEditView::OnEditCut(), CEditView::OnEditCopy(), CEditView::OnEditPaste().

iii) String search related commands can be implemented by the following undocumented functions: CEditView::OnEditFind, CEditView::OnEditReplace, CEditView::OnEditFindRepeat().

2) Class CRichEditView and CRichEditDoc support formatted text editing. The classes support two file formats: "rtf" format and plain text format. There is a member variable contained in class CRichEditDoc: CRichEditDoc::m_bRTF. If we want to open "rtf" type files, we need to set this variable to TRUE. If we want to edit plain ASCII text, we need to set this variable to FALSE.

3) To get or set the format of a paragraph, we can stuff structure PARAFORMAT and call function CRichEditView::GetParaFormat(...) or CRichEditView::SetParaFormat(...); to get or set the format of characters, we can stuff structure CHARFORMAT and call function CRichEditView::SetCharFormat(...) or CRichEditView::SetCharFormat(...).

4) To customize "File Open" dialog box, we need to override function CWinApp::OnFileOpen(...). To customize "Save As" dialog box, we need to override undocumented function CDocument::DoSave().

5) The following undocumented member functions can be used to implement commands for formatting characters or paragraph in a rich edit view:

CRichEditView::OnParaCenter();

CRichEditView::OnParaRight();

CRichEditView::OnParaLeft();

CRichEditView::OnCharBold();

CRichEditView::OnCharUnderline();

CRichEditView::OnCharItalic();

6) The styles of tree view and list view can be set by calling function ::SetWindowLong(...). Any style that can be used in function CTreeCtrl::Create(...) or CListCtrl::Create(...) can be changed dynamically by using this function.

7) We can call function _chdrive(...) to test if a drive (from drive A to drive Z) exits in the system. If the function returns -1, the drive does not exit. If it returns 0, the drive is available. We can also call this function to change the current working drive.

8) Class CFileFind can be used to enumerate all the files and directories under certain directory. To enumerate files and directories, we can call function CFileFind::FileFind() first then call function CFileFind::FindNextFile() repeatedly until it returns FALSE.

9) Function CFileFind::IsDirectory() can be used to check if an enumerated object is a directory. Function CFileFind::IsDot() can be used to check if a directory is "." or "..".

10) Function ::SHGetFileInfo(...) can be used to obtain the embedded or shell icons for a file.

11) Notification NM_CLICK can be used to trap mouse clicking events on the tree control. Notification TVN_ITEMEXPANDING indicates that a node is about to expand.

12) When the user clicks mouse on the tree control, we can call function CTreeCtrl::HitTest(...) to find out the handle of the item that was clicked.

13) We can call function CListCtrl::SortItems(...) to implement item sorting in the list control. In order to do this, we must assign each item contained in the list control a parameter, which will be used as the identification of the item. Then we need to prepare a callback function, within which rules of comparison are implemented.

14) To search for a specific item by its parameter, we can stuff structure LV_FINDINFO and call function CListCtrl::FIndItem(...).

15) To respond to the mouse clicking events on the columns of the list control, we need to trap notification LVN_COLUMNCLICK.


Page : << Previous 7