Sign in to follow this  
kingpinzs

Need help with getting filename only from OPENFILENAME

Recommended Posts

in notepad when a user saves there text file it puts the title in the caption bar at the top. Iam trying to do the same thing. But I can not get just the file name I am getting the fule path.
BOOL SaveAsFile(HWND hwnd, LPSTR pszFileName)
{
     OPENFILENAME ofn;
  
   ZeroMemory(&ofn, sizeof(ofn));
   pszFileName[0] = 0;
   string buffer2;

   ofn.lStructSize = sizeof(ofn);
   ofn.hwndOwner = hwnd;
   ofn.lpstrFilter = "Map Files (*.lvl)\0*.lvl\0All Files (*.*)\0*.*\0\0";
   ofn.lpstrFile = pszFileName;
   ofn.nMaxFile = MAX_PATH;
   ofn.lpstrDefExt = "lvl";
  
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR;
    ofn.lpstrInitialDir = "Level"; 
    
      if(GetSaveFileName(&ofn))
         {
           SaveLvlFile(pszFileName);
          //gives me full path not just file name

          SetWindowText(hwnd,pszFileName);
         }
   return TRUE;
}
[\source]


Share this post


Link to post
Share on other sites
Couldn't you just look for the last backslash in the full path, and take everything after it? Something like:


char* fileName = pszFileName + strlen(pszFileName);
assert(*fileName == 0);

while(; fileName != pszFileName && *fileName != '\\'; --fileName);

SetWindowText(hwnd, fileName);

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.
Are you serious? What about instead of:
SetWindowText(hwnd,pszFileName);
You use:
SetWindowText(hwnd,ofn.lpstrFileTitle);

Edit: actually, I think you've got to supply your own buffer for lpstrFileTitle, but it's no different to how you have to supply your own buffer for lpstrFile.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Quote:
Original post by kingpinzs
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.
Are you serious? What about instead of:
SetWindowText(hwnd,pszFileName);
You use:
SetWindowText(hwnd,ofn.lpstrFileTitle);

Edit: actually, I think you've got to supply your own buffer for lpstrFileTitle, but it's no different to how you have to supply your own buffer for lpstrFile.


Yeah, you have to supply your own buffer, so something like this.

char fileTitle[MAX_PATH];


//later in the file

ofn.lpstrFileTitle = fileTitle;

//later in file

SetWindowText(hWnd,fileTitle);


although, you could use ofn.lpstrFileTitle after the buffer has been set, but that's just ugly and wrong

Share this post


Link to post
Share on other sites
Thanks for all the help

setting the

char fileTitle[MAX_PATH];


//later in the file

ofn.lpstrFileTitle = fileTitle;
ofn.nMaxFileTitle = 255;
//later in file

SetWindowText(hWnd,fileTitle);

the issue was that I was trying to use a verible that being used to store the name and path already.
Just had to make a new verible. I know it was a dume error but with every ones help I got it to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this