一个功能增强的Delphi TListView组件——TSmartListView

2024-09-06 14:08

本文主要是介绍一个功能增强的Delphi TListView组件——TSmartListView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

unit SmartListView; 
{* |<PRE> 
================================================================================ 
* 单元名称:TSmartListView v1.01 
* 单元作者:HsuChong@hotmail.com 
* 备    注: 
* 开发平台:PWin2003Standard + Delphi 7.1 
* 修改记录:2006.9.12. 
*              
================================================================================ 
|</PRE>} 

interface 

uses 
  Windows, Messages, SysUtils, Classes, ComCtrls, CommCtrl, Graphics; 

type 
  TSmartListView = class(TListView) 
  private 
    FArrowUp: HBITMAP; 
    FArrowDown: HBITMAP; 
    FCurColumn: Integer; 
    FHeaderHandle: HWND; 
    FMsg1: string; 
    FMsg2: string; 
    FCop: string; 
    FBackgroundPicture: TPicture; 
    FSearchStr: string; 
    FSearchTickCount: Double; 
    FColumnSearch: boolean; 
    function GetCop: string; 
    procedure SetCop(const Value: string); 
    procedure SetHeaderBitmap(Value: Integer); 
    procedure SetBackgroundPicture(Value: TPicture); 
    procedure BackgroundPictureChanged(Sender: TObject); 
    procedure LVCustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean); 
    procedure DrawBackgroundPicture; 
  protected 
    procedure WndProc(var Msg: TMessage); override; 
    procedure KeyUp(var Key: Word; Shift: TShiftState); override; 
  public 
    constructor Create(AOwner: TComponent); override; 
    procedure CreateWnd; override; 
    destructor Destroy; override; 
    procedure SaveToFile(const FileName: string); 
    procedure LoadFromFile(const FileName: string); 
    function SaveToHTMLFile(const FileName: string; Center: Boolean): Boolean; 
    function SaveToExcelFile(const FileName: string): Boolean; 
    function GetCheckedItem: TListItem; 
    function MultiChecked: Boolean; 
    function IsChecked: Boolean; 
    procedure CheckAll(Checked: Boolean); 
    procedure MoveItem(OriginalIndex, NewIndex: Integer); 
    function StringSelect(const FindStr: string; ColumnIndex: Integer): boolean; 
    function SubStringSelect(const FindStr: string; ColumnIndex: Integer): boolean; 
  published 
    property Msg1: string read FMsg1 write FMsg1; 
    property Msg2: string read FMsg2 write FMsg2; 
    property BackgroundPicture: TPicture read FBackgroundPicture write SetBackgroundPicture; 
    property ColumnSearch: boolean read FColumnSearch write FColumnSearch default False; 
    property Copyright: string read GetCop write SetCop; 
  end; 

procedure Register; 

implementation 

{$R SmartListView.res} 

procedure Register; 
begin 
  RegisterComponents('FHTGPS', [TSmartListView]); 
end; 

//general Sort function 

function CustomSortProc(Item1, Item2: TListItem; lParam: LongInt): Integer; stdcall; 
begin 
  Result := 0; 
  if (Item1 = nil) or (Item2 = nil) then 
    Exit; 
  if lParam = 0 then 
    Result := CompareText(Item1.Caption, Item2.Caption) 
  else if lparam > 0 then 
  begin 
    if (LParam > Item1.SubItems.Count) or (LParam > Item2.SubItems.Count) then 
      Exit; 
    Result := CompareText(Item1.SubItems[Lparam - 1], Item2.SubItems[Lparam - 1]); 
  end; 
  Result := Result * Item1.ListView.Column[lParam].Tag; 
end; 

