private void ExpandOrCollapse(bool collapse) { CheckControlEnabled(); // Check if item can be expanded or collapsed. bool isCollapsed = IsCollapsed(); if ((!collapse && !isCollapsed) || (collapse && isCollapsed)) { throw new InvalidOperationException(SR.Get(SRID.OperationCannotBePerformed)); } NativeMethods.LVGROUP group = new NativeMethods.LVGROUP(); group.Init(Marshal.SizeOf(typeof(NativeMethods.LVGROUP))); // Note: If we set group.mask to LVGF_GROUPID | LVGF_STATE, // SetGroupInfo() will fail. Setting LVGF_STATE alone works, however. group.mask = NativeMethods.LVGF_STATE; group.iGroupID = _groupID; group.stateMask = NativeMethods.LVGS_COLLAPSED; group.state = collapse ? NativeMethods.LVGS_COLLAPSED : 0; if (!XSendMessage.SetGroupInfo(_hwnd, group)) { throw new InvalidOperationException(SR.Get(SRID.OperationCannotBePerformed)); } }
internal static bool IsCollapsed(IntPtr hwnd, int groupID) { bool isCollapsed = false; NativeMethods.LVGROUP group = new NativeMethods.LVGROUP(); group.Init(Marshal.SizeOf(typeof(NativeMethods.LVGROUP))); group.iGroupID = groupID; group.mask = NativeMethods.LVGF_STATE; group.stateMask = NativeMethods.LVGS_COLLAPSED; // Note: return code of GetGroupInfo() is not reliable. XSendMessage.GetGroupInfo(hwnd, ref group); // ignore return code. isCollapsed = (group.state & NativeMethods.LVGS_COLLAPSED) != 0; return isCollapsed; }