国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現listview Group收縮擴展的方法

2020-01-24 01:15:06
字體:
來源:轉載
供稿:網友

本文實例講述了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#相關內容感興趣的讀者可查看本站專題:《C#數據結構與算法教程》、《C#常見控件用法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹巴县| 永靖县| 印江| 武山县| 呼玛县| 共和县| 鄂州市| 永寿县| 休宁县| 晋中市| 清河县| 财经| 青龙| 来宾市| 武宣县| 永清县| 儋州市| 图片| 富源县| 威海市| 呼图壁县| 互助| 灵寿县| 扎兰屯市| 东平县| 永康市| 昆山市| 蓬安县| 南投县| 荃湾区| 珲春市| 崇文区| 广汉市| 沭阳县| 鲁甸县| 黄大仙区| 巴彦县| 涡阳县| 胶州市| 河南省| 沈阳市|