Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Everyone..

Am using Core WIN32 Programming. And I want to browse a folder on Button click. here is the Code i used to Open a File Dialog:

C++
#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<shlobj.h>
#include <stdio.h>

     
    // Declare WndProcedure
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam);
     
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
    {
        HWND       button;
        MSG        Msg;
        HWND       hWnd;
        HRESULT    hRet;
        WNDCLASSEX WndClsEx;
     
        // Populate the WNDCLASSEX structure
        WndClsEx.cbSize        = sizeof(WNDCLASSEX);
        WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
        WndClsEx.lpfnWndProc   = WndProcedure;
        WndClsEx.cbClsExtra    = 0;
        WndClsEx.cbWndExtra    = 0;
        WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        WndClsEx.lpszMenuName  = NULL;
        WndClsEx.lpszClassName = "GlowdotWin32TutorialPartI";
        WndClsEx.hInstance     = hInstance;
        WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
     
        // Register the class
        RegisterClassEx(&WndClsEx);
     
        // Create the window object
        hWnd = CreateWindow("GlowdotWin32TutorialPartI",
                  "Glowdot Win32 Tutorial - Part I",
                  WS_OVERLAPPEDWINDOW,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  CW_USEDEFAULT,
                  NULL,
                  NULL,
                  hInstance,
                  NULL);

		

//To Create a  button :
        button = CreateWindow( 
        "BUTTON",                                    
        "Open",                                       
        WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,    
                                                     
        800,                                         
        50,                                          
        50,                                         
        20,                                         
        hWnd,                                       
        NULL,
        hInstance,
        NULL      
    );

//To Create a Text box :
		CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 580, 20, hWnd, NULL, NULL, NULL); 

//To Browse for a folder :
    BROWSEINFO      bi;
    char            pszBuffer[MAX_PATH];
    LPITEMIDLIST    pidl;
    LPMALLOC        lpMalloc;
 
    // Initialize COM
    if(CoInitializeEx(0,COINIT_APARTMENTTHREADED) != S_OK)
    {
        MessageBox(NULL,("Error opening browse window"),("ERROR"),MB_OK);
        CoUninitialize();
        return 0;
    }
 
    // Get a pointer to the shell memory allocator
    if(SHGetMalloc(&lpMalloc) != S_OK)
    {
        MessageBox(NULL,("Error opening browse window"),("ERROR"),MB_OK);
        CoUninitialize();
        return 0;
    }
 
    bi.hwndOwner        =   NULL;
    bi.pidlRoot         =   NULL;
    bi.pszDisplayName   =   pszBuffer;
    bi.lpszTitle        =   ("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(("You selected the directory: %s\n"),pszBuffer);
        }
 
        lpMalloc->Free(pidl);
    }
    lpMalloc->Release();
    CoUninitialize();

if ( !button)
           return 0;
        // Verify window creation
        if( !hWnd ) // If the window was not created,
            return 0; // stop the application
     
        // Show the window
        ShowWindow(hWnd, SW_SHOWNORMAL);
        ShowWindow(button, SW_SHOWNORMAL);
        UpdateWindow(hWnd);
        UpdateWindow(button);
        // our message pump
        while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
        { 
            if (hRet == -1)
            {
            // handle the error and possibly exit
            }
            else
            {
                TranslateMessage(&Msg); 
                DispatchMessage(&Msg); 
            }
        }
    }
     
    //////////////////
    // WndProcedure //
    //////////////////
     
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
                   WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
		
        case WM_DESTROY:

            PostQuitMessage(WM_QUIT);
            break;
        default:

            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
     
        return 0;
    }

Please, Someone suggest me to Open the File Dialog On button Click..

Thanks in Advance..
Posted
Updated 8-Mar-12 20:35pm
v4

You should:

  • Move the browse for folder code to a new function.
  • Pass a unique ID as hMenu parameter to the CreateWindow call for the button.
  • Hanlde the BN_CLICKED notification (high word of wParam) in your message loop (WM_COMMAND message). When the low word of the wParam parameter is your button ID, call the browse for folder function.


[UPDATE: Example using code from the updated question]
C++
// Define button ID
#define IDC_BROWSE_BTN	1000

// Pass button ID
button = CreateWindow(
    "BUTTON", "Open",
    WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
    800, 50, 50, 20,
    hWnd,   
    (HMENU)IDC_BROWSE_BTN,
    hInstance,
    NULL);

BOOL BrowseForFolder()
{
    BOOL bRet = 0;
// Move the browse for folder code from WinMain to here
// Add line 'bRet = 1;' behind the printf statement when
// a directory has been choosen.
    return bRet;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    case WM_COMMAND :
        if (HIWORD(wParam) == BN_CLICKED)
        {
            if (LOWORD(wParam) == IDC_BROWSE_BTN)
                BrowseForFolder();
        }
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
 
Share this answer
 
v2
Comments
Guru_C++ 8-Mar-12 7:21am    
Please, Show me with code..
Jochen Arndt 8-Mar-12 7:32am    
I did not show code, because I did not know what you have done so far. If you update your question with the relevant code parts (your WindowProc function), I and others may give you some code. To update your question, use the 'Improve question' link.
Use SetWindowLong[^] with GWL_ID to give your button a command ID, like 1234.
Add a message handler for WM_COMMAND[^] to the button's parent window or wherever you do message handling, and if LOWORD(wParam) is the ID you gave to your button (e.g. 1234), then display that file dialog of yours.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900