1. 程式人生 > >支援選擇檔案和目錄(可多選)的檔案對話方塊CSelectDialog

支援選擇檔案和目錄(可多選)的檔案對話方塊CSelectDialog

MFC自帶的CFileDialog不支援選擇目錄,而且多選檔案配置也不方便。有一些實現比較好的目錄選擇對話方塊,但是既可以支援選擇檔案,又可以支援選擇目錄的非常難得。非常感謝Hojjat Bohlooli([email protected])的工作,給我們提供了一個非常方便的檔案選擇對話方塊CSelectDialog。國人自己也能有些拿得出手的東西,而不是藏藏捏捏的。

其使用也是非常簡單的。

CSelectDialog ofd(TRUE, _T("*.*"), NULL, OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, _T("All files and folders(*.*)|*.*||") );

if( ofd.DoModal() != IDOK )
	return;

for( int i=0; i<ofd.m_SelectedItemList.GetCount(); i++ ){
	// 
}

詳細的實現如下:

#pragma once

#include <dlgs.h> // for (MULTI)FILEOPENORD

// CSelectDialog
class CSelectDialog : public CFileDialog
{
	DECLARE_DYNAMIC(CSelectDialog)

public:
	CSelectDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
		LPCTSTR lpszDefExt = NULL,
		LPCTSTR lpszFileName = NULL,
		DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
						OFN_EXPLORER & (~OFN_SHOWHELP),
		LPCTSTR lpszFilter = NULL,
		CWnd* pParentWnd = NULL);
	virtual ~CSelectDialog();
	
protected:
	virtual void OnInitDone();
	virtual void OnFolderChange();
	virtual BOOL OnFileNameOK();
	static LRESULT CALLBACK WindowProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam);
	DECLARE_MESSAGE_MAP()

public:
	static CString m_strCurrendDirectory;
	static CStringArray m_SelectedItemList;	/*this list includes files and folders
											are selected by user. */
	static WNDPROC m_wndProc;
};

#include "stdafx.h"
#include "SelectDialog.h"

#pragma warning( push )
#pragma warning( disable : 4311 4312 )
// CSelectDialog
CString CSelectDialog::m_strCurrendDirectory;
CStringArray CSelectDialog::m_SelectedItemList;
WNDPROC CSelectDialog::m_wndProc = NULL;

IMPLEMENT_DYNAMIC(CSelectDialog, CFileDialog)

CSelectDialog::CSelectDialog(BOOL bOpenFileDialog,
							 LPCTSTR lpszDefExt,
							 LPCTSTR lpszFileName,
							 DWORD dwFlags,
							 LPCTSTR lpszFilter,
							 CWnd* pParentWnd)
							 :CFileDialog(
							 bOpenFileDialog,
							 lpszDefExt,
							 lpszFileName,
							 dwFlags | OFN_EXPLORER | OFN_HIDEREADONLY & (~OFN_SHOWHELP),
							 lpszFilter,
							 pParentWnd)
{
	dwFlags |= (OFN_EXPLORER | OFN_HIDEREADONLY & (~OFN_SHOWHELP));
};

CSelectDialog::~CSelectDialog()
{
};

BEGIN_MESSAGE_MAP(CSelectDialog, CFileDialog)
END_MESSAGE_MAP()

// CSelectDialog message handlers
BOOL CSelectDialog::OnFileNameOK()
{
	if (CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(GetParent()->m_hWnd))
	{
		CWnd* pWnd = pDlg->GetDlgItem(lst2);	//getting list
		if (pWnd == NULL)
			return FALSE;

		m_SelectedItemList.RemoveAll();			// emptying list
		
		CListCtrl* wndLst1 = (CListCtrl*)(pWnd->GetDlgItem(1));

		int nSelected = wndLst1->GetSelectedCount();
		if (!nSelected)		// nothing selected -- don't retrieve list
			return FALSE;
		CString strItemText, strDirectory = m_strCurrendDirectory;
		if (strDirectory.Right(1) != _T("\\"))
			strDirectory += _T("\\");

		CString fileslist = _T("");
		pDlg->SendMessage(CDM_GETSPEC, (WPARAM)MAX_PATH,
			(LPARAM)fileslist.GetBuffer(MAX_PATH));
		fileslist.ReleaseBuffer();

		strItemText = strDirectory + fileslist;
		if(nSelected == 1 && fileslist != _T(""))
		{
			m_SelectedItemList.Add(strItemText);
			return CFileDialog::OnFileNameOK();
		}
	}
	::MessageBeep( MB_ICONQUESTION );
	return 1; //don't let the dialog to close
};

