我想从列表 View 控件中选择并突出显示一个项目,并且正在使用以下代码

#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);
}

我得到的结果是

而不是选择并突出显示该项目

请让我知道可能出了什么问题

最佳答案

哪些进程可以将焦点设置在窗口上有限制,并且对话框处于 Activity 状态时,选择ListView项的应用可能无法满足这些限制。例如,必须将聚焦的HWND附加到调用线程的消息队列中。因此,突出显示应用程序必须在其他应用程序的窗口上调用AttachThreadInput()之前使用SetFocus()

10-02 01:36