Advertisement
Guest User

HX_unbanned

a guest
Jun 29th, 2012
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.38 KB | None | 0 0
  1. {
  2.         nsScreenshot NSIS Plugin
  3.         (c) 2003: Leon Zandman (leon@wirwar.com)
  4.  
  5.         Re-compiled by: Linards Liepins (linards.liepins@gmail.com)
  6.         Code by: http://www.delphitricks.com/source-code/forms/make_a_desktop_screenshot.html
  7.         (e) 2012.
  8. }
  9. library nsScreenshot;
  10.  
  11. uses
  12.   nsis in './nsis.pas',
  13.   Windows,
  14.   Jpeg,
  15.   graphics,
  16.   types,
  17.   SysUtils;
  18.  
  19. const
  20.   USER32 = 'user32.dll';
  21.  
  22. type
  23.   HWND = type LongWord;
  24.   {$EXTERNALSYM HWND}
  25.   HDC = type LongWord;
  26.   {$EXTERNALSYM HDC}
  27.   BOOL = LongBool;
  28.   {$EXTERNALSYM BOOL}
  29.  
  30. {$EXTERNALSYM GetDesktopWindow}
  31. function GetDesktopWindow: HWND; stdcall; external USER32 name 'GetDesktopWindow';
  32. {$EXTERNALSYM GetWindowDC}
  33. function GetWindowDC(hWnd: HWND): HDC; stdcall; external USER32 name 'GetWindowDC';
  34. {$EXTERNALSYM GetWindowRect}
  35. function GetWindowRect(hWnd: HWND; var lpRect: TRect): BOOL; stdcall; external USER32 name 'GetWindowRect';
  36. {$EXTERNALSYM ReleaseDC}
  37. function ReleaseDC(hWnd: HWND; hDC: HDC): Integer; stdcall; external user32 name 'ReleaseDC';
  38.  
  39. function GetScreenshot(Filename: string; Hwnd: HWND; var Width: integer; var Height: integer): boolean; forward;
  40. function ScreenShot(Bild: TBitMap; hWnd: HWND): boolean; forward;
  41.  
  42.  
  43. function Grab_FullScreen(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
  44. var
  45.   buf: array[0..1024] of char;
  46.   W,H: integer;
  47. begin
  48.   Result := 0;
  49.   // set up global variables
  50.   Init(hwndParent,string_size,variables,stacktop);
  51.  
  52.   // Get filename to save to
  53.   PopString;//(@buf);
  54.  
  55.   // Get a full-screen screenshot
  56.   if GetScreenShot(buf,GetDesktopWindow,W,H) then begin
  57.     // Everything went just fine...
  58.  
  59.     // Push image dimensions onto stack
  60.     PushString(PChar(IntToStr(H)));
  61.     PushString(PChar(IntToStr(W)));
  62.  
  63.     // Push result onto stack
  64.     PushString(PChar('ok'));
  65.     Result := 1;
  66.   end else begin
  67.     // Something went wrong...
  68.     PushString(PChar('error'));
  69.   end;
  70. end;
  71.  
  72. function Grab(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
  73. var
  74.   buf: array[0..1024] of char;
  75.   grabWnd: HWND;
  76.   Filename: string;
  77.   W,H: integer;
  78. begin
  79.   Result := 0;
  80.   // set up global variables
  81.   Init(hwndParent,string_size,variables,stacktop);
  82.  
  83.   try
  84.     // Get filename to save to
  85.     PopString;//(@buwf);
  86.     Filename := buf;
  87.  
  88.     // Get window handle of window to grab
  89.     PopString;//(@buf);
  90.     grabWnd := StrToInt(buf);
  91.   except
  92.     PushString(PChar('error'));
  93.     exit;
  94.   end;
  95.  
  96.   // Get screenshot of parent windows (NSIS)
  97.   if GetScreenShot(Filename,grabWnd,W,H) then begin
  98.     // Everything went just fine...
  99.  
  100.     // Push image dimensions onto stack
  101.     PushString(PChar(IntToStr(H)));
  102.     PushString(PChar(IntToStr(W)));
  103.  
  104.     // Push result onto stack
  105.     PushString(PChar('ok'));
  106.     Result := 1;
  107.   end else begin
  108.     // Something went wrong...
  109.     PushString(PChar('error'));
  110.   end;
  111. end;
  112.  
  113. function GetScreenshot(Filename: string; Hwnd: HWND; var Width: integer; var Height: integer): boolean;
  114. var
  115.   bmp: TBitmap;
  116. begin
  117.   Result := false;
  118.  
  119.   // Get screenshot
  120.   bmp := TBitmap.Create;
  121.   try
  122.     try
  123.       if ScreenShot(bmp,Hwnd) then begin
  124.         Width  := bmp.Width;
  125.         Height := bmp.Height;
  126.         bmp.SaveToFile(Filename);
  127.         Result := true;
  128.       end;
  129.     except
  130.       // Catch exception and do nothing (function return value remains 'false')
  131.     end;
  132.   finally
  133.     bmp.Free;
  134.   end;
  135. end;
  136.  
  137. function ScreenShot(Bild: TBitMap; hWnd: HWND): boolean;
  138. var
  139.   c: TCanvas;
  140.   r, t: TRect;
  141.   h: THandle;
  142. begin
  143.   Result := false;
  144.  
  145.   c := TCanvas.Create;
  146.   c.Handle := GetWindowDC(GetDesktopWindow);
  147.  
  148.   h := hWnd;
  149.   if h <> 0 then begin
  150.     GetWindowRect(h, t);
  151.     try
  152.       r := Rect(0, 0, t.Right - t.Left, t.Bottom - t.Top);
  153.       Bild.Width  := t.Right - t.Left;
  154.       Bild.Height := t.Bottom - t.Top;
  155.       Bild.Canvas.CopyRect(r, c, t);
  156.     finally
  157.       ReleaseDC(0, c.Handle);
  158.       c.Free;
  159.     end;
  160.     Result := true;
  161.   end;
  162. end;
  163.  
  164. function GetScreenToFile(FileName: string; Quality: Word; Percent: Word): boolean;
  165. var
  166.   Bmp: TBitmap;
  167.   Jpg: TJpegImage;
  168. begin
  169.   Bmp := TBitmap.Create;
  170.   Jpg := TJpegImage.Create;
  171.   try
  172.     Bmp.Width := GetDeviceCaps(GetDc(0), 8) * Percent div 100;
  173.     Bmp.Height := GetDeviceCaps(GetDc(0), 10) * Percent div 100;
  174.     SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
  175.     StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, GetDc(0), 0, 0, GetDeviceCaps(GetDc(0), 8), GetDeviceCaps(GetDc(0), 10), SRCCOPY);
  176.     Jpg.Assign(Bmp);
  177.     Jpg.CompressionQuality := Quality;
  178.     Jpg.SaveToFile(FileName);
  179.   finally
  180.     Jpg.free;
  181.     Bmp.free;
  182.   end;
  183. end;
  184.  
  185. function ScreenToFile(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
  186. var
  187.   buf: array[0..1024] of char;
  188.   grabWnd: HWND;
  189.   Filename: string;
  190.   W,H: integer;
  191. begin
  192.   Result := 0;
  193.   Init(hwndParent,string_size,variables,stacktop);
  194.   try
  195.     PopString;
  196.     Filename := buf;
  197.     PopString;
  198.     grabWnd := StrToInt(buf);
  199.   except
  200.     PushString(PChar('error'));
  201.     exit;
  202.   end;
  203.   if GetScreenToFile(Filename,W,H) then
  204.   begin
  205.     PushString(PChar('ok'));
  206.     Result := 1;
  207.   end else
  208.   begin
  209.     PushString(PChar('error'));
  210.   end;
  211. end;
  212.  
  213.   //ScreenToFile('SHOT.JPG', 50, 70);
  214.  
  215. exports Grab_FullScreen,
  216.         Grab,
  217.         ScreenToFile;
  218.  
  219. begin
  220. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement