文件选择问题

juhwali 2003-06-27 11:15:50
文件选择对话框中,我设为多选,可是一次只能选20个,多了就出错了,请高手指点。给出代码,谢谢。
...全文
100 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoxiny 2003-06-28
  • 打赏
  • 举报
回复
#ifndef _FOLDERDIALOG_H_
#define _FOLDERDIALOG_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CFileFolderDialog : public CFileDialog
{
DECLARE_DYNAMIC(CFileFolderDialog)

public:
CFileFolderDialog(BOOL bOpenFileDialog, DWORD dwFlags = OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING |OFN_EXPLORER,
LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL);

~CFileFolderDialog();
void SelectFolder(HWND hWnd,CString& strSelectPath);
virtual int DoModal();
CString GetNextPathName(POSITION &pos) const;
POSITION GetStartPosition();

protected:
//{{AFX_MSG(CFileFolderDialog)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

private:
// char* lpFile;
BOOL bParsed;
TCHAR *Folder;
TCHAR *Files;

virtual void OnFileNameChange();
virtual void OnInitDone( );
};

#endif

#include "stdafx.h"
#include "FileFolderDialog.h"
#include <DLGS.H>
#include <WINUSER.H>
#include "CDERR.H"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFolderDialog

IMPLEMENT_DYNAMIC(CFileFolderDialog, CFileDialog)

CFileFolderDialog::CFileFolderDialog(BOOL bOpenFileDialog,DWORD dwFlags,
LPCTSTR lpszFilter,CWnd* pParentWnd)
: CFileDialog(bOpenFileDialog,NULL,NULL,
dwFlags,lpszFilter,pParentWnd)
{
Files = NULL;
Folder = NULL;
bParsed = FALSE;
// m_ofn.nMaxFile = 2562;
// lpFile = new char[2562];
// m_ofn.lpstrFile = lpFile;
// m_ofn.lpstrFile[0] = NULL;
}

CFileFolderDialog::~CFileFolderDialog()
{
// delete lpFile;
if (Files)
{
delete[] Files;
delete[] Folder;
}
}

BEGIN_MESSAGE_MAP(CFileFolderDialog, CFileDialog)
//{{AFX_MSG_MAP(CFileFolderDialog)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


void CFileFolderDialog::OnInitDone()
{
HideControl(chx1);

const UINT iExtraSize = 150;
// Number of controls in the File Dialog
const UINT nControls = 7;

// Get a pointer to the original dialog box.
CWnd *wndDlg = GetParent();

RECT Rect;
wndDlg->GetWindowRect(&Rect);
// Change the size
wndDlg->SetWindowPos(NULL, 0, 0,
Rect.right - Rect.left,
Rect.bottom - Rect.top + iExtraSize,
SWP_NOMOVE);
wndDlg->CenterWindow();
// Control ID's - defined in <dlgs.h>
UINT Controls[nControls] = {stc3, stc2, // The two label controls
edt1, cmb1, // The eidt control and the drop-down box
IDOK, IDCANCEL,
lst1}; // Explorer vinduet

// Go through each of the controls in the dialog box, and move them to a new position
for (int i=0 ; i<nControls ; i++)
{
CWnd *wndCtrl = wndDlg->GetDlgItem(Controls[i]);
wndCtrl->GetWindowRect(&Rect);
wndDlg->ScreenToClient(&Rect); // Remember it is child controls

// Move all the controls according to the new size of the dialog.
if (Controls[i] != lst1)
wndCtrl->SetWindowPos(NULL,
Rect.left, Rect.top + iExtraSize,
0, 0, SWP_NOSIZE);
else // This is the explorer like window. It should be sized - not moved.
wndCtrl->SetWindowPos(NULL, 0, 0,
Rect.right - Rect.left,
Rect.bottom - Rect.top + iExtraSize,
SWP_NOMOVE);
}
}

int CFileFolderDialog::DoModal()
{
if (Files)
{
delete[] Files;
Files = NULL;
delete[] Folder;
Folder = NULL;
}

int ret = CFileDialog::DoModal();

if (ret == IDCANCEL)
{
DWORD err = CommDlgExtendedError();
if (err == FNERR_BUFFERTOOSMALL/*0x3003*/ && Files)
ret = IDOK;
}
return ret;
}

CString CFileFolderDialog::GetNextPathName(POSITION& pos) const
{
if (!Files)
return CFileDialog::GetNextPathName(pos);

ASSERT(pos);
TCHAR *ptr = (TCHAR *)pos;

CString ret = Folder;
ret += _T("\\");
ret += ptr;

ptr += _tcslen(ptr) + 1;
if (*ptr)
pos = (POSITION)ptr;
else
pos = NULL;

return ret;
}

