Click here to Skip to main content
13,259,771 members (52,427 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 22 Apr 2004

Delete Sub Menu

, 27 Apr 2004
Rate this:
Please Sign up or sign in to vote.
How to add delete submenu to the file menu of Document/View program in VC++.


Those who have worked on Paint Shop Pro 7.0 must have seen the delete submenu in the file menu of the menubar, and wondering how to implement the same, in their Document/View program to delete the current document from the hard-disk and sent it to the Recycle Bin programmatically from the program itself. I don't know any correct or standard method to do the same, but I have implemented a method, that I will be presenting here.


This tutorial assumes that you are familiar with Visual C++/MFC, Document/View architecture, Windows Registry and other technologies used in this tutorial. Though there is no need to understand their underpinnings in great details, you should at least have a working knowledge of what these underpinnings mean.


This implementation is not very hard, Windows provides a standard method to implement the deletion of any document from the memory to the Recycle Bin. Only, we have to call the command SHFileOperation with proper parameters. First of all, we have to write the menu handlers for the delete submenu in the Document class of the program and implement it as shown below. In this implementation, we first find out the actual path of the document in memory and fill up the SHFILEOPSTRUCT structure properly, calling the SHFileOperation command with the structure as parameter. This will delete the current document from the memory. Remember also to close the document. Then we have written the deleted document file path to the registry. Don't forgot to change the registry string to a proper path according to your program.

void CDemoDoc::OnFileDelete() 
    // OnFileDelete() Contributed by: 
    // <Priyank Bolia>
    // TODO: Add your command handler code here
    CString FileToDelete = GetPathName();
    if(SHFileOperation(&sh)==0 && 
        HKEY hchildkey;
        DWORD dwdispositon;
            0,REG_SZ,(const BYTE*)(LPCTSTR)FileToDelete,

void CDemoDoc::OnUpdateFileDelete(CCmdUI* pCmdUI) 
    // OnUpdateFileDelete(CCmdUI* pCmdUI) Contributed by: 
    // <Priyank Bolia>
    // TODO: Add your command update UI handler code here
    CString PathName = GetPathName();

The document has been deleted and closed, but still its path resides in MRU (Most Recently Used) list of the program. To remove it from there also, just write another Update menu handler for the MRU list in the application class and implement it as shown below. Here we read the deleted file path from the registry and matches it in the m_pRecentFileList array, if we found it there, then remove it from there. Don't forget here also to change the registry path to a proper one according to your program.

void CDemoApp::OnUpdateFileMru(CCmdUI* pCmdUI) 
    // <Priyank Bolia> 
    // TODO: Add your command update UI handler code here
    if (m_pRecentFileList == NULL) // no MRU files
        HKEY hresult;
            BYTE value[255];
            DWORD dwcount=255;
                CString str=(LPCTSTR)value;
                CString str2;
                for(int i=0;i<m_pRecentFileList->GetSize();i++)


This brings us to an end, we have learned how easy it is to give our software a professional touch. Now, we can all add delete submenu to our programs, within a matter of few seconds, without bothering about excessive burden of programming. The only thing to do, is to copy the code to your program, "Ready to eat meal". Your valuable suggestions are always welcome, to make this article better.


This code is provided "as is" with no expressed or implied warranty. You can use and abuse this code without any restriction. If you use this source code in any commercial product, acknowledgment is not required but would be appreciated.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Priyank Bolia
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Generaltypo Pin
Anonymous28-Apr-04 1:54
sussAnonymous28-Apr-04 1:54 
GeneralRe: typo Pin
Priyank Bolia28-Apr-04 18:39
memberPriyank Bolia28-Apr-04 18:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 28 Apr 2004
Article Copyright 2004 by Priyank Bolia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid