VC++中利用回调函数OFNHookProc只打开指定文件夹 ////在指定文件夹: D:\指定文件夹\ ////下选择 *.TXT文件 CString Filter,str,jobdir; jobdir=_T("D:\\指定文件夹\\"); Filter =_T("我的文件(*.TXT)|*.TXT||"); CFileDialog FileDlg (true, ".TXT", "123.TXT",OFN_HIDEREADONLY,Filter.GetBuffer(520),this); FileDlg.m_ofn.lpstrInitialDir=jobdir; FileDlg.m_ofn.lpstrTitle="我的文件"; FileDlg.m_ofn.lpstrDefExt=".TXT"; FileDlg.m_ofn.Flags|=OFN_NOCHANGEDIR|OFN_FILEMUSTEXIST|OFN_ENABLEHOOK|OFN_EXPLORER; FileDlg.m_ofn.lpfnHook=OFNHookProc; if (FileDlg.DoModal()==IDOK) //单击文件选择对话框的"确定"按钮 { str=FileDlg.GetFileName(); } Filter.ReleaseBuffer(-1); ////str为选定的文件 UINT CALLBACK OFNHookProc( HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam ) { LPNMHDR pnmh; char filename[1024]; HWND thw; switch (uiMsg) { case WM_NOTIFY: pnmh = (LPNMHDR) lParam; if (pnmh->code == CDN_FILEOK) { _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); pThreadState->m_pAlternateWndInit = NULL; return 0; } if ( (pnmh->code == CDN_FOLDERCHANGE) || (pnmh->code == CDN_SELCHANGE) || (pnmh->code == CDN_INCLUDEITEM) ) { char curdir[1024]; strcpy(curdir,jobdir); ::GetCurrentDirectory(1020,curdir); strcat(curdir,"\\"); //当前文件夹不是指定文件夹时,切换到指定文件夹 if(strlen(curdir)!=strlen(jobdir)) { } } //将文件名设为指定的文件夹,再模拟按下回车键,再按下回退键 ::SetWindowText(GetDlgItem(GetParent(hdlg),edt1),jobdir); SetFocus(GetDlgItem(GetParent(hdlg),edt1)); ///////模拟键盘////////// keybd_event(VK_RETURN,0x1c,0,0); keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0); Sleep(20); keybd_event(VK_BACK,0xe,0,0); keybd_event(VK_BACK,0xe,KEYEVENTF_KEYUP,0); if (pnmh->code == CDN_INITDONE) { //隐藏对话框的改变目录框 ::ShowWindow(GetDlgItem(GetParent(hdlg),cmb2),SW_HIDE); //隐藏对话框的工具条 thw=::FindWindowEx(GetParent(hdlg),NULL,TOOLBARCLASSNAME,NULL); if(thw!=NULL) ::ShowWindow(thw,SW_HIDE); //改变"搜索"为需要的文本 sprintf(filename,"文件位置 %s",jobdir); thw=GetDlgItem(GetParent(hdlg),stc4); if(thw!=NULL) { ::SetWindowText(thw,filename); CRect rect,recf,recc; int tt; ::GetWindowRect(GetParent(hdlg),&recf); ::GetClientRect(GetParent(hdlg),&recc); tt=recf.bottom-recc.bottom; ::GetWindowRect(thw,&rect); 本文来源:https://www.wddqw.com/doc/25341facaa00b52acec7ca62.html