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:
#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<shlobj.h>
#include <stdio.h>
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;
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);
RegisterClassEx(&WndClsEx);
hWnd = CreateWindow("GlowdotWin32TutorialPartI",
"Glowdot Win32 Tutorial - Part I",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
button = CreateWindow(
"BUTTON",
"Open",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
800,
50,
50,
20,
hWnd,
NULL,
hInstance,
NULL
);
CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 580, 20, hWnd, NULL, NULL, NULL);
BROWSEINFO bi;
char pszBuffer[MAX_PATH];
LPITEMIDLIST pidl;
LPMALLOC lpMalloc;
if(CoInitializeEx(0,COINIT_APARTMENTTHREADED) != S_OK)
{
MessageBox(NULL,("Error opening browse window"),("ERROR"),MB_OK);
CoUninitialize();
return 0;
}
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))
{
if(SHGetPathFromIDList(pidl,pszBuffer))
{
printf(("You selected the directory: %s\n"),pszBuffer);
}
lpMalloc->Free(pidl);
}
lpMalloc->Release();
CoUninitialize();
if ( !button)
return 0;
if( !hWnd ) return 0;
ShowWindow(hWnd, SW_SHOWNORMAL);
ShowWindow(button, SW_SHOWNORMAL);
UpdateWindow(hWnd);
UpdateWindow(button);
while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
{
if (hRet == -1)
{
}
else
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
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..