⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listviewfilter.cs

📁 This control is another extension to the now standard and widely used ListView control. I have inclu
💻 CS
📖 第 1 页 / 共 4 页
字号:
      {
        col_hdrctl = header;
      }

      
      /// <summary>
      /// Indexer method to get/set the Alignment for the column.
      /// </summary>
      public HorizontalAlignment this[ int index ]
      {
        get
        {

          // ensure that this is a valid column
          if ( index >= this.Count ) return HorizontalAlignment.Left;

          // get the current format for the column
          col_hditem.mask = W32_HDI.HDI_FORMAT;
          SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMW, index, ref col_hditem );

          // return the current setting
          if ( ( col_hditem.fmt & W32_HDF.HDF_CENTER ) != 0 )
            return HorizontalAlignment.Center;
          else if ( ( col_hditem.fmt & W32_HDF.HDF_RIGHT ) != 0 )
            return HorizontalAlignment.Right;
          else return HorizontalAlignment.Left;

        }
        set
        {

          // ensure that this is a valid column
          if ( index < this.Count )
          {

            // get the current format for the column
            col_hditem.mask = W32_HDI.HDI_FORMAT;
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMW, index, ref col_hditem );

            // turn off any existing alignment values
            col_hditem.fmt &= W32_HDF.HDF_NOJUSTIFY;

            // turn on the correct alignment
            switch ( value )
            {
              case HorizontalAlignment.Center:
                col_hditem.fmt |= W32_HDF.HDF_CENTER;
                break;
              case HorizontalAlignment.Right:
                col_hditem.fmt |= W32_HDF.HDF_RIGHT;
                break;
              default:
                col_hditem.fmt |= W32_HDF.HDF_LEFT;
                break;
            }

            // now update the column format
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_SETITEMW, index, ref col_hditem );

          }
        }
      }


      /// <summary>
      /// Return the number of columns in the header.
      /// </summary>
      public int Count
      {
        get
        {
          return SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMCOUNT, 0, 0 );
        }
      }

    }


    /// <summary>
    /// This is an indexer to the LVDataType values for each column.
    /// The data is stored in the lParam value of the column.
    /// </summary>
    public class ColumnDataTypeCollection
    {
      readonly ListViewFilterHeader col_hdrctl = null;         // owning header control
      private  HDITEM               col_hditem = new HDITEM(); // HDITEM instance

      /// <summary>
      /// Constructor this must be given the header instance for access
      /// to the Handle property so that messages can be sent to it.
      /// </summary>
      /// <param name="header">HeaderControl</param>
      public ColumnDataTypeCollection( ListViewFilterHeader header )
      {
        col_hdrctl = header;
      }

      
      /// <summary>
      /// Indexer method to get/set the LVDataType for the column.
      /// </summary>
      public LVFDataType this[ int index ]
      {
        get
        {

          // the lparam of the column header contains the datatype
          col_hditem.mask   = W32_HDI.HDI_LPARAM;
          col_hditem.lParam = (int)LVFDataType.String;

          // if it is valid the lparam is updated and then returned
          SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMW, index, ref col_hditem );
          return (LVFDataType)col_hditem.lParam;
        }
        set
        {

          // ensure that this is a valid column
          if ( index < this.Count )
          {

            // simply set the new LVDataType in the lparam and pass it on
            col_hditem.mask   = W32_HDI.HDI_LPARAM;
            col_hditem.lParam = (int)value;
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_SETITEMW, index, ref col_hditem );
          }
        }
      }


      /// <summary>
      /// Return the number of columns in the header.
      /// </summary>
      public int Count
      {
        get
        {
          return SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMCOUNT, 0, 0 );
        }
      }

    }


    /// <summary>
    /// This is an indexer to the text values for each column.
    /// </summary>
    public class ColumnNamesCollection
    {
      readonly ListViewFilterHeader col_hdrctl = null;         // owning header control
      private  HDITEM               col_hditem = new HDITEM(); // HDITEM instance

      /// <summary>
      /// Constructor this must be given the header instance for access
      /// to the Handle property so that messages can be sent to it.
      /// </summary>
      /// <param name="header">HeaderControl</param>
      public ColumnNamesCollection( ListViewFilterHeader header )
      {
        col_hdrctl = header;
      }

      
      /// <summary>
      /// Indexer method to get/set the text of the column.
      /// </summary>
      public string this[ int index ]
      {
        get
        {

          // set up to retrive the column header text
          col_hditem.mask       = W32_HDI.HDI_TEXT;
          col_hditem.pszText    = new string( new char[ 64 ]);
          col_hditem.cchTextMax = col_hditem.pszText.Length;

          // if successful the text has been retrieved and returned
          SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMA, index, ref col_hditem );
          return col_hditem.pszText;
        }
        set
        {

          // this must be a valid column index
          if ( index < this.Count )
          {

            // simply set the text and size in the structure and pass it on
            col_hditem.mask       = W32_HDI.HDI_TEXT;
            col_hditem.pszText    = value;
            col_hditem.cchTextMax = col_hditem.pszText.Length;
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_SETITEMA, index, ref col_hditem );
          }
        }
      }


      /// <summary>
      /// Return the number of columns in the header.
      /// </summary>
      public int Count
      {
        get
        {
          return SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMCOUNT, 0, 0 );
        }
      }

    }


    /// <summary>
    /// This is an indexer to the filter values for each column.  This
    /// is the most complex access to the HDITEM data since the filter
    /// data must be get/set via the HDTEXTFILTER structure referenced
    /// in the HDITEM structure.  It is simple to Marshall this, but
    /// figuring that out in the first place took alot of effort.
    /// </summary>
    public class ColumnFilterCollection
    {
      readonly ListViewFilterHeader col_hdrctl = null;               // owning header control
      private  HDITEM               col_hditem = new HDITEM();       // HDITEM instance
      private  HDTEXTFILTER         col_txtflt = new HDTEXTFILTER(); // HDTEXTFILTER instance

      /// <summary>
      /// Constructor this must be given the header instance for access
      /// to the Handle property so that messages can be sent to it.
      /// </summary>
      /// <param name="header">HeaderControl</param>
      public ColumnFilterCollection( ListViewFilterHeader header )
      {
        col_hdrctl = header;
      }

      
      /// <summary>
      /// Indexer method to get/set the filter text for the column.
      /// </summary>
      public string this[ int index ]
      {
        get
        {

          // if the column is invalid return nothing
          if ( index >= Count ) return "";

          // this is tricky since it involves marshalling pointers
          // to structures that are used as a reference in another
          // structure.  first initialize the receiving HDTEXTFILTER
          col_txtflt.pszText    = new string( new char[ 64 ]);
          col_txtflt.cchTextMax = col_txtflt.pszText.Length;

          // set the HDITEM up to request the current filter content
          col_hditem.mask = W32_HDI.HDI_FILTER;
          col_hditem.type = (uint)W32_HDFT.HDFT_ISSTRING;

          // marshall memory big enough to contain a HDTEXTFILTER 
          col_hditem.pvFilter = Marshal.AllocCoTaskMem( 
            Marshal.SizeOf( col_txtflt ) );

          // now copy the HDTEXTFILTER structure to the marshalled memory
          Marshal.StructureToPtr( col_txtflt, col_hditem.pvFilter, false );

          // retrieve the header filter string as non-wide string
          SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMA, index, ref col_hditem );

          // un-marshall the memory back into the HDTEXTFILTER structure
          col_txtflt = (HDTEXTFILTER)Marshal.PtrToStructure( 
            col_hditem.pvFilter, typeof( HDTEXTFILTER ));

          // remember to free the marshalled IntPtr memory...
          Marshal.FreeCoTaskMem( col_hditem.pvFilter );

          // return the string in the text filter area
          return col_txtflt.pszText;

        }
        set
        {

          // ensure that the column exists before attempting this
          if ( index < this.Count )
          {

            // this is just like the get{} except we don't have to
            // return anything and the message is HDM_SETITEMA. we
            // use the non-unicode methods for both the get and set.
            // reference the get{} method for marshalling details.
            col_txtflt.pszText    = value;
            col_txtflt.cchTextMax = 64;
            col_hditem.mask       = W32_HDI.HDI_FILTER;
            col_hditem.type       = (uint)W32_HDFT.HDFT_ISSTRING;
            col_hditem.pvFilter   = Marshal.AllocCoTaskMem( Marshal.SizeOf( col_txtflt ) );
            Marshal.StructureToPtr( col_txtflt, col_hditem.pvFilter, false );
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_SETITEMA, index, ref col_hditem );
            Marshal.FreeCoTaskMem( col_hditem.pvFilter );
          }
        }
      }


      /// <summary>
      /// Return the number of columns in the header.
      /// </summary>
      public int Count
      {
        get
        {
          return SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMCOUNT, 0, 0 );
        }
      }


    }


    /// <summary>
    /// This is an indexer to the READONLY Size values for a column.
    /// NOTE: The Size really contains the Width and Left position,
    /// the Left is sorted in the Height property of the Size class
    /// We do this because a Rectangle is not really necessary.
    /// </summary>
    public class ColumnSizeInfoCollection
    {
      readonly ListViewFilterHeader col_hdrctl = null;       // owning header control
      private  RECT                 col_rectng = new RECT(); // HDITEM instance

      /// <summary>
      /// Constructor this must be given the header instance for access
      /// to the Handle property so that messages can be sent to it.
      /// </summary>
      /// <param name="header">HeaderControl</param>
      public ColumnSizeInfoCollection( ListViewFilterHeader header )
      {
        col_hdrctl = header;
      }

      
      /// <summary>
      /// Indexer method to get/set the Size for the column.
      /// </summary>
      public Size this[ int index ]
      {
        get
        {

          // if the column is valid get the rectangle
          if ( index < Count )
          {
            SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMRECT,
              index, ref col_rectng );
            return new Size( ( col_rectng.right - col_rectng.left ),
              col_rectng.left );
          }

          // return null size
          else return new Size( 0, 0 );
        }
      }


      /// <summary>
      /// Return the number of columns in the header.
      /// </summary>
      public int Count
      {
        get
        {
          return SendMessage( col_hdrctl.Handle, W32_HDM.HDM_GETITEMCOUNT, 0, 0 );
        }
      }


    }


    #endregion

  }


  /// <summary>
  /// This class is used to compare ListViewItem entries by column
  /// in a particular manner for sorting the by owning ListView.
  /// The standard IComparer is extended with the Compare method
  /// </summary>
  internal class ListViewFilterSorter : IComparer
  {

    #region Private class data

    readonly ListViewFilter cmp_lstvew = null; // owning listview

    #endregion
    
    #region Constructor

    /// <summary>
    /// Constructor, we need the instance of the ListViewFilter.
    /// </summary>
    /// <param name="listview">Owner</param>
    public ListViewFilterSorter( ListViewFilter listview )
    {
      cmp_lstvew = listview;
    }


    #endregion

    #region Public methods

    /// <summary>
    /// Compare function for two given objects.  We use the
    /// internal ItemText and CompareData methods to do the
    /// work of getting the strings.  The listview also has
    /// the column, order, and datatype needed.
    /// </summary>
    /// <param name="o1">First object</param>
    /// <param name="o2">Second object</param>
    /// <returns>-1/0/1 result of comparison</returns>
    public int Compare( object o1, object o2 )
    {
    
      // initialize rezult to equal objects
      int rz = 0;
      
      // we can do nothing without a listview and two objects
      if ( ( o1 != null ) && ( o2 != null ) )
      {

        // get the item/subitem text for both strings
        string s1 = cmp_lstvew.ItemText( 
          (ListViewItem)o1, cmp_lstvew.SortColumn );
        string s2 = cmp_lstvew.ItemText( 
          (ListViewItem)o2, cmp_lstvew.SortColumn );

        // compare the two strings using the data type
        rz = cmp_lstvew.CompareData( s1, s2, cmp_lstvew.SortType, false );

      }

      // return +/- result depending upon sort order
      return ( cmp_lstvew.SortOrder ) ? rz : ( rz * -1 );
    }


    #endregion

  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -