我正在尝试对 TListView
的 WindowProc
进行子类化,以在编辑 TListView
标题后检测 ESC 键按下情况(如果用户取消编辑)。 ListViewWndProc 被明确调用,但应该检测的代码参数永远不会获取 LVN_ENDLABELEDIT 值。为什么注释部分永远不会被调用?我看不到错误,它应该发生。
TWndMethod OldWndProc;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
OldWndProc = ListView1->WindowProc;
ListView1->WindowProc = ListViewWndProc;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListViewWndProc(TMessage &Message)
{
if (Message.Msg == CN_NOTIFY)
{
LPNMHDR pnmh = reinterpret_cast<LPNMHDR>(Message.LParam);
if (pnmh->code == LVN_ENDLABELEDIT) // UPDATE: if LVN_ENDLABELEDIT is replaced with 4294967120 it works
{
// !!! THE FOLLOWING NEVER HAPPENS !!!
// UPDATE: Looks like LVN_ENDLABELEDIT is incorrectly defined in C++ Builder 2010
// if LVN_ENDLABELEDIT is replaced with 4294967120 the code works
LV_DISPINFO *pdi = reinterpret_cast<LV_DISPINFO*>(Message.LParam);
if (pdi->item.pszText == NULL)
{
Edit1->Text = "Cancelled";
return;
}
}
}
OldWndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView1Editing(TObject *Sender, TListItem *Item, bool &AllowEdit)
{
Edit1->Text = "Editing";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView1Edited(TObject *Sender, TListItem *Item, UnicodeString &S)
{
Edit1->Text = "Done";
}
最佳答案
在 C++ 中,LVN_ENDLABELEDEDIT
的值取决于项目的TCHAR_Mapping ,可以通过 "_TCHAR maps to"
在项目设置中进行更改配置项。默认情况下,_TCHAR
设置为wchar_t
在 C++Builder 2009 及更高版本中,除非您从早期版本迁移项目,在这种情况下它是 char
默认情况下。
LVN_ENDLABELEDIT
是一个映射到 LVN_ENDLABELEDITA
的宏(4294967190)当_TCHAR
是 char
,并发送至 LVN_ENDLABELEDITW
(4294967120)当_TCHAR
是 wchar_t
.
检查两个常量 LVN_ENDLABELEDEDITA
和LVN_ENDLABELEDEDITW
,就像在Delphi源代码中所做的那样,应该没问题。
void __fastcall TForm1::ListViewWndProc(TMessage &Message)
{
if (Message.Msg == CN_NOTIFY)
{
LPNMHDR pnmh = reinterpret_cast<LPNMHDR>(Message.LParam);
if ((pnmh->code == LVN_ENDLABELEDITA) || (pnmh->code == LVN_ENDLABELEDITW))
{
LV_DISPINFO *pdi = reinterpret_cast<LV_DISPINFO*>(Message.LParam);
if (pdi->item.pszText == NULL)
{
Edit1->Text = "Cancelled";
return;
}
}
}
OldWndProc(Message);
}
关于listview - TListView 检测 ESC 或未更改的编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50547701/