Programa de exemplo de Duplicação de Desktop – Show Delphi

DICAS

Visite a biblioteca de dicas da comunidade.

Saiba mais

ARTIGOS

Abordagens detalhadas sobre assuntos diversos.

Saiba mais

INICIANTES

Aprenda a programar de um modo simples e fácil.

Saiba mais

DOWNLOADS

Acesse os materiais exclusivos aos membros.

Saiba mais
voltar

PARA QUEM GOSTA DE DELPHI

Programa de exemplo de Duplicação de Desktop

 

Programa de exemplo base de captura da área de trabalho em tempo real

Autor: Gilberto Rocha da Silva

Método: Duplicação de Desktop com DXGI

Os Headers em DX11 e o update todo traduzido.

Obs. Só funciona do Windows 8 ou superior.

Veja o fonte da tela principal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
unit MainUnit;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.Samples.Spin, OutputManager, DuplicationManager, Threadmanager,
  CommonTypes, CommonUnit;
 
type
 
  TOutputLoopThread = class(TThread)
    procedure Execute; override;
    constructor CreateIt;
    destructor Destroy; override;
  public
    UnexpectedErrorEvent: Thandle;
    ExpectedErrorEvent: Thandle;
    TerminateThreadsEvent: Thandle;
    ThreadTerminatedEvent: Thandle;
    HandleToWrite: Thandle;
    SingleOutput: integer;
  private
    OutputCount: uint;
    DeskBounds: TRECT;
    FirstTime: boolean;
    fThreadMsgReport: TThreadMsgReport;
    ThreadMgr: TTHREADMANAGER;
    DynamicWait: TDYNAMIC_WAIT;
    procedure preMsgReport(errorMsg: String);
    procedure OnMsgFromOtherThreads(Sender: TObject; Msg: String);
  public
    property OnThreadMsgReport: TThreadMsgReport read fThreadMsgReport
      write fThreadMsgReport;
  end;
 
  TMainUnitFrm = class(TForm)
    btStart: TButton;
    Memo: TMemo;
    lblReport: TLabel;
    pnViewer: TPanel;
    lblViewer: TLabel;
    btStop: TButton;
    chkDupAllOutputs: TCheckBox;
    spnOutput: TSpinEdit;
    lblOutput: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btStartClick(Sender: TObject);
    procedure btStopClick(Sender: TObject);
    procedure chkDupAllOutputsClick(Sender: TObject);
  private
    SingleOutput: integer;
    UnexpectedErrorEvent: Thandle;
    ExpectedErrorEvent: Thandle;
    TerminateThreadsEvent: Thandle;
    ThreadTerminatedEvent: Thandle;
    OutputLoopThread: TOutputLoopThread;
    FWndProcPrevious: TFNWndProc;
    FWndProcInstanceStub: pointer;
    panelDefaultBackground: TBitmap;
    procedure PanelProcSubClassed(var Msg: TMessage);
    procedure blockStartButton;
    procedure OnThreadMsgReport(Sender: TObject; Msg: String);
  end;
 
var
  MainUnitFrm: TMainUnitFrm;
  Occluded: boolean = false;
  HandledOnemsg: boolean = false;
  OutMgr: TOutputManager = nil;
 
implementation
 
{$R *.dfm}
 
procedure TMainUnitFrm.blockStartButton;
begin
  btStart.Enabled := false;
end;
 
procedure TMainUnitFrm.btStartClick(Sender: TObject);
begin
  btStop.Enabled := true;
  btStart.Enabled := false;
  if chkDupAllOutputs.Checked then
    SingleOutput := -1
  else
    SingleOutput := spnOutput.Value;
  if OutMgr = nil then
    OutMgr := TOutputManager.Create
  else
  begin
    OutMgr.Free;
    OutMgr.Create;
  end;
  OutputLoopThread := TOutputLoopThread.CreateIt;
  with OutputLoopThread do
  begin
    HandleToWrite := pnViewer.Handle;
    SingleOutput := self.SingleOutput;
    OnThreadMsgReport := self.OnThreadMsgReport;
    UnexpectedErrorEvent := self.UnexpectedErrorEvent;
    ExpectedErrorEvent := self.ExpectedErrorEvent;
    TerminateThreadsEvent := self.TerminateThreadsEvent;
    ThreadTerminatedEvent := self.ThreadTerminatedEvent;
    FreeOnTerminate := true;
    Start;
  end;
  Memo.Lines.Add('Output Thread Started');
