Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在具有可编辑子项的listview中正确处理子项编辑(或取消子项编辑) 导言:_C++_C_Listview_Winapi_Datagrid - Fatal编程技术网

C++ 在具有可编辑子项的listview中正确处理子项编辑(或取消子项编辑) 导言:

C++ 在具有可编辑子项的listview中正确处理子项编辑(或取消子项编辑) 导言:,c++,c,listview,winapi,datagrid,C++,C,Listview,Winapi,Datagrid,我试图用可编辑的子项实现listview控件。对于项目/子项目的就地编辑,我使用编辑控件 我相信我已经成功地将编辑控件的代码放置在项/子项上方 问题: 我不知道应该在哪些事件上结束/取消子项编辑(隐藏编辑控件、设置子项文本等),以及应该如何完成 为了澄清,我谈到了用户完成/取消就地编辑的时刻 此时不再需要编辑控件,所以我应该隐藏它(我不喜欢每次都重新创建它;我相信创建一次,然后在需要时显示/隐藏它会更有效) 我的目标是Visual Studio中的行为属性窗口(请参阅所附图像以准确查看我所指的窗

我试图用可编辑的子项实现listview控件。对于项目/子项目的就地编辑,我使用编辑控件

我相信我已经成功地将编辑控件的代码放置在项/子项上方

问题: 我不知道应该在哪些事件上结束/取消子项编辑(隐藏编辑控件、设置子项文本等),以及应该如何完成

为了澄清,我谈到了用户完成/取消就地编辑的时刻

此时不再需要编辑控件,所以我应该隐藏它(我不喜欢每次都重新创建它;我相信创建一次,然后在需要时显示/隐藏它会更有效)

我的目标是Visual Studio中的行为属性窗口(请参阅所附图像以准确查看我所指的窗口)

我想实现编辑/取消,就像用户按ESC键/单击另一个窗口/单击滚动条等时该窗口所做的那样

我为解决这个问题所做的努力: 使用谷歌,我发现了一些例子,但它们都是旧的,并没有解决所有相关的情况,所以这就是为什么我在这里寻求帮助

<>但是,我可以发现,我必须考虑的一个事件是,当用户按下ESC/Enter键时,当用户点击除编辑控件之外的其他事件时, 编辑:

我已经成功地处理了ESC和输入键,以及当用户单击另一个同级控件或使用ALT+TAB切换窗口时的情况。我已更新了SSCCE,并进行了相关更改

问题: 为了实现网格的默认行为(如果Windows应用程序有),我必须处理哪些消息/事件

您还可以指出我应该在哪里编辑子项和隐藏编辑控件,以及我应该在哪里隐藏编辑控件吗

编辑:

我唯一的问题是处理用户单击listview滚动条或主窗口背景时的情况。我只是不知道如何处理这件事,如果能得到所有帮助,我将不胜感激

有关资料: 我在Windows7x86上使用VisualStudio2013

我在C++中使用原始的WiAPI进行开发;p> SSCCE: 以下是我到目前为止的解决方案。我已经尝试过对它进行彻底的评论,但是如果需要更多的信息,请留下评论,我会更新我的帖子

#include <windows.h>
#include <windowsx.h>   // various listview macros etc
#include <CommCtrl.h>
#include <stdio.h>      // swprintf_s()

// enable Visual Styles
#pragma comment( linker, "/manifestdependency:\"type='win32' \
                         name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
                         processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
                         language='*'\"")

// link with Common Controls library
#pragma comment( lib, "comctl32.lib") 

//global variables
HINSTANCE hInst;

// listview subclass procedure
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 edit control has the focus take it away and give to listview
        if (GetFocus() == GetDlgItem(GetParent(hwnd), 5000))
            SetFocus(hwnd);  // use WM_NEXTDLGCTL for dialogbox !!!!
        break;
    case WM_NCDESTROY:
        ::RemoveWindowSubclass(hwnd, ListViewSubclassProc, uIdSubclass);
        return DefSubclassProc(hwnd, message, wParam, lParam);
    }
    return ::DefSubclassProc(hwnd, message, wParam, lParam);
}

