Use the cfiledialog hook function to solve the multi-Choice Problem in the dialog box.

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://www.cnblogs.com/clever101

 

A few days ago, the lead asked me a question: the cfiledialog class is used to set the File Buffer of the selected file when multiple selections are set. I don't know how much suitable the setting is. If domodal is set to a smaller value, the returned result is "failed, use the commdlgextendederror function to obtain the error code fnerr_buffertoosmall (that is, the buffer is too small), which is a waste of memory. (We have to select hundreds of files at a time, but I really don't know how big it is ).

I talked about my ideas: the data member m_ofn of cfiledialog has a data member who is the hook function pointer. By setting this function, you can check the message consumption of cfiledialog, for example, the user changes the path message, then obtains the number of files in the current path, and sets the buffer size based on this. The leader did not quite understand my ideas. He searched the internet and found a way to do this by deriving the cfiledialog class. The details are as follows:

Multiple Selection in a file dialog

 

The method mentioned above is indeed feasible. But I also believe that my method is feasible. After work, I searched the internet and found a solution to this problem on the Microsoft official website. The link is as follows:

How to handleFnerr_buffertoosmall

TheCodeSuitableWin 32OfProgram, Is not suitable for the MFC program, and when I build a Win32 program to test the code in this example, I find a problem, that is, when there are too many files selected, if there are too many buffers to be allocated, the heapalloc function in the link may encounter an error. The error message is as follows:

 

 

Therefore, heapalloc andHeapfreeThe functions are replaced by the new and delete operators of C ++.

 

Based on the practices provided on the official Microsoft website, I found out the practices for using the MFC program. The specific code is as follows:

// Hook Function
Uint_ptr callback myofnhookproc (hwnd hdlg, // Handle to child dialog box
Uint uimsg, // Message identifier
Wparam, // Message Parameter
Lparam // Message Parameter
)
{
Int Nresult = False;

If (Hdlg = Null)
Return   0 ;
# Ifdef _ debug
// From "_ afxcommdlgproc ()" of the file "dlgcomm. cpp"
_ Afx_thread_state * Pthreadstate = Afxgetthreadstate ();
If (Pthreadstate -> M_palternatewndinit ! = Null)
Pthreadstate
-> M_palternatewndinit = NULL;
# Endif

Switch (Uimsg)
{
Case Wm_notify:
{
Lpofnotify pofn
= (Lpofnotify) lparam;
Switch (Pofn -> HDR. Code)
{
Case Cdn_selchange:
{
Tchar dummy_buffer;

// Get the required size for the 'files 'buffer
Hwnd howner = Getparent (hdlg );
Hwnd hparent
= Getparent (howner );
Uint nfiles
= Commdlg_opensave_getspec (howner, & Dummy_buffer, 1 );

//Get the required size for the 'folder' Buffer
IntCblength=Commdlg_opensave_getspec (getparent (hdlg), null,0);

Cblength + = _ Max_path;
If (Cblength > (Pofn -> Lpofn) -> Nmaxfile)
{
Delete (pofn
-> Lpofn) -> Lpstrfile;
(Pofn
-> Lpofn) -> Lpstrfile =   New Tchar [cblength];
Zeromemory (pofn
-> Lpofn) -> Lpstrfile, cblength );
(Pofn
-> Lpofn) -> Nmaxfile = Cblength;
}

Nresult = True;
Break ;
}
Default :
Break ;
}
Break ;
}
Default :
Break ;
}
Return Nresult;
}

# DefineNamebuckets 1024

//Call a function
VoidCmultiselectdlg: onbutton1 ()
{
M_listbox.resetcontent ();
M_static.setwindowtext (_ T (
"0 files selected"));

tchar szfilters [] = _ T ( " mytype files (*. doc) | *. doc | " );

// Create an open dialog; the default file name extension is ". doc ".
cfiledialog filedlg (true, _ T ( " doc " ), _ T ( " *. doc " ),
ofn_filemustexist
| ofn_hidereadonly | ofn_allowmultiselect, szfilters);

Filedlg. m_ofn.lpstrfile = New Tchar [namebuf]; // Redefinition of lpstrfile buffer size
Memset (filedlg. m_ofn.lpstrfile, 0 , Namebuf ); // Buffer for initialization Definition

Filedlg. m_ofn.nmaxfile
= Namebuf; // Redefinition of nmaxfile
Filedlg. m_ofn.lpfnhook = (Lpofnhookproc) myofnhookproc;

Int_ptr RET
= Filedlg. domodal ();
If (Ret = Idok)
{
Int Width =   0 ;
Cstring STR;
CDC
* PDC = M_listbox.getdc ();
Int Saved = PDC -> Savedc ();
PDC
-> SelectObject (getfont ());

Uint count= 0;

Position POS = Filedlg. getstartposition ();
While (POS)
{
Str
= Filedlg. getnextpathname (POS );
M_listbox.addstring (STR );
Csize size (
0 , 0 );
Size
= PDC -> Gettextextent (STR );
Width
= Width > Size. CX ? Width: size. CX;
++ Count;
}
PDC
-> Restoredc (saved );
Releasedc (PDC );
M_listbox.sethorizontalextent (width
+   5 );

Str. Format (_ T ( " % U files selected " ), Count );
M_static.setwindowtext (STR );
}
DWORD dwcode
= Commdlgextendederror ();
If (Fnerr_buffertoosmall = Dwcode)
{
Afxmessagebox (_ T (
" An error occurred while obtaining the file path! " ));
}
Delete [] filedlg. m_ofn.lpstrfile;
Filedlg. m_ofn.lpstrfile
= NULL;
}

 

Another serious disadvantage of using hook functions is that the program must use the Unicode Character Set for compilation.Fnerr_buffertoosmall(This has been tested, and it is hard to understand why Microsoft does not support multi-byte programs at this point ). My testing environment is vs C ++ 2005 + SP1, Win XP + SP3, and Unicode Character Set.

 

 

 

 

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.