Unicode字符集改变后,CListCtrl类中GetHotItem获取热选项一直为-1

本文解决了在VS2005中更改字符集后,GetHotItem函数总是返回-1的问题。通过使用NM_CLICK通知消息中的iItem索引来可靠地获取当前选中的项索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:当我们在VS2005中把Unicode字符集改为多字节字符集后,GetHotItem返回总是-1;

解决:利用通知消息NM_CLICK(或双击消息)为例.

 

void CManagerUser::OnNMClickUserList(NMHDR *pNMHDR, LRESULT *pResult)
{
 // TODO: 在此添加控件通知处理程序代码
    LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE)pNMHDR;
 m_UserList.SetHotItem(lpnmitem->iItem); 
    int nIndex=m_UserList.GetHotItem();

 //没有热选项判断
    if(nIndex==-1)
  return;

 //省略.......

}

 

MSDN中明确说到,参数pNMHDR中包含了响应该选项索引值,这是最可靠的.很多初学者直接利用GetHotItem返回,改变了其它条件后就不可靠了.当然在Unicode字符集下使用正常.

 

以下是NM_CLICK通知消息参数lParam传递的结构,说明如下:

 

NMITEMACTIVATE Structure


 

Contains information about an LVN_ITEMACTIVATE notification message.

Syntax

typedef struct tagNMITEMACTIVATE {
    NMHDR hdr;
    int iItem;
    int iSubItem;
    UINT uNewState;
    UINT uOldState;
    UINT uChanged;
    POINT ptAction;
    LPARAM lParam;
    UINT uKeyFlags;
} NMITEMACTIVATE, *LPNMITEMACTIVATE;

Members

hdr
NMHDR structure that contains information about this notification message.
iItem
Index of the list-view item. If the item index is not used for the notification, this member will contain -1.
iSubItem
One-based index of the subitem. If the subitem index is not used for the notification or the notification does not apply to a subitem, this member will contain zero.
uNewState
New item state. This member is zero for notification messages that do not use it.
uOldState
Old item state. This member is zero for notification messages that do not use it.
uChanged
Set of flags that indicate the item attributes that have changed. This member is zero for notifications that do not use it. Otherwise, it can have the same values as the mask member of the LVITEM structure.
ptAction
POINT structure that indicates the location at which the event occurred. This member is undefined for notification messages that do not use it.
lParam
Application-defined value of the item. This member is undefined for notification messages that do not use it.
uKeyFlags
Modifier keys that were pressed at the time of the activation. This member contains zero or a combination of the following flags:
LVKF_ALT
The key is pressed.
LVKF_CONTROL
The key is pressed.
LVKF_SHIFT
The key is pressed.

Structure Information

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值