POSITION CFileFolderDialog::GetStartPosition()
{
if (!Files)
return CFileDialog::GetStartPosition();

if (!bParsed)
{
CString temp = Files;
temp.Replace(_T("\" \""), _T("\""));
temp.Delete(0, 1); // remove leading quote mark
temp.Delete(temp.GetLength() - 1, 1); // remove trailing space

_tcscpy(Files, temp);

TCHAR *ptr = Files;
while (*ptr)
{
if ('\"' == *ptr)
*ptr = '\0';
++ptr;
}
bParsed = TRUE;
}

return (POSITION)Files;
}

void CFileFolderDialog::OnFileNameChange()
{
TCHAR dummy_buffer;

// Get the required size for the 'files' buffer
UINT nfiles = CommDlg_OpenSave_GetSpec(GetParent()->m_hWnd, &dummy_buffer, 1);

// Get the required size for the 'folder' buffer
UINT nfolder = CommDlg_OpenSave_GetFolderPath(GetParent()->m_hWnd, &dummy_buffer, 1);

// Check if lpstrFile and nMaxFile are large enough
if (nfiles + nfolder > m_ofn.nMaxFile)
{
bParsed = FALSE;
if (Files)
delete[] Files;
Files = new TCHAR[nfiles + 1];
CommDlg_OpenSave_GetSpec(GetParent()->m_hWnd, Files, nfiles);

if (Folder)
delete[] Folder;
Folder = new TCHAR[nfolder + 1];
CommDlg_OpenSave_GetFolderPath(GetParent()->m_hWnd, Folder, nfolder);
}
else if (Files)
{
delete[] Files;
Files = NULL;
delete[] Folder;
Folder = NULL;
}

CFileDialog::OnFileNameChange();
}

#define BIF_NEWDIALOGSTYLE 0x0040
#define BIF_USENEWUI (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)

void CFileFolderDialog::SelectFolder(HWND hWnd,CString& strSelectPath)
{
BROWSEINFO bi;
char FileName[MAX_PATH];

ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = hWnd;
bi.pszDisplayName = FileName;
bi.lpszTitle="Select Folder";
bi.ulFlags= BIF_USENEWUI;

LPITEMIDLIST idl=SHBrowseForFolder(&bi);
if(idl==NULL)
return;

SHGetPathFromIDList(idl,FileName);
strSelectPath.Format("%s",FileName);
}
juhwali 2003-06-28
  • 打赏
  • 举报
回复
fd.m_ofn.nMaxFile = MAXFILE;确实不管用,这个我去年就试过了,如果你确定你的代码可行的话,发到juhwali@163.com,这个问题确实能解决的话,给80分。
Camdyn 2003-06-27
  • 打赏
  • 举报
回复
同意zyleon(zy)的说法
feichen8 2003-06-27
  • 打赏
  • 举报
回复
我也碰到过
帮你up
zyleon 2003-06-27
  • 打赏
  • 举报
回复
缓冲区必须设得足够大.
http://www.codeproject.com/dialog/PJA_MultiSelect.asp

DWORD MAXFILE = 2562; //2562 is the max
fd.m_ofn.nMaxFile = MAXFILE;
zyoujie 2003-06-27
  • 打赏
  • 举报
回复
CFileDialog中没有限制,
你检查程序的其它部分吧
gister 2003-06-27
  • 打赏
  • 举报
回复
给我你的邮箱,我给你发个控件用用!
lemong 2003-06-27
  • 打赏
  • 举报
回复
没用过,有地方设置数目上限吗?
zhucde 2003-06-27
  • 打赏
  • 举报
回复
gz
xiaobaolove 2003-06-27
  • 打赏
  • 举报
回复
DWORD MAXFILE = 2562; //2562 is the max
fd.m_ofn.nMaxFile = MAXFILE;

是的,把文件名绶冲区设大一点

查一查msdn...

不行的话,我这里有代码。。。
wocan 2003-06-27
  • 打赏
  • 举报
回复
同意zyleon(zy)的说法
juhwali 2003-06-27
  • 打赏
  • 举报
回复
nMaxFile无法设为大于2176的值,当设为2176以上时,程序直接就崩溃了,根本连选择对话框都不出现了。
希望提供确实能运行的代码。
coyer 2003-06-27
  • 打赏
  • 举报
回复
同意zyleon(zy)的说法

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