我想从列表视图控件中选择并突出显示一个项目,并使用以下代码
#include <Windows.h>
#include <commctrl.h>
int main() {
//Hardcoded Handle to the ListView Windows of Add Printer Dialog
HWND hwndListView = (HWND)0x000206D6;
DWORD dwProcessID;
::GetWindowThreadProcessId( hwndListView, &dwProcessID );
HANDLE process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
LVITEM lvi;
LVITEM* _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
lvi.state = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.mask = LVIF_STATE;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hwndListView, LVM_SETITEMSTATE, (WPARAM)0, (LPARAM)_lvi);
VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
}
我得到的结果是
而不是选中并突出显示的项目
请告诉我可能出现的问题
答案 0 :(得分:2)
对于哪些进程可以将注意力集中在窗口上存在限制,并且在对话框处于活动状态时,选择ListView项的应用程序可能无法满足这些限制。例如,要聚焦的HWND必须附加到调用线程的消息队列。因此,在其他应用的窗口上调用AttachThreadInput()
之前,突出显示的应用必须使用SetFocus()
。