Home > MFC, Windows API > Displaying “Properties window” for any file!

Displaying “Properties window” for any file!

Right click on any file in windows and select properties, you will see a tabbed properties window pop up. Ever wondered how to do this programmatically on any file that you choose…

Here is a demo…

#include <afxwin .h>
#include <afxdlgs .h> // CFileDialog      

int main()
{
    // initialize MFC and print an error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        std::cerr < < _T("Fatal Error: MFC initialization failed") << std::endl;
        return 1;
    }        

    // Our code starts here //      

    CFileDialog cfDlg( TRUE );<a title="Properties window demo" href="https://nibuthomas.files.wordpress.com/2007/11/processviewerprops.jpg">
    cfDlg.DoModal();      

    // Get file name
    CString csFileName = cfDlg.GetPathName();
    if (!csFileName.IsEmpty())
    {
        SHELLEXECUTEINFO shExecInfo = { 0 };
        shExecInfo.cbSize = sizeof(shExecInfo);
        shExecInfo.lpFile = csFileName;
        shExecInfo.lpVerb = _T("properties"); // Context menu item
        shExecInfo.fMask  = SEE_MASK_INVOKEIDLIST; // This is the key, see MSDN       

        // Show properties
        ShellExecuteEx(&shExecInfo); 

        // Wait, else console application will exit
        Sleep( 5000 );
    }// End if
}// End main

The key to this is this flag SEE_MASK_INVOKEIDLIST which means lpVerb is valid and it has a command which is valid. This command will be one of the items we see when we right click on a file, this can be “explore”, “edit”, “print” ;), “open”, “properties”, “find”. I’ve given “properties” hence we see the properties window. Try “print”.

Screenshot for properties…

Properties window demo

Advertisements
  1. James
    March 6, 2008 at 8:45 am

    This is great was looking for this.
    What would be a sample to change to Read-Only property for the selected Folder?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: