در مورد file browser از روش زیر استفاده میکنم :
در ابتدا تابعی رو به صورت زیر تعریف میکنم :
char* openfile(char filter[],char defuext[],char title[])
{
char filename[500]={0};
char filetitle[100];
OPENFILENAME of;
memset(&of,0,sizeof(of));
of.lStructSize=sizeof(OPENFILENAME);
of.hwndOwner=NULL;
of.hInstance=NULL;
of.lpstrFilter=TEXT(filter);
of.lpstrCustomFilter=NULL;
of.nMaxCustFilter=0;
of.nFilterIndex=1;
of.lpstrFile=filename;
of.nMaxFile=1000;
of.nMaxFileTitle=99;
of.lpstrFileTitle=filetitle;
of.lpstrTitle=title;
of.Flags=OFN_NOREADONLYRETURN;
of.lpstrDefExt=defuext;
of.lCustData=NULL;
of.lpfnHook=NULL;
of.lpTemplateName=NULL;
GetOpenFileName(&of);
return(filename);
}
و در جایی مثلا به صورت زیر عمل میکنم :
UpdateData();
strcpy(filename,openfile("Picture files (*.jpg;*.bmp;*.png)\0*.jpg;*.bmp;*.png","jpg;bmp;p ng","Choose pictures..."));
if(filename[0]!='\0'){
filen=filename;
filen.MakeUpper();
CString ext=filen.Right(3);}
UpdateData(FALSE);
که filen یک متغیر CString برای edit box است .
در مورد folder browser هم به این صورت عمل میکنم :
void CFolderBrowserDlg::OnButtonBrowse()
{
// TODO: Add your control notification
// handler code here
LPMALLOC pMalloc = NULL;
LPITEMIDLIST pidl = NULL;
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
// set the bi's default values
bi.hwndOwner = m_hWnd;
bi.lpszTitle = _T("Current folder is:");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = BrowseCallbackProc;
pidl = SHBrowseForFolder(&bi);
if(pidl != NULL)
{
SHGetPathFromIDList(pidl,
m_strFolderPath.GetBuffer(
m_strFolderPath.GetLength()));
UpdateData(FALSE);
// free memory
if(SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc);
pMalloc->Free(pidl);
pMalloc->Release();
}
}
int CALLBACK BrowseCallbackProc(
HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch(uMsg)
{
case BFFM_INITIALIZED:
{
// add your initialization code here
}
break;
case BFFM_SELCHANGED:
{
TCHAR szText[MAX_PATH] = {0};
SHGetPathFromIDList(
reinterpret_cast<LPITEMIDLIST>(lParam), szText);
SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0,
reinterpret_cast<LPARAM>(szText));
}
break;
}
return 0;
}
که در نهایت به این صورت عمل میکنم :
BOOL h=CopyFile(filen,m_strFolderPath,TRUE);
if(h==0)
MessageBox(_T("function fail"));
که با MessageBox مواجه میشم.