Autor Beitrag
glotzer
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 393
Erhaltene Danke: 49

Win 7
Lazarus
BeitragVerfasst: Fr 05.11.10 18:41 
Das Programm soll auf tastendruck einen Screenshoot von einem Fenster mit dem Title "Paint" machen

DLL:
ausblenden volle Höhe Delphi-Quelltext
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:
library KeyboardHook;

uses
  Windows;

type
  TCatchFunction =  Procedure of Object;

var
  HookHandle: Cardinal = 0;
  CatchKey: Cardinal = 0;
  CatchFunction: TCatchFunction;

procedure UpdateCatchKey(key:Cardinal); stdcall;
begin
  CatchKey := key;
end;

procedure UpdateCatchFunction(AFunction: TCatchFunction); stdcall;
begin
  CatchFunction := AFunction;
end;

function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  //it's possible to call CallNextHookEx conditional only.
  Result := CallNextHookEx(HookHandle, nCode, wParam, lParam);
  case nCode < 0 of
    TRUE: exit; //wenn code kleiner 0 wird nix gemacht
                //if code smaller 0 nothing has to be done
    FALSE:
      begin
        if WParam = CatchKey then CatchFunction;
      end;
  end;
end;

function InstallHook: Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0 then
  begin
    //First install the hook
    HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
    //Save the given window handle
    Result := TRUE;
  end;
end;

function UninstallHook: Boolean; stdcall;
begin
  //Uninstall hook from hook chain
  Result := UnhookWindowsHookEx(HookHandle);
  HookHandle := 0;
end;

exports
  //Export the installation and deinstallation routine
  InstallHook,
  UninstallHook,
  //Exporting the Update rutine
  UpdateCatchKey,
  UpdateCatchFunction;
end.


programm(gekürtzt):
ausblenden volle Höhe Delphi-Quelltext
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:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, pngimage, StdCtrls, ExtCtrls, XPMan, FileCtrl, ComCtrls, HookDllInterface,
  SelectKey;

[...]

var
  MainForm: TMainForm;

Const
  Title = 'Paint';

implementation

{$R *.dfm}


procedure TMainForm.StartStopBtnClick(Sender: TObject);
begin
  Catching := not Catching;
  if not Catching then
  begin
    StartStopBtn.Caption := 'Start Catching';
    EndCatching;
  end else
  begin
    StartStopBtn.Caption := 'End Catching';
    BeginCatching;
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  UpdateOptions(self);
  Catching := false;
  HookKeyEChange(self);
  UpdateCatchFunction(self.CatchScreenshoot);
end;

procedure TMainForm.HookKeyEChange(Sender: TObject);
begin
  UpdateCatchKey(strtoint(HookKeyE.Text));
end;

procedure TMainForm.BeginCatching;
begin
  WindowID := FindWindow(nil,Title);
  if WindowID = 0 then
  begin
    ShowMessage('Please start Paint before starting to catch Screenshoots');
    StartStopBtn.Caption := 'Start Catching';
    Catching := false;
    exit;
  end;
  InstallHook;
end;

procedure TMainForm.EndCatching;
begin
  UninstallHook;
end;

function FormularSaveScreenShot(FileName: String; h : hWnd): Boolean;
var
  Rec: TRect;
  iWidth, iHeight: Integer;
begin
  with TBitmap.Create do try
    GetWindowRect(h, Rec);

    iWidth  := Rec.Right - Rec.Left;
    iHeight := Rec.Bottom - Rec.Top;

    Width := iWidth;
    Height := iHeight;

    BitBlt(Canvas.Handle, 00, iWidth, iHeight, GetWindowDC(h), 00, SRCCOPY);

    Result := True;

    try SaveToFile(Filename) except Result := False end;
  finally
    ReleaseDC(h, GetWindowDC(h));
    Free;
  end;
end;

procedure TMainForm.CatchScreenshoot;
begin
  FormularSaveScreenShot('test.bmp', WindowID);
end;

//Taste auswählen
procedure TMainForm.Button1Click(Sender: TObject);
var tmp: word;
begin
  SelectKeyF.ShowModal;
  tmp := SelectKeyF.PressedKey;
  HookKeyE.Text := inttostr(tmp);
end;

end.


Wenn ich mein eigenes Fenster im Vordergrund habe funktioniert alles ohne probleme...
aber sobald es irgendein anderes ist pasiert einfach garnichts.

Danke im voraus.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 05.11.10 19:53 
Warum einen Hook? Belastet nur das System unnötig und kann, wenn er nicht sauber programmiert ist, das System instabil machen. Ein HotKey reicht da vollkommen.

Für diesen Beitrag haben gedankt: glotzer
glotzer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 393
Erhaltene Danke: 49

Win 7
Lazarus
BeitragVerfasst: Fr 05.11.10 19:59 
omg, YA daran hab ich garnicht gedacht :D

tausend Dank