Board index » delphi » Active Window (Need a little help)

Active Window (Need a little help)

ARGH I NEED HELP. I try to write an application who is self attached to the
active window like a parasite (not a virus :-) ).

To do that, my idea was to write a shell hook which reinstall at the top a
CallWnd hook every time a new application is created. The CallWnd check if
a
wm_Activate is occured and then it inform me.

To test it, I wrote a little code, but it don't work properly. I tryed to
keep a trace while executing into a file and I notice that it never install

the CallWnd hook (I guess ?!) although my system become a little unstable.
In
addition, all the application created after the launch of this code are
halted with an "error in the <unknown> module at blablabla". And that is
not
the aim of my program.

Could anyone tell me what's wrong with this code or give me another way to
resolve my problem ???

I don't have an e-mail address for the moment so the answer can be posted
with the 'RE: Active Window' title and I'll find it. GREAT THANKS.

Here is main program who call the DLL:

{------------------------------------------------------------}
program TestHook;

uses Dialogs;

procedure LibMain; external 'A{*word*163}' index 1;
procedure Wep; external 'A{*word*163}' index 4;

begin
   LibMain;
   {This dialog box is only to keep the hook for a while in memory}
   MessageDlg('Wait', mtInformation, [mbOk], 0);
   Wep;
end.
{------------------------------------------------------------}

And here is the DLL:

{------------------------------------------------------------}
library A{*word*163};

uses
  Windows, Messages;

var
  F: Text;
  hShellHook: hHook;
  hWndProcHook: hHook;

function WndProcHook(Code: Integer; wParam: LongInt; lParam: LongInt):
LongInt; stdcall;
begin
   if Code = wm_Activate then
      WriteLn(F, 'A window was activated or desactivated');
   WndProcHook := CallNextHookEx(hWndProcHook, Code, wParam, lParam);
end;

procedure RemoveWndProcHook;
begin
   WriteLn(F, 'Unloading the WndProcHook');
   if hWndProcHook <> 0 then UnhookWindowsHookEx(hWndProcHook);
   hWndProcHook := 0;
end;

procedure InstallWndProcHook;
begin
   WriteLn(F, 'Loading the WndProcHook');
   if hWndProcHook <> 0 then RemoveWndProcHook;
   hWndProcHook := SetWindowsHookEx(WH_CALLWNDPROC, WndProcHook, hInstance,
0);
end;

function ShellHook(Code: Integer; wParam: LongInt; lParam: LongInt):
LongInt; stdcall;
begin
   if Code = HSHELL_WINDOWCREATED then begin
      RemoveWndProcHook;
      InstallWndProcHook;
      WriteLn(F, 'An apps was created');
   end;
   ShellHook := CallNextHookEx(hShellHook, Code, wParam, lParam);
end;

procedure InstallHookDLL;
begin
   Assign(F, 'Journal.txt'); Rewrite(F);
   WriteLn(F, 'Loading the ShellHook');
   hShellHook := SetWindowsHookEx(WH_SHELL, ShellHook, hInstance, 0);
end;

procedure RemoveHookDLL;
begin
   WriteLn(F, 'Unloading the ShellHook');
   if hShellHook <> 0 then UnhookWindowsHookEx(hShellHook);
   RemoveWndProcHook;
   Close(F);
end;

exports
  InstallHookDLL index 1,
  ShellHook index 2,
  WndProcHook index 3,
  RemoveHookDLL index 4;
begin
end.
{------------------------------------------------------------}

 

Re:Active Window (Need a little help)


I've done something similar (I think) to what you want.  I overrite the icon on any active window (the one
in the top left had corner of the active window's title bar) with an icon of my own.  It effectively hids
the system icon (the one that allows you to close, resize, etc).  Clicking on my icon swaps to my
programme without the need to alt-tab.  I know this isn't what you want to do but the principle works the
same -- namely I attach my pgm to any active application.

To do this takes two steps:

  1)    As you have done, I start a background (hidden) task.  This has a window the same size as the icon;

  2)    I then start a timer that clicks every 1/2 second.  It checks to see see what the active window is.
        If it ain't 'it' then it brings itself to the top op of the task list WITHOUT making itself active
        so the active window remains the same.

The code below shows the logic to this.  This bit of code goes in the WMTimer routine.  MAXIMISE your window
before reading, else it looks a mess !!!!

  AcWnd := GetActiveWindow;                                     {find id of active window}

  if TsrWin = AcWnd then begin                                  {if it is the active window then}
    BringWindowToTop(HWindow);                                  {it just repains itself.
  end
  else begin                                                    {else it makes sure the active win}
    t2Wnd := GetParent(AcWnd);                                  {isn't a child of some other window}
    if t2Wnd = 0 then t2Wnd := AcWnd;                           (if it is the child then it adjusts}
    GetWindowRect(t2Wnd, Rect);                                 {now it finds the position of the active win}
    ShowWindow(TsrWin, sw_ShowNA);                              {and re-positions itself over the icon}
    MoveWindow(TsrWin, Rect.left+3, Rect.top+3, 19, 18, False);
  end;

Hope this is of some use.

Phil

Other Threads