private void AddTriggerToList(Trigger tr, int index) { int imgIdx = (int)tr.TriggerType; string txt = tr.ToString(); ListViewItem lvi = new ListViewItem(new string[] { TaskEnumGlobalizer.GetString(tr.TriggerType), txt, tr.Enabled ? EditorProperties.Resources.Enabled : EditorProperties.Resources.Disabled }, imgIdx) { Tag = tr, ToolTipText = txt }; if (index < 0) { lvi = triggerListView.Items.Add(lvi); } else { lvi = triggerListView.Items.Insert(index, lvi); } if (modern) { var nlvi = new NativeMethods.LVITEM(lvi.Index) { VisibleTileColumns = new int[] { 1 } }; if (!tr.Enabled) { nlvi.OverlayImageIndex = 1; } NativeMethods.SendMessage(triggerListView.Handle, NativeMethods.ListViewMessage.SetItem, 0, nlvi); } }
public static void SetItemState(IntPtr handle, int itemIndex, int mask, int value) { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM { stateMask = mask, state = value }; NativeMethods.SendMessageLVItem(handle, LVM_SETITEMSTATE, itemIndex, ref lvItem); }
// retrieves listview item/subitem text internal static string GetText(IntPtr hwnd, int item, int subitem) { NativeMethods.LVITEM lvitem = new NativeMethods.LVITEM(); lvitem.mask = NativeMethods.LVIF_TEXT; lvitem.iItem = item; lvitem.iSubItem = subitem; return(WindowsListView.GetItemText(hwnd, lvitem)); }
public static void SetOverlayImage(this ListViewItem lvi, int imageIndex) { if (imageIndex < 1 || imageIndex > 15) { throw new ArgumentOutOfRangeException(nameof(imageIndex)); } if (lvi.ListView == null) { throw new ArgumentNullException(nameof(lvi), "ListViewItem must be attached to a valid ListView."); } NativeMethods.LVITEM nItem = new NativeMethods.LVITEM(lvi.Index); nItem.OverlayImageIndex = (uint)imageIndex; if (NativeMethods.SendMessage(lvi.ListView.Handle, NativeMethods.ListViewMessage.SetItem, 0, nItem).ToInt32() == 0) { throw new Win32Exception(); } }
internal void SetSelected(ListViewItem i, bool selected) { if (i == null) { throw new ArgumentNullException("i"); } else if (VirtualMode || !IsHandleCreated) { i.Selected = true; return; } NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.mask = NativeMethods.LVIF_STATE; lvItem.state = selected ? NativeMethods.LVIS_SELECTED : 0; lvItem.stateMask = NativeMethods.LVIS_SELECTED; NativeMethods.SendMessage(Handle, NativeMethods.LVM_SETITEMSTATE, (IntPtr)i.Index, ref lvItem); }
private void AddTriggerToList(Trigger tr, int index) { int imgIdx = (int)tr.TriggerType; string txt = tr.ToString(); ListViewItem lvi = new ListViewItem(new string[] { TaskEnumGlobalizer.GetString(tr.TriggerType), txt, tr.Enabled ? EditorProperties.Resources.Enabled : EditorProperties.Resources.Disabled }, imgIdx) { Tag = tr, ToolTipText = txt }; if (index < 0) lvi = triggerListView.Items.Add(lvi); else lvi = triggerListView.Items.Insert(index, lvi); if (modern) { var nlvi = new NativeMethods.LVITEM(lvi.Index) { VisibleTileColumns = new int[] { 1 } }; if (!tr.Enabled) nlvi.OverlayImageIndex = 1; NativeMethods.SendMessage(triggerListView.Handle, NativeMethods.ListViewMessage.SetItem, 0, nlvi); } }
private int DisplayIndexToID(int displayIndex) { Debug.Assert(!owner.VirtualMode, "in virtual mode, this method does not make any sense"); if (owner.IsHandleCreated && !owner.ListViewHandleDestroyed) { // Obtain internal index of the item NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.mask = NativeMethods.LVIF_PARAM; lvItem.iItem = displayIndex; UnsafeNativeMethods.SendMessage(new HandleRef(owner, owner.Handle), NativeMethods.LVM_GETITEM, 0, ref lvItem); return (int) lvItem.lParam; } else { return this[displayIndex].ID; } }
internal void SetItemText(int itemIndex, int subItemIndex, string text) { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); SetItemText(itemIndex, subItemIndex, text, ref lvItem); }
internal void SetItemState(int index, int state, int mask) { if (index < -1 || ((this.VirtualMode && index >= this.VirtualListSize) || (!this.VirtualMode && index >= itemCount))) { throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture))); } Debug.Assert(index == -1 || this.IsHandleCreated, "How did we add items without a handle?"); if (this.IsHandleCreated) { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.mask = NativeMethods.LVIF_STATE; lvItem.state = state; lvItem.stateMask = mask; UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.LVM_SETITEMSTATE, index, ref lvItem); } }
internal void SetItemIndentCount(int index, int indentCount) { if (index < 0 || ((this.VirtualMode && index >= this.VirtualListSize) || (!this.VirtualMode && index >= itemCount))) { throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture))); } if (this.IsHandleCreated) { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.mask = NativeMethods.LVIF_INDENT; lvItem.iItem = index; lvItem.iIndent = indentCount; UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.LVM_SETITEM, 0, ref lvItem); } }
/// <include file='doc\ListView.uex' path='docs/doc[@for="ListView.RealizeAllSubItems"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> private void RealizeAllSubItems() { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); for (int i = 0; i < itemCount; i++) { int subItemCount = Items[i].SubItems.Count; for (int j = 0; j < subItemCount; j++) { SetItemText(i, j, Items[i].SubItems[j].Text, ref lvItem); } } }
/// <devdoc> /// Inserts a new ListViewItem into the list view itself. /// This only will be called when the Handle has been created for the list view. /// This method loops through the items, sets up their state then adds them. /// </devdoc> private int InsertItemsNative(int index, ListViewItem[] items) { if (items == null || items.Length == 0) { return 0; } Debug.Assert(IsHandleCreated, "InsertItemsNative precondition: list-view handle must be created"); // Much more efficient to call the native insert with max + 1, than with max. The + 1 // for the display index accounts for itemCount++ above. // if (index == itemCount - 1) { index++; } // Create and add the LVITEM NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); int actualIndex = -1; IntPtr hGlobalColumns = IntPtr.Zero; int maxColumns = 0; this.listViewState1[LISTVIEWSTATE1_insertingItemsNatively] = true; try { // Set the count of items first. // SendMessage(NativeMethods.LVM_SETITEMCOUNT, itemCount, 0); // Now add the items. // for (int i = 0; i < items.Length; i++) { ListViewItem li = items[i]; Debug.Assert(this.Items.Contains(li), "Make sure ListView.Items contains this item before adding the native LVITEM. Otherwise, custom-drawing may break."); lvItem.Reset(); lvItem.mask = NativeMethods.LVIF_TEXT | NativeMethods.LVIF_IMAGE | NativeMethods.LVIF_PARAM | NativeMethods.LVIF_INDENT; lvItem.iItem = index + i; lvItem.pszText = li.Text; lvItem.iImage = li.ImageIndexer.ActualIndex; lvItem.iIndent = li.IndentCount; lvItem.lParam = (IntPtr)li.ID; if (GroupsEnabled) { lvItem.mask |= NativeMethods.LVIF_GROUPID; lvItem.iGroupId = GetNativeGroupId(li); #if DEBUG Debug.Assert(SendMessage(NativeMethods.LVM_ISGROUPVIEWENABLED, 0, 0) != IntPtr.Zero, "Groups not enabled"); Debug.Assert(SendMessage(NativeMethods.LVM_HASGROUP, lvItem.iGroupId, 0) != IntPtr.Zero, "Doesn't contain group id: " + lvItem.iGroupId.ToString(CultureInfo.InvariantCulture)); #endif } lvItem.mask |= NativeMethods.LVIF_COLUMNS; lvItem.cColumns = this.columnHeaders != null ? Math.Min(MAXTILECOLUMNS, this.columnHeaders.Length) : 0; // make sure that our columns memory is big enough. // if not, then realloc it. // if (lvItem.cColumns > maxColumns || hGlobalColumns == IntPtr.Zero) { if (hGlobalColumns != IntPtr.Zero) { Marshal.FreeHGlobal(hGlobalColumns); } hGlobalColumns = Marshal.AllocHGlobal(lvItem.cColumns * Marshal.SizeOf(typeof(int))); maxColumns = lvItem.cColumns; } // now build and copy in the column indexes. // lvItem.puColumns = hGlobalColumns; int[] columns = new int[lvItem.cColumns]; for(int c = 0; c < lvItem.cColumns; c++) { columns[c] = c + 1; } Marshal.Copy(columns, 0, lvItem.puColumns, lvItem.cColumns); // Inserting an item into a ListView with checkboxes causes one or more // item check events to be fired for the newly added item. // Therefore, we disable the item check event handler temporarily. // ItemCheckEventHandler oldOnItemCheck = onItemCheck; onItemCheck = null; int insertIndex; try { li.UpdateStateToListView(lvItem.iItem, ref lvItem, false); insertIndex = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.LVM_INSERTITEM, 0, ref lvItem); if (actualIndex == -1) { actualIndex = insertIndex; // and update our starting index. so we're going from the same point. // index = actualIndex; } } finally { // Restore the item check event handler. // onItemCheck = oldOnItemCheck; } if (-1 == insertIndex) { throw new InvalidOperationException(SR.GetString(SR.ListViewAddItemFailed)); } // add all sub items for (int nItem = 1; nItem < li.SubItems.Count; ++nItem) { SetItemText(insertIndex, nItem, li.SubItems[nItem].Text, ref lvItem); } // PERF. // Use StateSelected in order to avoid a call into the native list view. if (li.StateImageSet || li.StateSelected) { // lvItem.state and lvItem.stateMask are set when the lvItem is updated in UpdateStateToListView call. SetItemState(insertIndex, lvItem.state, lvItem.stateMask); } } } finally { if (hGlobalColumns != IntPtr.Zero) { Marshal.FreeHGlobal(hGlobalColumns); } this.listViewState1[LISTVIEWSTATE1_insertingItemsNatively] = false; } if (this.listViewState1[LISTVIEWSTATE1_selectedIndexChangedSkipped]) { // VSWhidbey 549967 - SelectedIndexChanged event was delayed this.listViewState1[LISTVIEWSTATE1_selectedIndexChangedSkipped] = false; OnSelectedIndexChanged(EventArgs.Empty); } if (this.FlipViewToLargeIconAndSmallIcon) { this.FlipViewToLargeIconAndSmallIcon = false; this.View = View.LargeIcon; this.View = View.SmallIcon; } return actualIndex; }
// when the user adds a group, this helper method makes sure that all the items // in the list view are parented by a group - be it the DefaultGroup or some other group internal void InsertGroupInListView(int index, ListViewGroup group) { Debug.Assert(this.groups != null && this.groups.Count > 0, "this method should be used only when the user adds a group, not when we add our own DefaultGroup"); Debug.Assert(group != this.DefaultGroup, "this method should be used only when the user adds a group, not when we add our own DefaultGroup"); // the first time we add a group we have to group the items in the Default Group bool groupItems = (this.groups.Count == 1) && this.GroupsEnabled; UpdateGroupView(); EnsureDefaultGroup(); InsertGroupNative(index, group); // take all the list view items which don't belong to any group and put them in the default group // if (groupItems) { for (int i = 0; i < this.Items.Count; i ++) { ListViewItem item = this.Items[i]; if (item.Group == null) { item.UpdateStateToListView(item.Index); } } } #if DEBUG // sanity check: all the items in the list view should have a group ID if (this.GroupsEnabled) { for (int i = 0; i < this.Items.Count; i ++) { ListViewItem item = this.Items[i]; NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.iItem = item.Index; lvItem.mask = NativeMethods.LVIF_GROUPID; UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.LVM_GETITEM, 0, ref lvItem); Debug.Assert(lvItem.iGroupId != -1, "there is a list view item which is not parented"); } } #endif }
internal void UpdateStateFromListView(int displayIndex, bool checkSelection) { if (listView != null && listView.IsHandleCreated && displayIndex != -1) { // Get information from comctl control // NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); lvItem.mask = NativeMethods.LVIF_PARAM | NativeMethods.LVIF_STATE | NativeMethods.LVIF_GROUPID; if (checkSelection) { lvItem.stateMask = NativeMethods.LVIS_SELECTED; } // we want to get all the information, including the state image mask lvItem.stateMask |= NativeMethods.LVIS_STATEIMAGEMASK; if (lvItem.stateMask == 0) { // perf optimization: no work to do. // return; } lvItem.iItem = displayIndex; UnsafeNativeMethods.SendMessage(new HandleRef(listView, listView.Handle), NativeMethods.LVM_GETITEM, 0, ref lvItem); // Update this class' information // if (checkSelection) { StateSelected = (lvItem.state & NativeMethods.LVIS_SELECTED) != 0; } SavedStateImageIndex = ((lvItem.state & NativeMethods.LVIS_STATEIMAGEMASK) >> 12) - 1; // a-jegros: bug 154131 - group needs to be updated, too! group = null; foreach (ListViewGroup lvg in ListView.Groups) { if (lvg.ID == lvItem.iGroupId) { group = lvg; break; } } } }
internal void UpdateStateToListView(int index) { NativeMethods.LVITEM lvItem = new NativeMethods.LVITEM(); UpdateStateToListView(index, ref lvItem, true); }
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, [In, Out] ref NativeMethods.LVITEM lParam);