File: win32wsshellctrls.pp

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (159 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
 *****************************************************************************
 *                              WSShellCtrls.pp                              * 
 *                              -------------                                * 
 *                                                                           *
 *                                                                           *
 *****************************************************************************

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit win32wsshellctrls;

{$mode objfpc}{$H+}
{$I win32defines.inc}

interface

uses
  SysUtils, Classes, ComCtrls, ShellCtrls, Types,
////////////////////////////////////////////////////
// To get as little as posible circles,
// uncomment only when needed for registration
////////////////////////////////////////////////////
//  Graphics, ImgList, Controls, ShellCtrls,
////////////////////////////////////////////////////
  WSShellCtrls;

type

  { TWin32WSCustomShellTreeView }

  TWin32WSCustomShellTreeView = class(TWSCustomShellTreeView)
  published
    class function DrawBuiltInIcon(ATreeView: TCustomShellTreeView; ANode: TTreeNode;
      ARect: TRect): TSize; override;
    class function GetBuiltinIconSize: TSize; override;
  end;

  { TWin32WSCustomShellListView }
  TWin32WSCustomShellListView = class(TWSCustomShellListView)
  published
    class function GetBuiltInImageIndex(AListView: TCustomShellListView;
      const AFileName: String; ALargeImage: Boolean): Integer; override;
  end;

implementation

uses
  windows, shellapi, graphics;

var
  ShellIconSize: TSize = (CX: -1; CY: -1);

function GetShellIcon(const AFileName: WideString): TIcon;
var
  FileInfo: TSHFileInfoW;
  imgHandle: DWORD_PTR;
begin
  imgHandle := SHGetFileInfoW(PWideChar(AFileName), 0, FileInfo, SizeOf(FileInfo),
    SHGFI_ICON or SHGFI_SMALLICON or SHGFI_SYSICONINDEX);
  if imgHandle <> 0 then
  begin
    Result := TIcon.Create;
    Result.Handle := FileInfo.hIcon;
  end else
    Result := nil;
end;


{ TWin32WSCustomShellTreeView }

class function TWin32WSCustomShellTreeView.DrawBuiltInIcon(ATreeView: TCustomShellTreeView;
  ANode: TTreeNode; ARect: TRect): TSize;
var
  filename: WideString;
  ico: TIcon;
begin
  fileName := WideString(ATreeView.GetPathFromNode(ANode));
  ico := GetShellIcon(fileName);
  if ico = nil then
  begin
    Result := Types.Size(0, 0);
    exit;
  end;
  try
    ATreeView.Canvas.Draw(ARect.Left, (ARect.Top + ARect.Bottom - ico.Height) div 2, ico);
    Result := Types.Size(ico.Width, ico.Height);
  finally
    ico.Free;
  end;
end;

class function TWin32WSCustomShellTreeView.GetBuiltinIconSize: TSize;
var
  ico: TIcon;
begin
  if (ShellIconSize.CX = -1) and (ShellIconSize.CY = -1) then
  begin
    ico := GetShellIcon(WideString('C:'));
    if ico = nil then
    begin
      Result := Types.Size(0, 0);
      exit;
    end;
    try
      Result := Types.Size(ico.Width, ico.Height);
      ShellIconSize := Result;
    finally
      ico.Free;
    end;
  end else
    Result := ShellIconSize;
end;


{ TWin32WSCustomShellListView }

class function TWin32WSCustomShellListView.GetBuiltInImageIndex(
  AListView: TCustomShellListView; const AFileName: String;
  ALargeImage: Boolean): Integer;
var
  fullName: WideString;
  info: TSHFILEINFOW;
  sysImageHandle: DWORD_PTR;
  listHandle: HWND;
  flags: DWord;
  lvsil: LongInt;
  attr: LongInt;
begin
  Result := -1;
  fullName := WideString(AFileName);
  attr := FileGetAttr(fullName);
  if ALargeImage then begin
    flags := SHGFI_LARGEICON or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES;
    lvsil := LVSIL_NORMAL;
  end else
  begin
    flags := SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES;
    lvsil := LVSIL_SMALL;
  end;
  sysImageHandle := SHGetFileInfoW(PWideChar(fullName), attr, info, SizeOf(info), flags);
  if sysImageHandle = 0 then
    Exit;
  listHandle := AListView.Handle;
  if ListView_GetImageList(ListHandle, lvsil) = 0 then
  begin
    SetWindowLongPtrW(listHandle, GWL_STYLE,
      GetWindowLong(listHandle, GWL_STYLE) or LVS_SHAREIMAGELISTS);
    ListView_SetImageList(listHandle, sysImageHandle, lvsil);
  end;
  Result := info.iIcon;
end;

end.