mfc_Drag_Drop_3 - 8BitsCoding/RobotMentor GitHub Wiki



ListBox์˜ OwnerDraw = Fixed, Has String = True ๋ณ€๊ฒฝ

TW_ListBox ํด๋ž˜์Šค๋ฅผ ์ธํด๋ฃจ๋“œ


์„œ๋ธŒํด๋ž˜์‹ฑ

#include "TW_ListBox.h"

class TW_FileListBox : public TW_ListBox
{
	virtual void DrawUserItem(CDC *ap_dc, RECT *ap_rect, int a_index, void * ap_data, unsigned char a_select_flag, unsigned char a_focus_flag)
	{
		CString str;
		GetText(a_index, str);

		if (GetItemData(a_index)) ap_dc->SetTextColor(RGB(255, 255, 128));
		else ap_dc->SetTextColor(RGB(128, 200, 200));

		ap_dc->TextOut(ap_rect->left + 5, ap_rect->top + 3, str);
	}
};
void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_DROP_LIST, m_drop_list);
}
BOOL CMFCApplication1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// ์ด ๋Œ€ํ™” ์ƒ์ž์˜ ์•„์ด์ฝ˜์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.  ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์ฃผ ์ฐฝ์ด ๋Œ€ํ™” ์ƒ์ž๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ์—๋Š”
	//  ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ด ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
	SetIcon(m_hIcon, TRUE);			// ํฐ ์•„์ด์ฝ˜์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
	SetIcon(m_hIcon, FALSE);		// ์ž‘์€ ์•„์ด์ฝ˜์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

	// TODO: ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

	m_drop_list.SubclassDlgItem(IDC_DROP_LIST, this);

	return TRUE;  // ํฌ์ปค์Šค๋ฅผ ์ปจํŠธ๋กค์— ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด TRUE๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
}

๊ณตํ†ต๊ฒฝ๋กœ ํ™•์ธํ•˜๊ธฐ

void CMFCApplication1Dlg::OnDropFiles(HDROP hDropInfo)
{
	m_drop_list.ResetContent();

	int count = DragQueryFile(hDropInfo, 0xFFFFFFFF/*-1*/, NULL, 0);

	wchar_t temp_path[MAX_PATH];

	if (count > 0) {
		int len = DragQueryFile(hDropInfo, 0, temp_path, MAX_PATH);
		if (len > 3) {
			wchar_t *p = temp_path + len;
			while (*p != '\\') p--;
			*(p + 1) = 0;
			SetDlgItemText(IDC_PATH_EDIT, temp_path);
		}
	}

	for (int i = 0; i < count; i++) {
		DragQueryFile(hDropInfo, i, temp_path, MAX_PATH);
		m_drop_list.InsertString(i, temp_path);
		/*
		if (GetFileAttributes(temp_path) & FILE_ATTRIBUTE_DIRECTORY) {
			// ๋””๋ ‰ํ† ๋ฆฌ๋ผ๋ฉด 1
			m_drop_list.SetItemData(i, 1);
		}
		else {
			// ํŒŒ์ผ์ด๋ผ๋ฉด 0
			m_drop_list.SetItemData(i, 0);
		}
		*/
		// ์†Œ์Šค๊ฐ„๋‹จํ™”
		m_drop_list.SetItemData(i, (GetFileAttributes(temp_path) & FILE_ATTRIBUTE_DIRECTORY) >> 4);
	}

	MakeFileList();

	CDialogEx::OnDropFiles(hDropInfo);
}

๊ณตํ†ต๊ฒฝ๋กœ ์ œ์™ธํ•˜๊ธฐ

class TW_FileListBox : public TW_ListBox
{
private:
	char m_simple_mode = 0;
	wchar_t m_base_path[MAX_PATH];
	int m_path_len;

public:
	void SetBasePath(const wchar_t * ap_path, int a_length) 
	{
		m_path_len = a_length;
		memcpy(m_base_path, ap_path, a_length + 1);
	}

	void SetSimpleMode(char a_is_simple)
	{
		if (m_simple_mode != a_is_simple) {
			m_simple_mode = a_is_simple;
			Invalidate();
		}
	}

	virtual void DrawUserItem(CDC *ap_dc, RECT *ap_rect, int a_index, void * ap_data, unsigned char a_select_flag, unsigned char a_focus_flag)
	{
		CString str;
		GetText(a_index, str);

		if (GetItemData(a_index)) ap_dc->SetTextColor(RGB(255, 255, 128));
		else ap_dc->SetTextColor(RGB(128, 200, 200));

		if (m_simple_mode) {
			ap_dc->TextOut(ap_rect->left + 5, ap_rect->top + 3, (const wchar_t*)str + m_path_len, str.GetLength() - m_path_len);
		} else ap_dc->TextOut(ap_rect->left + 5, ap_rect->top + 3, str);
	}
};
void CMFCApplication1Dlg::OnBnClickedHidePathCheck()
{
	m_drop_list.SetSimpleMode(m_hide_path_check.GetCheck());
}
void CMFCApplication1Dlg::OnDropFiles(HDROP hDropInfo)
{
	m_drop_list.ResetContent();

	int count = DragQueryFile(hDropInfo, 0xFFFFFFFF/*-1*/, NULL, 0);

	wchar_t temp_path[MAX_PATH];

	if (count > 0) {
		int len = DragQueryFile(hDropInfo, 0, temp_path, MAX_PATH);
		if (len > 3) {
			wchar_t *p = temp_path + len;
			while (*p != '\\') p--;
			*(p + 1) = 0;
			SetDlgItemText(IDC_PATH_EDIT, temp_path);
			m_drop_list.SetBasePath(temp_path, p - temp_path + 1);
		}
	}

	for (int i = 0; i < count; i++) {
		DragQueryFile(hDropInfo, i, temp_path, MAX_PATH);
		m_drop_list.InsertString(i, temp_path);
		/*
		if (GetFileAttributes(temp_path) & FILE_ATTRIBUTE_DIRECTORY) {
			// ๋””๋ ‰ํ† ๋ฆฌ๋ผ๋ฉด 1
			m_drop_list.SetItemData(i, 1);
		}
		else {
			// ํŒŒ์ผ์ด๋ผ๋ฉด 0
			m_drop_list.SetItemData(i, 0);
		}
		*/
		// ์†Œ์Šค๊ฐ„๋‹จํ™”
		m_drop_list.SetItemData(i, (GetFileAttributes(temp_path) & FILE_ATTRIBUTE_DIRECTORY) >> 4);
	}

	MakeFileList();

	CDialogEx::OnDropFiles(hDropInfo);
}