从列表视图中选择并突出显示项目 [英] Selecting and Highlighting an Item from List View

查看:37
本文介绍了从列表视图中选择并突出显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to select and highlight an item from a list view control and am using the following code

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

我得到的结果是

而不是项目被选中和突出显示

instead of the item getting selected and highlighted

请告诉我可能出了什么问题

Please let me know what might be going wrong

推荐答案

对于哪些进程可以将焦点设置在窗口上存在限制,并且很有可能在对话框处于活动状态时选择 ListView 项目的应用不满足这些限制.例如,被聚焦的 HWND 必须附加到调用线程的消息队列.因此,突出显示的应用程序必须在另一个应用程序的窗口上调用 SetFocus() 之前使用 AttachThreadInput().

There are restrictions on which processes can set focus on a window, and chances are that the app selecting the ListView items does not satisfy those restrictions while the dialog is active. For example, the HWND being focused must be attached to the calling thread's message queue. So the highlighting app will have to use AttachThreadInput() before calling SetFocus() on another app's windows.

这篇关于从列表视图中选择并突出显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