Introduction:
I am trying to implement a listview control with editable subitems. To edit elements / subelements in place, I use the edit control.
I believe that I was able to correctly copy the placement of the control over the element / subitem.
Problem:
I do not know at what events I should end / cancel editing of sub-elements (hide editing controls, set the text of a sub-element, etc.) and how to do it.
To clarify, I'm talking about the moment when the user finishes / cancels editing in place.
Editing control is no longer required at this point, so I have to hide it (I don’t like to recreate it every time, I believe that creating it once and then showing / hiding it is more efficient if necessary).
I orient the behavior of Properties in Visual Studio (see the attached image to see exactly the window I'm referring to).

I want to achieve editing / canceling in the same way that this window does when the user presses the ESC key / clicks on another window / clicks on the scroll bar, etc.
MY EFFORTS TO SOLVE THIS:
Using Google, I found some examples, but they are old and do not cover all the relevant cases, so I ask for help here.
, , , EN_KILLFOCUS, , ESC/ENTER , -, .
EDIT:
ESC ENTER, , ALT + TAB. SSCCE
:
( Windows), / ?
, , ?
EDIT:
- , listview . , , , .
:
Visual Studio 2013, Windows 7 x86;
++, raw WinAPI;
SSCCE:
, . , , , .
#include <windows.h>
#include <windowsx.h> // various listview macros etc
#include <CommCtrl.h>
#include <stdio.h> // swprintf_s()
#pragma comment( linker, "/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
language='*'\"")
#pragma comment( lib, "comctl32.lib")
HINSTANCE hInst;
LRESULT CALLBACK ListViewSubclassProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (message)
{
case WM_VSCROLL:
case WM_HSCROLL:
if (GetFocus() == GetDlgItem(GetParent(hwnd), 5000))
SetFocus(hwnd);
break;
case WM_NCDESTROY:
::RemoveWindowSubclass(hwnd, ListViewSubclassProc, uIdSubclass);
return DefSubclassProc(hwnd, message, wParam, lParam);
}
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK InPlaceEditControl_SubclassProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (message)
{
case WM_GETDLGCODE:
return (DLGC_WANTALLKEYS | DefSubclassProc(hwnd, message, wParam, lParam));
case WM_KILLFOCUS:
ShowWindow(hwnd, SW_HIDE);
return DefSubclassProc(hwnd, message, wParam, lParam);
case WM_CHAR:
switch (wParam)
{
case VK_RETURN:
return 0L;
case VK_ESCAPE:
return 0L;
default:
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
{
HWND hwndLV = GetDlgItem(GetParent(hwnd), 2000);
RECT rc = { 0 };
GetClientRect(hwnd, &rc);
MapWindowPoints(hwnd, hwndLV, (LPPOINT)&rc, (sizeof(RECT) / sizeof(POINT)));
LVHITTESTINFO lvhti = { 0 };
lvhti.pt.x = rc.left;
lvhti.pt.y = rc.top;
ListView_SubItemHitTest(hwndLV, &lvhti);
wchar_t txt[50] = L"";
Edit_GetText(hwnd, txt, 50);
ListView_SetItemText(hwndLV, lvhti.iItem, lvhti.iSubItem, txt);
SetFocus(hwndLV);
}
return 0L;
case VK_ESCAPE:
SetFocus(GetDlgItem(GetParent(hwnd), 2000));
return 0L;
default:
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
break;
case WM_NCDESTROY:
::RemoveWindowSubclass(hwnd, InPlaceEditControl_SubclassProc, uIdSubclass);
return DefSubclassProc(hwnd, message, wParam, lParam);
}
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
RECT rec = { 0 };
GetClientRect(hwnd, &rec);
HWND hwndLV = CreateWindowEx(0, WC_LISTVIEW,
L"", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS | LVS_REPORT,
50, 50, 250, 200, hwnd, (HMENU)2000, hInst, 0);
HWND hwndEdit = CreateWindowEx(0, WC_EDIT, L"", ES_AUTOHSCROLL | WS_CHILD | WS_BORDER,
200, 265, 100, 25, hwnd, (HMENU)5000, hInst, 0);
HFONT hf = (HFONT)SendMessage(hwndLV, WM_GETFONT, 0, 0);
if (hf)
SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hf, (LPARAM)TRUE);
SetWindowSubclass(hwndEdit, InPlaceEditControl_SubclassProc, 0, 0);
ListView_SetExtendedListViewStyle(hwndLV, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
SetWindowSubclass(hwndLV, ListViewSubclassProc, 0, 0);
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
for (long nIndex = 0; nIndex < 5; nIndex++)
{
wchar_t txt[50];
swprintf_s(txt, 50, L"Column %d", nIndex);
lvc.iSubItem = nIndex;
lvc.cx = 60;
lvc.pszText = txt;
ListView_InsertColumn(hwndLV, nIndex, &lvc);
}
LVITEM lvi;
lvi.mask = LVIF_TEXT;
for (lvi.iItem = 0; lvi.iItem < 10000; lvi.iItem++)
{
for (long nIndex = 0; nIndex < 5; nIndex++)
{
wchar_t txt[50];
swprintf_s(txt, 50, L"Item %d%d", lvi.iItem, nIndex);
lvi.iSubItem = nIndex;
lvi.pszText = txt;
if (!nIndex)
SendDlgItemMessage(hwnd, 2000, LVM_INSERTITEM, 0, reinterpret_cast<LPARAM>(&lvi));
else
SendDlgItemMessage(hwnd, 2000, LVM_SETITEM, 0, reinterpret_cast<LPARAM>(&lvi));
}
}
}
return 0L;
case WM_NOTIFY:
{
if (((LPNMHDR)lParam)->code == NM_DBLCLK)
{
switch (((LPNMHDR)lParam)->idFrom)
{
case 2000:
{
LPNMITEMACTIVATE lpnmia = (LPNMITEMACTIVATE)lParam;
if ((lpnmia->uKeyFlags || 0) == 0)
{
RECT rc = { 0, 0, 0, 0 };
int topIndex = ListView_GetTopIndex(lpnmia->hdr.hwndFrom);
int visibleCount = ListView_GetCountPerPage(lpnmia->hdr.hwndFrom);
if ((topIndex + visibleCount) == lpnmia->iItem)
{
ListView_GetSubItemRect(lpnmia->hdr.hwndFrom, lpnmia->iItem - 1, lpnmia->iSubItem, LVIR_LABEL, &rc);
ListView_EnsureVisible(lpnmia->hdr.hwndFrom, lpnmia->iItem, FALSE);
}
else
ListView_GetSubItemRect(lpnmia->hdr.hwndFrom, lpnmia->iItem, lpnmia->iSubItem, LVIR_LABEL, &rc);
RECT rcClient = { 0 };
GetClientRect(lpnmia->hdr.hwndFrom, &rcClient);
if (rcClient.right < rc.right)
{
ListView_Scroll(lpnmia->hdr.hwndFrom, rc.right - rcClient.right, 0);
rc.left -= rc.right - rcClient.right;
rc.right = rcClient.right;
}
if (rcClient.left > rc.left)
{
ListView_Scroll(lpnmia->hdr.hwndFrom, rc.left - rcClient.left, 0);
rc.right += rcClient.left - rc.left;
rc.left = rcClient.left;
}
HWND hwndEdit = GetDlgItem(hwnd, 5000);
wchar_t text[51];
ListView_GetItemText(lpnmia->hdr.hwndFrom, lpnmia->iItem, lpnmia->iSubItem, text, 50);
Edit_SetText(hwndEdit, text);
Edit_SetSel(hwndEdit, 0, -1);
MapWindowPoints(lpnmia->hdr.hwndFrom, hwnd, (LPPOINT)&rc, (sizeof(RECT) / sizeof(POINT)));
SetWindowPos(hwndEdit, HWND_TOP, rc.left, rc.top, rc.right - rc.left,
rc.bottom - rc.top, SWP_SHOWWINDOW);
HWND previousWnd = SetFocus(hwndEdit);
}
}
break;
default:
break;
}
}
}
break;
case WM_CLOSE:
::DestroyWindow(hwnd);
return 0L;
case WM_DESTROY:
{
::PostQuitMessage(0);
}
return 0L;
default:
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
hInst = hInstance;
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"Main_Window";
wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_STANDARD_CLASSES;
InitCommonControlsEx(&iccex);
hwnd = CreateWindowEx(0, L"Main_Window", L"Grid control",
WS_OVERLAPPEDWINDOW, 50, 50, 400, 400, NULL, NULL, hInstance, 0);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}