MFC remembers some of the problems that the last path---encountered

Source: Internet
Author: User

Today to complete a requirement, is to remember the user's choice of file path, first familiar with the code, and then find a solution on the internet, a feeling of nothing at first, the online method is similar, it should be easy to make, the result is really card for a half a day, to the evening himself slowly to figure out.

Have a lot of problems, record a lot of details, really do not write do not know.

1. Basic methods

http://blog.csdn.net/shuilan0066/article/details/7302904

Http://www.cnblogs.com/Hisin/archive/2012/02/27/2370614.html

These two articles are quite clear.

I met you first.

SHBrowseForFolder opens the folder every time it is opened from the root directory. To remember the last path or to set the default path, you need to write a back-off function.

CFileDialog will automatically remember the last path

intCALLBACK Browsecallbackfun (HWND hwnd, UINT umsg, LPARAM LPARAM, LPARAM lpdata) {Switch(umsg) { CaseBffm_initialized://Select Folder dialog box to initialize//set the default path to lpdata that is ' d:\ ':: SendMessage (hwnd, Bffm_setselection, TRUE, lpdata); //display the current path in the StatusText area:: SendMessage (hwnd, Bffm_setstatustext,0, lpdata); //set the title of the Select Folder dialog box:: SetWindowText (hwnd, TEXT ("Please set up a working directory first"));  Break;  CaseBffm_selchanged://When you select a folder change{TCHAR Pszpath[max_path]; //get the current selection pathSHGetPathFromIDList ((lpcitemidlist) LParam, Pszpath); //display the current path in the StatusText area:: SendMessage (hwnd, Bffm_setstatustext, TRUE, (LPARAM) pszpath); }           Break; }      return 0; }    

The return function first clarify the meaning of the parameter

2.SHBrowseForFolder function

Open the File directory dialog, I found the method is to use the SHBrowseForFolder function, the prototype of this function is Lpitemidlist SHBrowseForFolder (Lpbrowseinfo lpbi). The function is simple, just a return value and a parameter. The parameters are simply listed as follows

typedef struct _BROWSEINFO {

HWND hWndOwner; parent Window Handle

Lpcitemidlist Pidlroot; Root of the File Directory dialog box to be displayed (root)

LPTSTR pszDisplayName; Buffer to save the selected folder path

LPCTSTR Lpsztitle; Displays the title at the top left of the dialog box

UINT ulflags; Flags that specify the appearance and functionality of the dialog box

Bffcallback LPFN; callback function for handling events

LPARAM LPARAM; Parameters that the application passes to the callback function

int iImage; Picture Index of the folder dialog box

} browseinfo, *pbrowseinfo, *lpbrowseinfo

In general, the parent window handle (hWndOwner) and the root (pidlroot) are set to NULL, pszDisplayName sets a buffer of MAX_PATH size, and the parameters associated with the display are the dialog prompt caption (lpsztitle), dialog box Style (ulflags), set the default path for the dialog box (LPFN and lparam), and the icon (IImage) that appears on the dialog box taskbar.

Because the return value lpitemidlist is a pointer to Itemidlist, this itemidlist relates to the knowledge points in Windows shell about managing objects such as files, computers on the network, Control Panel programs, recycle bins, and so on, windows In order to identify each object, the shell uses Itemid to uniquely identify and differentiate, while Itemidlist is a complete object path. Obviously this function can be used to browse non-file objects, such as the computer in the LAN, and so on, where the Lpitemidlist return object path is a folder path, Windows provides a function bool SHGetPathFromIDList ( Lpcitemidlist pidl, LPSTR Pszpath) to convert from an object path to a folder path.

Figure out the meaning of each parameter before setting the initial value as required. First too blind, only know the set of others, there is no understanding.

Lpcitemidlist Pidl =NULL;                Browseinfo bi; Bi.hwndowner= AfxGetMainWnd ()GetSafeHwnd (); Bi.pidlroot=NULL; Bi.pszdisplayname=FolderName; Bi.lpsztitle= _t ("Please select a folder to save"); Bi.ulflags= Bif_returnonlyfsdirs |Bif_statustext;; BI.LPFN= Browsecallbackproc;//callback functionBi.iimage =0; Bi.lparam=Long(&szpath);//set the default path to the parameter that is passed back to the functionPidl= SHBrowseForFolder (&i); if(pidl) {shgetpathfromidlist (pidl, szpath); }
3. Rollback function

First look at others say set global static variables, and then set static variables and static functions, start compiling pass, to figure out how to write this special static variables and functions.

Static variable initialization, in CPP, returns the definition of the function. h static,cpp not needed.

feel back function just send some message, there is no other use, then someone with lpdata set default path, I also tried it, did not succeed.

4. Remember the path

When the software is on, set the global variables, each update the default path, but when you re-open the software, the first to write to the registry, and then read the registry path, so that the problem is resolved. can also be written to the INI file.

5.CString and TCHAR arrays convert each other

TCHAR array go to CString is simple: Use the format of CString.

TCHAR m_buf[100] = _t ("Hello");

CString str;

Str. Format (L "%s", m_buf);

Now it's a bit of a hassle to CString to TCHAR arrays. Because there are a lot of solutions on the Internet, but they are not ideal. We use the _tcscpy () macro.

CString str = L "Sssssss";

TCHAR M_BUF[20];

_tcscpy (M_buf, str); The conversion between types is really troublesome, knowing the benefits of defining this macro.

Looking for a long day, this method is very effective! Not with memcpy,strcpy.

TCHAR Szdefaultdir[max_path];                CString strdef (_t ("e:\\"));                 2 2 );                Strdef.releasebuffer ();                 0;

It's also a way of doing it.

MFC remembers some of the problems that the last path---encountered

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.