unit MainForm;
{$mode objfpc}{$H+}
interface
uses
Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls;
type
PFindWindowStruct = ^TFindWindowStruct;
TFindWindowStruct =
record
Caption:
string;
ClassName:
String;
WindowHandle: THandle;
end;
type
Tfrm_main =
class(TForm)
// Buttons, Labels, ...
Timer1: TTimer;
procedure FormActivate(Sender: TObject);
procedure FormKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
// restliche Prozeduren
private
FExtWinHandle: THandle;
{ private declarations }
public
{ public declarations }
end;
var
frm_main: Tfrm_main;
function FindAWindow(WinCaption:
String; WinClassName:
String): THandle;
function EnumWindowsProc(hWindow: hWnd; lParam: PtrInt): Bool;
stdcall;
implementation
{$R *.lfm}
{ Tfrm_main }
//------------------------------------------------------------------------------
procedure Tfrm_main.FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
case Key
of
37:
// Aktion für VK_LEFT;
40:
// Aktion für VK_DOWN;
39:
// Aktion für VK_RIGHT;
38:
// Aktion für VK_UP;;
end;
end;
//------------------------------------------------------------------------------
procedure Tfrm_main.FormActivate(Sender: TObject);
begin
FExtWinHandle := 0;
end;
//------------------------------------------------------------------------------
function EnumWindowsProc(hWindow: hWnd; lParam: PtrInt): Bool;
Stdcall;
var
lpBuffer: PChar;
WindowCaptionFound: Boolean;
ClassNameFound: Boolean;
begin
GetMem(lpBuffer, 255);
Result := True;
WindowCaptionFound := False;
ClassNameFound := False;
try
if GetWindowText(hWindow, lpBuffer,255)>0
then
if Pos(PFindWindowStruct(lParam)^.Caption, StrPas(lpBuffer))>0
then
WindowCaptionFound := True;
if PFindWindowStruct(lParam)^.ClassName='
'
then
ClassNameFound := True
else if GetClassName(hWindow, lpBuffer, 255)>0
then
if Pos(PFindWindowStruct(lParam)^.ClassName, StrPas(lpBuffer))>0
then
ClassNameFound := True;
if (WindowCaptionFound
and ClassNameFound)
then
begin
PFindWindowStruct(lParam)^.WindowHandle := hWindow;
Result := False;
end;
finally
FreeMem(lpBuffer, SizeOf(lpBuffer^));
end;
end;
//------------------------------------------------------------------------------
function FindAWindow(WinCaption:
String; WinClassName:
String): THandle;
var
WindowInfo: TFindWindowStruct;
begin
with WindowInfo
do
begin
Caption := WinCaption;
ClassName := WinClassName;
WindowHandle := 0;
EnumWindows(@EnumWindowsProc, PtrInt(@WindowInfo));
Result := WindowHandle;
end;
end;
//------------------------------------------------------------------------------
procedure Tfrm_main.Timer1Timer(Sender: TObject);
var
FMyHandle: THandle;
begin
FMyHandle := FindAWindow('
- Editor', '
');
if GetForeGroundWindow <>
Handle then
begin
if FExtWinHandle = 0
then
FExtWinHandle := GetForeGroundWindow;
if FExtWinHandle = FMyHandle
then
begin
BringToFront;
{if frm_main.WindowState = wsMinimized then
frm_main.WindowState := wsNormal;
if not frm_main.Active then
SetActiveWindow(Application.MainForm.Handle);
if not frm_main.Focused then
Application.MainForm.SetFocus;
SetForegroundWindow(Application.MainForm.Handle);}
end;
end;
end;