Sunday, August 24, 2008

File List Control

Like the standard 'File' dialog that lists files of the specified types we wish to create a control that would list files of the specified type present in the specified directory. As against the standard 'File' dialog this control can be embedded into a dialog or a form. Steps Involved: We have used the 'MFC ActiveX Control Wizard' to develop the directory control. The steps involved are enumerated below:

(a) Select 'MFC ActiveX Control Wizard' as new project type with project name as 'filelist'. Click on 'OK'.

(b) In Step 1 of 2 select '1' for 'How many controls would you like your project to have?'. Click on 'Next'.

(c) In Step 2 of 2 select 'SysListView32' for 'Which window class, if any, should this control subclass?'. Click on 'Finish' to let the wizard generate the code.

Click on the 'Class View' tab and in the PreCreateWindow( ) virtual function you can see the line cs.lpszClass = _T("SysListView32"); This line indicates that SysListView32 window class has been made as a subclass of the listexplorer control. Hence the messages would be received by the control class before reaching the SysListView32 window class. To change the style of the tree window we have added:

cs.style |= WS_CHILD | WS_BORDER | LVS_SMALLICON | LVS_SHOWSELALWAYS ;

Next we have added an OnCreate( ) handler to the CFilelistCtrl class. From the OnCreate( ) handler we have called addtolist( ) function. This function finds the files depending on the type of the specified directory. The default type is '*.*' and the default specified directory is 'c:\'. The default type and the directory are represented by two member variables of the CFilelistCtrl class. The addtolist( ) function uses a pointer to CListCtrl class's object to add filenames to the list control. The pointer variable has been defined as a public data member of the CListexplorerCtrl class. The pointer has been initailised with the handle of the list control in the OnCreate( ) handler. On selecting items (filenames) from the list control we have fired an event consisting the fullpath with filename. This is done in onitemchanged( ) function which gets called on selecting any item. The event has been fired by using the function FireGetPathFromList( ) function. We have also added a custom property page which consists of two edit boxes one for the type of file to be listed and the second for the directory path. This requires two property variables 'path' and 'format' to be added. Using these two variables we can access values from the custom property page in DoPropExchange( ) function. As we add two property variables (path and format) two functions(OnPathChanged( ) and OnFormatChanged( ) respectively) are added to the CFilelistCtrl class. These functions get called as we click on 'Apply' button of the property dialog box. From these functions we have called addtolist( ) function to reflect the changes in the list control. If we run this program the control will register itself with the Windows registry on that machine. A dialog box would be displayed asking for the exe file to be used as a container for this control. We can 'Microsoft's ActiveX Test Container' to display the control. Here we have to insert our control from the 'Insert control' box. The container contains of a splitter window. The upper window displays the control and the lower window displays the events generated by the control.

No comments:

Your Title