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);
}