리스트 컨트롤에 데이터 추가하기
#include <windows.h> #include <TCHAR.H> #include <commctrl.h> #include "resource.h" ... void MakeColumn(HWND hDlg); void InsertData(HWND hDlg); ... BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_INITDIALOG: MakeColumn(hDlg); return 1; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON_INSERT: InsertData(hDlg); return 0; case IDOK: EndDialog(hDlg, 0); break; case IDCANCEL: EndDialog(hDlg, 0); break; } break; } return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE: break; case WM_LBUTTONDOWN: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); }
void MakeColumn(HWND hDlg) { LPCTSTR name[2] = { _T("이름"), _T("전화번호") }; LVCOLUMN lvCol = { 0, }; HWND hList; int i; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvCol.fmt = LVCFMT_LEFT;
for (i = 0; i < 2; i++) { lvCol.cx = 90; lvCol.iSubItem = i; lvCol.pszText = (LPTSTR)name[i]; SendMessage(hList, LVM_INSERTCOLUMN, (WPARAM)i, (LPARAM)&lvCol); } }
void InsertData(HWND hDlg) { LVITEM item; HWND hList; LPCTSTR name[20] = { _T("김철수"), _T("김영희") }; LPCTSTR phone[20] = { _T("010-2573-8574"), _T("011-9872-5867") }; int i; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER);
for (i = 0; i < 2; i++) { item.mask = LVIF_TEXT; item.iItem = i; item.iSubItem = 0; item.pszText = (LPTSTR)name[i]; ListView_InsertItem(hList, &item); ListView_SetItemText(hList, i, 1, (LPTSTR)phone[i]); } } |
실행결과]
리스트 컨트롤에 데이터 추가하기
#include <windows.h> #include <TCHAR.H> #include <commctrl.h> #include "resource.h" ... void MakeColumn(HWND hDlg); void InsertData(HWND hDlg); ... BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_INITDIALOG: MakeColumn(hDlg); return 1; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON_INSERT: InsertData(hDlg); return 0; case IDOK: EndDialog(hDlg, 0); break; case IDCANCEL: EndDialog(hDlg, 0); break; } break; } return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE: break; case WM_LBUTTONDOWN: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); }
void MakeColumn(HWND hDlg) { LPCTSTR name[2] = { _T("이름"), _T("전화번호") }; LVCOLUMN lvCol = { 0, }; HWND hList; int i; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvCol.fmt = LVCFMT_LEFT;
for (i = 0; i < 2; i++) { lvCol.cx = 90; lvCol.iSubItem = i; lvCol.pszText = (LPTSTR)name[i]; SendMessage(hList, LVM_INSERTCOLUMN, (WPARAM)i, (LPARAM)&lvCol); } }
void InsertData(HWND hDlg) { int count; LVITEM item; HWND hList; TCHAR name[20], phone[20];
GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 20); SetDlgItemText(hDlg, IDC_EDIT_NAME, _T("")); if (_tcscmp(name, _T("")) == 0) GetDlgItemText(hDlg, IDC_EDIT_PHONE, phone, 20); SetDlgItemText(hDlg, IDC_EDIT_PHONE, _T(""));
hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); count = ListView_GetItemCount(hList); item.mask = LVIF_TEXT; item.iItem = count; item.iSubItem = 0; item.pszText = name; ListView_InsertItem(hList, &item); ListView_SetItemText(hList, count, 1, phone); } |
실행결과]
리스트 컨트롤에 있는 항목을 선택하면 에디트 박스에 출력하기
int SelectItem(HWND hDlg, LPARAM lParam); ... BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { LPNMHDR hdr; HWND hList;
switch (iMsg) { case WM_INITDIALOG: MakeColumn(hDlg); return 1; case WM_NOTIFY: hdr = (LPNMHDR)lParam; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); if (hdr->hwndFrom == hList && hdr->code == LVN_ITEMCHANGING) return 1; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON_INSERT: InsertData(hDlg); return 0; case IDOK: EndDialog(hDlg, 0); break; case IDCANCEL: EndDialog(hDlg, 0); break; } break; } return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE: break; case WM_LBUTTONDOWN: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); } ... int SelectItem(HWND hDlg, LPARAM lParam) { LPNMLISTVIEW nlv; HWND hList; TCHAR name[20], phone[20]; nlv = (LPNMLISTVIEW)lParam;
SetDlgItemText(hDlg, IDC_EDIT_NAME, name); SetDlgItemText(hDlg, IDC_EDIT_PHONE, phone);
}
|
실행결과]
리스트 컨트롤에 있는 항목 수정 및 삭제하기
void ModifyItem(HWND hDlg, int selection); void DeleteItem(HWND hDlg, int selection); #define UNSELECTED -1
BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { LPNMHDR hdr; HWND hList; static int selection;
switch (iMsg) { case WM_INITDIALOG: MakeColumn(hDlg); selection = UNSELECTED; return 1; case WM_NOTIFY: hdr = (LPNMHDR)lParam; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); if (hdr->hwndFrom == hList && hdr->code == LVN_ITEMCHANGING) return 1; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON_INSERT: InsertData(hDlg); return 0; case IDC_BUTTON_MODIFY: if (selection == UNSELECTED) break; ModifyItem(hDlg, selection); selection = UNSELECTED; return 0; case IDC_BUTTON_DELETE: if (selection == UNSELECTED) break; DeleteItem(hDlg, selection); selection = UNSELECTED; return 0; case IDOK: EndDialog(hDlg, 0); break; case IDCANCEL: EndDialog(hDlg, 0); break; } break; } return 0; } void ModifyItem(HWND hDlg, int selection) { static HWND hList; TCHAR name[20], phone[20]; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 20); GetDlgItemText(hDlg, IDC_EDIT_PHONE, phone, 20); if (_tcscmp(name, _T("")) == 0) return; ListView_SetItemText(hList, selection, 0, name); ListView_SetItemText(hList, selection, 1, phone); SetDlgItemText(hDlg, IDC_EDIT_NAME, _T("")); SetDlgItemText(hDlg, IDC_EDIT_PHONE, _T("")); return; }
void DeleteItem(HWND hDlg, int selection) { static HWND hList; hList = GetDlgItem(hDlg, IDC_LIST_MEMBER); ListView_DeleteItem(hList, selection); SetDlgItemText(hDlg, IDC_EDIT_NAME, _T("")); SetDlgItemText(hDlg, IDC_EDIT_PHONE, _T("")); return; } |
실행결과]