How can show right click properties in user-designed dialog?

If we right click on a file or a directory a pop up menu is displayed from which we can select the ‘Properties’ menu item to display the properties of the selected file/directory. How can we achieve the same in a user-designed dialog?

We will write a small program in which if user clicks a button it displays the Properties dialog box for a specified file.

Create a dialog based application and add two buttons ‘Browse’ and ‘Show’ having IDs IDC_BROWSE and IDC_SHOW. Also add an edit box having ID IDC_FILE to the dialog template. Clicking the Browse button will show the standard ‘Open’ dialog box from which user can select a file. After selecting the file, its name will be displayed in the edit box. Thereafter, clicking the ‘Show’ button will display a dialog box showing the properties of the file. Add a value variable m_file of type CString for the edit control. Also add a message handler OnBrowse( ) for the Browse button. Write following code in it.

void CPropdialogDlg::OnBrowse( )

{

CFileDialog fd ( TRUE, NULL, NULL, NULL, “All Files( *.* )|*.*||” ) ;

fd.m_ofn.lpstrTitle = “Browse” ;

if ( fd.DoModal( ) == IDOK )

m_file = fd.GetPathName( ) ;

UpdateData ( FALSE ) ;

}

Here, we have displayed the ‘Open’ dialog box and stored the path of selected file in the variable m_file. We have displayed the filename in the edit box by calling UpdateData ( FALSE ) function which performs a data exchange between the edit control and the associated value variable.

Now add a handler for the ‘Show’ button. Write the following code in the handler.

void CPropdialogDlg::OnShow( )

{

if ( m_file.IsEmpty( ) )

{

MessageBox ( “Please select a file” ) ;

return ;

}

SHELLEXECUTEINFO sh = { 0 } ;

sh.cbSize = sizeof ( sh ) ;

sh.fMask = SEE_MASK_INVOKEIDLIST ;

sh.lpVerb = “properties” ;

sh.lpFile = m_file ;

ShellExecuteEx ( &sh ) ;

}

Here, firstly, it is ascertained that m_file is not empty. Next, the SHELLEXECUTEINFO structure is filled and its address is passed to ShellExecuteEx( ). The cbSize element of the structure is initialised with the size of the structure. If fMask member is SEE_MASK_INVOKEIDLIST it uses the item identifier list given by the lpIDList member to invoke an application. If lpIDList is NULL the function creates an item identifier list and invokes the application. Since we have initialized lpVerb element with ‘properties’ it displays the Properties dialog box of the file specified in lpFile element. Had we set ‘open’ in lpVerb the file would have been opened.

How to create a list box with check boxes?

You must have seen a list box with check boxes while installing any professional software. For example, you can take a look at the dialog box that opens when you choose ‘Custom Installation’ while installing Visual C++. We too can implement such a list box in our program using CCheckListBox MFC class.

To create our own checklist box, we must derive our own class from CCheckListBox and call Create( ) using object of the derived class. Let use understand this with the help of a program.

Create a dialog based application checklist. Add a button ‘Show’ to the dialog template. Insert a new class by selecting ‘Insert | New Class’. Select the Class type as ‘Generic Class’. Give the class name as mychecklist. In the ‘Base class’ list control give the base class name as CCheckListBox. Click OK button. The two files ‘mychecklist.cpp’ and ‘mychecklist.h’ would get added to the project. Create an object m_checklist of type mychecklist class in CChecklistDlg class. #include ‘mychecklist.h’ file in ‘checklistDlg.h’ file. Add the following code in CChecklistDlg::OnInitDialog( ) function.

BOOL CChecklistDlg::OnInitDialog( )

{

m_checklist.Create ( WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_HASSTRINGS | LBS_OWNERDRAWFIXED,

CRect ( 50, 50, 200, 200 ), this, 1 ) ;

m_checklist.AddString ( “Basic” ) ;

m_checklist.AddString ( “COBOL” ) ;

m_checklist.AddString ( “C” ) ;

m_checklist.AddString ( “C++” ) ;

m_checklist.AddString ( “VC++” ) ;

m_checklist.AddString ( “Java” ) ;

}

Add a message handler OnShow( ) for the ‘Show’ button using ClassWizard. Add the following code to this function:

void CChecklistDlg::OnShow( )

{

int n = m_checklist.GetCount( ) ;

CString str, resultstr ;

resultstr = “You know \n” ;

for ( int i = 0 ; i < n ; i++ )

{

if ( m_checklist.GetCheck ( i ) )

{

m_checklist.GetText ( i, str ) ;

resultstr += str + ” ” ;

}

}

resultstr += “\n\nYou don’t know\n” ;

for ( i = 0 ; i < n ; i++ )

{

if ( !m_checklist.GetCheck ( i ) )

{

m_checklist.GetText ( i, str ) ;

resultstr += str + ” ” ;

}

}

MessageBox ( resultstr ) ;

}

In this function we have retrieved the selected and unselected strings and displayed them in a message box. CListBox::GetCount( ) function returns the number of items in the list box. Using CListBox::GetCheck( ) function we have obtained the status of the item and accordingly added it to the resultstr variable. After running the program you will see the output as given below.

a

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

UserOnline

Improve the web with Nofollow Reciprocity.

Powered by Yahoo! Answers