目录选择框如何记录上次选择的路径?

Freshyy 2005-09-11 09:35:33
路径太长,每次选择很麻烦,如何记录上次选择的路径?
...全文
193 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dasiu 2005-09-12
1)通过注册表保存操作路径;
2)通过以上类对象打开上一次操作的路径
回复
dasiu 2005-09-12
给你一个好的代码类吧

// BrowseDir.h: interface for the CBrowseDir class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BROWSEDIR_H__34F5431E_2750_457A_A5E2_6DB131771DF8__INCLUDED_)
#define AFX_BROWSEDIR_H__34F5431E_2750_457A_A5E2_6DB131771DF8__INCLUDED_

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

class CBrowseDir
{
public:
struct struct_browsedir
{
HWND hOwnerWnd;
BOOL bIncludeSubDir;
char *lpszInitDir;
char *lpszTitle;
char szDisplayName[255];
char szPath[255];
WNDPROC proc;
DWORD uFlag;
} ;
private:
struct_browsedir m_dir;
public:
CBrowseDir( CWnd * phParentWnd,BOOL bIncludeSubDir=NULL,
LPSTR lpszInitDir=NULL,LPSTR lpszTitle=NULL,
DWORD uFlag=BIF_RETURNONLYFSDIRS)
{
m_dir.szPath[0]=0;
m_dir.szDisplayName[0]=0;
m_dir.hOwnerWnd =phParentWnd->m_hWnd;
m_dir.lpszInitDir=lpszInitDir;
m_dir.bIncludeSubDir=bIncludeSubDir;
m_dir.uFlag=uFlag;
m_dir.lpszTitle=lpszTitle;
};

~CBrowseDir()
{

};

public:

LPSTR GetPathName(){return m_dir.szPath;}
LPSTR GetDisplayName(){return m_dir.szDisplayName;}
BOOL IsIncludeSubDir(){return m_dir.bIncludeSubDir;}
int DoModal();
public:
static LRESULT CALLBACK Browse_WndProc(HWND hWnd,UINT uMsg,
WPARAM wParam,LPARAM lParam);
static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,
LPARAM lParam,LPARAM lpData);

};
#endif // !defined(AFX_BROWSEDIR_H__34F5431E_2750_457A_A5E2_6DB131771DF8__INCLUDED_)

// BrowseDir.cpp: implementation of the CBrowseDir class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BrowseDir.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int CBrowseDir::DoModal()
{
CWaitCursor wait;

BROWSEINFO bi;
RtlZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner=m_dir.hOwnerWnd;
bi.ulFlags=m_dir.uFlag;
bi.pszDisplayName = m_dir.szDisplayName;
bi.lpszTitle=m_dir.lpszTitle;
if (m_dir.lpszInitDir || m_dir.bIncludeSubDir)
{
bi.lParam=(LPARAM)&m_dir;//lParam传给回调函数中的lpData
bi.lpfn=BrowseCallbackProc;
}

//调用浏览目录
ITEMIDLIST *pIDList = SHBrowseForFolder(&bi);

//确定选择的路径
if(pIDList)
{
SHGetPathFromIDList(pIDList, m_dir.szPath);
LPMALLOC pMalloc=NULL;
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
pMalloc->Free(pIDList);
pMalloc->Release();
return IDOK;
}
}
return IDCANCEL;
}

//////////////////////////////////////////////////////////////////////
LRESULT CALLBACK CBrowseDir::Browse_WndProc(HWND hWnd,UINT uMsg,
WPARAM wParam,LPARAM lParam)
{
CBrowseDir::struct_browsedir * lpDir;
lpDir=(CBrowseDir::struct_browsedir *)GetWindowLong(hWnd,GWL_USERDATA);

//窗口快要摧毁了,
//赶紧看看有没有选中"包含子文件夹"
if (uMsg==WM_DESTROY )
{
lpDir->bIncludeSubDir=SendDlgItemMessage(hWnd,1000,BM_GETCHECK,0,0);
}
return ::CallWindowProc(lpDir->proc,hWnd,uMsg,wParam,lParam);
}

int CALLBACK CBrowseDir::BrowseCallbackProc(HWND hwnd,UINT uMsg,
LPARAM lParam,LPARAM lpData)
{
if (uMsg==BFFM_INITIALIZED)
{
CBrowseDir::struct_browsedir * lpDir;
lpDir=(CBrowseDir::struct_browsedir*)lpData;//看DoModal()
//设置初始目录
if (lpDir->lpszInitDir)
SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)lpDir->lpszInitDir);

//是否 支持包含子目录
if (lpDir->bIncludeSubDir)
{
//创建一个Check按钮,ID为1000
CreateWindowEx(0,"button","包含子文件夹",
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
5,5,100,20,hwnd,(HMENU)1000,::GetModuleHandle(0),0);

//用默认的GUI字体

SendDlgItemMessage(hwnd,1000,WM_SETFONT ,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT),TRUE );

//由于默认的回调函数不能处理标准的消息
//所以必须改变窗体的回调函数
lpDir->proc=(WNDPROC)SetWindowLong(hwnd,GWL_WNDPROC,(LONG)Browse_WndProc);

//OOP时代据说不兴用全局变量...
SetWindowLong(hwnd,GWL_USERDATA,(LONG)lpDir);
}
}
return TRUE;
}
回复
Freshyy 2005-09-11
不过试了好像不行?
回复
Freshyy 2005-09-11
bool Tools::SelectFolder(HWND hWndOwner, CString csTitle, CString &csOutFolderPath, UINT uFlags)
{
BROWSEINFO bInfo;
TCHAR pathName[MAX_PATH];
LPITEMIDLIST pIDList;

LPITEMIDLIST lpItemIDList;
if ( NOERROR != ::SHGetSpecialFolderLocation( NULL, CSIDL_RECENT, &lpItemIDList))
{
return false;
}


bInfo.hwndOwner = hWndOwner;
bInfo.pidlRoot = NULL;
bInfo.pszDisplayName = pathName;
bInfo.lpszTitle = csTitle;
bInfo.ulFlags = uFlags;
bInfo.pidlRoot = lpItemIDList;

bInfo.lpfn = NULL;
bInfo.lParam = 0;
bInfo.iImage = 0;

pIDList = ::SHBrowseForFolder( &bInfo );

TCHAR buff[ _MAX_PATH] = {0};
if ( NULL != pIDList )
{
::SHGetPathFromIDList( pIDList, buff );
csOutFolderPath = buff;

LPMALLOC pMalloc;
if ( NOERROR == ::SHGetMalloc( &pMalloc ))
{
pMalloc->Free( pIDList );
pMalloc->Release();
}
}
return ( pIDList != NULL);
}

我能通过bInfo.pidlRoot 来设置上次的路径吗?
回复
winehero 2005-09-11
ini/注册表/数据表等等都可以,依具体情况而定
回复
smartcwj 2005-09-11
可以在选完路径后传递参数时生成一个ini配置文件,然后每次打开文件时先读取ini文件就行了
回复
相关推荐
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2005-09-11 09:35
社区公告
暂无公告