⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interceptscreenimpl.~pas

📁 用delphi开发的网页截屏控件,嵌入在网页中使用.
💻 ~PAS
字号:
unit InterceptScreenImpl;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, InterceptScreenProj_TLB, StdVcl, EncdDecd, ExtCtrls,
  jpeg, Registry;

type
  TInterceptScreen = class(TActiveForm, IInterceptScreen)
    Image1: TImage;
  private
    { Private declarations }
    FEvents: IInterceptScreenEvents;
    procedure ActivateEvent(Sender: TObject);
    procedure ClickEvent(Sender: TObject);
    procedure CreateEvent(Sender: TObject);
    procedure DblClickEvent(Sender: TObject);
    procedure DeactivateEvent(Sender: TObject);
    procedure DestroyEvent(Sender: TObject);
    procedure KeyPressEvent(Sender: TObject; var Key: Char);
    procedure PaintEvent(Sender: TObject);
  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    function Get_Active: WordBool; safecall;
    function Get_AlignDisabled: WordBool; safecall;
    function Get_AutoScroll: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_Cursor: Smallint; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DropTarget: WordBool; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_HelpFile: WideString; safecall;
    function Get_HelpKeyword: WideString; safecall;
    function Get_HelpType: TxHelpType; safecall;
    function Get_KeyPreview: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_PrintScale: TxPrintScale; safecall;
    function Get_Scaled: WordBool; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    procedure _Set_Font(var Value: IFontDisp); safecall;
    procedure Set_AutoScroll(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_Cursor(Value: Smallint); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DropTarget(Value: WordBool); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(const Value: IFontDisp); safecall;
    procedure Set_HelpFile(const Value: WideString); safecall;
    procedure Set_HelpKeyword(const Value: WideString); safecall;
    procedure Set_HelpType(Value: TxHelpType); safecall;
    procedure Set_KeyPreview(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_PrintScale(Value: TxPrintScale); safecall;
    procedure Set_Scaled(Value: WordBool); safecall;
    procedure Set_Visible(Value: WordBool); safecall;

    function  GetJpegString: WideString; safecall;                //返回转换为Base64编码的字符串
    procedure InterceptScreen; safecall;                          //截屏
    function  GetInternetTempFilePath: WideString; safecall;      //得到internet临时文件路径
    function  GetInternetTmpPath: String;                        //得到internet临时文件夹路径
  public
    { Public declarations }
    procedure Initialize; override;
  end;

var
  msout: String;
  internetTempFile: String;

implementation

uses ComObj, ComServ;

{$R *.DFM}

{ TInterceptScreen }

procedure TInterceptScreen.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  { Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_InterceptScreenPage); }
end;

procedure TInterceptScreen.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IInterceptScreenEvents;
  inherited EventSinkChanged(EventSink);
end;

procedure TInterceptScreen.Initialize;
begin
  inherited Initialize;
  OnActivate := ActivateEvent;
  OnClick := ClickEvent;
  OnCreate := CreateEvent;
  OnDblClick := DblClickEvent;
  OnDeactivate := DeactivateEvent;
  OnDestroy := DestroyEvent;
  OnKeyPress := KeyPressEvent;
  OnPaint := PaintEvent;
end;

function TInterceptScreen.Get_Active: WordBool;
begin
  Result := Active;
end;

function TInterceptScreen.Get_AlignDisabled: WordBool;
begin
  Result := AlignDisabled;
end;

function TInterceptScreen.Get_AutoScroll: WordBool;
begin
  Result := AutoScroll;
end;

function TInterceptScreen.Get_AutoSize: WordBool;
begin
  Result := AutoSize;
end;

function TInterceptScreen.Get_AxBorderStyle: TxActiveFormBorderStyle;
begin
  Result := Ord(AxBorderStyle);
end;

function TInterceptScreen.Get_Caption: WideString;
begin
  Result := WideString(Caption);
end;

function TInterceptScreen.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;

function TInterceptScreen.Get_Cursor: Smallint;
begin
  Result := Smallint(Cursor);
end;

function TInterceptScreen.Get_DoubleBuffered: WordBool;
begin
  Result := DoubleBuffered;
end;

function TInterceptScreen.Get_DropTarget: WordBool;
begin
  Result := DropTarget;
end;

function TInterceptScreen.Get_Enabled: WordBool;
begin
  Result := Enabled;
end;

function TInterceptScreen.Get_Font: IFontDisp;
begin
  GetOleFont(Font, Result);
end;

function TInterceptScreen.Get_HelpFile: WideString;
begin
  Result := WideString(HelpFile);
end;

function TInterceptScreen.Get_HelpKeyword: WideString;
begin
  Result := WideString(HelpKeyword);
end;

function TInterceptScreen.Get_HelpType: TxHelpType;
begin
  Result := Ord(HelpType);
end;

function TInterceptScreen.Get_KeyPreview: WordBool;
begin
  Result := KeyPreview;
end;

function TInterceptScreen.Get_PixelsPerInch: Integer;
begin
  Result := PixelsPerInch;
end;

function TInterceptScreen.Get_PrintScale: TxPrintScale;
begin
  Result := Ord(PrintScale);
end;

function TInterceptScreen.Get_Scaled: WordBool;
begin
  Result := Scaled;
end;

function TInterceptScreen.Get_Visible: WordBool;
begin
  Result := Visible;
end;

function TInterceptScreen.Get_VisibleDockClientCount: Integer;
begin
  Result := VisibleDockClientCount;
end;

procedure TInterceptScreen._Set_Font(var Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TInterceptScreen.ActivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnActivate;
end;

procedure TInterceptScreen.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure TInterceptScreen.CreateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnCreate;
end;

procedure TInterceptScreen.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure TInterceptScreen.DeactivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDeactivate;
end;

procedure TInterceptScreen.DestroyEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDestroy;
end;

procedure TInterceptScreen.KeyPressEvent(Sender: TObject; var Key: Char);
var
  TempKey: Smallint;
begin
  TempKey := Smallint(Key);
  if FEvents <> nil then FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

procedure TInterceptScreen.PaintEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnPaint;
end;

procedure TInterceptScreen.Set_AutoScroll(Value: WordBool);
begin
  AutoScroll := Value;
end;

procedure TInterceptScreen.Set_AutoSize(Value: WordBool);
begin
  AutoSize := Value;
end;

procedure TInterceptScreen.Set_AxBorderStyle(
  Value: TxActiveFormBorderStyle);
begin
  AxBorderStyle := TActiveFormBorderStyle(Value);
end;

procedure TInterceptScreen.Set_Caption(const Value: WideString);
begin
  Caption := TCaption(Value);
end;

procedure TInterceptScreen.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;

procedure TInterceptScreen.Set_Cursor(Value: Smallint);
begin
  Cursor := TCursor(Value);
end;

procedure TInterceptScreen.Set_DoubleBuffered(Value: WordBool);
begin
  DoubleBuffered := Value;
end;

procedure TInterceptScreen.Set_DropTarget(Value: WordBool);
begin
  DropTarget := Value;
end;

procedure TInterceptScreen.Set_Enabled(Value: WordBool);
begin
  Enabled := Value;
end;

procedure TInterceptScreen.Set_Font(const Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TInterceptScreen.Set_HelpFile(const Value: WideString);
begin
  HelpFile := String(Value);
end;

procedure TInterceptScreen.Set_HelpKeyword(const Value: WideString);
begin
  HelpKeyword := String(Value);
end;

procedure TInterceptScreen.Set_HelpType(Value: TxHelpType);
begin
  HelpType := THelpType(Value);
end;

procedure TInterceptScreen.Set_KeyPreview(Value: WordBool);
begin
  KeyPreview := Value;
end;

procedure TInterceptScreen.Set_PixelsPerInch(Value: Integer);
begin
  PixelsPerInch := Value;
end;

procedure TInterceptScreen.Set_PrintScale(Value: TxPrintScale);
begin
  PrintScale := TPrintScale(Value);
end;

procedure TInterceptScreen.Set_Scaled(Value: WordBool);
begin
  Scaled := Value;
end;

procedure TInterceptScreen.Set_Visible(Value: WordBool);
begin
  Visible := Value;
end;

//新增内容

function TInterceptScreen.GetJpegString: WideString;
begin
  Result := msout;
end;

procedure TInterceptScreen.InterceptScreen;
var
  Fullscreen: TBitmap;
  ScreenJpeg: TJpegImage;
  FullscreenCanvas: TCanvas;
  dc: HDC;
  msin: TStringStream;
  filePath: String;
begin
  try
    begin
      Fullscreen := TBitmap.Create;
      ScreenJpeg := TJpegImage.Create;
      msin := TStringStream.Create('');
      //msout := TStringStream.Create('');
      //创建一个Bitmap来存放图象
      Fullscreen.Width := screen.Width;
      Fullscreen.Height := screen.Height;
      //取得屏幕的DC,参数0指的是屏幕
      DC := GetDC(0);
      FullscreenCanvas := TCanvas.Create;
      //创建一个Canvas对象
      FullscreenCanvas.Handle := DC;
      //把整个屏幕复制到Bitmap中
      Fullscreen.Canvas.CopyRect(Rect(0,0,screen.Width,screen.Height),FullscreenCanvas,Rect(0,0,screen.Width,screen.Height));
      //释放Canvas对象
      FullscreenCanvas.Free;
      //释放DC
      ReleaseDC(0,DC);
      //拷贝图象到Image中
      Image1.Picture.Bitmap := Fullscreen;
      Image1.Width := Fullscreen.Width;
      Image1.Height := Fullscreen.Height;
      //Image1.Width := iWidth;
      //Image1.Height := iHeight;
      //释放Bitmap
      messagebeep(1);
      ScreenJpeg.Assign(Fullscreen);
      ScreenJpeg.CompressionQuality:=StrToInt('75');
      ScreenJpeg.Compress;
      filePath := GetInternetTmpPath + '\' + FormatDateTime('yyyymmddHHMMss',now) + '.jpg';
      internetTempFile := filePath;
      ScreenJpeg.SaveToFile(filePath);
      ScreenJpeg.SaveToStream(msin);
      Fullscreen.Free;
      //复原窗口状态
      //form1.WindowState := wsNormal;
      //form1.Show;
      if(msin.DataString='') then
      begin
        showmessage('bmp转换成字符流失败');
        exit;
      end
      else
      begin
        msout := EncodeString(msin.DataString);
        if(msout='') then
        begin
          showmessage('转成Base64编码失败');
          exit;
        end;
      end;
    end;
  except
    showmessage('截屏失败');
  end;
end;

function TInterceptScreen.GetInternetTmpPath: String;
var
  reg: TRegistry;
  Cachedir: String;
begin
Reg:=TRegistry.Create;
  try
    begin
      Reg.RootKey := HKEY_CURRENT_USER;
      Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\',False);
      Cachedir := Reg.ReadString('Cache');
    end;
  finally
    reg.Free;
  end;
  Result := Cachedir;
end;

function TInterceptScreen.GetInternetTempFilePath: WideString;
begin
  Result := internetTempFile;
end;


initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    TInterceptScreen,
    Class_InterceptScreen,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -