Examples of viewing dialog box and general dialog box in VC and how to dynamically load ICO files to the picture control

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.