win32 commctrl
include <windows.h>
include <tchar.h>
include "resource.h"
include <commctrl.h>
using namespace std;
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DialogProc1(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
void MakeColumn(HWND hDlg);
HINSTANCE hInst;
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
hInst = hInstance;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = _T("Window Class Name");
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
RegisterClassEx(&wcex);
hwnd = CreateWindow(_T("Window Class Name"), _T("제목표시줄"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_P1:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc);
break;
case ID_P2:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hwnd, DialogProc1);
break;
}
break;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
void InsertData(HWND hList, int nItem, int nSubItem, const TCHAR* strItem)
{
LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = nItem;
lvItem.iSubItem = nSubItem;
lvItem.pszText = (LPWSTR)strItem;
if (nSubItem == 0)
ListView_InsertItem(hList, &lvItem);
else
ListView_SetItem(hList, &lvItem);
}
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hList;
static int iRow;
static LPNMHDR hdr;
static LPNMLISTVIEW nListView;
static int selection;
switch (iMsg)
{
case WM_NOTIFY:
hdr = (LPNMHDR)lParam;
if (hdr->hwndFrom == hList && hdr->code == LVN_ITEMCHANGING)
{
nListView = (LPNMLISTVIEW)lParam;
TCHAR comp[30];
TCHAR tel[30];
TCHAR name[30];
TCHAR pos[30];
ListView_GetItemText(hList, nListView->iItem, 0, comp, 30);
ListView_GetItemText(hList, nListView->iItem, 1, tel, 30);
ListView_GetItemText(hList, nListView->iItem, 2, name, 30);
ListView_GetItemText(hList, nListView->iItem, 3, pos, 30);
SetDlgItemText(hDlg, IDC_EDIT_COM, comp);
SetDlgItemText(hDlg, IDC_EDIT_TEL, tel);
SetDlgItemText(hDlg, IDC_EDIT_NAME, name);
SetDlgItemText(hDlg, IDC_EDIT_POS, pos);
selection = nListView -> iItem;
}
break;
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
case WM_INITDIALOG:
{
selection = -1;
hList = GetDlgItem(hDlg, IDC_LIST_NAMECARD);
RECT rect;
GetClientRect(hList, &rect);
const TCHAR* title[4] = { _T("회사명"),_T("전화번호"),_T("이름"),_T("직급") };
int nWidth[4] = { rect.right * 0.25 ,rect.right * 0.25 ,rect.right * 0.25 ,rect.right * 0.25 };
LVCOLUMN lvCol;
for (int i = 0; i < 4; i++)
{
lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvCol.fmt = LVCFMT_CENTER;
lvCol.cx = nWidth[i];
lvCol.pszText = (LPTSTR)title[i];
lvCol.iSubItem = i;
ListView_InsertColumn(hList, i, &lvCol);
}
ListView_SetExtendedListViewStyle
(hList, ListView_GetExtendedListViewStyle(hList) | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON_MODIFY:
if (selection == -1)
break;
TCHAR comp[30];
TCHAR tel[30];
TCHAR name[30];
TCHAR pos[30];
GetDlgItemText(hDlg, IDC_EDIT_COM, comp, 30);
GetDlgItemText(hDlg, IDC_EDIT_TEL, tel, 30);
GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 30);
GetDlgItemText(hDlg, IDC_EDIT_POS, pos, 30);
ListView_SetItemText(hList, selection, 0, comp);
ListView_SetItemText(hList, selection, 1, tel);
ListView_SetItemText(hList, selection, 2, name);
ListView_SetItemText(hList, selection, 3, pos);
SetDlgItemText(hDlg, IDC_EDIT_COM, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_TEL, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_NAME, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_POS, _T(""));
selection = -1;
break;
case IDC_BUTTON_DELETE:
if (selection == -1)
break;
ListView_DeleteItem(hList, selection);
//에디트 클리어
SetDlgItemText(hDlg, IDC_EDIT_COM, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_TEL, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_NAME, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_POS, _T(""));
selection = -1;
break;
case IDC_BUTTON_INSERT:
{
TCHAR comp[30];
TCHAR tel[30];
TCHAR name[30];
TCHAR pos[30];
GetDlgItemText(hDlg, IDC_EDIT_COM, comp, 30);
GetDlgItemText(hDlg, IDC_EDIT_TEL, tel, 30);
GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 30);
GetDlgItemText(hDlg, IDC_EDIT_POS, pos, 30);
iRow = ListView_GetItemCount(hList);
InsertData(hList, iRow, 0, comp);
InsertData(hList, iRow, 1, tel);
InsertData(hList, iRow, 2, name);
InsertData(hList, iRow, 3, pos);
iRow++;
SetDlgItemText(hDlg, IDC_EDIT_COM, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_TEL, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_NAME, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_POS, _T(""));
break;
}
case IDC_BUTTON_END:
EndDialog(hDlg, 0);
break;
}
}
return 0;
}
INT_PTR CALLBACK DialogProc1(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hList, hCombo;
static int selection, iRow, Radio;
TCHAR sex[2][30] = { _T("여자"), _T("남자") };
static LPNMHDR hdr;
static LPNMLISTVIEW nListView;
switch (iMsg)
{
case WM_INITDIALOG:
{
selection = -1;
Radio = -1;
hList = GetDlgItem(hDlg, IDC_LIST_MEMBER1);
hCombo = GetDlgItem(hDlg, IDC_COMBO_YEAR);
TCHAR num_char[30];
for (int i = 1970; i < 2022; i++)
{
_stprintf_s(num_char, _countof(num_char), _T("%d"), i);
SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)num_char);
}
MakeColumn(hDlg);
ListView_SetExtendedListViewStyle
(hList, ListView_GetExtendedListViewStyle(hList) | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
break;
}
case WM_NOTIFY:
{
hdr = (LPNMHDR)lParam;
if (hdr->hwndFrom == hList && hdr->code == LVN_ITEMCHANGING)
{
nListView = (LPNMLISTVIEW)lParam;
TCHAR name[30];
TCHAR tel[30];
TCHAR radio[30];
TCHAR year[30];
ListView_GetItemText(hList, nListView->iItem, 0, name, 30);
ListView_GetItemText(hList, nListView->iItem, 1, tel, 30);
ListView_GetItemText(hList, nListView->iItem, 2, radio, 30);
ListView_GetItemText(hList, nListView->iItem, 3, year, 30);
SetDlgItemText(hDlg, IDC_EDIT_NAME, name);
SetDlgItemText(hDlg, IDC_EDIT_TEL, tel);
if ((_tcscmp(radio, _T("남자"))) == 0)
{
CheckDlgButton(hDlg, IDC_RADIO_MALE, BST_CHECKED);
CheckDlgButton(hDlg, IDC_RADIO_FEMALE, BST_UNCHECKED);
}
else
{
CheckDlgButton(hDlg, IDC_RADIO_FEMALE, BST_CHECKED);
CheckDlgButton(hDlg, IDC_RADIO_MALE, BST_UNCHECKED);
}
SetDlgItemText(hDlg, IDC_COMBO_YEAR, year);
selection = nListView->iItem;
}
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON_NEW1:
//초기화
SetDlgItemText(hDlg, IDC_EDIT_NAME, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_TEL, _T(""));
SendMessage(hCombo, CB_SETCURSEL, -1, 0);
CheckDlgButton(hDlg, IDC_RADIO_FEMALE, BST_UNCHECKED);
CheckDlgButton(hDlg, IDC_RADIO_MALE, BST_UNCHECKED);
Radio = -1;
break;
case IDC_BUTTON_INSERT1:
{
TCHAR name[30];
TCHAR tel[30];
TCHAR year[10];
GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 30);
GetDlgItemText(hDlg, IDC_EDIT_TEL, tel, 30);
GetDlgItemText(hDlg, IDC_COMBO_YEAR, year, 10);
iRow = ListView_GetItemCount(hList);
InsertData(hList, iRow, 0, name);
InsertData(hList, iRow, 1, tel);
InsertData(hList, iRow, 2, sex[Radio]);
InsertData(hList, iRow, 3, year);
iRow++;
break;
}
case IDC_BUTTON_DELETE1:
{
if (selection == -1)
break;
ListView_DeleteItem(hList, selection);
SetDlgItemText(hDlg, IDC_EDIT_NAME, _T(""));
SetDlgItemText(hDlg, IDC_EDIT_TEL, _T(""));
SendMessage(hCombo, CB_SETCURSEL, -1, 0);
CheckDlgButton(hDlg, IDC_RADIO_FEMALE, BST_UNCHECKED);
CheckDlgButton(hDlg, IDC_RADIO_MALE, BST_UNCHECKED);
selection = -1;
break;
}
case IDC_BUTTON_MODIFY1:
{
if (selection == -1)
break;
TCHAR name[30];
TCHAR tel[30];
TCHAR year[10];
GetDlgItemText(hDlg, IDC_EDIT_NAME, name, 30);
GetDlgItemText(hDlg, IDC_EDIT_TEL, tel, 30);
GetDlgItemText(hDlg, IDC_COMBO_YEAR, year, 10);
ListView_SetItemText(hList, selection, 0, name);
ListView_SetItemText(hList, selection, 1, tel);
if (Radio == 0)
{
ListView_SetItemText(hList, selection, 2, (LPWSTR)_T("여자"));
}
else if (Radio == 1)
{
ListView_SetItemText(hList, selection, 2, (LPWSTR)_T("남자"));
}
ListView_SetItemText(hList, selection, 3, year);
selection = -1;
break;
}
case IDC_RADIO_FEMALE:
Radio = 0;
break;
case IDC_RADIO_MALE:
Radio = 1;
break;
case IDC_BUTTON_END1:
EndDialog(hDlg, 0);
break;
}
break;
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
}
return 0;
}
void MakeColumn(HWND hDlg)
{
LVCOLUMN lvCol;
HWND hList;
hList = GetDlgItem(hDlg, IDC_LIST_MEMBER1);
RECT rect;
GetClientRect(hList, &rect);
const TCHAR* title[4] = { _T("이름"), _T("전화번호"), _T("성별"), _T("출생연도") };
int nWidth[4] = { rect.right * 0.25, rect.right * 0.25, rect.right * 0.25, rect.right * 0.25 };
int Fmt[3] = { LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_CENTER };
for (int i = 0; i < 4; i++)
{
lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvCol.fmt = LVCFMT_CENTER;
lvCol.cx = nWidth[i];
lvCol.pszText = (LPTSTR)title[i];
lvCol.iSubItem = i;
ListView_InsertColumn(hList, i, &lvCol);
}
}