Guest User

Untitled

a guest
Mar 10th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. unit ScreenShot;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, SysUtils, Classes, Controls, Graphics, Forms, Messages, ScrView,gdiputil,gdipobj,gdipapi;
  7.  
  8. type
  9. TWaterMarkPos=(wpTopLeft, wpTopRight, wpBottomLeft, wpBottomRight);
  10.  
  11. type
  12. TWaterMark=class(TPersistent)
  13. private
  14. FEnabled: Boolean;
  15. FText: string;
  16. FCircuitColor: TColor;
  17. FFont: TFont;
  18. FCircuitWidth: Integer;
  19. FPosition: TWaterMarkPos;
  20. procedure SetCircuitColor(const Value: TColor);
  21. procedure SetEnabled(const Value: Boolean);
  22. procedure SetFont(const Value: TFont);
  23. procedure SetText(const Value: string);
  24. procedure SetCircuitWidth(const Value: Integer);
  25. procedure SetPosition(const Value: TWaterMarkPos);
  26. public
  27. published
  28. property Enabled:Boolean read FEnabled write SetEnabled default False;
  29. property Text:string read FText write SetText;
  30. property Font:TFont read FFont write SetFont;
  31. property CircuitColor:TColor read FCircuitColor write SetCircuitColor default clNone;
  32. property CircuitWidth:Integer read FCircuitWidth write SetCircuitWidth default 2;
  33. property Position:TWaterMarkPos read FPosition write SetPosition default wpBottomRight;
  34. constructor Create;
  35. destructor Destroy; override;
  36. end;
  37.  
  38. TScreenShot = class(TComponent)
  39. private
  40. FFrm:TForm;
  41. FOldWndProc:TWNDMethod;
  42. FID:WORD;
  43. FBitmap: TBitMap;
  44. FClientOnly: Boolean;
  45. FWinH: HWnd;
  46. FShowAfterMake: Boolean;
  47. FTimeStamp: TWaterMark;
  48. FWaterMark: TWaterMark;
  49. procedure SetBitmap(const Value: TBitMap);
  50. procedure SetClientOnly(const Value: Boolean);
  51. procedure SetShowAfterMake(const Value: Boolean);
  52. procedure SetTimeStamp(const Value: TWaterMark);
  53. procedure SetWaterMark(const Value: TWaterMark);
  54. { Private declarations }
  55. protected
  56. procedure GetWindowBmp;
  57. procedure GetClientBmp;
  58. procedure NewWndProc(var MSG:TMessage);
  59. procedure DrawWaterMark;
  60. public
  61. constructor Create(AOwner:TComponent); override;
  62. destructor Destroy; override;
  63. procedure Make;
  64. property Bitmap:TBitMap read FBitmap write SetBitmap;
  65. published
  66. property WaterMark:TWaterMark read FWaterMark write SetWaterMark;
  67. property TimeStamp:TWaterMark read FTimeStamp write SetTimeStamp;
  68. property ClientOnly:Boolean read FClientOnly write SetClientOnly default True;
  69. property ShowAfterMake:Boolean read FShowAfterMake write SetShowAfterMake default True;
  70. end;
  71.  
  72. procedure Register;
  73.  
  74. implementation
  75.  
  76. {$R *.res}
  77.  
  78. var
  79. ScrViewFrm:TScrViewFrm=nil;
  80.  
  81. function ScrView:TScrViewFrm;
  82. begin
  83. if Assigned(ScrViewFrm) then
  84. Result:=ScrViewFrm
  85. else
  86. begin
  87. ScrViewFrm:=TScrViewFrm.Create(Application);
  88. Result:=ScrViewFrm;
  89. end;
  90. end;
  91.  
  92. procedure Register;
  93. begin
  94. RegisterComponents('ScrShot', [TScreenShot]);
  95. end;
  96.  
  97. { TScreenShot }
  98.  
  99. constructor TScreenShot.Create(AOwner: TComponent);
  100. begin
  101. inherited Create(AOwner);
  102. FBitMap:=TBitMap.Create;
  103. FClientOnly:=True;
  104. FShowAfterMake:=True;
  105. FWaterMark:=TWaterMark.Create;
  106. with FWaterMark do
  107. begin
  108. with Font do
  109. begin
  110. Name:='Tahoma';
  111. Size:=19;
  112. Style:=[fsBold];
  113. Color:=clWhite;
  114. end;
  115. CircuitColor:=RGB(253,38,9);
  116. end;
  117. FTimeStamp:=TWaterMark.Create;
  118. FID:=0;
  119. FWinH:=0;
  120. if (AOwner is TForm) then
  121. begin
  122. FFrm:=TForm(AOwner);
  123. FWinH:=TForm(AOwner).Handle;
  124. if not (csDesigning in ComponentState) then
  125. begin
  126. FOldWndProc:=FFrm.WindowProc;
  127. FFrm.WindowProc:=NewWndProc;
  128. //FID := GlobalAddAtom('~Hotkey');
  129. //RegisterHotKey(FFrm.Handle, 0, MOD_CONTROL, VK_SNAPSHOT);
  130. end;
  131. end;
  132. end;
  133.  
  134. destructor TScreenShot.Destroy;
  135. begin
  136. if Assigned(OWner) then
  137. begin
  138. if not (csDesigning in ComponentState) then
  139. begin
  140. UnregisterHotKey((Owner as TWinControl).Handle, FID);
  141. GlobalDeleteAtom(FID);
  142. (Owner as TWinControl).WindowProc:=FOldWndProc;
  143. end;
  144. end;
  145. FBitMap.Free;
  146. FTimeStamp.Free;
  147. FWaterMark.Free;
  148. inherited;
  149. end;
  150.  
  151. procedure TScreenShot.DrawWaterMark;
  152. var
  153. DPen: TGPPen;
  154. Drawer: TGPGraphics;
  155. DBrush: TGPSolidBrush;
  156. DFntFam: TGPFontFamily;
  157. DPath: TGPGraphicsPath;
  158. IC,BC:Integer;
  159. ICL, BCL:TGPColor;
  160. W:WideString;
  161. rt:TGPRectF;
  162. GP:TGPPoint;
  163. begin
  164. W:=FWaterMark.Text;
  165. IC:=ColortoRGB(FWaterMark.Font.Color);
  166. BC:=ColorToRGB(FWaterMark.CircuitColor);
  167. ICl:=MakeColor(GetRValue(IC), GetGValue(IC), GetBValue(IC));
  168. BCL:=MakeColor(GetRValue(BC), GetGValue(BC), GetBValue(BC));
  169. Drawer:=TGPGraphics.Create(FBitMap.Canvas.Handle);
  170. Drawer.SetCompositingQuality(CompositingQualityHighQuality);
  171. Drawer.SetSmoothingMode(SmoothingModeAntiAlias);
  172. Drawer.SetTextRenderingHint(TextRenderingHintAntiAlias);
  173. DPath:=TGPGraphicsPath.Create;
  174. DPen:=TGPPen.Create(BCL, FWaterMark.FCircuitWidth);
  175. DBrush:=TGPSolidBrush.Create(ICL);
  176. DFntFam:=TGPFontFamily.Create(FWaterMark.Font.Name);
  177.  
  178. RT.X:=0;
  179. RT.Y:=0;
  180. RT.Width:=FBitMap.Width;
  181. RT.Height:=FBitMap.Height;
  182.  
  183. GP.X:=0;
  184. GP.Y:=0;
  185. messagebox(0,'','',0);
  186. DPath.AddString(W, Length(W), DFntFam, FontStyleBold, FWaterMark.Font.Size, GP, TGPStringFormat.Create());
  187. DPath.GetBounds(RT, nil, DPen);
  188. DPath.Reset;
  189. case FWaterMark.Position of
  190. wpBottomRight:
  191. begin
  192. RT.Y:=FBitMap.Height-RT.Height;
  193. RT.X:=FBitMap.Width-RT.Width;
  194. end;
  195. wpBottomLeft:
  196. begin
  197. RT.Y:=FBitMap.Height-RT.Height;
  198. RT.X:=0;
  199. end;
  200. wpTopLeft:
  201. begin
  202. RT.Y:=0;
  203. RT.X:=0;
  204. end;
  205. wpTopRight:
  206. begin
  207. RT.Y:=0;
  208. RT.X:=FBitMap.Width-RT.Width;
  209. end;
  210. end;
  211. DPath.AddString(W, Length(W), DFntFam, FontStyleBold, FWaterMark.Font.Size, RT, TGPStringFormat.GenericDefault);
  212. Drawer.DrawPath(DPen, DPath);
  213. Drawer.FillPath(DBrush, DPath);
  214. DFntFam.Free;
  215. DBrush.Free;
  216. DPen.Free;
  217. DPath.Free;
  218. Drawer.Free;
  219. end;
  220.  
  221. procedure TScreenShot.GetClientBmp;
  222. var
  223. DC:HDC;
  224. WW, WH:Integer;
  225. R:TRect;
  226. begin
  227. DC:=GetDC(FWinH);
  228. Windows.GetClientRect(FWinH, R);
  229. WW:=R.Right-R.Left;
  230. WH:=R.Bottom-R.Top;
  231. FBitMap.Width:=WW;
  232. FBitMap.Height:=WH;
  233. BitBlt(FBitMap.Canvas.Handle,0,0,WW,WH,DC,0,0,SRCCOPY);
  234. end;
  235.  
  236. procedure TScreenShot.GetWindowBmp;
  237. var
  238. DC:HDC;
  239. WW, WH:Integer;
  240. R:TRect;
  241. begin
  242. DC:=GetWindowDC(FWinH);
  243. Windows.GetWindowRect(FWinH, R);
  244. WW:=R.Right-R.Left;
  245. WH:=R.Bottom-R.Top;
  246. FBitMap.Width:=WW;
  247. FBitMap.Height:=WH;
  248. BitBlt(FBitMap.Canvas.Handle,0,0,WW,WH,DC,0,0,SRCCOPY);
  249. end;
  250.  
  251. procedure TScreenShot.Make;
  252. begin
  253. if IsWindow(FWinH) then
  254. begin
  255. if FClientOnly then
  256. GetClientBmp
  257. else
  258. GetWindowBmp;
  259. DrawWaterMark;
  260. if FShowAfterMake then
  261. begin
  262. with ScrView do
  263. begin
  264. ImgView.Picture.Assign(FBitMap);
  265. ScrlBox.VertScrollBar.Range:=ImgView.Height;
  266. ScrlBox.HorzScrollBar.Range:=ImgView.Width;
  267. if not Visible then
  268. ShowModal;
  269. end;
  270. end;
  271. end
  272. else
  273. MessageBox(0, 'Cannot make a screenshot','Something is bad',48);
  274. end;
  275.  
  276. procedure TScreenShot.NewWndProc(var MSG: TMessage);
  277. begin
  278. if MSG.Msg=WM_HOTKEY then
  279. begin
  280. Make;
  281. end;
  282. FOldWndProc(MSG);
  283. end;
  284.  
  285. procedure TScreenShot.SetBitmap(const Value: TBitMap);
  286. begin
  287. FBitmap := Value;
  288. end;
  289.  
  290. procedure TScreenShot.SetClientOnly(const Value: Boolean);
  291. begin
  292. FClientOnly := Value;
  293. end;
  294.  
  295. procedure TScreenShot.SetShowAfterMake(const Value: Boolean);
  296. begin
  297. FShowAfterMake := Value;
  298. end;
  299.  
  300. procedure TScreenShot.SetTimeStamp(const Value: TWaterMark);
  301. begin
  302. FTimeStamp := Value;
  303. end;
  304.  
  305. procedure TScreenShot.SetWaterMark(const Value: TWaterMark);
  306. begin
  307. FWaterMark := Value;
  308. end;
  309.  
  310. { TWaterMark }
  311.  
  312. constructor TWaterMark.Create;
  313. begin
  314. FEnabled:=False;
  315. FCircuitWidth:=2;
  316. FFont:=TFont.Create;
  317. with FFont do
  318. begin
  319. Style:=[];
  320. Color:=clRed;
  321. Name:='Arial';
  322. end;
  323. FPosition:=wpBottomRight;
  324. end;
  325.  
  326. destructor TWaterMark.Destroy;
  327. begin
  328. inherited;
  329. end;
  330.  
  331. procedure TWaterMark.SetCircuitColor(const Value: TColor);
  332. begin
  333. FCircuitColor := Value;
  334. end;
  335.  
  336. procedure TWaterMark.SetCircuitWidth(const Value: Integer);
  337. begin
  338. FCircuitWidth := Value;
  339. end;
  340.  
  341. procedure TWaterMark.SetEnabled(const Value: Boolean);
  342. begin
  343. FEnabled := Value;
  344. end;
  345.  
  346. procedure TWaterMark.SetFont(const Value: TFont);
  347. begin
  348. FFont.Assign(Value);
  349. end;
  350.  
  351. procedure TWaterMark.SetPosition(const Value: TWaterMarkPos);
  352. begin
  353. FPosition := Value;
  354. end;
  355.  
  356. procedure TWaterMark.SetText(const Value: string);
  357. begin
  358. FText := Value;
  359. end;
  360.  
  361. end.
  362.  
  363.  
  364.  
  365. /////Код формы
  366.  
  367.  
  368. unit ScrView;
  369.  
  370. interface
  371.  
  372. uses
  373. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  374. Dialogs, ExtCtrls, PngImage, Menus, ExtDlgs;
  375.  
  376. type
  377. TScrViewFrm = class(TForm)
  378. ScrlBox: TScrollBox;
  379. ImgView: TImage;
  380. ImgPop: TPopupMenu;
  381. Savetofile1: TMenuItem;
  382. SaveDlg: TSaveDialog;
  383. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
  384. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  385. procedure Savetofile1Click(Sender: TObject);
  386. private
  387. { Private declarations }
  388. public
  389. { Public declarations }
  390. end;
  391.  
  392. implementation
  393.  
  394. {$R *.dfm}
  395.  
  396. procedure TScrViewFrm.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  397. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  398. var
  399. I: Integer;
  400. begin
  401. Handled := PtInRect(ScrlBox.ClientRect, ScrlBox.ScreenToClient(MousePos));
  402. if Handled then
  403. for I := 1 to Mouse.WheelScrollLines do
  404. try
  405. if WheelDelta > 0 then
  406. ScrlBox.Perform(WM_VSCROLL, SB_LINEUP, 0)
  407. else
  408. ScrlBox.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  409. finally
  410. ScrlBox.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
  411. end;
  412. end;
  413.  
  414. procedure TScrViewFrm.Savetofile1Click(Sender: TObject);
  415. var
  416. Png:TPngObject;
  417. begin
  418. if SaveDlg.Execute then
  419. begin
  420. Png:=TPngObject.Create;
  421. try
  422. Png.Assign(ImgView.Picture.Graphic);
  423. Png.SaveToFile(SaveDlg.FileName);
  424. finally
  425. Png.Free;
  426. end;
  427. end;
  428. end;
  429.  
  430. end.
Advertisement
Add Comment
Please, Sign In to add comment