1. Browsing dialog box, browsing path
// Browse File Dialog Box
Cstring sfolder;
Lpmalloc pmalloc;
// Gets the shell's default Allocator
If (: shgetmalloc (& pmalloc) = noerror)
{
Browseinfo Bi;
Char pszbuffer [max_path];
Lpitemidlist pidl;
Bi. hwndowner = getdomaintopwindow ()-> m_hwnd;
Bi. pidlroot = NULL;
Bi. pszdisplayname = pszbuffer;
Bi. lpsztitle = _ T ("select a directory ...");
Bi. ulflags = bif_returnfsancestors | bif_returnonlyfsdirs;
Bi. lpfn = NULL;
Bi. lparam = 0;
// This next call issues the dialog box.
If (pidl =: shbrowseforfolder (& BI ))! = NULL)
{
If (: shgetpathfromidlist (pidl, pszbuffer ))
{
// At this point pszbuffer contains the selected path
Sfolder = pszbuffer;
}
// Free the pidl allocated by shbrowseforfolder.
Pmalloc-> free (pidl );
}
// Release the shell's allocator.
Pmalloc-> release ();
}
2. General dialog box to browse files
// Method 1: Use the General dialog box to open the specified file (multiple file path names can be retained at the same time)
/*
Cfiledialog dlgfile (true, "select icon", null, ofn_hidereadonly | ofn_overwriteprompt,
"ICO file (*. ICO) | *. ICO | icon file (*. icon) | *. icon | all files (*. *) | *. * | ", null );
Cstring pathname;
Const int c_cmaxfiles = 100;
Const int c_cbbuffsize = (c_cmaxfiles * (max_path + 1) + 1;
Dlgfile. getofn (). lpstrfile = pathname. getbuffer (c_cbbuffsize );
Dlgfile. getofn (). nmaxfile = c_cmaxfiles;
Dlgfile. domodal ();
Pathname. releasebuffer ();
*/
// Method 2: open a specified file in the general dialog box
Cstring pathname (_ T (""); // full file path name
Cstring filename (_ T (""); // file name
// Set filtering Conditions
Tchar szfilters [] = _ T ("ICO file (*. ICO) | *. ICO | icon file (*. icon) | *. icon | all files (*. *) | *. * | ");
// Create an object in the "open file" dialog box. The default extension is "*. ICO" and the title is "select icon ".
Cfiledialog filedlg (true, _ T ("select icon"), _ T ("*. ICO "),
Ofn_filemustexist | ofn_hidereadonly, szfilters );
// Display the dialog box. If you press "OK", save the file path name.
If (filedlg. domodal () = idok)
{
// Obtain the full path name of the file
Pathname = filedlg. getpathname ();
// Open and read files here.
// Change the window title to the file name.
Filename = filedlg. getfiletitle ();
Setwindowtext (filename );
}
// 3. Load the icon
// Load the icon. pathname indicates the full path name of the icon file.
Hicon = NULL;
Hinstance = AfxGetInstanceHandle ();
Hicon = (hicon) LoadImage (hinstance, pathname, image_icon, 0, 0, lr_loadfromfile );
// Display the icon in the picture control idc_picture_ico
(Cstatic *) (getdlgitem (idc_picture_ico)-> seticon (hicon );