constructor TSmartListView.Create(AOwner: TComponent); 
begin 
  inherited Create(AOwner); 
  FBackgroundPicture := TPicture.Create; 
  FBackgroundPicture.OnChange := BackgroundPictureChanged; 
  OnCustomDraw := LVCustomDraw; 
  FArrowUp := LoadImage(hInstance, 'ArrowUp', IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); 
  FArrowDown := LoadImage(hInstance, 'ArrowDown', IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); 
  Msg1 := 'File "%s" does not exist!'; 
  Msg2 := '"%s" is not a ListView file!'; 
  FCop := 'Copyright(C) 2006 by HsuChong@hotmail.com '; 
  FHeaderHandle := 0; 
  FSearchStr := ''; 
  FSearchTickCount := 0; 
  FCurColumn := 0; 
end; 

procedure TSmartListView.CreateWnd; 
begin 
  inherited CreateWnd; 
  if HandleAllocated then 
    HandleNeeded; 
  FHeaderHandle := ListView_GetHeader(Handle); 
end; 

destructor TSmartListView.Destroy; 
begin 
  DeleteObject(FArrowUp); 
  DeleteObject(FArrowDown); 
  FBackgroundPicture.Free; 
  inherited Destroy; 
end; 

procedure TSmartListView.SetHeaderBitmap(Value: Integer); 
var 
  HdItem: THdItem; 