void CSelectDialog::OnFolderChange()
{
	m_strCurrendDirectory = GetFolderPath();
	CFileDialog::OnFolderChange();
};

void CSelectDialog::OnInitDone()
{
	m_strCurrendDirectory = GetFolderPath();
	CWnd* pFD = GetParent();

	HideControl(edt1);
	HideControl(cmb1);
	HideControl(stc2);

	//HideControl(cmb13);
	//HideControl(stc3);

	CRect rectCancel; pFD->GetDlgItem(IDCANCEL)->GetWindowRect(&rectCancel);
	pFD->ScreenToClient(&rectCancel);

	CRect rectOK; pFD->GetDlgItem(IDOK)->GetWindowRect(&rectOK);
	pFD->ScreenToClient(&rectOK);
	pFD->GetDlgItem(IDOK)->SetWindowPos(0,rectCancel.left - rectOK.Width() - 5, rectCancel.top, 0,0, SWP_NOZORDER | SWP_NOSIZE);

	CRect rectList2; pFD->GetDlgItem(lst1)->GetWindowRect(&rectList2);
	pFD->ScreenToClient(&rectList2);
	pFD->GetDlgItem(lst1)->SetWindowPos(0,0,0,rectList2.Width(), abs(rectList2.top - (rectCancel.top - 5)), SWP_NOMOVE | SWP_NOZORDER);

	CRect rectStatic;pFD->GetDlgItem(stc3)->GetWindowRect(&rectStatic);
	pFD->ScreenToClient(&rectStatic);
	pFD->GetDlgItem(stc3)->SetWindowPos(0,rectCancel.left - 375,rectCancel.top + 5, rectStatic.Width(), rectStatic.Height(), SWP_NOZORDER);

	CRect rectEdit1;pFD->GetDlgItem(cmb13)->GetWindowRect(&rectEdit1);
	pFD->ScreenToClient(&rectEdit1);
	pFD->GetDlgItem(cmb13)->SetWindowPos(0,rectCancel.left - 320,rectCancel.top, rectEdit1.Width() - 15, rectEdit1.Height(), SWP_NOZORDER);

	SetControlText(stc3, _T("Item name:"));
	SetControlText(IDOK, _T("Select"));

	m_wndProc = (WNDPROC)::SetWindowLong(pFD->m_hWnd, GWL_WNDPROC, (long)WindowProcNew);
	pFD->CenterWindow();
};

