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

📄 k5.htm

📁 C++Builder教学大全
💻 HTM
字号:
 

<html>

<head>

<title>在C++Builder中实现ListView的列标头点击排序功能</title>

<meta http-equiv="目录类型" content="文本/html; 字符集=gb2312">

</head>

<body bgcolor="#FFFFFF">

<table width="100%" border="0" height="285">

  <tr> 

    <td height="35"> 

      <div align="center" class="p14"><font color="#000000">在C++Builder中    

        实现ListView的列标头点击排序功能</font><br>   

        <font color="#000000">天津大学电子与信息工程学院 <br>   

        宋天宁</font></div>   

    </td>   

  </tr>   

  <tr valign="top">    

    <td>  

      <p><font color="#000000">---- 列表框控件在应用程序中的使用率很高,在Windows中经常见到,它有四种常见的式样,分别具有显示大图标,小目标,含有图标的多列列表,含有列标头的列表。我们也经常见到一些扩展功能,例如我们在资源管理器中的文件列表框中,在选用详细资料察看时,用鼠标单击列标头,如大小,则所列文件按大小排序;单机日期,则按文件日期进行排序.这一功能增强了应用程序的可用性,使操作更直观、方便。笔者在以Borland    

        C++Builder 3.0(以下简称BCB)作为开发平台实现这一功能时发现,仅通过设定列表框控件的属性值的方法,只能根据项目的首列加以排序,不能根据子项目(SubItems)排序。因此,我们需要编写自己的代码以完成这一功能。</font>   

      <p><font color="#000000">---- 以下描述如何在BCB开发平台上为列表框添加列标头点击排序功能,当我们点击的次数为奇数时,所列内容以降序排列;当我们点击的次数为偶数时,所列内容以升序排列。</font>    

      <p><font color="#000000">---- 首先将一个列表框控件安放在Form上,并将其名称设为ListView1。然后在其中添加若干项目作为试验对象。方法为:用鼠标右键单击控件,在弹出的对话框中选择Columns    

        Editor用来添加列和子列;再选Items Editor用来添加项目(包含子列和主列上的内容)。</font>    

      <p><font color="#000000">---- 为了显示出子项目内容,需要在Object Inspector中修改ListView属性值,将ViewStyle设置为vsReport。另外,还必须将SortType设置为None,以使我们的排序程序起作用。这样,程序的外观已经符合需要,下面应该增加排序功能的代码了。为此需要添加类的方法到源代码中。添加位置为:</font>    

      <pre><font color="#000000">

void __fastcall TForm1::ListView1ColumnClick

(TObject *Sender,

      TListColumn *Column)

</font></pre> 

      <font color="#000000">---- 在该方法的传递参数中,指向TListColumn类型的指针Column中含有用户所点击的列的信息。</font>    

      <p><font color="#000000">---- 下面先定义变量:</font>    

      <pre><font color="#000000">	int i,m,n,j;

	static bool od=true;

	od=!od;

</font></pre> 

      <font color="#000000">---- od代表用户点击的次数,奇数时为true,偶数时为false。注意此处od的存储类型设定为static,可以保证其数值的连续性。用n记录用户点击的列号,m记录列表框中的总项目数。</font>    

      <pre><font color="#000000">	n=Column- &gt;Index;

	m=ListView1- &gt;Items- &gt;Count;

</font></pre> 

      <font color="#000000">---- 在列表框中临时添加一个项目作为排序中交换用的临时空间。</font>    

      <pre><font color="#000000">	ListView1- &gt;Items- &gt;Add();

</font></pre> 

      <font color="#000000">---- 当用户点击第一列列标头时,排序按ListView1- &gt;Items- &gt;Item[i]-    

      &gt;Caption进行,与其它列不同,所以要单独进行排序。</font>    

      <pre><font color="#000000">if (n==0)

   {

  for(i=0;i&lt; m-1;i++)

  for(j=i+1;j&lt; m;j++)

   if(od)

      {

       if(ListView1- &gt;Items- &gt;Item[i]- &gt;Caption &gt;

          ListView1- &gt;Items- &gt;Item[j]- &gt;Caption)

        {

            ListView1- &gt;Items- &gt;Item[m]=

ListView1- &gt;Items- &gt;Item[i];

            ListView1- &gt;Items- &gt;Item[i]=

ListView1- &gt;Items- &gt;Item[j];

            ListView1- &gt;Items- &gt;Item[j]=

ListView1- &gt;Items- &gt;Item[m];

        }

       }

   else

      {

       if(ListView1- &gt;Items- &gt;Item[i]- &gt;Caption&lt; 

          ListView1- &gt;Items- &gt;Item[j]- &gt;Caption)

        {

            ListView1- &gt;Items- &gt;Item[m]=

ListView1- &gt;Items- &gt;Item[i];

            ListView1- &gt;Items- &gt;Item[i]=

ListView1- &gt;Items- &gt;Item[j];

            ListView1- &gt;Items- &gt;Item[j]=

ListView1- &gt;Items- &gt;Item[m];

        }

      }

ListView1- &gt;Items- &gt;Delete(m);

    return;

    }

</font></pre> 

      <font color="#000000">---- 点击其它列时,需要将所点击的列内容从ListView1- &gt;Items-    

      &gt;Item[i]- &gt; SubItems- &gt;Text中抽出,并参照其进行排序。子项目在存储中,形式为_子项目1\r\n子项目2\r\n子项目3\r\n...子项目N\r\n_。为抽取其中某一子项目,需编写如下函数,并将其放于void    

      __fastcall TForm1::ListView1ColumnClick前面即可。</font>    

      <pre><font color="#000000">#include &lt; vcl/dstring.h &gt;

AnsiString __stdcall sg(AnsiString str,int n)

{

    int l,i;

    AnsiString qq(str);

    l=qq.Length();

    AnsiString p(str);

    char a[]=&quot;\r\n&quot;;

    int j=0,k=0;

    for(j=1;j&lt; l;j++)      

     {

        i=1;

        while(qq[j]!=a[0] &amp;&amp; qq[j+1]!=a[1])

          {

            p[i++]=qq[j];

            j++;

           }

        k++;

        if((k-1)==n)

          break;

       }

    p[i]=NULL;

    return (p);

}

</font></pre> 

      <font color="#000000">---- 有了此函数以后,只需将上面的ListView1- &gt;Items- &gt;Item[i]-    

      &gt;Caption更换为sg(ListView1- &gt;Items- &gt;Item[i]- &gt;SubItems- &gt;Text,n-1)即可完成对此项目的点击排序功能。</font>    

      <pre><font color="#000000">for(i=0;i&lt; m-1;i++)

  for(j=i+1;j&lt; m;j++)

   if(od)

      {

       if(sg(ListView1- &gt;Items- &gt;

Item[i]- &gt;SubItems- &gt;Text,n-1) &gt;

          sg(ListView1- &gt;Items- &gt;

Item[j]- &gt;SubItems- &gt;Text,n-1))

// ...以下从略



最后,删除临时的交换项目。

ListView1- &gt;Items- &gt;Delete(m);

</font></pre> 

      <font color="#000000">---- 编译运行程序后,即会看到我们预期的结果。另外,本程序是按照字符串方式进行排序的,如果需要按照数字或其它方式排序,只需进行相应的类型转换即可。理解本程序后,读者即掌握了对ListView控件编程的一条基本思路,对今后使用BCB以及对Windows编程起到良好作用。</font></td>   

  </tr>   

</table>   

<br>   

</body>   

</html>   

⌨️ 快捷键说明

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