SelColorFrm.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:8k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit SelColorFrm;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ExtCtrls,RealMessengerUnit,MSHtml,ActiveX, ExtDlgs,UrlMon,Global,ChatingFrm,Registry;
  6. type
  7.   TSelColorForm = class(TForm)
  8.     ImgDraw: TImage;
  9.     ImageCustom: TImage;
  10.     Image1: TImage;
  11.     Image2: TImage;
  12.     Image4: TImage;
  13.     Image5: TImage;
  14.     Image3: TImage;
  15.     Image6: TImage;
  16.     Image7: TImage;
  17.     Image9: TImage;
  18.     Image10: TImage;
  19.     Image11: TImage;
  20.     Image12: TImage;
  21.     Image14: TImage;
  22.     Shape1: TShape;
  23.     Timer1: TTimer;
  24.     ColorDialog1: TColorDialog;
  25.     ImageDefault: TImage;
  26.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  27.     procedure FormDeactivate(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure FormShow(Sender: TObject);
  30.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  31.       Shift: TShiftState; X, Y: Integer);
  32.     procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
  33.       Shift: TShiftState; X, Y: Integer);
  34.     procedure Timer1Timer(Sender: TObject);
  35.     procedure Image1Click(Sender: TObject);
  36.     procedure ImageCustomClick(Sender: TObject);
  37.     procedure ImageDefaultClick(Sender: TObject);
  38.     procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  39.       Y: Integer);
  40.   private
  41.     ImgIndex,LastImgIndex:Integer;
  42.   public
  43.     ParentForm:TChatingForm;
  44.   end;
  45. var
  46.   SelColorForm: TSelColorForm;
  47. implementation
  48. {$R *.dfm}
  49. {onClose事件,释放内存}
  50. procedure TSelColorForm.FormClose(Sender: TObject;
  51.   var Action: TCloseAction);
  52. begin
  53.   Action:=caFree;
  54.   SelColorForm:=nil;
  55. end;
  56. {OnDeactivate时关闭窗口}
  57. procedure TSelColorForm.FormDeactivate(Sender: TObject);
  58. begin
  59.   Close;
  60. end;
  61. {OnCreate事件,设置DoubleBuffered=True,以防止闪烁}
  62. procedure TSelColorForm.FormCreate(Sender: TObject);
  63. begin
  64.     Self.DoubleBuffered:=True;
  65.     SetWindowLong( Handle, GWL_EXSTYLE,GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
  66. end;
  67. {OnShow事件,显示系统提供的N种基本颜色}
  68. procedure TSelColorForm.FormShow(Sender: TObject);
  69. begin
  70.     Image1.Canvas.Brush.Color:=$00d2d2d2;
  71.     Image1.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);
  72.     Image2.Canvas.Brush.Color:=$00EFD0C1;
  73.     Image2.Canvas.Rectangle(0,0,Image2.Width,Image2.Height);
  74.     Image3.Canvas.Brush.Color:=$00D7C0A7;
  75.     Image3.Canvas.Rectangle(0,0,Image3.Width,Image3.Height);
  76.     Image4.Canvas.Brush.Color:=$00D7B3C4;
  77.     Image4.Canvas.Rectangle(0,0,Image4.Width,Image4.Height);
  78.     Image5.Canvas.Brush.Color:=$00E5A5DB;
  79.     Image5.Canvas.Rectangle(0,0,Image5.Width,Image5.Height);
  80.     Image6.Canvas.Brush.Color:=$00E9B1D4;
  81.     Image6.Canvas.Rectangle(0,0,Image6.Width,Image6.Height);
  82.     Image7.Canvas.Brush.Color:=$00A6DFAB;
  83.     Image7.Canvas.Rectangle(0,0,Image7.Width,Image7.Height);
  84.     Image9.Canvas.Brush.Color:=$00A5D1FF;
  85.     Image9.Canvas.Rectangle(0,0,Image9.Width,Image9.Height);
  86.     Image10.Canvas.Brush.Color:=$00A7C7DD;
  87.     Image10.Canvas.Rectangle(0,0,Image10.Width,Image10.Height);
  88.     Image11.Canvas.Brush.Color:=$00A5C8CA;
  89.     Image11.Canvas.Rectangle(0,0,Image11.Width,Image11.Height);
  90.     Image12.Canvas.Brush.Color:=$00A7A7DF;
  91.     Image12.Canvas.Rectangle(0,0,Image12.Width,Image12.Height);
  92.     Image14.Canvas.Brush.Color:=$00BBCDA5;
  93.     Image14.Canvas.Rectangle(0,0,Image14.Width,Image14.Height);
  94.     ImageCustom.Canvas.Pen.Color:=$00781E1D;
  95.     ImageCustom.Canvas.TextOut(4,4,'更多颜色...');
  96.     ImageDefault.Canvas.Pen.Color:=$00781E1D;
  97.     ImageDefault.Canvas.TextOut(4,4,'取消自定义颜色');
  98.     SelColorForm.Left:=ParentForm.Left + ParentForm.PnlLeftBottom.Left + ParentForm.ImgSelColor.Left+4;
  99.     SelColorForm.Top:=ParentForm.Top + ParentForm.PnlLeftBottom.Top + ParentForm.ImgSelColor.Top - Height + 3;
  100. end;
  101. {模拟按钮按下}
  102. procedure TSelColorForm.Image1MouseDown(Sender: TObject;
  103.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  104. var
  105.     Img:TImage;
  106. begin
  107.     Img:=(Sender as TImage);
  108.     With ImgDraw.Canvas do
  109.     begin
  110.       Pen.Mode :=pmCopy;
  111.       Pen.Style:=psClear;
  112.       Rectangle(0,0,ImgDraw.Width+1,ImgDraw.Height+1);
  113.       Pen.Style:=psSolid;
  114.       Pen.Color:=EndColor;
  115.       Pen.Width:=1;
  116.       MoveTo(Img.Left+Img.Width-ImgDraw.Left+2,Img.Top-ImgDraw.Top-2);
  117.       LineTo(PenPos.X-((Sender as TImage).Width+4),PenPos.Y);
  118.       LineTo(PenPos.X,PenPos.Y+(Img.Height+4));
  119.       Pen.Color:=$00F5F5F5;
  120.       LineTo(PenPos.X+(Sender as TImage).Width+4,PenPos.Y);
  121.       LineTo(PenPos.X,PenPos.Y-(Img.Height+4));
  122.       Refresh;
  123.     end;
  124.     LastImgIndex:=ImgIndex;
  125.     Timer1.Enabled:=False;
  126.     Img.Left:=Img.Left+1;
  127.     Img.Top:=Img.Top+1;
  128. end;
  129. {恢复按钮的状态}
  130. procedure TSelColorForm.Image1MouseUp(Sender: TObject;
  131.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  132. var
  133.     Img:TImage;
  134. begin
  135.     Img:=(Sender as TImage);
  136.     Img.Left:=Img.Left-1;
  137.     Img.Top:=Img.Top-1;
  138.     With ImgDraw.Canvas do
  139.     begin
  140.       Pen.Mode :=pmCopy;
  141.       Pen.Style:=psClear;
  142.       Rectangle(0,0,ImgDraw.Width+1,ImgDraw.Height+1);
  143.       Pen.Style:=psSolid;
  144.       Pen.Color:=$00F5F5F5;
  145.       Pen.Width:=1;
  146.       MoveTo(Img.Left+Img.Width-ImgDraw.Left+2,Img.Top-ImgDraw.Top-2);
  147.       LineTo(PenPos.X-((Sender as TImage).Width+4),PenPos.Y);
  148.       LineTo(PenPos.X,PenPos.Y+(Img.Height+4));
  149.       Pen.Color:=EndColor;
  150.       LineTo(PenPos.X+(Sender as TImage).Width+4,PenPos.Y);
  151.       LineTo(PenPos.X,PenPos.Y-(Img.Height+4));
  152.       Refresh;
  153.     end;
  154.     LastImgIndex:=ImgIndex;
  155.     Timer1.Enabled:=True;
  156. end;
  157. {检查Mouse是否还在指定的范围之内,不在则恢复所有按钮的状态}
  158. procedure TSelColorForm.Timer1Timer(Sender: TObject);
  159. var
  160.    p : TPoint;
  161. begin
  162.    GetCursorPos(p);
  163.    if (P.X < Left) or
  164.       (P.X > Left + Width) or
  165.       (P.Y < Top) or
  166.       (P.Y > Top + Height) then
  167.    begin
  168.       With ImgDraw.Canvas do
  169.       begin
  170.         Pen.Mode :=pmCopy;
  171.         Pen.Style:=psClear;
  172.         Rectangle(0,0,ImgDraw.Width+1,Shape1.Height+1);
  173.       end;
  174.       ImgIndex:=-1;
  175.       LastImgIndex:=-1;
  176.       Timer1.Enabled:=False;
  177.    end;
  178. end;
  179. {选择了某个颜色}
  180. procedure TSelColorForm.Image1Click(Sender: TObject);
  181. var
  182.   TempReg:TRegistry;
  183. begin
  184.     EndColor:=(Sender as TImage).Canvas.Pixels[1,1];
  185.     ChangeAllColor(EndColor);
  186.     TempReg:=TRegistry.Create;
  187.     try
  188.       TempReg.RootKey:=HKEY_LOCAL_MACHINE;
  189.       if TempReg.OpenKey(AppKey+''+LoginName+'Init', True) then
  190.       begin
  191.         TempReg.WriteString('CustomColor',IntToStr(EndColor));
  192.       end;
  193.     finally
  194.       TempReg.Free;
  195.     end;
  196.     Close;
  197. end;
  198. {打开自定义颜色对话框,以供用户选择更多的颜色}
  199. procedure TSelColorForm.ImageCustomClick(Sender: TObject);
  200. var
  201.   TempReg:TRegistry;
  202. begin
  203.   ColorDialog1.Color:=EndColor;
  204.   if ColorDialog1.Execute then
  205.   begin
  206.     EndColor:=ColorDialog1.Color;
  207.     ChangeAllColor(EndColor);
  208.     TempReg:=TRegistry.Create;
  209.     try
  210.       TempReg.RootKey:=HKEY_LOCAL_MACHINE;
  211.       if TempReg.OpenKey(AppKey+''+LoginName+'Init', True) then
  212.       begin
  213.         TempReg.WriteString('CustomColor',IntToStr(EndColor));
  214.       end;
  215.     finally
  216.       TempReg.Free;
  217.     end;
  218.     Close;
  219.   end;
  220. end;
  221. {取消自定义颜色,显示系统默认颜色}
  222. procedure TSelColorForm.ImageDefaultClick(Sender: TObject);
  223. var
  224.   TempReg:TRegistry;
  225. begin
  226.     EndColor:=DefaultColor;
  227.     ChangeAllColor(EndColor);
  228.     TempReg:=TRegistry.Create;
  229.     try
  230.       TempReg.RootKey:=HKEY_LOCAL_MACHINE;
  231.       if TempReg.OpenKey(AppKey+''+LoginName+'Init', True) then
  232.       begin
  233.         TempReg.WriteString('CustomColor','');
  234.       end;
  235.     finally
  236.       TempReg.Free;
  237.     end;
  238.     Close;
  239. end;
  240. {MouseMove时使平面的按钮变为凸起状态}
  241. procedure TSelColorForm.Image1MouseMove(Sender: TObject;
  242.   Shift: TShiftState; X, Y: Integer);
  243. var
  244.     Img:TImage;
  245. begin
  246.     Img:=(Sender as TImage);
  247.     ImgIndex:=Img.Tag;
  248.     if ImgIndex=LastImgIndex then exit;
  249.     With ImgDraw.Canvas do
  250.     begin
  251.       Pen.Mode :=pmCopy;
  252.       Pen.Style:=psClear;
  253.       Rectangle(0,0,ImgDraw.Width+1,ImgDraw.Height+1);
  254.       Pen.Style:=psSolid;
  255.       Pen.Width:=1;
  256.       Pen.Color:=$00F5F5F5;
  257.       MoveTo(Img.Left+Img.Width-ImgDraw.Left+2,Img.Top-ImgDraw.Top-2);
  258.       LineTo(PenPos.X-(Img.Width+4),PenPos.Y);
  259.       LineTo(PenPos.X,PenPos.Y+(Img.Height+4));
  260.       Pen.Color:=EndColor;
  261.       LineTo(PenPos.X+Img.Width+4,PenPos.Y);
  262.       LineTo(PenPos.X,PenPos.Y-(Img.Height+4));
  263.       Refresh;
  264.     end;
  265.     LastImgIndex:=ImgIndex;
  266.     Timer1.Enabled:=True;
  267. end;
  268. end.