listview - TListView 检测 ESC 或未更改的编辑

标签 listview subclass c++builder tlistview

我正在尝试对 TListViewWindowProc 进行子类化,以在编辑 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)当_TCHARchar ,并发送至 LVN_ENDLABELEDITW (4294967120)当_TCHARwchar_t .

检查两个常量 LVN_ENDLABELEDEDITALVN_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/

相关文章:

macos - 在Mac OS X上从资源加载样式

android - 我需要调用通讯录中的特定号码

java - 从其他类的 ListView 中选择一行

java - 如何在父类(super class)中使用私有(private)实例变量?

python - 用同名的@property 覆盖基类属性

c++ - 使用 OLE 以编程方式保存 Excel 文件

c++ - 贷款计算器变更

C#设置ListView的Item的Subitem的文本不显示

Android多列ListView

Objective-C:调用子类的类方法