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

Delphi控件源码

开发平台:

Delphi

  1. unit CopyScreenFrm;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ExtCtrls,Color,ChatingFrm, StdCtrls,JPeg,RealMessengerUnit,Global,
  6.   AppEvnts;
  7. type
  8.   TScreenState = (msDefault,msDrag,msSelected);
  9.   TCopyScreenForm = class(TForm)
  10.     ImgScreen: TImage;
  11.     PnlInfo: TPanel;
  12.     LblRGB: TLabel;
  13.     LblActionInfo: TLabel;
  14.     LblCancelInfo: TLabel;
  15.     ApplicationEvents1: TApplicationEvents;
  16.     Label1: TLabel;
  17.     procedure FormShow(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure PnlInfoMouseMove(Sender: TObject; Shift: TShiftState; X,
  20.       Y: Integer);
  21.     procedure ImgScreenMouseMove(Sender: TObject; Shift: TShiftState; X,
  22.       Y: Integer);
  23.     procedure ImgScreenMouseDown(Sender: TObject; Button: TMouseButton;
  24.       Shift: TShiftState; X, Y: Integer);
  25.     procedure ImgScreenMouseUp(Sender: TObject; Button: TMouseButton;
  26.       Shift: TShiftState; X, Y: Integer);
  27.     procedure ApplicationEvents1Message(var Msg: tagMSG;
  28.       var Handled: Boolean);
  29.     procedure ImgScreenDblClick(Sender: TObject);
  30.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  31.   private
  32.     DX,DY,RectLeft,RectTop,RectBottom,RectRight:Integer;
  33.     MouseIsDown,
  34.     Trace:Boolean;
  35.     ScreenState:TScreenState;
  36.     procedure Cancel;
  37.     procedure SendImg;
  38.   public
  39.     ParentForm:TChatingForm;
  40.   end;
  41. var
  42.   CopyScreenForm: TCopyScreenForm;
  43. implementation
  44. {$R *.dfm}
  45. procedure TCopyScreenForm.FormShow(Sender: TObject);
  46. begin
  47.     ScreenState:=msDefault;
  48.     MouseIsDown:=False;
  49.     Trace:=False;
  50.     RectLeft:=-1;
  51.     RectTop:=-1;
  52.     RectBottom:=-1;
  53.     RectRight:=-1;
  54.     ImgScreen.Canvas.Pen.mode:=pmnot; //笔的模式为取反
  55.     ImgScreen.canvas.pen.color:=clblack; //笔为黑色
  56.     ImgScreen.canvas.pen.Width:=2;
  57.     ImgScreen.canvas.brush.Style:=bsclear; //空白刷子end;
  58. end;
  59. procedure TCopyScreenForm.FormCreate(Sender: TObject);
  60. begin
  61.   Self.DoubleBuffered:=True;
  62. end;
  63. procedure TCopyScreenForm.Cancel;
  64. begin
  65.   if ScreenState=msDefault then
  66.     Close
  67.   else
  68.   begin
  69.     if Trace then ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  70.     Trace:=False;
  71.     ScreenState:=msDefault;
  72.     LblActionInfo.Caption:='按住鼠标左键不放选择截取范围';
  73.     LblCancelInfo.Caption:='按鼠标右键退出';
  74.     exit;
  75.   end;
  76. end;
  77. procedure TCopyScreenForm.PnlInfoMouseMove(Sender: TObject;
  78.   Shift: TShiftState; X, Y: Integer);
  79. begin
  80.   if PnlInfo.Left=8 then
  81.     PnlInfo.Left:=Screen.Width-8-PnlInfo.Width
  82.   else
  83.     PnlInfo.Left:=8;
  84. end;
  85. procedure TCopyScreenForm.ImgScreenMouseMove(Sender: TObject;
  86.   Shift: TShiftState; X, Y: Integer);
  87. var
  88.   R,G,B:Integer;
  89. begin
  90.   if (X>PnlInfo.Left) and (X<PnlInfo.Left+PnlInfo.Width) and (Y>PnlInfo.Top) and (Y<PnlInfo.Top+PnlInfo.Height) then
  91.   begin
  92.     PnlInfoMouseMove(Sender,Shift,X,Y);
  93.   end;
  94.   if (ScreenState=msSelected) then
  95.   begin
  96.     ImgScreen.Cursor:=crDefault;
  97.     if (X>RectLeft) and (X<RectRight) and (Y>RectTop) and (Y<RectBottom) then
  98.     begin
  99.       ImgScreen.Cursor:=crSizeAll;
  100.       if MouseIsDown then
  101.       begin
  102.         ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  103.         if (RectLeft+(X-DX)>=1) and (RectRight+(X-DX)<=Screen.Width) then
  104.         begin
  105.           RectLeft:=RectLeft+(X-DX);
  106.           RectRight:=RectRight+(X-DX);
  107.         end;
  108.         if (RectTop+(Y-DY)>=1) and (RectBottom+(Y-DY)<=Screen.Height) then
  109.         begin
  110.           RectTop:=RectTop+(Y-DY);
  111.           RectBottom:=RectBottom+(Y-DY);
  112.         end;
  113.         
  114.         ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  115.         DX:=X;
  116.         DY:=Y;
  117.       end;
  118.     end;
  119.   end
  120.   else
  121.   begin
  122.     ImgScreen.Cursor:=crCross;
  123.   end;
  124.   if (ScreenState=msDrag) and MouseIsDown then
  125.   begin
  126.     if Trace then ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  127.     RectRight:=X;
  128.     RectBottom:=Y;
  129.     ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  130.     Trace:=True;
  131.   end;
  132.   
  133.   R:=getRvalue(ImgScreen.Canvas.Pixels[X, Y]);
  134.   G:=getGvalue(ImgScreen.Canvas.Pixels[X, Y]);
  135.   B:=getBvalue(ImgScreen.Canvas.Pixels[X, Y]);
  136.   LblRGB.Caption:='当前像素RGB值('+IntToStr(R)+'、'+IntToStr(G)+'、'+IntToStr(B)+')';
  137. end;
  138. procedure TCopyScreenForm.ImgScreenMouseDown(Sender: TObject;
  139.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  140. begin
  141.   if Button=mbRight then
  142.   begin
  143.     Cancel;
  144.     Exit;
  145.   end;
  146.   if (ScreenState=msSelected) and (ImgScreen.Cursor<>crDefault) then
  147.   begin
  148.     MouseIsDown:=True;
  149.     DX:=X;
  150.     DY:=Y;
  151.   end;
  152.   if ScreenState<>msDefault then exit;
  153.   if Trace then ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  154.   MouseIsDown:=True;
  155.   Trace:=False;
  156.   ScreenState:=msDrag;
  157.   RectLeft:=X;
  158.   RectTop:=Y;
  159.   RectRight:=X;
  160.   RectBottom:=Y;
  161.   LblActionInfo.Caption:='松开鼠标左键以确定最终截取范围';
  162.   LblCancelInfo.Caption:='按鼠标右键取消当前选区';
  163. end;
  164. procedure TCopyScreenForm.ImgScreenMouseUp(Sender: TObject;
  165.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  166. begin
  167.     MouseIsDown:=False;
  168.     if ScreenState=msDrag then
  169.     begin
  170.       LblActionInfo.Caption:='按空格/回车/双击左键发送当前选区的图像';
  171.       LblCancelInfo.Caption:='按鼠标右键取消当前选区';
  172.       ScreenState:=msSelected;
  173.     end;
  174. end;
  175. procedure TCopyScreenForm.SendImg;
  176. var
  177.   newbitmap:TBitmap;
  178.   newjpg:TJPegImage;
  179.   TempInt:Integer;
  180.   ScreenFileName:String;
  181.   Receiver:Integer;
  182.   ReceiverName:String;
  183. begin
  184.   if ScreenState=msSelected then
  185.   begin
  186.     if RectLeft>RectRight then
  187.     begin
  188.       TempInt:=RectLeft;
  189.       RectLeft:=RectRight;
  190.       RectRight:=TempInt;
  191.     end;
  192.     
  193.     if RectTop>RectBottom then
  194.     begin
  195.       TempInt:=RectTop;
  196.       RectTop:=RectBottom;
  197.       RectBottom:=TempInt;
  198.     end;
  199.     //Dec(RectRight);
  200.     //Dec(RectBottom);
  201.     newbitmap:=Tbitmap.create;
  202.     newbitmap.width:=RectRight-RectLeft;
  203.     newbitmap.height:=RectBottom-RectTop;
  204.     if Trace then ImgScreen.Canvas.Rectangle(RectLeft,RectTop,RectRight,RectBottom);
  205.     newbitmap.Canvas.CopyRect(Rect(0, 0, newbitmap.width, newbitmap.height),ImgScreen.canvas,Rect (RectLeft, RectTop,RectRight,RectBottom)); //拷贝
  206.     newjpg:=TJPegImage.Create;
  207.     newjpg.Assign(newbitmap);
  208.     newjpg.CompressionQuality:=90;
  209.     newjpg.Compress;
  210.     ScreenFileName:=ApplicationPath+'Screens'+IntToStr(Me.ID)+'SC'+IntToStr(GetTickCount)+'.JPG';
  211.     if not DirectoryExists(ExtractFilePath(ScreenFileName)) then CreateDir(ExtractFilePath(ScreenFileName));
  212.     newjpg.SaveToFile(ScreenFileName);
  213.     newjpg.Free;
  214.     newbitmap.free;
  215.     Receiver    :=PEmployee(ParentForm.TVUserList.Items.GetFirstNode.Data).ID;
  216.     ReceiverName:=PEmployee(ParentForm.TVUserList.Items.GetFirstNode.Data).Name;
  217.     ScreenState:=msDefault;
  218.     Close;
  219.     TTransmitFile.Create(tfSend,Me.ID,Me.Name,Receiver,ReceiverName,ScreenFileName,ParentForm,'',0,'',0,0,'','',True);
  220.   end;
  221. end;
  222. procedure TCopyScreenForm.ApplicationEvents1Message(var Msg: tagMSG;
  223.   var Handled: Boolean);
  224. begin
  225.   if Msg.wParam=27 then
  226.   begin
  227.     Cancel;
  228.   end;
  229.   if (Msg.wParam=32) or (Msg.wParam=13) then
  230.   begin
  231.     if ScreenState=msSelected then SendImg;
  232.     Handled:=True;
  233.   end;
  234. end;
  235. procedure TCopyScreenForm.ImgScreenDblClick(Sender: TObject);
  236. begin
  237.     if ScreenState=msSelected then SendImg;
  238. end;
  239. procedure TCopyScreenForm.FormClose(Sender: TObject;
  240.   var Action: TCloseAction);
  241. begin
  242.   Action:=caFree;
  243.   ShowWindow(ParentForm.Handle,SW_SHOW);
  244.   CopyScreenForm:=nil;
  245. end;
  246. end.