open a file browser from a win32 dialog

This is a discussion on open a file browser from a win32 dialog within the Windows Programming forums, part of the Platform Specific Boards category; I am working in win32 with c++ and using a dialog as my main window. I am trying to open ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    open a file browser from a win32 dialog

    I am working in win32 with c++ and using a dialog as my main window. I am trying to open a file browser to select a folder, but nothing happens when i tell it to pop up. Also how do i make it only show files, because i juse want the user to select a directory, not a file. Heres what i have. Thanks
    Code:
    	static OPENFILENAME openFileName;
    	openFileName.lStructSize = sizeof(OPENFILENAME);
    	openFileName.hwndOwner = hWnd;
    	openFileName.hInstance = NULL;
    	openFileName.lpstrFilter = "text (*.txt)\0*.txt\0)";
    	openFileName.lpstrCustomFilter = NULL;
    	openFileName.nMaxCustFilter = 0;
    	openFileName.lpstrFile = NULL;
    	openFileName.nMaxFile = MAX_PATH;
    	openFileName.lpstrFileTitle = NULL;
    	openFileName.nMaxFileTitle = MAX_PATH;
    	openFileName.lpstrInitialDir = NULL;
    	openFileName.lpstrTitle = NULL;
    	openFileName.Flags = 0;
    	openFileName.nFileOffset = 0;
    	openFileName.nFileExtension = 0;
    	openFileName.lpstrDefExt = "txt";
    	openFileName.lCustData = 0L;
    	openFileName.lpfnHook = NULL;
    	openFileName.lpTemplateName = NULL;
    
    		case BTN_BROWSE:
    			openFileName.hwndOwner = hWnd;
    			char directory[500];
    			openFileName.lpstrFile = directory;
    			openFileName.lpstrFileTitle = "test";
    			openFileName.Flags = OFN_CREATEPROMPT;
    			GetOpenFileName(&openFileName);

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Welcome to the boards!

    Try including this line of code to your program:
    Code:
    InitCommonControls();
    I believe the GetOpenFileName function uses common controls stuff....but I may be mistaken.

    Also check out
    http://msdn.microsoft.com/library/de...ialogboxes.asp
    There's information there for opening the Open dialog box.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    "text (*.txt)\0*.txt\0)";

    try removing the last brace here and possibly adding another terminator (thought the string should be null terminated.)
    ie
    "text (*.txt)\0*.txt\0\0";

    MSDN
    lpstrFilter
    "Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters. "
    Last edited by novacain; 11-09-2004 at 11:20 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This code should do what you are asking for:

    Code:
    #define _WIN32_DCOM
    
    #include <windows.h>
    #include <shlobj.h>
    #include <tchar.h>
    #include <stdio.h>
    
    int main()
    {
    	BROWSEINFO      bi;
    	char            pszBuffer[MAX_PATH]; 
    	LPITEMIDLIST    pidl; 
    	LPMALLOC		lpMalloc;
    
    	// Initialize COM
    	if(CoInitializeEx(0,COINIT_APARTMENTTHREADED) != S_OK)
    	{
    		MessageBox(NULL,_T("Error opening browse window"),_T("ERROR"),MB_OK);
    		CoUninitialize();
    		return 0;
    	}
    
    	// Get a pointer to the shell memory allocator
    	if(SHGetMalloc(&lpMalloc) != S_OK)
    	{
    		MessageBox(NULL,_T("Error opening browse window"),_T("ERROR"),MB_OK);
    		CoUninitialize();
    		return 0;
    	}
    
    	bi.hwndOwner        =   NULL; 
    	bi.pidlRoot         =   NULL;
    	bi.pszDisplayName   =   pszBuffer; 
    	bi.lpszTitle        =   _T("Select a install Directory"); 
    	bi.ulFlags          =   BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS; 
    	bi.lpfn             =   NULL; 
    	bi.lParam           =   0;
    	
    	if(pidl = SHBrowseForFolder(&bi))
    	{
    		// Copy the path directory to the buffer
    		if(SHGetPathFromIDList(pidl,pszBuffer))
    		{
    			// pszBuffer now holds the directory path
    			printf(_T("You selected the directory: %s\n"),pszBuffer);
    		}
    
    		lpMalloc->Free(pidl);
    	}
    	lpMalloc->Release();
    	CoUninitialize();
    
    	return 0;
    }

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21