In front of the two personalities of the folder selection dialog box to complete the method, see the article "Win7 interface and API to complete the Old Interface Folder selection dialog box code complete detailed explanation》。 and the Old Interface folder selection dialog box is very small, also makes the selection is not easy, so many people do not like this dialog box. But today's customization of this folder selection dialog box style and size, may be able to greatly enhance the user experience it. Hey.
First of all, look at the finished, look at the picture to speak, there is a picture!
The top chart is horizontal, the second picture is vertical, and the third picture is horizontal and vertical. dialog box, you also added an edit box that allows you to enter the folder path directly. When a folder is selected, the selected folder path is automatically updated to the edit box. Look, the effect is good, below to see how to customize, disjointed original small selection box bar.
The principle of completion is actually very simple, which is to supply a callback function to customize. Why the callback function is simple to complete the customization, see the article "" the deepening of the planning principle of the function call and the callback function "detailed anatomy. The callback function mechanism is not discussed in detail here. Using a callback function, that is, gives you the opportunity to customize the interface, so the callback function is a very good thing oh. In the callback function, the audio bffm_initialized is echoed, and then all the child controls in the folder selection dialog box traverse to use a callback function, it is necessary to provide a callback function, in the Folder selection dialog box in the constructor of the member Lpfn pass the callback function address. The code for the Start Folder selection dialog box is as follows:
Stores the selected folder path Browseinfo bi;memset (&bi, 0, sizeof (BI)); Lpitemidlist Pitemlist;bi.hwndowner = m_hwnd;//-Sets the handle of the user window, if null, equivalent to the non-modal window Bi.lpsztitle = _t ("★c++ skill NET hint ★?" Select folder?); /-Pick the hint text in the folder Bi.ulflags = Bif_returnonlyfsdirs | BIF_EDITBOX;BI.LPFN = browsecallbackproc;//-Callback function for Customizing Folder selection dialog box bi.lparam= (lParam) lppath;//- Incoming tacitly selected folder Pitemlist = SHBrowseForFolder (&BI); if (pitemlist!=null) {shgetpathfromidlist (pitemlist,lptemp);//- Get the selected folder path}
The code illustrates:
The comments in the code have been written very clearly, each step is clearly marked, so there is no more explanation. The BROWSEINFO structure is the one to say. This constructor is used to pass various parameters to the folder selection dialog box, including a callback function for customizing the interface. The members of the ulflags are able to use multiple identities to assemble and complete the interface customization. Bif_editbox is the more out of the edit box. Bif_returnonlyfsdirs indicates that only the folder is returned. There are many of these signs, detailed to be self-check MSDN, Inside said very clear, I do not repeat the commentary, just notice you way, you have to go to learn from a hands-on. SHBrowseForFolder is the Shell function that launches the Folder selection dialog box. After performing, then judging is not successful, then call SHGetPathFromIDList to get the folder in the list of selected folders, stored in lptemp.
We're going to supply a callback. function First, declare the callback function as follows:
static int __stdcall Browsecallbackproc (HWND hwnd, UINT umsg, LPARAM LPARAM, LPARAM lpdata);
storage The style of the child control, stored at the mercy of the bottom margin, width and height. This information can be stored in a structured way, simplifying the code and making the logic more clear. and the use of TypeDef to redefine the structure of the type, so that the use of more bento.
The constructor definition is as follows:
typedef struct controllayout{int iwidth; -width int iheight; -height int imargin_left; -Left margin int imargin_right; -Right margin int imargin_top; -Top margin int imargin_bottom;//-bottom margin}ctrllayout;
callback function The code is done as follows:
int CALLBACK Cfolderdlg::browsecallbackproc (HWND hwnd,uint umsg,lparam lparam,lparam lpdata) {cwnd* pmainwnd; CRect orgmainclientrect;//-Adjust Front Folder dialog box client area rectangle CRect newmainwndrect;//-Resize Folder dialog box All window rectangle cwnd* pchildwnd;//-subwindow pointer tchar szclassname[max_path];//-Control Windows window class class name CRect Treerect; -Tree control Rectangular area crect buttonrect;//-button control rectangular area ctrllayout treelayout={0};//-tree control style ctrllayout buttonlayout[2]={0};//- tree control style int iIndex = 0;//-Count button arraySwitch (umsg) {Case bffm_initialized:pmainwnd = Cwnd::fromhandle (hwnd);//-depending on the window handle, getsFolder selection Dialog pointer Pmainwnd->getclientrect (orgmainclientrect)//-Gets the size of the main window client area, that is, the area where the control is placed Pchildwnd = pmainwnd-> GetWindow (Gw_child); -Get a pointer to child window/*-the control of the Recycle Bento folder Pick dialog box, andStore control's azimuth information-*/while (Pchildwnd! = NULL) {getclassname (Pchildwnd->m_hwnd, Szclassname, MAX_PATH), if (_TCSCMP (_t (" SysTreeView32 "), szclassname) = = 0) {/*-storage tree control style-*/pchildwnd->getwindowrect (treerect);p mainwnd-> ScreenToClient (Treerect); Treelayout.imargin_left = Treerect.left; Treelayout.imargin_top = Treerect.top; Treelayout.imargin_right = Orgmainclientrect.right-treerect.right; Treelayout.imargin_bottom = Orgmainclientrect.bottom-treerect.bottom;} if (_tcscmp (_t ("button"), szclassname) = = 0) {/*-Store Button control Styles-*/pchildwnd->getwindowrect (buttonrect);p mainwnd->screentoclient (buttonrect); Buttonlayout[iindex].iwidth = Buttonrect.width (); Buttonlayout[iindex].iheight = Buttonrect.height (); Buttonlayout[iindex].imargin_right = Orgmainclientrect.right-buttonrect.right; Buttonlayout[iindex].imargin_bottom = orgmainclientrect.bottom-buttonrect.bottom;iindex++;} Pchildwnd = Pchildwnd->getnextwindow ();} /*-IncreaseThe client area in the Folder dialog box and dialog box-*/pmainwnd->getwindowrect (Newmainwndrect); orgmainclientrect.inflaterect (0, 0, 240, 300); Newmainwndrect.inflaterect (0, 0, +);p Mainwnd->movewindow (newmainwndrect);/*-correction control size and azimuth-*/iindex = 0; Pchildwnd = Pmainwnd->getwindow (gw_child);//-Move the main window and get the child window pointer while (Pchildwnd! = NULL) {getclassname (pchildwnd->m _hwnd, Szclassname, MAX_PATH); if (_tcscmp (_t ("SysTreeView32"), szclassname) = = 0) {Treerect.left = Treelayout.imargin_ Left; Treerect.top = Treelayout.imargin_top; Treerect.right = Orgmainclientrect.right-treelayout.imargin_right; Treerect.bottom = Orgmainclientrect.bottom-treelayout.imargin_bottom;pchildwnd->movewindow (TreeRect);} if (_tcscmp (_t ("button"), szclassname) = = 0) {buttonrect.right = Orgmainclientrect.right-buttonlayout[iindex].imargin _right; Buttonrect.bottom = Orgmainclientrect.bottom-buttonlayout[iindex].imargin_bottom; Buttonrect.left = Buttonrect.right-buttonlayout[iindex].iwidth; Buttonrect.top = Buttonrect.bottom-buttonlayout[iinDex].iheight;pchildwnd->movewindow (buttonrect); iindex++;} Pchildwnd = Pchildwnd->getnextwindow ();} Pmainwnd->sendmessage (Bffm_setselection, TRUE, lpdata);//-Set acquiescence to the selectedfolder Break;default:break;} return 0;}
The code illustrates:
The comments for each code are very clear at all. Here's the whole idea of the process. The callback function only echoes the bffm_initialized audio, that is, only the initializationFolderSelect the dialog box to customize the dialog box, the other audio will not be the tube.
Using TreelayoutStorage folder tree size and planning (relative orientation), using the Buttonlayout array to store the size and planning of two buttons. Use the incoming folder to pick the dialog handle to get the main window pointer, and then get the child window and client area. The folder selection dialog is constructed like this: All windows are the parent window and each control in the window is a child window. To correct these child windows, iterate through the Subwindow, call GetWindow (Gw_child) to get the top child window, and then call Getnextwindow () to get the rear subwindow until Getnextwindow () Gets the child window pointer to null to stop. According to the corresponding control class, store the corresponding plan. Once again, the control is adjusted, of course, if the Yaotu easy, but also can be modified at once, is also able. Here is just a look at the code that can be optimized on that basis. The Inflaterect () function expands the rectangle, the parameter is positive, then expands the rectangle, and the negative number decreases the rectangle. Please check the MSDN commentary for details. Eventually send an audio setting that tacitly selects the folder. shenzhen Silk foot
Custom Folder selection dialog box style and size implementation method