我正在尝试使用可编辑子项实现 ListView 控件。对于项目/子项目的就地编辑,我使用编辑控件。
我相信我已经设法正确编码将编辑控件放置在项目/子项目之上。
我不知道应该在哪些事件上结束/取消子项编辑(隐藏编辑控件、设置子项文本等)以及我应该如何做。
为了澄清,我说的是用户完成/取消就地编辑的那一刻。
此时不再需要编辑控件,所以我应该隐藏它(我不喜欢每次都重新创建它;我相信创建一次然后在需要时显示/隐藏它效率更高)。
我的目标是 Properties 窗口在 Visual Studio 中的行为(请参阅附图以准确查看我所指的窗口)。
当用户按下 ESC 键/点击另一个窗口/点击滚动条等时,我想以与此窗口相同的方式实现编辑/取消。
使用谷歌,我发现了一些例子,但它们是旧的并且没有解决所有相关案例,所以这就是我在这里寻求帮助的原因。
但是,我发现我必须考虑的事件之一是 EN_KILLFOCUS ,用户按下 ESC/ENTER 键的情况以及用户单击编辑控件以外的其他内容的情况。
编辑:
我已经设法处理 ESC 和 ENTER 键,以及用户单击另一个同级控件或使用 ALT + TAB 切换窗口时的情况。我已使用相关更改更新 SSCCE
为了实现网格的默认行为(如果有用于 Windows 应用程序的行为),我必须处理哪些消息/事件?
您能否也指出我应该在哪里编辑子项目并隐藏编辑控件,以及我应该在哪里隐藏编辑控件?
编辑:
我剩下的唯一问题是处理用户单击 ListView 滚动条或主窗口背景时的情况。我只是不知道如何处理这个问题,希望能得到所有帮助。
我在 Windows 7 x86 上使用 Visual Studio 2013;
我正在使用原始 WinAPI 在 C++ 中进行开发;
以下是我目前的解决方案。我已尝试对它进行彻底的评论,但如果需要更多信息,请发表评论,我会更新我的帖子。
#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;
}
最佳答案
更新:
转念一想,我之前贴的方法是错误的。我认为在这样的编辑框中使用 SetCapture 是设计错误,它会干扰其他一些事情。我要删除我的旧答案并假装没有人看到它!
您自己的方法可以很好地检查 KILLFOCUS,您只需要 ListView 的子类来检查滚动消息以模仿 LVN_XXXLABELEDIT
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(...);
}
关于c++ - 正确处理listview中可编辑子项的子项编辑(或取消子项编辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251328/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我在OSX上(如果重要的话)。如果我使用RVM安装Ruby,它会默认将Bundler安装到@globalgemset假设我想要一个不同版本的bundler。我假设我需要做的就是执行geminstallbundler--version但是,这会将bundler安装到默认gemset并且RVM不会为其设置路径。因此,如果我键入bundler,它仍会启动一个与Ruby一起安装到@global中的bundler两个问题:如何将bundler安装到@globalgemset。将bundler安装到@globalgemset中的模式是否正确,或者我遗漏了什么 最佳答案