end;
 
procedure TMainUnitFrm.btStopClick(Sender: TObject);
begin
  OutputLoopThread.Terminate;
  WaitForSingleObjectEx(ThreadTerminatedEvent, 1000, false);
  application.ProcessMessages;
  FreeAndNil(OutMgr);
  btStart.Enabled := true;
  btStop.Enabled := false;
  resetEvent(TerminateThreadsEvent);
end;
 
procedure TMainUnitFrm.chkDupAllOutputsClick(Sender: TObject);
begin
  spnOutput.Enabled := not chkDupAllOutputs.Checked;
end;
 
procedure TMainUnitFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(panelDefaultBackground);
  if UnexpectedErrorEvent <> 0 then
    closehandle(UnexpectedErrorEvent);
  if ExpectedErrorEvent <> 0 then
    closehandle(ExpectedErrorEvent);
  if TerminateThreadsEvent <> 0 then
    closehandle(TerminateThreadsEvent);
  if ThreadTerminatedEvent <> 0 then
    closehandle(ThreadTerminatedEvent);
  if OutMgr <> nil then
    OutMgr.Free;
  if FWndProcPrevious <> nil then
  begin
    SetWindowlong(pnViewer.Handle, GWL_WNDPROC, NativeInt(FWndProcPrevious));
    FreeObjectInstance(FWndProcInstanceStub);
    FWndProcPrevious := nil;
    FWndProcInstanceStub := nil;
  end;
end;
 
procedure TMainUnitFrm.FormCreate(Sender: TObject);
begin
  if not(((AWWinVerVIx.dwMajorVersion = 6) and (AWWinVerVIx.dwMinorVersion >= 2)
    ) or (AWWinVerVIx.dwMajorVersion > 6)) then
  begin
    showmessage('Required Windows 8 or higher');
    application.Terminate;
  end;
 
  UnexpectedErrorEvent := CreateEvent(nil, true, false, nil);
  if UnexpectedErrorEvent = 0 then
  begin
    Memo.Lines.Add('UnexpectedErrorEvent creation failed');
    blockStartButton();
  end;
  ExpectedErrorEvent := CreateEvent(nil, true, false, nil);
  if ExpectedErrorEvent = 0 then
  begin
    Memo.Lines.Add('ExpectedErrorEvent creation failed');
    blockStartButton();
  end;
  TerminateThreadsEvent := CreateEvent(nil, true, false, nil);
  if TerminateThreadsEvent = 0 then
  begin
    Memo.Lines.Add('TerminateThreadsEvent creation failed');
    blockStartButton();
  end;
  ThreadTerminatedEvent := CreateEvent(nil, false, false, nil);
  if ThreadTerminatedEvent = 0 then
  begin
    Memo.Lines.Add('ThreadTerminatedEvent creation failed');
    blockStartButton();
  end;
  panelDefaultBackground := TBitmap.Create;
  panelDefaultBackground.Canvas.Brush.Color := clBtnFace;
  panelDefaultBackground.PixelFormat := pf32bit;
  FWndProcPrevious := TFNWndProc(GetWindowLong(pnViewer.Handle, GWL_WNDPROC));
  FWndProcInstanceStub := MakeObjectInstance(PanelProcSubClassed);
  SetWindowlong(pnViewer.Handle, GWL_WNDPROC, NativeInt(FWndProcInstanceStub));
end;
 
procedure TMainUnitFrm.OnThreadMsgReport(Sender: TObject; Msg: String);
begin
  Memo.Lines.Add(Msg);
end;
 
procedure TMainUnitFrm.PanelProcSubClassed(var Msg: TMessage);
begin
  if Msg.Msg = OCCLUSION_STATUS_MSG then
  begin
    Occluded := true;
  end
  else if Msg.Msg = WM_SIZE then
  begin
    if OutMgr <> nil then
    begin
      OutMgr.WindowResize();
    end;
  end;
  HandledOnemsg := true;
  Msg.Result := CallWindowProc(FWndProcPrevious, pnViewer.Handle, Msg.Msg,
    Msg.WParam, Msg.LParam);
