Click here to Skip to main content
15,902,911 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Microsecond timer Pin
jschell13-Sep-11 11:42
jschell13-Sep-11 11:42 
GeneralRe: Microsecond timer [modified] Pin
azhari2413-Sep-11 18:57
azhari2413-Sep-11 18:57 
GeneralRe: Microsecond timer Pin
Erudite_Eric13-Sep-11 22:46
Erudite_Eric13-Sep-11 22:46 
GeneralRe: Microsecond timer Pin
azhari2413-Sep-11 18:58
azhari2413-Sep-11 18:58 
GeneralRe: Microsecond timer Pin
azhari2413-Sep-11 18:54
azhari2413-Sep-11 18:54 
QuestionGetting the handle of a PictureBox in MFC Pin
Tom Moore11-Sep-11 2:16
Tom Moore11-Sep-11 2:16 
AnswerRe: Getting the handle of a PictureBox in MFC Pin
Randor 11-Sep-11 3:34
professional Randor 11-Sep-11 3:34 
QuestionHow to use the hook function of Windows FileOpen dialog Pin
clever10110-Sep-11 1:29
clever10110-Sep-11 1:29 
hi,everyone.

When using the Windows FileOpen dialog with multiple selection do you ever wonder how much memory you have to allocate for the buffer. Is one kilobyte going to be enough? Or should you make it ten? How about one Megabyte just to be safe?

It seems that no matter what you choose, you are either going to waste a whole bunch of memory just to be safe, or your user is going to select a bunch of files only to find their selection didn't work because your buffer was too small.

one way is Derive the CFileDialog class , the article as follow introduce it:

Multiple Selection in a File Dialog

but I have the other method: use the hook function of CFileDialog. I write some code as follow:

C++
UINT_PTR CALLBACK MyOFNHookProc( HWND hdlg,  // handle to child dialog box
        UINT uiMsg,  // message identifier
        WPARAM wParam, // message parameter
        LPARAM 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();
 // =_AfxActivationWndProc(hwnd,uint,uint,long)
 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
     int cbLength = CommDlg_OpenSave_GetSpec(GetParent(hdlg), NULL, 0);
 
     cbLength += _MAX_PATH;
 
     if(cbLength>(pOfn->lpOFN)->nMaxFile)
     {
 
      if((pOfn->lpOFN)->lpstrFile)
       HeapFree(GetProcessHeap(),
       0,
       (pOfn->lpOFN)->lpstrFile);
 
      (pOfn->lpOFN)->lpstrFile = (LPTSTR) HeapAlloc(GetProcessHeap(),
       HEAP_ZERO_MEMORY,
       cbLength);
            (pOfn->lpOFN)->nMaxFile = cbLength;
     }
 
     nResult = TRUE;
     break;
    }
   default:
    break;
   }
   break;
  }
 default:
  break;
 }
 return nResult;
}
 
// call function
void CMultiSelectDlg::OnButton1() 
{
  #define NAMEBUF 1024
 
 TCHAR szFilters[]= _T("MyType Files (*.doc)|*.doc||");
 
   CFileDialog fileDlg(TRUE, _T("doc"), _T("*.doc"),
  OFN_FILEMUSTEXIST | OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, szFilters);
 
   fileDlg.m_ofn.lpstrFile= (LPTSTR) HeapAlloc(GetProcessHeap(),
  HEAP_ZERO_MEMORY,
  NAMEBUF);
 
 fileDlg.m_ofn.nMaxFile = NAMEBUF;   // redefine 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)
 {
  int i =0;
 }
 
 HeapFree(GetProcessHeap(),0,
  (fileDlg.m_ofn.lpstrFile));
}


the code Reference the article as follow :

How To Handle FNERR_BUFFERTOOSMALL in Windows

but I found when the file names buffer is enough large , it run successly; but when when the file names buffer is not enough large (it means the program will enter if(cbLength>(pOfn->lpOFN)->nMaxFile) ), when after run INT_PTR ret = fileDlg.DoModal(); the value of ret is IDCANCEL. I catch the error code, it is FNERR_BUFFERTOOSMALL . In the end even I allocate a enough large memory ,the error code still is FNERR_BUFFERTOOSMALL . Why???
AnswerRe: How to use the hook function of Windows FileOpen dialog Pin
Philippe Mori10-Sep-11 16:25
Philippe Mori10-Sep-11 16:25 
QuestionLive video processing Pin
Smith#9-Sep-11 21:32
Smith#9-Sep-11 21:32 
AnswerRe: Live video processing Pin
Richard MacCutchan9-Sep-11 23:02
mveRichard MacCutchan9-Sep-11 23:02 
AnswerRe: Live video processing Pin
barneyman11-Sep-11 17:58
barneyman11-Sep-11 17:58 
QuestionMFC CTabCtrl problems in VS2010 Pin
j_schultz9-Sep-11 11:28
j_schultz9-Sep-11 11:28 
AnswerRe: MFC CTabCtrl problems in VS2010 Pin
TheGreatAndPowerfulOz9-Sep-11 12:00
TheGreatAndPowerfulOz9-Sep-11 12:00 
GeneralRe: MFC CTabCtrl problems in VS2010 Pin
j_schultz12-Sep-11 9:38
j_schultz12-Sep-11 9:38 
QuestionHow to create a object of mainframe in application class Pin
Amrit Agr9-Sep-11 1:43
Amrit Agr9-Sep-11 1:43 
AnswerRe: How to create a object of mainframe in application class Pin
Code-o-mat9-Sep-11 2:24
Code-o-mat9-Sep-11 2:24 
QuestionHow to check i am clicking on the header of the list control Pin
Amrit Agr8-Sep-11 5:49
Amrit Agr8-Sep-11 5:49 
QuestionRe: How to check i am clicking on the header of the list control Pin
David Crow8-Sep-11 5:52
David Crow8-Sep-11 5:52 
AnswerRe: How to check i am clicking on the header of the list control [modified] Pin
Chuck O'Toole8-Sep-11 11:50
Chuck O'Toole8-Sep-11 11:50 
Questionquestion with locale [modified] Pin
Cold_Fearing_Bird8-Sep-11 4:38
Cold_Fearing_Bird8-Sep-11 4:38 
QuestionCRichEditCtrl Pin
john56328-Sep-11 2:33
john56328-Sep-11 2:33 
AnswerRe: CRichEditCtrl Pin
Orjan Westin8-Sep-11 3:15
professionalOrjan Westin8-Sep-11 3:15 
GeneralRe: CRichEditCtrl Pin
john56328-Sep-11 19:02
john56328-Sep-11 19:02 
QuestionEncryption and Decryption between C++ and C# Pin
vipin_nvk8-Sep-11 0:13
vipin_nvk8-Sep-11 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.