一个功能增强的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

相关文章

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Python实战之屏幕录制功能的实现

《Python实战之屏幕录制功能的实现》屏幕录制,即屏幕捕获,是指将计算机屏幕上的活动记录下来,生成视频文件,本文主要为大家介绍了如何使用Python实现这一功能,希望对大家有所帮助... 目录屏幕录制原理图像捕获音频捕获编码压缩输出保存完整的屏幕录制工具高级功能实时预览增加水印多平台支持屏幕录制原理屏幕

Python实现自动化表单填写功能

《Python实现自动化表单填写功能》在Python中,自动化表单填写可以通过多种库和工具实现,本文将详细介绍常用的自动化表单处理工具,并对它们进行横向比较,可根据需求选择合适的工具,感兴趣的小伙伴跟... 目录1. Selenium简介适用场景示例代码优点缺点2. Playwright简介适用场景示例代码

MySQL中闪回功能的方案讨论及实现

《MySQL中闪回功能的方案讨论及实现》Oracle有一个闪回(flashback)功能,能够用户恢复误操作的数据,这篇文章主要来和大家讨论一下MySQL中支持闪回功能的方案,有需要的可以了解下... 目录1、 闪回的目标2、 无米无炊一3、 无米无炊二4、 演示5、小结oracle有一个闪回(flashb

MySQL使用binlog2sql工具实现在线恢复数据功能

《MySQL使用binlog2sql工具实现在线恢复数据功能》binlog2sql是大众点评开源的一款用于解析MySQLbinlog的工具,根据不同选项,可以得到原始SQL、回滚SQL等,下面我们就来... 目录背景目标步骤准备工作恢复数据结果验证结论背景生产数据库执行 SQL 脚本,一般会经过正规的审批

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

基于Python和Tkinter实现高考倒计时功能

《基于Python和Tkinter实现高考倒计时功能》随着高考的临近,每个考生都在紧锣密鼓地复习,这时候,一款实用的倒计时软件能有效帮助你规划剩余时间,提醒你不要浪费每一分每一秒,今天,我们来聊聊一款... 目录一、软件概述:二、功能亮点:1. 高考倒计时2. 添加目标倒计时3. 励志语句4. 透明度调节与