end;
 
{ TOutputLoopThread }
 
constructor TOutputLoopThread.CreateIt;
begin
  inherited Create(true);
end;
 
destructor TOutputLoopThread.Destroy;
begin
  inherited;
end;
 
procedure TOutputLoopThread.Execute;
var
  Ret: TDUPL_RETURN;
  SharedHandle: Thandle;
begin
  ThreadMgr := TTHREADMANAGER.Create;
  ThreadMgr.OnMsgReport := OnMsgFromOtherThreads;
  DynamicWait := TDYNAMIC_WAIT.Create;
  FirstTime := true;
  Occluded := true;
  try
    while not terminated do
    begin
      Ret := DUPL_RETURN_SUCCESS;
      if Occluded or HandledOnemsg then
      begin
        if Occluded then
        begin
          Occluded := false;
        end
        else
        begin
          HandledOnemsg := false;
        end
      end
      else if WaitForSingleObjectEx(UnexpectedErrorEvent, 0, false) = WAIT_OBJECT_0
      then
      begin
        preMsgReport('LoopThread - UnexpectedErrorEvent');
        break;
      end
      else if FirstTime or (WaitForSingleObjectEx(ExpectedErrorEvent, 0, false)
        = WAIT_OBJECT_0) then
      begin
        if not FirstTime then
        begin
          SetEvent(TerminateThreadsEvent);
          ThreadMgr.WaitForThreadTermination();
          resetEvent(TerminateThreadsEvent);
          resetEvent(ExpectedErrorEvent);
          ThreadMgr.Clean();
          OutMgr.CleanRefs();
          DynamicWait.Wait();
        end
        else
          FirstTime := false;
        Ret := OutMgr.InitOutput(HandleToWrite, SingleOutput, OutputCount,
          DeskBounds);
        if Ret = DUPL_RETURN_SUCCESS then
        begin
          SharedHandle := OutMgr.getSharedHandle;
          if SharedHandle <> 0 then
          begin
            Ret := ThreadMgr.Initialize(SingleOutput, OutputCount,
              UnexpectedErrorEvent, ExpectedErrorEvent, TerminateThreadsEvent,
              SharedHandle, @DeskBounds);
          end
          else
          begin
            preMsgReport('Failed to get handle of shared surface');
            Ret := DUPL_RETURN_ERROR_UNEXPECTED;
          end;
        end;
        Occluded := true;
      end
      else
      begin
        if not Occluded then
          Ret := OutMgr.UpdateApplicationWindow(ThreadMgr.GetPointerInfo(),
            Occluded);
      end;
      if Ret <> DUPL_RETURN_SUCCESS then
      begin
        if Ret = DUPL_RETURN_ERROR_EXPECTED then
          SetEvent(ExpectedErrorEvent)
        else
          break;
      end;
      if terminated then
        preMsgReport('Received Terminate');
    end;
  finally
    if SetEvent(TerminateThreadsEvent) then
      ThreadMgr.WaitForThreadTermination;
    SetEvent(ThreadTerminatedEvent);
    DynamicWait.Free;
    ThreadMgr.Free;
  end;
  ReturnValue := 0;
end;
 
procedure TOutputLoopThread.OnMsgFromOtherThreads(Sender: TObject; Msg: String);
begin
  preMsgReport(Msg);
end;
 
procedure TOutputLoopThread.preMsgReport(errorMsg: String);
begin
  queue(
    procedure
    begin
      if assigned(fThreadMsgReport) then
        fThreadMsgReport(self, errorMsg);
    end);
end;
 
end.

Baixe o exemplo completo com fontes:
showdelphi.com.br/download/2031/

Exemplo testado no Delphi 10 seatle

Facebook Comments

Posts Relacionados

  • Administrador
  • 156 views
  • 0 comentários
  • 26 de agosto de 2016

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Ir ao topo

© 2016 Infus Soluções em Tecnologia - Todos os Direitos Reservados