LRESULT CALLBACK CSelectDialog::WindowProcNew(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message ==  WM_COMMAND)
	{
		if (HIWORD(wParam) == BN_CLICKED)
		{
			if (LOWORD(wParam) == IDOK)
			{
				if (CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle(hwnd))
				{
					m_SelectedItemList.RemoveAll();			// emptying list
					CWnd* pWnd = pDlg->GetDlgItem(lst2);	//getting list
					if (pWnd == NULL)
						return FALSE;

					CListCtrl* wndLst1 = (CListCtrl*)(pWnd->GetDlgItem(1));

					int nSelected = wndLst1->GetSelectedCount();
					if (!nSelected)		// nothing selected -- don't retrieve list
						return FALSE;
					CString strItemText, strDirectory = m_strCurrendDirectory;
					if (strDirectory.Right(1) != _T("\\"))
						strDirectory += _T("\\");

					int nItem = wndLst1->GetNextItem(-1,LVNI_SELECTED);
					CString fileslist = _T("");
					pDlg->SendMessage(CDM_GETSPEC, (WPARAM)MAX_PATH,
						(LPARAM)fileslist.GetBuffer(MAX_PATH));
					fileslist.ReleaseBuffer();
					//////////////////   Add directory names to list
					while((nSelected--) > 0)
					{
						strItemText = wndLst1->GetItemText(nItem,0);
						strItemText = strDirectory + strItemText;
						DWORD attr = GetFileAttributes(strItemText);
						if((attr != 0xFFFFFFFF) && (attr & FILE_ATTRIBUTE_DIRECTORY))
							m_SelectedItemList.Add(strItemText);							
						nItem = wndLst1->GetNextItem(nItem, LVNI_SELECTED);
					}
					//////////////////   Add FILE names to list
					strItemText = _T("");
					nSelected = wndLst1->GetSelectedCount();
					if(nSelected > m_SelectedItemList.GetCount())
					{
						int MoreThanOnFile = fileslist.Find(_T("\""));
						if(MoreThanOnFile != -1)
						{
							for(int i=0; i<fileslist.GetLength(); i++)
								if(fileslist[i] != '\"')
								{
									strItemText.AppendFormat(_T("%c"),fileslist[i]);
									if(fileslist[i-1] == '\"' && fileslist[i] == ' ')
										strItemText.Delete(strItemText.GetLength()-1);
								}
								else if(!strItemText.IsEmpty())
								{
									m_SelectedItemList.Add((strDirectory+strItemText));
									strItemText.Empty();
								}
						}
						else
							m_SelectedItemList.Add(strDirectory+fileslist);
					}
					pDlg->EndDialog(IDOK);
					return NULL;
				} // if IDOK
			}
		} // if BN_CLICKED
	}// if WM_COMMAND
	return CallWindowProc(m_wndProc, hwnd, message, wParam, lParam);
}

#pragma warning( pop )

相關推薦

支援選擇檔案目錄檔案對話方塊CSelectDialog

MFC自帶的CFileDialog不支援選擇目錄,而且多選檔案配置也不方便。有一些實現比較好的目錄選擇對話方塊,但是既可以支援選擇檔案,又可以支援選擇目錄的非常難得。非常感謝Hojjat Bohlooli([email protected])的工作,給我們提供了一

Unix/Linux程式設計-檔案目錄

檔案和目錄(二) 4.8 更改檔案實際使用者ID和實際組ID #include <unistd.h> int  chown(const char *pathname, uid_t owner, gid_t group);

Unix/Linux程式設計-檔案目錄

檔案和目錄(一) 4.1 4個stat函式 #include <sys/stat.h> int  stat(const char *pathname, struct stat *buf); int  fstat(int

04_檔案目錄理解

檔案和目錄(理解) 目標 理解 Linux 檔案目錄的結構 01. 單使用者作業系統和多使用者作業系統(科普) 單使用者作業系統:指一臺計算機在同一時間 只能由一個使用者 使用,一個使用者獨自享用系統的全部硬體和軟體資源 Windows XP&nb

檔案目錄理解

目標 理解 Linux 檔案目錄的結構 01. 單使用者作業系統和多使用者作業系統(科普) 單使用者作業系統:指一臺計算機在同一時間 只能由一個使用者 使用,一個使用者獨自享用系統的全部硬體和軟體資源 Windows XP 之前的版本都是單使

設定SVN忽略檔案目錄資料夾

在多數專案中你總會有檔案和目錄不需要進行版本控制。這可能包括一些由編譯器生成的檔案,*.obj,*.lst,或許是一個用於存放可執行程式的輸出資料夾。只要你提交修改,TortoiseSVN 就會在提交對話方塊的檔案列表中顯示出未版本控制檔案。當然你可以關閉這個顯示,不過你可

檔案目錄--unix環境高階程式設計

     普通檔案和目錄linux中最多的兩類檔案,linux中一共有七種型別的檔案,如下:1.普通檔案 2.目錄 3.字元特殊裝置 4.塊特殊裝置 5.FIFO,又叫命名管道 6.Socket,即套接字 7.符號連結 獲取一個檔案的詳細資訊可以使用stat函式組,stat

用ECharts畫柱子柱狀圖

其實用ECharts畫柱子還是挺簡單的,多去官網看看官方文件,就理解了。 columLabel是多柱子的名字。columName 是橫座標。bgColorList 是柱子的顏色。arr 是每個柱子的值。list 是後臺傳過來的各個柱子的值,但是要經過處理賦值給arr ,賦值的方法也不只這一種

