Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to give a File New command from inside a program

How to give a File New command from inside a program

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IlanTal
    wrote on last edited by
    #1

    I would like to give a command from inside my program to do a File New command. I tried pApp->CWinApp::OnFileNew() but it tells me I can't access a protected member. Fair enough, but is there some system command I can give instead? Thanks, Ilan

    realJSOPR 1 Reply Last reply
    0
    • I IlanTal

      I would like to give a command from inside my program to do a File New command. I tried pApp->CWinApp::OnFileNew() but it tells me I can't access a protected member. Fair enough, but is there some system command I can give instead? Thanks, Ilan

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Try this:

      ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), ID_FILE_OPEN, 0, 0);

      or even

      AfxGetMainWnd()-> PostMessage(ID_FILE_OPEN, 0, 0);

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      I P 2 Replies Last reply
      0
      • realJSOPR realJSOP

        Try this:

        ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), ID_FILE_OPEN, 0, 0);

        or even

        AfxGetMainWnd()-> PostMessage(ID_FILE_OPEN, 0, 0);

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        I Offline
        I Offline
        IlanTal
        wrote on last edited by
        #3

        Up front it looks like a good idea, but I put in break points and it doesn't reach them. So, I'm still stuck on the proper way to do it.

        realJSOPR 1 Reply Last reply
        0
        • I IlanTal

          Up front it looks like a good idea, but I put in break points and it doesn't reach them. So, I'm still stuck on the proper way to do it.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          You're going to have to give more detail if you want help. WHERE did you put the breakpoints?

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          I 1 Reply Last reply
          0
          • realJSOPR realJSOP

            You're going to have to give more detail if you want help. WHERE did you put the breakpoints?

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            I Offline
            I Offline
            IlanTal
            wrote on last edited by
            #5

            Sorry, I didn't realize you continued to look at my posts. I have break points at both OnNewDocument and OnOpenDocument BOOL CCardiacDoc::OnNewDocument() { char buff[100]; if( m_dataDirectory.GetLength() == 0 && _getcwd(buff, 98)) { m_dataDirectory = buff; m_dataDirectory += "\\*"; } if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } BOOL CCardiacDoc::OnOpenDocument(LPCTSTR lpszPathName) { m_dataDirectory = lpszPathName; int j = m_dataDirectory.ReverseFind('\\'); if( j > 0) { m_dataDirectory = m_dataDirectory.Left(j+1) + "*"; } if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; // TODO: Add your specialized creation code here return TRUE; } I see that when I start the program it goes to OnNewDocument. Likewise, before I override the OnOpen, it goes to OnOpenDocument. The function I use is: void CCardiacDoc::OnFileOpen() { CString path1; int flags; CFileDialog fdlg( true, NULL, "dummy entry"); fdlg.m_ofn.lpstrTitle = "Choose directory"; flags = fdlg.m_ofn.Flags; fdlg.m_ofn.Flags = flags; if( fdlg.DoModal() == IDOK) { path1 = fdlg.m_ofn.lpstrFile; int i = path1.GetLength(); i -= (int) strlen(fdlg.m_ofn.lpstrFileTitle); m_dataDirectory = path1.Left(i) + "*"; AfxGetMainWnd()->PostMessage(ID_FILE_NEW, 0, 0); } } It never again reaches OnNewDocument. If I would use ID_FILE_OPEN (which I tried), I would expect it to reach my overridden routine, which it doesn't. I really want it to go to OnNewDocument which I why I assume you had a small typo. Thanks, Ilan

            realJSOPR 1 Reply Last reply
            0
            • I IlanTal

              Sorry, I didn't realize you continued to look at my posts. I have break points at both OnNewDocument and OnOpenDocument BOOL CCardiacDoc::OnNewDocument() { char buff[100]; if( m_dataDirectory.GetLength() == 0 && _getcwd(buff, 98)) { m_dataDirectory = buff; m_dataDirectory += "\\*"; } if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } BOOL CCardiacDoc::OnOpenDocument(LPCTSTR lpszPathName) { m_dataDirectory = lpszPathName; int j = m_dataDirectory.ReverseFind('\\'); if( j > 0) { m_dataDirectory = m_dataDirectory.Left(j+1) + "*"; } if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; // TODO: Add your specialized creation code here return TRUE; } I see that when I start the program it goes to OnNewDocument. Likewise, before I override the OnOpen, it goes to OnOpenDocument. The function I use is: void CCardiacDoc::OnFileOpen() { CString path1; int flags; CFileDialog fdlg( true, NULL, "dummy entry"); fdlg.m_ofn.lpstrTitle = "Choose directory"; flags = fdlg.m_ofn.Flags; fdlg.m_ofn.Flags = flags; if( fdlg.DoModal() == IDOK) { path1 = fdlg.m_ofn.lpstrFile; int i = path1.GetLength(); i -= (int) strlen(fdlg.m_ofn.lpstrFileTitle); m_dataDirectory = path1.Left(i) + "*"; AfxGetMainWnd()->PostMessage(ID_FILE_NEW, 0, 0); } } It never again reaches OnNewDocument. If I would use ID_FILE_OPEN (which I tried), I would expect it to reach my overridden routine, which it doesn't. I really want it to go to OnNewDocument which I why I assume you had a small typo. Thanks, Ilan

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Well see? That's completely different than what you originally asked. In OnOpenFile(), instead of posting the ID_FILE_OPEN message, try this line instead:

              OpenDocumentFile(path1);

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              I 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Well see? That's completely different than what you originally asked. In OnOpenFile(), instead of posting the ID_FILE_OPEN message, try this line instead:

                OpenDocumentFile(path1);

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                I Offline
                I Offline
                IlanTal
                wrote on last edited by
                #7

                I didn't explain the whole problem. I don't want to open the file the user gives, just to use the directory he gives (because it actually opens a whole bunch of files, not just a single file). The problem is, what happens when the user "opens" the same file twice? (i.e. he points to the same directory and picks the same file). Windows says it is open and doesn't open it again. Again the problem is the directory structure can be complicated and I'll have to ask for more help, after he has chosen the directory. In short, I want to use the New command and then there is no problem with the file already being open. The New command would solve the problem nicely (as it does when the program is started). Thanks, Ilan

                realJSOPR 1 Reply Last reply
                0
                • I IlanTal

                  I didn't explain the whole problem. I don't want to open the file the user gives, just to use the directory he gives (because it actually opens a whole bunch of files, not just a single file). The problem is, what happens when the user "opens" the same file twice? (i.e. he points to the same directory and picks the same file). Windows says it is open and doesn't open it again. Again the problem is the directory structure can be complicated and I'll have to ask for more help, after he has chosen the directory. In short, I want to use the New command and then there is no problem with the file already being open. The New command would solve the problem nicely (as it does when the program is started). Thanks, Ilan

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  If the FileOpen dialog is only used to select a folder, how does the user specify the filename to open? You're still not sufficiently describing your problem. It looks like you need to completely redesign your file handling if you're having these kinds of issues. Since I don't know what you mean by "opens a whole bunch of files", I can't suggest an alternative file-handling mechanism.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  I 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    If the FileOpen dialog is only used to select a folder, how does the user specify the filename to open? You're still not sufficiently describing your problem. It looks like you need to completely redesign your file handling if you're having these kinds of issues. Since I don't know what you mean by "opens a whole bunch of files", I can't suggest an alternative file-handling mechanism.

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    I Offline
                    I Offline
                    IlanTal
                    wrote on last edited by
                    #9

                    In fact the user need not specify a file. There is "dummy entry" as a default. The whole bunch of files can represent different views of a cardiac study, which are all displayed. The trouble is that lately there can be multiple studies in the same directory and those I don't want to display together. I have to make a list and ask the user which one he wants, then I go pick up just the parts of the chosen study. All this is already working. The only part which bothers me is if he goes back to the same directory to pick another study. If he happens to select the same file, then it is already "open". (A bit of a lie here. The "dummy entry" doesn't work in the normal open file, as the file doesn't exist.) I've been looking around and I found another suggestion on how to solve the problem. That suggests to use ID_FILE_NEW up front and inside OnNewDocument distinguish if it is the first time around or not. If not, add my dialog to change the directory. Someone suggested this way back in 1997. Thanks for your suggestions. I still don't know why they don't work. It may have something to do with the fact that I am in the middle of an ID_FILE_OPEN command. Ilan

                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Try this:

                      ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), ID_FILE_OPEN, 0, 0);

                      or even

                      AfxGetMainWnd()-> PostMessage(ID_FILE_OPEN, 0, 0);

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      P Offline
                      P Offline
                      PJ Arends
                      wrote on last edited by
                      #10

                      ID_FILE_OPEN is not a message, it is a command. One could get away with posting a WM_COMMAND message with ID_FILE_OPEN as the menu item.

                      PostMessage(AfxGetMainWnd(), WM_COMMAND, MAKEWPARAM(0, ID_FILE_OPEN), 0);


                      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good, use it!!!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups