예제 #1
0
        /// <summary>
        /// 选择指定项
        /// </summary>
        /// <param name="itemIndex"></param>
        public void SelectItem(Int32 itemIndex)
        {
            WinCtrlAPI.ListView.LVITEM listView = new WinCtrlAPI.ListView.LVITEM();
            //listView.iItem = itemIndex;
            listView.mask       = SystemDefinedMessages.CommonControl.LVIF_STATE;
            listView.stateMask  = SystemDefinedMessages.CommonControl.LVIS_FOCUSED | SystemDefinedMessages.CommonControl.LVIS_SELECTED;
            listView.state      = SystemDefinedMessages.CommonControl.LVIS_FOCUSED | SystemDefinedMessages.CommonControl.LVIS_SELECTED;
            listView.cchTextMax = 0;
            listView.iImage     = 0;

            IntPtr listViewPtr = Marshal.AllocHGlobal(Marshal.SizeOf(listView));

            Marshal.StructureToPtr(listView, listViewPtr, false);
            Int32 re = API.SendMessage(m_ListViewPtr, SystemDefinedMessages.CommonControl.LVM_SETITEMSTATE, itemIndex, listViewPtr.ToInt32());

            Marshal.FreeHGlobal(listViewPtr);
        }
예제 #2
0
        /// <summary>
        /// 获取制定图标的文本
        /// </summary>
        /// <returns></returns>
        public String GetItemText(Int32 itemIndex)
        {
            String result = String.Empty;
            UInt32 pref   = 0;

            WinCtrlAPI.ListView.LVITEM _lv = new WinCtrlAPI.ListView.LVITEM();

            // 打开桌面进程
            IntPtr proPtr = API.OpenProcess(API.ProcessSecurityAccessRight.PROCESS_VM_OPERATION | API.ProcessSecurityAccessRight.PROCESS_VM_READ | API.ProcessSecurityAccessRight.PROCESS_VM_WRITE, false, (UInt32)m_ProcessID);

            // 在桌面进程给文本分配一段内存
            IntPtr textPtr = API.VirtualAllocEx(proPtr, IntPtr.Zero, 260, API.MemoryAllocationType.MEM_COMMIT, API.MemoryProtectionConstants.PAGE_EXECUTE_READWRITE);

            _lv.pszText = textPtr;

            _lv.mask       = SystemDefinedMessages.CommonControl.LVIF_TEXT;
            _lv.iSubItem   = 0;
            _lv.cchTextMax = 260;

            // 给LVItem分配一段内存
            IntPtr lvPtr = API.VirtualAllocEx(proPtr, IntPtr.Zero, Marshal.SizeOf(_lv), API.MemoryAllocationType.MEM_COMMIT, API.MemoryProtectionConstants.PAGE_EXECUTE_READWRITE);

            // 在当前进程分配一段内存,将ListView对象转化为结构体写入当前进程内存,
            IntPtr tmpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_lv));

            Marshal.StructureToPtr(_lv, tmpPtr, false);

            // 将当前进程内存中的结构体写入到桌面进程内存中。
            Boolean writeResult = API.WriteProcessMemory(proPtr, lvPtr, tmpPtr, Marshal.SizeOf(_lv), ref pref);

            if (!writeResult)
            {
                throw new ApplicationException("写入内存错误:" + API.GetLastError().ToString());
            }

            Marshal.FreeHGlobal(tmpPtr);

            // 发送消息,接收文本
            Int32 msgResult = API.SendMessage(this.m_ListViewPtr, SystemDefinedMessages.CommonControl.LVM_GETITEMTEXT, itemIndex, lvPtr);

            if (msgResult == 0)
            {
                throw new ApplicationException(API.GetLastError().ToString());
            }

            // 在本程序分配一段内存空间,将桌面进程的内存读取到本进程,然后在转换为文本。
            IntPtr  tmpPtr2    = Marshal.AllocHGlobal(260);
            Boolean readResult = API.ReadProcessMemory(proPtr, textPtr, tmpPtr2, 260, ref pref);

            if (readResult)
            {
                result = Marshal.PtrToStringAuto(tmpPtr2);
            }
            Marshal.FreeHGlobal(tmpPtr2);

            // 释放桌面进程中开辟的内存
            API.VirtualFreeEx(proPtr, lvPtr, 0, API.MemoryFreeType.MEM_RELEASE);
            API.VirtualFreeEx(proPtr, textPtr, 0, API.MemoryFreeType.MEM_RELEASE);

            // 关闭桌面进程
            API.CloseHandle(proPtr);

            return(result);
        }