Android 給TextView 中 部分文字加下劃線 並加入超連結

Android 本身自帶的TextView 並沒有直接的方法可以給文字加下劃線和可點選的超連結,使用以下方法即可實現: /** * * @param content 文字內容 * @param textView 載入文字的textview *

C#-XML檔案提取字串+字串存為XML檔案+建立XML自定義節點檔案+讀取節點內容

一、將字串寫入xml檔案(並儲存) 寫入:  XmlDocument xdoc = new XmlDocument();  xdoc.LoadXml(“xmlstring”); 儲存:  xdoc.Save(“pathsave.xml”) 二、將

Python Selenium專案實戰—— 怎麼去驗證一個按鈕是啟用的

Q: 使用 Python Selenium WebDriver 怎麼去驗證一個按鈕是啟用的(可點選)? A:Selenium WebDriver API 裡面給出瞭解決方法is_enabled() 使用WebDriver API —— driver.find_element_by_css_selector()

C#--第12周實驗--任務2設計一個窗體--開啟對話方塊

/* (程式頭部註釋開始) * 程式的版權和版本宣告部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 檔名稱:訊息對話方塊 * 作 者: 雷恆鑫 * 完成日期: 2

一:檔案目錄5——搜尋檔案

動作 roo 速度 ket 有關 -name comm 找不到 ont 1.搜尋執行檔——which which指令主要是在PATH所包含的目錄中去搜尋執行檔的檔名,若加上-a參數就可以搜尋除所有包含檔案的信息 上面列出root賬號的PATH包含的目錄,which命令會在

Linux學習筆記之1——檔案目錄管理硬連線軟連線(連結檔,相當於快捷方式

在這節將要學習linux的連線檔,在之前用"ls -l" 檢視檔案屬性的命令時, 其中第二個屬性是連線數。那麼這個連線數是幹什麼的?這就要理解inode。     先說一下檔案是怎麼儲存的。檔案儲存在硬碟上,硬碟的最小儲存單位叫做"扇區"(Sector),每個扇區儲存512位元

實現評論頁面的五星評價圖片選擇拖動

先上圖: https://github.com/simonFong/CommentDemo 想用的直接到github下載就可以了,星星控制元件和新增圖片的控制元件在imageadd的lib裡 使用方法: 1.下載lib,匯入自己的工程 2.星星控制元件 直接在自己的佈局檔案裡新增

《Linux程式設計》第三章標準IO庫、格式化輸入輸出、檔案目錄的維護、掃描目錄

標準IO庫 在啟動程式時,有三個檔案流是自動開啟的,分別是stdin,stdout,stderr。 1. fopen函式:用於檔案和終端的輸入和輸出。函式原型如下: #include <stdio.h> FILE *fopen(const char* f

python檔案目錄操作方法大全含例項

一、python中對檔案、資料夾操作時經常用到的os模組和shutil模組常用方法。 1.得到當前工作目錄,即當前Python指令碼工作的目錄路徑: os.getcwd() 2.返回指定目錄下的所有檔案和目錄名:os.listdir() 3.函式用來刪除一個檔案:os.rem

JAVA輸入/輸出流程式例題檔案目錄、位元組流、字元流

一.檔案和目錄 1.顯示檔案的基本資訊。 2.顯示目錄的基本資訊。 3.在指定目錄下建立單個檔案。 4.指定目錄下建立多個臨時檔案。 二、位元組流 1.生成ZIP壓縮檔案 2.解壓縮zip檔案 3.生成Excel檔案 4.讀取excel檔案 5.生成PDF檔案 6.讀取P

[Linux]Samba伺服器支援訪問軟連線檔案目錄

原創文章,歡迎轉載。轉載請註明:轉載自 祥的部落格 原文連結:https://blog.csdn.net/humanking7/article/details/85058471 文章目錄 @[toc] 1.

SpringBoot檔案上傳下載檔案上傳圖文詳解

最近在學習SpringBoot,以下是最近學習整理的實現檔案上傳下載的java程式碼: 1、開發環境: IDEA15+ Maven+JDK1.8 2、新建一個maven工程: 3、工程框架 4、pom.xml檔案依賴項 <proje