#pragma once
// CFileBrowser dialog
class CFileBrowser : public CDialog
{
DECLARE_DYNAMIC(CFileBrowser)
public:
CFileBrowser(CWnd*
pParent = NULL); //
standard constructor
virtual
~CFileBrowser();
// Dialog Data
enum
{ IDD = IDD_DIALOG_NEW_PROJECT };
protected:
virtual
void DoDataExchange(CDataExchange* pDX); //
DDX/DDV support
afx_msg
void OnBnClickedButtonBroswer ();
afx_msg
void OnBnClickedOk();
afx_msg
void OnBnClickedCancel();
DECLARE_MESSAGE_MAP()
private:
CString
m_strName;
CString
m_strPath;
protected:
void
GetUserDocumentPath();
public:
CString
GetProjectName();
CString
GetProjectPath();
virtual
BOOL OnInitDialog();
LPITEMIDLIST
GetItemIDListFromFilePat h( const std::string &strFilePath );
};
// FileBrowser.cpp : implementation file
//
#include "stdafx.h"
#include
#include
#include "FileBrowser.h"
#pragma comment( lib, "shell32.lib")
// CFileBrowser dialog
IMPLEMENT_DYNAMIC(CFileBrowser, CDialog)
CFileBrowser::CFileBrowser(CWnd* pParent )
:
CDialog(CFileBrowser::IDD, pParent)
,
m_strName(_T(""))
,
m_strPath(_T(""))
{
GetUserDocumentPath();
}
CFileBrowser::~CFileBrowser()
{
}
void CFileBrowser::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,
IDC_EDIT_NEW_PROJ_NAME, m_strName);
DDX_Text(pDX,
IDC_EDIT_NEW_PROJ_PATH, m_strPath);
}
BEGIN_MESSAGE_MAP(CFileBrowser, CDialog)
ON_BN_CLICKED(IDOK,
&CFileBrowser::OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL,
&CFileBrowser::OnBnClickedCancel)
ON_BN_CLICKED(IDC_BUTTON_BROSWER,
&CFileBrowser::OnBnClickedButtonBroswer )
END_MESSAGE_MAP()
void CFileBrowser::GetUserDocumentPath()
{
char szDocument[MAX_PATH]={0};
HKEY hKey;
LPCTSTR
data_Set=_T("Software\\EasyFeature");
LONG
lRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,data_Set,&hKey);
if(lRet!=ERROR_SUCCESS)
{
::RegCreateKey(HKEY_LOCAL_MACHINE,data_Set,&hKey);
}
//查询有关的数据
LPBYTE
pFileWorkSpace = new BYTE[MAX_PATH];
DWORD
dwType=REG_SZ;//定义数据类型
DWORD
dwSize=MAX_PATH;//定义数据长度
lRet
= ::RegQueryValueEx(hKey,_T("FileWorkSpace"),NULL,&dwType,pFileWorkSpace,&dwSize);
if(lRet!=ERROR_SUCCESS)
{
LPITEMIDLIST
pidl=NULL;
SHGetSpecialFolderLocati on(NULL,CSIDL_PERSONAL,&pidl);
if (pidl && SHGetPathFromIDList(pidl, szDocument))
{
m_strPath
= szDocument;
lRet
= ::RegSetValueEx(hKey,_T("FileWorkSpace"),NULL,dwType,(const BYTE*)szDocument,dwSize);
if
(lRet!=ERROR_SUCCESS)
{
TRACE(_T("写入注册表失败!\n"));
}
}
}
else
{
m_strPath
= pFileWorkSpace;
}
::RegCloseKey(hKey);
delete
pFileWorkSpace;
}
// CFileBrowser message handlers
void CFileBrowser::OnBnClickedOk()
{
UpdateData(TRUE);
//
TODO: Add your control notification handler code here
if(m_strName.IsEmpty())
{
MessageBox(_T("请输入文件名"));
return;
}
else
if(m_strPath.IsEmpty())
{
MessageBox(_T("请输入路径"));
return
;
}
OnOK();
}
void CFileBrowser::OnBnClickedCancel()
{
//
TODO: Add your control notification handler code here
OnCancel();
}
CString CFileBrowser::GetProjectName()
{
return
this->m_strName;
}
CString CFileBrowser::GetProjectPath()
{
return
this->m_strPath;
}
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch(uMsg)
{
case
BFFM_INITIALIZED:
{
HKEY hKey;
LPCTSTR
data_Set=_T("Software\\EasyFeature");
LONG
lRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,data_Set,&hKey);
if(lRet!=ERROR_SUCCESS)
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,lpData);
break;
}
//查询有关的数据
LPBYTE
pFileWorkSpace = new BYTE[MAX_PATH];
DWORD
dwType=REG_SZ;//定义数据类型
DWORD
dwSize=MAX_PATH;//定义数据长度
lRet
= ::RegQueryValueEx(hKey,_T("FileWorkSpace"),NULL,&dwType,pFileWorkSpace,&dwSize);
if(lRet!=ERROR_SUCCESS)
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,lpData);
}
else
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)(LPCSTR)pFileWorkSpace);
}
delete
pFileWorkSpace;
}
break;
case
BFFM_SELCHANGED:
{
TCHAR
szText[MAX_PATH] = {0};
SHGetPathFromIDList(reinterpret_cast(lParam),
szText);
SendMessage(hwnd,
BFFM_SETSTATUSTEXT, 0,
reinterpret_cast(szText));
}
break;
}
return
0;
}
void CFileBrowser::OnBnClickedButtonBroswer ()
{
HKEY hKey;
LPCTSTR
data_Set=_T("Software\\EasyFeature");
LONG
lRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,data_Set,&hKey);
if(lRet!=ERROR_SUCCESS)
{
::RegCreateKey(HKEY_LOCAL_MACHINE,data_Set,&hKey);
}
//查询有关的数据
LPBYTE
pFileWorkSpace = new BYTE[MAX_PATH];
DWORD
dwType=REG_SZ;//定义数据类型
DWORD
dwSize=MAX_PATH;//定义数据长度
BROWSEINFO
bi;
bi.hwndOwner
= m_hWnd;
bi.pidlRoot
= NULL;
bi.pszDisplayName
= (LPSTR)pFileWorkSpace;
bi.lpszTitle
= _T("请选择目录:");
bi.ulFlags
= 0x0040|BIF_EDITBOX;
bi.lpfn
= BrowseCallbackProc;
bi.lParam
= NULL;
bi.iImage
= 0;
//弹出选择目录对话框
LPITEMIDLIST
lp = SHBrowseForFolder(&bi);
if(lp
&& SHGetPathFromIDList(lp, (LPSTR)pFileWorkSpace))
{
this->m_strPath
= pFileWorkSpace;
lRet
= ::RegSetValueEx(hKey,_T("FileWorkSpace"),NULL,dwType,(const BYTE*)pFileWorkSpace,dwSize);
if
(lRet!=ERROR_SUCCESS)
{
TRACE(_T("写入注册表失败!\n"));
}
}
UpdateData(FALSE);
::RegCloseKey(hKey);
delete
pFileWorkSpace;
}
BOOL CFileBrowser::OnInitDialog()
{
CDialog::OnInitDialog();
//
TODO: Add extra initialization here
GotoDlgCtrl(GetDlgItem(IDC_EDIT_NEW_PROJ_NAME));
return
FALSE; // return TRUE unless you set the focus to a control
//
EXCEPTION: OCX Property Pages should return FALSE
}
LPITEMIDLIST CFileBrowser::GetItemIDListFromFilePat h(
const std::string &strFilePath )
{
if
( strFilePath.empty() )
{
return
NULL;
}
//
得到桌面的目录
LPSHELLFOLDER
pDesktopFolder = NULL;
HRESULT
hr = SHGetDesktopFolder( &pDesktopFolder );
if
( FAILED(hr) )
{
return
NULL;
}
//
将文件路径转换至OLECHAR格式
OLECHAR
strOleFilePath[MAX_PATH];
MultiByteToWideChar(
CP_ACP, MB_PRECOMPOSED, strFilePath.c_str(), -1, strOleFilePath, MAX_PATH );
//
得到文件路径对应的ItemIDList
LPITEMIDLIST
pItemIDList = NULL;
hr
= pDesktopFolder->ParseDisplayName( NULL, NULL, strOleFilePath, NULL, &pItemIDList, NULL );
pDesktopFolder->Release();
if
( FAILED(hr) )
{
return
NULL;
}
return
pItemIDList;
}
// CFileBrowser dialog
class CFileBrowser : public CDialog
{
public:
// Dialog Data
protected:
private:
protected:
public:
};
// FileBrowser.cpp : implementation file
//
#include "stdafx.h"
#include
#include
#include "FileBrowser.h"
#pragma comment( lib, "shell32.lib")
// CFileBrowser dialog
IMPLEMENT_DYNAMIC(CFileBrowser, CDialog)
CFileBrowser::CFileBrowser(CWnd* pParent )
{
}
CFileBrowser::~CFileBrowser()
{
}
void CFileBrowser::DoDataExchange(CDataExchange* pDX)
{
}
BEGIN_MESSAGE_MAP(CFileBrowser, CDialog)
END_MESSAGE_MAP()
void CFileBrowser::GetUserDocumentPath()
{
}
// CFileBrowser message handlers
void CFileBrowser::OnBnClickedOk()
{
}
void CFileBrowser::OnBnClickedCancel()
{
}
CString CFileBrowser::GetProjectName()
{
}
CString CFileBrowser::GetProjectPath()
{
}
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
}
void CFileBrowser::OnBnClickedButtonBroswer
{
}
BOOL CFileBrowser::OnInitDialog()
{
}
LPITEMIDLIST CFileBrowser::GetItemIDListFromFilePat
{
}