begin 
  FillChar(HdItem, SizeOf(HdItem), #0); 

  HdItem.Mask := HDI_FORMAT; 
  Header_GetItem(FHeaderHandle, Value, HdItem); 
  HdItem.Mask := HDI_BITMAP or HDI_FORMAT; 

  if Column[Value].Tag = -1 then 
  begin                                 //reverse arrow 反向 
    HdItem.fmt := HdItem.fmt or HDF_BITMAP or HDF_BITMAP_ON_RIGHT; 
    HdItem.hbm := FArrowDown; 
  end 
  else if Column[Value].Tag = 1 then 
  begin                                 //obverse arrow 正向 
    HdItem.fmt := HdItem.fmt or HDF_BITMAP or HDF_BITMAP_ON_RIGHT; 
    HdItem.hbm := FArrowUp; 
  end 
  else if Column[Value].Tag = 0 then 
  begin                                 // clear arrow 消除箭头 
    HdItem.fmt := HdItem.fmt and not (HDF_BITMAP or HDF_BITMAP_ON_RIGHT); 
    HdItem.hbm := 0; 
  end; 
  Header_SetItem(FHeaderHandle, Value, HdItem); 
end; 

procedure TSmartListView.WndProc(var Msg: TMessage); 
var 
  pHD: PHDNotify; 
  I: Integer; 
begin 
  inherited WndProc(Msg); 
  //如果截获的消息是WM_NOTIFY 
  if Msg.Msg = WM_NOTIFY then 
  begin 
    pHD := PHDNotify(Msg.LParam); 
    if (pHD.Hdr.hwndFrom = FHeaderHandle) and (FHeaderHandle <> 0) then 
    begin 
      case pHD.HDr.code of 
        // 如果是点击Header 
        HDN_ITEMCLICK, HDN_ITEMCLICKW: 
          begin 
            FCurColumn := Columns.Items[pHD.item].Index; 
            // 做标记,正向或反向排序 
            for I := 0 to Columns.Count - 1 do 
            begin 
              if I = FCurColumn then 
              begin 
                if Column[I].Tag = 0 then 
                  Column[I].Tag := 1 
                else 
                  Column[I].Tag := -1 * Column[I].Tag; 
                SetHeaderBitmap(I); 
              end 
              else 
              begin 
                if Column[I].Tag <> 0 then 
                begin 
                  Column[I].Tag := 0; 
                  SetHeaderBitmap(I); 
                end; 
              end; 
            end;                        {of for} 
            //排序 
            CustomSort(@CustomSortProc, FCurColumn); 
          end; 
        // 拖动改变宽度时,ColumnItem <> 原来排序的列 
        HDN_ENDTRACK, HDN_ENDTRACKW: 
          begin 
            FCurColumn := Columns.Items[pHD.item].Index; 
            if Columns[FCurColumn].Tag <> 0 then 
              SetHeaderBitmap(FCurColumn); 
          end; 
      end; 
    end; 
  end;                                  // end if 
end; 

procedure TSmartListView.SaveToFile(const FileName: string); 
var 
  idxItem, idxSub, IdxImage: Integer; 
  Stream: TFileStream; 
  pText: pChar; 
  sText: string; 
  W, ItemCount, SubCount: word; 
  MySignature: array[0..2] of char; 
begin 
  //Initialization 
  ItemCount := 0; 
  SubCount := 0; 
  //**** 
  MySignature := 'LVF';                 //  ListViewFile 
  Stream := TFileStream.Create(FileName, fmCreate or fmOpenWrite); 
  try 
    Stream.Write(MySignature, sizeof(MySignature)); 
    if Items.Count = 0 then 
      ItemCount := 0 
    else 
      ItemCount := Items.Count; 
    Stream.Write(ItemCount, Sizeof(ItemCount)); 

    if Items.Count > 0 then 
    begin 
      for idxItem := 1 to ItemCount do 
      begin 
        with items[idxItem - 1] do 
        begin 
          //Save subitems count 
          if SubItems.Count = 0 then 
            SubCount := 0 
          else 
            SubCount := Subitems.Count; 
          Stream.Write(SubCount, Sizeof(SubCount)); 
          //Save ImageIndex 
          IdxImage := ImageIndex; 
          Stream.Write(IdxImage, Sizeof(IdxImage)); 
          //Save Caption 
          sText := Caption; 
          w := length(sText); 
          pText := StrAlloc(Length(sText) + 1); 
          StrPLCopy(pText, sText, Length(sText)); 
          Stream.Write(w, sizeof(w)); 
          Stream.Write(pText^, w); 
          StrDispose(pText); 
          if SubCount > 0 then 
          begin 
            for idxSub := 0 to SubItems.Count - 1 do 
            begin                       //Save Item's subitems 
              sText := SubItems[idxSub]; 
              w := length(sText); 
              pText := StrAlloc(Length(sText) + 1); 
              StrPLCopy(pText, sText, Length(sText)); 
              Stream.Write(w, sizeof(w)); 
              Stream.Write(pText^, w); 
              StrDispose(pText); 
            end; 
          end; 
        end; 
      end; 
    end; 
  finally 
    FreeAndNil(Stream); 
  end; 
end; 

procedure TSmartListView.LoadFromFile(const FileName: string); 
var 
  Stream: TStream; 
  IdxItem, IdxSubItem, IdxImage: Integer; 
  W, ItemCount, SubCount: Word; 
  pText: pchar; 
  PTemp: pChar; 
  MySignature: array[0..2] of Char; 
  sExeName: string; 
begin 
  ItemCount := 0; 
  SubCount := 0; 
  sExeName := ExtractFileName(FileName); 
  if not FileExists(FileName) then 
  begin 
    MessageBox(Handle, pChar(format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR); 
    Exit; 
  end; 
  Stream := TFileStream.Create(FileName, fmOpenRead); 
  try 
    Stream.Read(MySignature, sizeof(MySignature)); 
    if MySignature <> 'LVF' then 
    begin 
      MessageBox(Handle, pChar(format(Msg2, [sExeName])), 'I/O Error', MB_ICONERROR); 
      Exit; 
    end; 
    Stream.Read(ItemCount, sizeof(ItemCount)); 
    Items.Clear; 
    Items.BeginUpdate; 
    for idxItem := 1 to ItemCount do 
    begin 
      with Items.Add do 
      begin 
        //Read imageindex 
        Stream.Read(SubCount, sizeof(SubCount)); 
        //Read imageindex 
        Stream.Read(IdxImage, sizeof(IdxImage)); 
        ImageIndex := IdxImage; 
        //Read the Caption 
        Stream.Read(w, SizeOf(w)); 
        pText := StrAlloc(w + 1); 
        pTemp := StrAlloc(w + 1); 
        Stream.Read(pTemp^, W); 
        StrLCopy(pText, pTemp, W); 
        Caption := StrPas(pText); 
        StrDispose(pTemp); 
        StrDispose(pText); 
        if SubCount > 0 then 
        begin 
          for idxSubItem := 1 to SubCount do 
          begin 
            Stream.Read(w, SizeOf(w)); 
            pText := StrAlloc(w + 1); 
            pTemp := StrAlloc(w + 1); 
            Stream.Read(pTemp^, W); 
            StrLCopy(pText, pTemp, W); 
            Items[idxItem - 1].SubItems.Add(StrPas(pText)); 
            StrDispose(pTemp); 
            StrDispose(pText); 
          end; 
        end; 
      end; 
    end; 
  finally 
    Items.EndUpdate; 
    FreeAndNil(Stream); 
  end; 

end; 

{Save a TListView as an HTML page} 
{This Code from http://www.swissdelphicenter.ch/ Autor: Robert Muth  } 

function TSmartListView.SaveToHTMLFile(const FileName: string; Center: Boolean): Boolean; 
var 
  i, j: Integer; 
  tfile: TextFile; 
begin 
  try 
    ForceDirectories(ExtractFilePath(FileName)); 
    AssignFile(tfile, FileName); 
    try 
      ReWrite(tfile); 
      WriteLn(tfile, '<html>'); 
      WriteLn(tfile, '<head>'); 
      WriteLn(tfile, '<title>HTML-Ansicht: ' + FileName + '</title>'); 
      WriteLn(tfile, '</head>'); 
      // WriteLn(tfile, '<table border="1" bordercolor="#000000">'); 
      // Modified by HsuChong <Hsuchong@hotmail.com> 2006-12-13 10:03:06 
      WriteLn(tfile, '<table border=1 cellspacing=0 cellpadding=0 bordercolor="#000000">'); 
      WriteLn(tfile, '<tr>'); 
      for i := 0 to Columns.Count - 1 do 
      begin 
        if center then 
          WriteLn(tfile, '<td><b><center>' + Columns[i].Caption + '</center></b></td>') 
        else 
          WriteLn(tfile, '<td><b>' + Columns[i].Caption + '</b></td>'); 
      end; 
      WriteLn(tfile, '</tr>'); 
      WriteLn(tfile, '<tr>'); 
      for i := 0 to Items.Count - 1 do 
      begin 
        WriteLn(tfile, '<td>' + Items.Item[i].Caption + '</td>'); 
        for j := 0 to Columns.Count - 2 do 
        begin 
          if Items.Item[i].SubItems[j] = '' then 
            Write(tfile, '<td>-</td>') 
          else 
            Write(tfile, '<td>' + Items.Item[i].SubItems[j] + '</td>'); 
        end; 
        Write(tfile, '</tr>'); 
      end; 
      WriteLn(tfile, '</table>'); 
      WriteLn(tfile, '</html>

这篇关于一个功能增强的Delphi TListView组件——TSmartListView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142220

相关文章

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

使用EasyPoi快速导出Word文档功能的实现步骤

《使用EasyPoi快速导出Word文档功能的实现步骤》EasyPoi是一个基于ApachePOI的开源Java工具库,旨在简化Excel和Word文档的操作,本文将详细介绍如何使用EasyPoi快速... 目录一、准备工作1、引入依赖二、准备好一个word模版文件三、编写导出方法的工具类四、在Export

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

C#实现高性能拍照与水印添加功能完整方案

《C#实现高性能拍照与水印添加功能完整方案》在工业检测、质量追溯等应用场景中,经常需要对产品进行拍照并添加相关信息水印,本文将详细介绍如何使用C#实现一个高性能的拍照和水印添加功能,包含完整的代码实现... 目录1. 概述2. 功能架构设计3. 核心代码实现python3.1 主拍照方法3.2 安全HBIT

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功