程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成listview Group壓縮擴大的辦法

C#完成listview Group壓縮擴大的辦法

編輯:C#入門知識

C#完成listview Group壓縮擴大的辦法。本站提示廣大學習愛好者:(C#完成listview Group壓縮擴大的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成listview Group壓縮擴大的辦法正文


本文實例講述了C#完成listview Group壓縮擴大的辦法。分享給年夜家供年夜家參考,詳細以下:

1、本實例是完美了codeprofect下面charju先生“Add Group Collapse Behavior on a Listview Control”的一個限制(點擊分組前面的圖標不克不及壓縮和擴大);

2、本實列實用於win2008,vista;

3、僅供參考,若有更好的辦法,望年夜家不惜交換~

完全代碼以下(只需建一個windows工程,在窗體上拖一個listview控件,取名為aoc,右擊編纂代碼,把上面的代碼粘到窗口便可以了~,但須要留意事宜對應):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ListViewGroup
{
  public partial class MainForm : Form
  {
    public MainForm()
    {
      InitializeComponent();
    }
    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr window, int message, int wParam, ref LVHITTESTINFO lParam);
    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr window, int message, int wParam, IntPtr lParam);
    private void btCollapse_Click(object sender, EventArgs e)
    {
      SetGroupCollapse(GroupState.COLLAPSED | GroupState.COLLAPSIBLE);
    }
    private void btExpand_Click(object sender, EventArgs e)
    {
      SetGroupCollapse(GroupState.EXPANDED | GroupState.COLLAPSIBLE);
    }
    private void SetGroupCollapse(GroupState state)
    {
      for (int i = 0; i <= aoc.Groups.Count; i++)
      {
        LVGROUP group = new LVGROUP();
        group.cbSize = Marshal.SizeOf(group);
        group.state = (int)state; // LVGS_COLLAPSIBLE 
        group.mask = 4; // LVGF_STATE 
        group.iGroupId = i;
        IntPtr ip = IntPtr.Zero;
        try
        {
          ip = Marshal.AllocHGlobal(group.cbSize);
          Marshal.StructureToPtr(group, ip, true);
          SendMessage(aoc.Handle, 0x1000 + 147, i, ip); // #define LVM_SETGROUPINFO (LVM_FIRST + 147) 
        }
        catch (Exception ex)
        {
          System.Diagnostics.Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
        }
        finally
        {
          if (null != ip) Marshal.FreeHGlobal(ip);
        }
      }
    }
    private void MainForm_Load(object sender, EventArgs e)
    {
      SetGroupCollapse(GroupState.COLLAPSIBLE);
      for (int i = 0; i < aoc.Groups.Count; i++)
      {
        aoc.Groups[i].Tag = "EXPANDED";
      }
    }
    private void aoc_MouseDown(object sender, MouseEventArgs e)
    {
      LVHITTESTINFO lvHitInfo = new LVHITTESTINFO();
      Point p = new Point(e.X, e.Y);
      lvHitInfo.pt = p;
      try
      {
        int id = SendMessage(aoc.Handle, 0x1000 + 18, -1, ref lvHitInfo);
        if (lvHitInfo.flags == 0x50000000)
        {
          if (aoc.Groups[id].Tag.ToString() =="EXPANDED")
          {
            SetGroupCollapseEx(id, GroupState.COLLAPSED | GroupState.COLLAPSIBLE);
            aoc.Groups[id].Tag = "COLLAPSED";
          }
          else if ( aoc.Groups[id].Tag.ToString() == "COLLAPSED")
          {
            SetGroupCollapseEx(id, GroupState.EXPANDED | GroupState.COLLAPSIBLE);
            aoc.Groups[id].Tag = "EXPANDED";
          }
        }
        //MessageBox.Show(string.Format("RESULT={0}   FLAGS=0x{1:X}", id, lvHitInfo.flags));
      }
      catch (Exception ex)
      {
        Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
      }
      finally
      {
        ;
      }
    }
    private void SetGroupCollapseEx(int id, GroupState groupState)
    {
      int i = id;
      LVGROUP group = new LVGROUP();
      group.cbSize = Marshal.SizeOf(group);
      group.state = (int)groupState; // LVGS_COLLAPSIBLE 
      group.mask = 4; // LVGF_STATE 
      group.iGroupId = i;
      IntPtr ip = IntPtr.Zero;
      try
      {
        ip = Marshal.AllocHGlobal(group.cbSize);
        Marshal.StructureToPtr(group, ip, true);
        SendMessage(aoc.Handle, 0x1000 + 147, i, ip); // #define LVM_SETGROUPINFO (LVM_FIRST + 147) 
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
      }
      finally
      {
        if (null != ip) Marshal.FreeHGlobal(ip);
      }
    }
  }
  [StructLayout(LayoutKind.Sequential)]
  public struct LVGROUP
  {
    public int cbSize;
    public int mask;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string pszHeader;
    public int cchHeader;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string pszFooter;
    public int cchFooter;
    public int iGroupId;
    public int stateMask;
    public int state;
    public int uAlign;
  }
  public enum GroupState
  {
    COLLAPSIBLE = 8,
    COLLAPSED = 1,
    EXPANDED = 0
  }
  [StructLayout(LayoutKind.Sequential)]
  public struct LVHITTESTINFO
  {
    public Point pt;
    public int flags;
    public int iItem;
    public int iSubItem;
    public int iGroup;
  }
}

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved