多数情况下,接受 WM_LBUTTONDOWN事件,并使用如下代码
SHRGINFO shrg;
shrg.cbSize = sizeof(shrg);
shrg.hwndClient = hWnd;
shrg.ptDown.x = LOWORD(lParam);
shrg.ptDown.y = HIWORD(lParam);
shrg.dwFlags = SHRG_RETURNCMD | SHRG_NOANIMATION;
if (SHRecognizeGesture(&shrg) == GN_CONTEXTMENU) {
}
在LISTVIEW TREE等控件中,可以直接处理WM_NOTIFY的子消息 GN_CONTEXTMENU
GN_CONTEXTMENU 传递的参数
res = (WPARAM) wParam;
pnmrgi = (PNMRGINFO) lParam;
以下是该结构体定义
The NMRGINFO structure is sent through WM_NOTIFY when SHRG_NOTIFYPARENT is used.
typedef struct tagNMRGINFO {
NMHDR hdr;
POINT ptAction;
DWORD dwItemSpec;
} NMRGINFO, *PNMRGINFO;
Members
hdr
Standard NMHDR characteristics here.
ptAction
X and Y screen coordinates. These are returned in screen coordinates so the receiving application has coordinates that can be used directly in a call to TrackPopupMenu.
dwItemSpec
This member is reserved for GN_CONTEXTMENU and NM_RECOGNIZEGESTURE.
PNMRGINFO结构体中包含事件发生的指点坐标,但是基于SCREEN的,需要使用SCREENTOCLIENT转换,完整代码如下
LV_DISPINFO *pLvdi = (LV_DISPINFO *)lParam;
NM_LISTVIEW *pNm = (NM_LISTVIEW *)lParam;
CONTACTINFO *pPerson = (CONTACTINFO *)(pLvdi->item.lParam);
PNMRGINFO pnmrgi=(PNMRGINFO)lParam;
static TCHAR szText[20];
if (wParam != ID_LISTVIEW)
return 0L;
switch(pLvdi->hdr.code)
{
case GN_CONTEXTMENU: //tap and hold
// 画出右键菜单
RECT rc;
POINT pt;
pt = pnmrgi->ptAction;
GetClientRect(hWnd, (LPRECT)&rc);
ScreenToClient(hWnd,&pt);
// Temporary porting macro
if (PtInRect((LPRECT)&rc, pt))
{
HandlePopupMenu(hWnd, pt);
}
}