CListCtrl控件上模拟右键单击事件无法得到正确结果,求解!
用FindWindow找到任务管理器的进程列表(CListCtrl控件),对列表中的第一行发送右键单击事件成功了,但是却没有出现右键的菜单,
// op_caidao.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "op_caidao.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
BOOL CALLBACK EnumChildProc(
HWND hwnd, // handle to child window
LPARAM lParam // application-defined value
)
{
WCHAR wszClassName[MAX_PATH] = {0};
GetClassNameW(hwnd, wszClassName, MAX_PATH);
wstring wsClassName = wszClassName;
if ( _T("SysListView32") == wsClassName )
{
SetForegroundWindow(hwnd);
DWORD PID = 0;
::GetWindowThreadProcessId(hwnd, &PID);
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
if(hProcess)
{
RECT rc = {0};
RECT* pRect = NULL;
pRect = (RECT*)::VirtualAllocEx(hProcess, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
if(NULL == pRect)
{
::CloseHandle(hProcess);
return FALSE;
}
// 这里得到CListCtrl控件的item = 2的RECT区域,即CListCtrl控件的第三行
::SendMessage(hwnd, LVM_GETITEMRECT, 2, (LPARAM)pRect);
DWORD dwRead = 0;
::ReadProcessMemory(hProcess, pRect, &rc, sizeof(RECT), &dwRead);
POINT pt = {0};
pt.x = rc.left + (rc.right - rc.left) / 2;
pt.y = rc.top + (rc.bottom - rc.top) / 2;
::SendMessage(hwnd, WM_LBUTTONDBLCLK, MK_LBUTTON, (LPARAM)&MAKEPOINTS(pt.x, pt.y));
::VirtualFreeEx(hProcess, pRect, sizeof(RECT), MEM_RELEASE);
::CloseHandle(hProcess);
}
return FALSE;
}
return TRUE;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
HWND hCaiDao = FindWindow(_T("#32770"), _T("Windows 任务管理器"));
if ( hCaiDao )
{
EnumChildWindows(hCaiDao, EnumChildProc, NULL);
}
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}