CnListBox.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2018 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnListBox;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:界面控件包
  24. * 单元名称:界面控件包自画 ListBox 实现单元
  25. * 单元作者:匿名 + 网上代码移植
  26. * 备 注:该界面效果类似于 CnWizards 的设置对话框中的列表框效果。
  27. * 每行字符串中可用 \n 来控制换行
  28. * 开发平台:PWin98SE + Delphi 5.0
  29. * 兼容测试:PWin9X/2000/XP + Delphi 5/6
  30. * 本 地 化:该单元中的字符串均符合本地化处理方式
  31. * 单元标识:$Id$
  32. * 修改记录:2008.06.04 V0.1
  33. * 实现单元
  34. ================================================================================
  35. |</PRE>}
  36. interface
  37. {$I CnPack.inc}
  38. uses
  39. SysUtils, Classes, Controls, StdCtrls, Graphics, Windows;
  40. type
  41. TCnListBox = class(TListBox)
  42. private
  43. FTextColor: TColor;
  44. FBackColor: TColor;
  45. FItemBackColor: TColor;
  46. FItemFrameColor: TColor;
  47. FSelectedBackColor: TColor;
  48. FSelectedTextColor: TColor;
  49. FImages: TImageList;
  50. FRoundWidth: Integer;
  51. FSelectedList: TStrings;
  52. FSubList: TStrings;
  53. function GetSelectedList: TStrings;
  54. procedure ListDrawItem(Control: TWinControl; Index: Integer;
  55. Rect: TRect; State: TOwnerDrawState);
  56. procedure SetImages(const Value: TImageList);
  57. procedure SetBackColor(const Value: TColor);
  58. procedure SetItemBackColor(const Value: TColor);
  59. procedure SetItemFrameColor(const Value: TColor);
  60. procedure SetSelectedBackColor(const Value: TColor);
  61. procedure SetSelectedTextColor(const Value: TColor);
  62. procedure SetTextColor(const Value: TColor);
  63. procedure SetRoundWidth(const Value: Integer);
  64. protected
  65. procedure DrawItem(Index: Integer; Rect: TRect;
  66. State: TOwnerDrawState); override;
  67. public
  68. property SelectedList: TStrings read GetSelectedList;
  69. {* 多选时获得的选中的字符串列表}
  70. constructor Create(AOwner: TComponent); override;
  71. destructor Destroy; override;
  72. published
  73. property RoundWidth: Integer read FRoundWidth write SetRoundWidth default 8;
  74. {* Item 自画的圆角矩形的圆角宽度,0 为不圆角}
  75. property BackColor: TColor Read FBackColor Write SetBackColor default clWhite;
  76. {* 背景颜色}
  77. property TextColor: TColor Read FTextColor Write SetTextColor default clBlack;
  78. {* 文字颜色}
  79. property ItemBackColor: TColor Read FItemBackColor Write SetItemBackColor default TColor($00FFF7F7);
  80. {* Item 的背景颜色}
  81. property ItemFrameColor: TColor Read FItemFrameColor Write SetItemFrameColor default TColor($00131315);
  82. {* Item 的边框颜色}
  83. property SelectedBackColor: TColor Read FSelectedBackColor Write SetSelectedBackColor default TColor($00FFB2B5);
  84. {* 选中的 Item 的背景颜色}
  85. property SelectedTextColor: TColor Read FSelectedTextColor Write SetSelectedTextColor default clBlue;
  86. {* 选中的 Item 的文字颜色}
  87. property Images: TImageList Read FImages Write SetImages;
  88. {* 外接的 ImageList,图标可供绘制}
  89. end;
  90. implementation
  91. procedure SplitString(Source, Deli: string; List: TStrings);
  92. var
  93. EndOfCurrentString: byte;
  94. begin
  95. List.Clear;
  96. while Pos(Deli, Source) > 0 do
  97. begin
  98. EndOfCurrentString := Pos(Deli, Source);
  99. List.Add(Copy(Source, 1, EndOfCurrentString - 1));
  100. Source := Copy(Source, EndOfCurrentString + Length(Deli),
  101. Length(Source) - EndOfCurrentString);
  102. end;
  103. List.Add(Source);
  104. end;
  105. { TCnListBox }
  106. constructor TCnListBox.Create(AOwner: TComponent);
  107. begin
  108. inherited Create(AOwner);
  109. Style := lbOwnerDrawFixed;
  110. Ctl3D := False;
  111. ItemHeight := 50;
  112. FRoundWidth := 8;
  113. BackColor := clWindow;
  114. Color := BackColor;
  115. TextColor := clBlack;
  116. ItemBackColor := TColor($00FFF7F7);
  117. ItemFrameColor := TColor($00131315);
  118. SelectedBackColor := TColor($00FFB2B5);
  119. SelectedTextColor := clBlue;
  120. end;
  121. destructor TCnListBox.Destroy;
  122. begin
  123. FSelectedList.Free;
  124. FSubList.Free;
  125. inherited Destroy;
  126. end;
  127. procedure TCnListBox.DrawItem(Index: Integer; Rect: TRect;
  128. State: TOwnerDrawState);
  129. begin
  130. if Assigned(OnDrawItem) then
  131. OnDrawItem(Self, Index, Rect, State)
  132. else
  133. ListDrawItem(Self, Index, Rect, State);
  134. end;
  135. function TCnListBox.GetSelectedList: TStrings;
  136. begin
  137. if FSelectedList = nil then
  138. FSelectedList := TStringList.Create;
  139. if (Items.Count > 0) and (ItemIndex > -1) then
  140. SplitString(Items[ItemIndex], '\n', FSelectedList);
  141. Result := FSelectedList;
  142. end;
  143. procedure TCnListBox.ListDrawItem(Control: TWinControl; Index: Integer;
  144. Rect: TRect; State: TOwnerDrawState);
  145. var
  146. useImage: Boolean;
  147. itemLeft: Integer;
  148. subItemHeight: Integer;
  149. i: Integer;
  150. begin
  151. Canvas.Brush.Color := BackColor;
  152. if Enabled then
  153. Canvas.Font.Color := TextColor
  154. else
  155. Canvas.Font.Color := clGray;
  156. Canvas.FillRect(Rect);
  157. Canvas.Brush.Color := ItemBackColor;
  158. Canvas.Pen.Color := ItemFrameColor;
  159. Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
  160. Rect.Right - 2, Rect.Bottom - 2, FRoundWidth, FRoundWidth);
  161. Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
  162. Rect.Right - 3, Rect.Bottom - 3, FRoundWidth, FRoundWidth);
  163. if (odSelected in State) then
  164. begin
  165. Canvas.Brush.Color := SelectedBackColor;
  166. Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
  167. Rect.Right - 3, Rect.Bottom - 3, FRoundWidth, FRoundWidth);
  168. Canvas.Font.Color := SelectedTextColor;
  169. if (odFocused in State) then
  170. DrawFocusRect(Canvas.Handle, Rect);
  171. end;
  172. if Images = nil then
  173. useImage := False
  174. else
  175. useImage := True;
  176. if useImage then
  177. Images.Draw(
  178. Canvas,
  179. Rect.Left + 7,
  180. Rect.top + (ItemHeight - Images.Height) div 2,
  181. Index);
  182. if FSubList = nil then
  183. FSubList := TStringList.Create;
  184. SplitString(Items[index], '\n', FSubList);
  185. if useImage then
  186. itemLeft := Rect.Left + ItemHeight - 4
  187. else
  188. itemLeft := Rect.Left + 10;
  189. subItemHeight := (ItemHeight - 8) div FSubList.Count;
  190. for i := 0 to FSubList.Count - 1 do
  191. begin
  192. Canvas.TextOut(itemLeft, rect.Top + 4 + (i * subItemHeight),
  193. FSubList[i]);
  194. end;
  195. end;
  196. procedure TCnListBox.SetBackColor(const Value: TColor);
  197. begin
  198. if FBackColor <> Value then
  199. begin
  200. FBackColor := Value;
  201. Invalidate;
  202. end;
  203. end;
  204. procedure TCnListBox.SetImages(const Value: TImageList);
  205. begin
  206. if FImages <> Value then
  207. begin
  208. FImages := Value;
  209. Invalidate;
  210. end;
  211. end;
  212. procedure TCnListBox.SetItemBackColor(const Value: TColor);
  213. begin
  214. if FItemBackColor <> Value then
  215. begin
  216. FItemBackColor := Value;
  217. Invalidate;
  218. end;
  219. end;
  220. procedure TCnListBox.SetItemFrameColor(const Value: TColor);
  221. begin
  222. if FItemFrameColor <> Value then
  223. begin
  224. FItemFrameColor := Value;
  225. Invalidate;
  226. end;
  227. end;
  228. procedure TCnListBox.SetRoundWidth(const Value: Integer);
  229. begin
  230. if FRoundWidth <> Value then
  231. begin
  232. FRoundWidth := Value;
  233. Invalidate;
  234. end;
  235. end;
  236. procedure TCnListBox.SetSelectedBackColor(const Value: TColor);
  237. begin
  238. if FSelectedBackColor <> Value then
  239. begin
  240. FSelectedBackColor := Value;
  241. Invalidate;
  242. end;
  243. end;
  244. procedure TCnListBox.SetSelectedTextColor(const Value: TColor);
  245. begin
  246. if FSelectedTextColor <> Value then
  247. begin
  248. FSelectedTextColor := Value;
  249. Invalidate;
  250. end;
  251. end;
  252. procedure TCnListBox.SetTextColor(const Value: TColor);
  253. begin
  254. if FTextColor <> Value then
  255. begin
  256. FTextColor := Value;
  257. Invalidate;
  258. end;
  259. end;
  260. end.