BitmapButton.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. unit BitmapButton;
  2. interface
  3. uses
  4. Windows, Messages, RealICQUIColor, RealICQColors,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Buttons;
  5. type
  6. TBitmapButton = class(TGraphicControl,IRealICQUIColor)
  7. private
  8. FBitmap: TBitmap;
  9. FLighter: TBitmap;
  10. FDarker: Tbitmap;
  11. FPushDown:boolean;
  12. FMouseOver:boolean;
  13. FLatching: boolean;
  14. FDown: boolean;
  15. FHotTrack: boolean;
  16. procedure SetBitmap(const Value: TBitmap);
  17. procedure MakeDarker;
  18. procedure MakeLighter;
  19. procedure SetLatching(const Value: boolean);
  20. procedure SetDown(const Value: boolean);
  21. procedure SetHotTrack(const Value: boolean);
  22. { Private declarations }
  23. protected
  24. { Protected declarations }
  25. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
  26. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
  27. procedure MouseMove(Shift: TShiftState; X, Y: Integer);override;
  28. procedure Click;override;
  29. procedure CMMouseLeave(var Message:TMessage); message CM_MouseLeave;
  30. procedure Loaded;override;
  31. procedure Resize;override;
  32. public
  33. { Public declarations }
  34. constructor Create(AOwner:TComponent);override;
  35. destructor Destroy;override;
  36. procedure Paint; override;
  37. procedure ChangeUIColor(AColor: TColor);
  38. published
  39. { Published declarations }
  40. property Align;
  41. property Bitmap:TBitmap read FBitmap write SetBitmap;
  42. property Down:boolean read FDown write SetDown;
  43. property Latching:boolean read FLatching write SetLatching;
  44. property HotTrack:boolean read FHotTrack write SetHotTrack;
  45. property OnClick;
  46. property ShowHint;
  47. property Hint;
  48. property OnmouseDown;
  49. property OnMouseUp;
  50. property OnMouseMove;
  51. end;
  52. procedure Register;
  53. implementation
  54. { TBitmapButton }
  55. procedure TBitmapButton.Click;
  56. begin
  57. inherited Click;
  58. // if FPushDown then
  59. // if assigned(onclick) then
  60. // OnClick(self);
  61. end;
  62. constructor TBitmapButton.Create(AOwner: TComponent);
  63. begin
  64. inherited;
  65. width:=24;
  66. height:=24;
  67. FPushDown:=false;
  68. FMouseOver:=false;
  69. FLatching:=false;
  70. FHotTrack:=true;
  71. FDown:=false;
  72. FBitmap:=TBitmap.create;
  73. Fbitmap.width:=24;
  74. Fbitmap.Height:=24;
  75. Fbitmap.canvas.brush.color:=clgray;
  76. FBitmap.canvas.FillRect (rect(1,1,23,23));
  77. FLighter:=Tbitmap.create;
  78. FDarker:=Tbitmap.create;
  79. end;
  80. destructor TBitmapButton.Destroy;
  81. begin
  82. FBitmap.free;
  83. FLighter.free;
  84. FDarker.free;
  85. inherited;
  86. end;
  87. procedure TBitmapButton.MouseDown(Button: TMouseButton;
  88. Shift: TShiftState; X, Y: Integer);
  89. begin
  90. inherited;
  91. if FBitmap.canvas.pixels[x,y]<>Fbitmap.canvas.pixels[0,FBitmap.height-1] then
  92. FPushDown:=true
  93. else
  94. FPushDown:=false;
  95. Paint;
  96. // if assigned(OnMouseDown) then
  97. // OnMouseDown(self,button,shift,x,y);
  98. end;
  99. procedure TBitmapButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
  100. X, Y: Integer);
  101. begin
  102. inherited MouseUp(Button, Shift, X, Y);
  103. FPushDown:=false;
  104. if Latching then
  105. FDown:= not FDown
  106. else
  107. FDown:=false;
  108. Paint;
  109. // if assigned(OnMouseUp) then
  110. // OnMouseUp(self,button,shift,x,y);
  111. end;
  112. procedure TBitmapButton.Paint;
  113. var Acolor:TColor;
  114. begin
  115. inherited;
  116. if assigned(FBitmap) then
  117. begin
  118. AColor:=FBitmap.canvas.pixels[0,FBitmap.height-1];
  119. Fbitmap.transparent:=true;
  120. Fbitmap.transparentcolor:=Acolor;
  121. FLighter.transparent:=true;
  122. Flighter.TransparentColor :=AColor;
  123. FDarker.transparent:=true;
  124. FDarker.TransparentColor :=AColor;
  125. if FPushdown then
  126. begin
  127. canvas.draw(1,1,FBitmap)// FDarker
  128. end
  129. else
  130. begin
  131. if Down then
  132. canvas.Draw(1,1,FBitmap)
  133. else if (FMouseOver and FHotTrack) then
  134. canvas.draw(0,0,FLighter)
  135. else
  136. canvas.Draw (0,0,FBitmap);
  137. end;
  138. end;
  139. end;
  140. procedure TBitmapButton.SetBitmap(const Value: TBitmap);
  141. begin
  142. FBitmap.assign(Value);
  143. FBitmap.transparent:=true;
  144. FBitmap.TransparentColor :=FBitmap.Canvas.pixels[0,FBitmap.Height-1];
  145. width:=FBitmap.Width ;
  146. height:=FBitmap.Height ;
  147. MakeLighter;
  148. MakeDarker;
  149. end;
  150. procedure TBitmapButton.MakeLighter;
  151. var p1,p2:Pbytearray;
  152. x,y:integer;
  153. rt,gt,bt:byte;
  154. r,g,b:byte;
  155. AColor:TColor;
  156. begin
  157. FLighter.Width :=FBitmap.Width ;
  158. FLighter.Height :=FBitmap.height;
  159. Acolor:=colortorgb(FBitmap.canvas.pixels[0,FBitmap.height-1]);
  160. rt:=GetRValue(Acolor);
  161. gt:=GetGValue(AColor);
  162. bt:=getBValue(AColor);
  163. FBitmap.PixelFormat :=pf24bit;
  164. FLighter.PixelFormat :=pf24bit;
  165. for y:=0 to Fbitmap.height-1 do
  166. begin
  167. p1:=Fbitmap.ScanLine [y];
  168. p2:=FLighter.ScanLine [y];
  169. for x:=0 to FBitmap.width-1 do
  170. begin
  171. if (p1[x*3]=bt)and (p1[x*3+1]=gt)and (p1[x*3+2]=rt) then
  172. begin
  173. p2[x*3]:=p1[x*3];
  174. p2[x*3+1]:=p1[x*3+1];
  175. p2[x*3+2]:=p1[x*3+2];
  176. end
  177. else
  178. begin
  179. p2[x*3]:=$FF-round(0.8*abs($FF-p1[x*3]));
  180. p2[x*3+1]:=$FF-round(0.8*abs($FF-p1[x*3+1]));
  181. p2[x*3+2]:=$FF-round(0.8*abs($FF-p1[x*3+2]));
  182. end;
  183. end;
  184. end;
  185. end;
  186. procedure TBitmapButton.MakeDarker;
  187. var p1,p2:Pbytearray;
  188. x,y:integer;
  189. rt,gt,bt:byte;
  190. r,g,b:byte;
  191. AColor:TColor;
  192. begin
  193. FDarker.Width :=FBitmap.Width ;
  194. FDarker.Height :=FBitmap.height;
  195. Acolor:=colortorgb(FBitmap.canvas.pixels[0,FBitmap.height-1]);
  196. rt:=GetRValue(Acolor);
  197. gt:=GetGValue(AColor);
  198. bt:=getBValue(AColor);
  199. FBitmap.PixelFormat :=pf24bit;
  200. FDarker.PixelFormat :=pf24bit;
  201. for y:=0 to Fbitmap.height-1 do
  202. begin
  203. p1:=Fbitmap.ScanLine [y];
  204. p2:=FDarker.ScanLine [y];
  205. for x:=0 to FBitmap.width-1 do
  206. begin
  207. if (p1[x*3]=bt)and (p1[x*3+1]=gt)and (p1[x*3+2]=rt) then
  208. begin
  209. p2[x*3]:=p1[x*3];
  210. p2[x*3+1]:=p1[x*3+1];
  211. p2[x*3+2]:=p1[x*3+2];
  212. end
  213. else
  214. begin
  215. p2[x*3]:=round(0.7*p1[x*3]);
  216. p2[x*3+1]:=round(0.7*p1[x*3+1]);
  217. p2[x*3+2]:=round(0.7*p1[x*3+2]);
  218. end
  219. end;
  220. end;
  221. end;
  222. procedure TBitmapButton.CMMouseLeave(var Message: TMessage);
  223. begin
  224. FMouseOver:=false;
  225. Paint;
  226. end;
  227. procedure TBitmapButton.Loaded;
  228. begin
  229. inherited;
  230. if not FBitmap.Empty then
  231. begin
  232. MakeDarker;
  233. MakeLighter;
  234. end;
  235. end;
  236. procedure TBitmapButton.SetLatching(const Value: boolean);
  237. begin
  238. FLatching := Value;
  239. if not FLatching then
  240. begin
  241. FDown:=false;
  242. paint;
  243. end;
  244. end;
  245. procedure TBitmapButton.SetDown(const Value: boolean);
  246. begin
  247. if FLatching then
  248. begin
  249. FDown := Value;
  250. paint;
  251. end
  252. else
  253. begin
  254. FDown:=false;
  255. paint;
  256. end;
  257. end;
  258. procedure TBitmapButton.Resize;
  259. begin
  260. inherited;
  261. if assigned(Fbitmap) then
  262. begin
  263. width:=FBitmap.width;
  264. height:=FBitmap.Height ;
  265. end
  266. else
  267. begin
  268. width:=24;
  269. height:=24;
  270. end;
  271. end;
  272. procedure TBitmapButton.SetHotTrack(const Value: boolean);
  273. begin
  274. FHotTrack := Value;
  275. end;
  276. procedure TBitmapButton.MouseMove(Shift: TShiftState; X, Y: Integer);
  277. var Value:Boolean;
  278. begin
  279. inherited;
  280. Value:= FBitmap.canvas.pixels[x,y]<>Fbitmap.canvas.pixels[0,FBitmap.height-1];
  281. if value<>FMouseOver then
  282. begin
  283. FMouseOver:=value;
  284. Paint;
  285. end;
  286. // if Assigned(OnMouseMove) then
  287. // OnMouseMove(self,shift,x,y);
  288. end;
  289. procedure TBitmapButton.ChangeUIColor(AColor: TColor);
  290. begin
  291. ConvertBitmapToColor(FBitmap, AColor);
  292. SetBitmap(FBitmap);
  293. Invalidate;
  294. end;
  295. procedure Register;
  296. begin
  297. RegisterComponents('ICQComponnets', [TBitmapButton]);
  298. end;
  299. end.