// subclass procedure for edit control
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:
        //Process this message to avoid message beeps.
        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:
        {
            // get listview handle
            HWND hwndLV = GetDlgItem(GetParent(hwnd), 2000);
            // get edit control's client rectangle
            RECT rc = { 0 };
            GetClientRect(hwnd, &rc);
            // since edit control lies inside item rectangle
            // we can test any coordinate inside edit control's
            // client rectangle
            // I chose ( rc.left, rc.top )
            MapWindowPoints(hwnd, hwndLV, (LPPOINT)&rc, (sizeof(RECT) / sizeof(POINT)));
            // get item and subitem indexes
            LVHITTESTINFO lvhti = { 0 };
            lvhti.pt.x = rc.left;
            lvhti.pt.y = rc.top;
            ListView_SubItemHitTest(hwndLV, &lvhti);
            // get edit control's text
            wchar_t txt[50] = L"";
            Edit_GetText(hwnd, txt, 50);
            // edit cell text
            ListView_SetItemText(hwndLV, lvhti.iItem, lvhti.iSubItem, txt);
            // restore focus to listview
            // this triggers EN_KILLFOCUS
            // which will hide edit control
            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);
}
// main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
    {
        //================ create controls
        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);
        // in place edit control
        HWND hwndEdit = CreateWindowEx(0, WC_EDIT, L"", ES_AUTOHSCROLL | WS_CHILD | WS_BORDER,
            200, 265, 100, 25, hwnd, (HMENU)5000, hInst, 0);
        // edit control must have the same font as listview
        HFONT hf = (HFONT)SendMessage(hwndLV, WM_GETFONT, 0, 0);
        if (hf)
            SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hf, (LPARAM)TRUE);
        // subclass edit control, so we can edit subitem with ENTER, or
        // cancel editing with ESC
        SetWindowSubclass(hwndEdit, InPlaceEditControl_SubclassProc, 0, 0);
        // set extended listview styles
        ListView_SetExtendedListViewStyle(hwndLV, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
        // subclass listview
        SetWindowSubclass(hwndLV, ListViewSubclassProc, 0, 0);

        // add some columns
        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);
        }

        // add some items
        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)  // item 
                    SendDlgItemMessage(hwnd, 2000, LVM_INSERTITEM, 0, reinterpret_cast<LPARAM>(&lvi));
                else          // sub-item
                    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: // remember, this was our listview's ID
            {
                LPNMITEMACTIVATE lpnmia = (LPNMITEMACTIVATE)lParam;

                // SHIFT/ALT/CTRL/their combination, must not be pressed
                if ((lpnmia->uKeyFlags || 0) == 0)
                {
                    // store item/subitem rectangle
                    RECT rc = { 0, 0, 0, 0 };
                    // helper values, needed for handling partially visible items
                    int topIndex = ListView_GetTopIndex(lpnmia->hdr.hwndFrom);
                    int visibleCount = ListView_GetCountPerPage(lpnmia->hdr.hwndFrom);
                    // if item is vertically partially visible, make it fully visible
                    if ((topIndex + visibleCount) == lpnmia->iItem)
                    {
                        // get the rectangle of the above item -> lpnmia->iItem - 1
                        ListView_GetSubItemRect(lpnmia->hdr.hwndFrom, lpnmia->iItem - 1, lpnmia->iSubItem, LVIR_LABEL, &rc);
                        // ensure clicked item is visible
                        ListView_EnsureVisible(lpnmia->hdr.hwndFrom, lpnmia->iItem, FALSE);
                    }
                    else // item is fully visible, just get its ectangle
                        ListView_GetSubItemRect(lpnmia->hdr.hwndFrom, lpnmia->iItem, lpnmia->iSubItem, LVIR_LABEL, &rc);

                    RECT rcClient = { 0 };  // listview client rectangle, needed if item partially visible
                    GetClientRect(lpnmia->hdr.hwndFrom, &rcClient);
                    // item is horizontally partially visible -> from the right side
                    if (rcClient.right < rc.right)  
                    {
                        // show the whole item
                        ListView_Scroll(lpnmia->hdr.hwndFrom, rc.right - rcClient.right, 0);
                        // adjust rectangle so edit control is properly displayed
                        rc.left -= rc.right - rcClient.right;
                        rc.right = rcClient.right;
                    }
                    // item is horizontally partially visible -> from the left side
                    if (rcClient.left > rc.left)  
                    {
                        // show the whole item
                        ListView_Scroll(lpnmia->hdr.hwndFrom, rc.left - rcClient.left, 0);
                        // adjust rectangle so edit control is properly displayed
                        rc.right += rcClient.left - rc.left;
                        rc.left = rcClient.left;
                    }
                    // it is time to position edit control, we start by getting its window handle
                    HWND hwndEdit = GetDlgItem(hwnd, 5000);
                    //  get item text and set it as edit control's text
                    wchar_t text[51];
                    ListView_GetItemText(lpnmia->hdr.hwndFrom, lpnmia->iItem, lpnmia->iSubItem, text, 50);
                    Edit_SetText(hwndEdit, text);
                    // select entire text
                    Edit_SetSel(hwndEdit, 0, -1);
                    // map listview client rectangle to parent rectangle
                    // so edit control can be properly placed above the item
                    MapWindowPoints(lpnmia->hdr.hwndFrom, hwnd, (LPPOINT)&rc, (sizeof(RECT) / sizeof(POINT)));
                    // move the edit control
                    SetWindowPos(hwndEdit, HWND_TOP, rc.left, rc.top, rc.right - rc.left, 
                        rc.bottom - rc.top, SWP_SHOWWINDOW);
                    // set focus to our edit control
                    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;
}

// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
    int nCmdShow)
{
    // store hInstance in global variable for later use
    hInst = hInstance;

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // register main window class
    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;
    }

    // initialize common controls
    INITCOMMONCONTROLSEX iccex;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_STANDARD_CLASSES;
    InitCommonControlsEx(&iccex);

    // create main window
    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;
}
#包括
#包括//各种listview宏等
#包括
#包括//swprintf_s()
//启用视觉样式
#pragma注释(链接器,“/manifestdependency:\”类型='win32'\
name='Microsoft.Windows.Common控件'version='6.0.0.0'\
processorArchitecture='*'publicKeyToken='6595b64144ccf1df'\
语言=“*”\“”)
//与公共控件库的链接
#pragma注释(lib,“comctl32.lib”)
//全局变量
HINSTANCE hInst;
//listview子类过程
LRESULT回调ListViewSubclassProc(HWND HWND,UINT消息,
WPARAM WPARAM,LPARAM LPARAM,
UINT_PTR uIdSubclass,DWORD_PTR dwRefData)
{
开关(信息)
{
案例WM_VSCROLL:
案例WM_Hscoll:
//如果编辑控件具有焦点,则将其移除并交给listview
if(GetFocus()==GetDlgItem(GetParent(hwnd),5000))
SetFocus(hwnd);//对对话框使用WM_NEXTDLGCTL!!!!
打破
案例WM\NCU:
::RemoveWindowsSubClass(hwnd、ListViewSubclassProc、uIdSubclass);
返回defsublassproc(hwnd、message、wParam、lParam);
}
return::defsublassproc(hwnd、message、wParam、lParam);
}
//编辑控件的子类过程
LRESULT回调InPlaceEditControl_子类proc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM,
UINT_PTR uIdSubclass,DWORD_PTR dwRefData)
{
开关(信息)
{
案例WM_GETDLGCODE:
返回(DLGC_WANTALLKEYS | defsublassproc(hwnd、message、wParam、lParam));
案例WM_KILLFOCUS:
显示窗口(hwnd、SW_隐藏);
返回defsublassproc(hwnd、message、wParam、lParam);
案例WM_CHAR:
//处理此消息以避免消息嘟嘟声。
交换机(wParam)
{
案例VK_返回:
返回0升;
案件VK_逃逸:
返回0升;
违约:
return::defsublassproc(hwnd、message、wParam、lParam);
}
打破
案例WM_键控:
交换机(wParam)
{
案例VK_返回:
{
//获取listview句柄
HWND hwndLV=GetDlgItem(GetParent(HWND),2000);
//获取编辑控件的客户端矩形
RECT rc={0};
GetClientRect(hwnd和rc);
//由于编辑控件位于项目矩形内
//我们可以测试编辑控件中的任何坐标
//客户端矩形
//我选择了(rc.left,rc.top)
MapWindowPoints(hwnd、hwndLV(LPPOINT)和rc(sizeof(RECT)/sizeof(POINT));
//获取项和子项索引
LVHITTESTINFO lvhti={0};
lvhti.pt.x=rc.left;
lvhti.pt.y=rc.top;
ListView_子项HitTest(hwndLV和lvhti);
//获取编辑控件的文本
wchar_t txt[50]=L”“;
编辑_GetText(hwnd,txt,50);
//编辑单元格文本
ListView_SetItemText(hwndLV、lvhti.iItem、lvhti.iSubItem、txt);
//将焦点恢复到listview
//这会触发EN_KILLFOCUS
//它将隐藏编辑控件
设置焦点(hwndLV);
}
返回0升;
案件VK_逃逸:
SetFocus(GetDlgItem(GetParent(hwnd),2000年));
返回0升;
违约:
return::defsublassproc(hwnd、message、wParam、lParam);
}
打破
案例WM\NCU:
::RemoveWindowsSubClass(hwnd,InPlaceEditControl\u子类Proc,uIdSubclass);
void hideEdit(BOOL save)
{
    //save or not...
    ShowWindow(hedit, SW_HIDE);
}

LRESULT CALLBACK EditProc...
{
    if (msg == WM_KILLFOCUS)
        hideEdit(1);

    if (msg == WM_CHAR)
    {
        if (wParam == VK_ESCAPE){
            hideEdit(0);
            return 0;
        }
        if (wParam == VK_RETURN){
            hideEdit(1);
            return 0;
        }
    }

    return DefSubclassProc(...);
}

LRESULT CALLBACK ListProc...
{
    if (msg == WM_VSCROLL || msg == WM_HSCROLL) hideEdit(1);
    return DefSubclassProc(...);
}