Capture application switching

Hi..

Is there any easy way to capture application switching?
I want to do some action if the applicaiton switches.
It has to work on windows 2000/xp/...
Looking for Sample code.

sun4sunday
LVL 9
sun4sundayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZhaawZSoftware DeveloperCommented:
Sure there is ;) Here's source and compiled version:
http://lovelysoft.sytes.net/monitor_window_switching.zip (13KB)


And here's the same (2 source files - hooklib.dpr and monwndswitch.dpr)


hooklib.dpr:
------------------------------------------------------------------------------------------
library hooklib;

uses
  windows;

var
  hook : cardinal;

procedure ShowWindowInfo (wnd : cardinal);
var
  txt : array [0..255] of char;
begin
GetWindowText(wnd, @txt, sizeof(txt));
SetWindowText(FindWindow('Monitor Window Switching', nil), @txt)
end;

function CBTProc (nCode, wParam, lParam : integer) : integer; stdcall;
begin
if nCode = hcbt_activate then ShowWindowInfo(wParam);
result := CallNextHookEx(hook, nCode, wParam, lParam);
end;

procedure SetCBTHook(install : byte); stdcall;
begin
case install of
  0 : UnhookWindowsHookEx(hook);
  1 : hook := SetWindowsHookEx(wh_cbt, @CBTProc, hInstance, 0);
end;
end;

exports
  SetCBTHook;

end.
------------------------------------------------------------------------------------------



monwndswitch.dpr
------------------------------------------------------------------------------------------
program monwndswitch;

uses
  windows, messages;

var
  msg : tagMsg;
  cls : wndClass;
  frm : cardinal;

procedure SetCBTHook(install : byte); stdcall; external 'hooklib.dll';

function wndProc (wnd, msg : cardinal; wParam, lParam : cardinal) : integer; stdcall;

  procedure default;
  begin
  result := DefWindowProc(wnd, msg, wParam, lParam);
  end;

begin
case msg of
  wm_close : begin
    PostQuitMessage(0);
    result := 0;
  end;
else
  default;
end;
end;

begin
ZeroMemory(@cls, sizeof(cls));
with cls do begin
  style         := cs_parentdc;
  lpfnWndProc   := @wndProc;
  hInstance     := sysInit.hInstance;
  hIcon         := LoadIcon(0, idi_application);
  hCursor       := LoadCursor(0, idc_arrow);
  hbrBackground := color_btnface + 1;
  lpszClassName := 'Monitor Window Switching';
end;
RegisterClass(cls);
frm := CreateWindowEx(
  ws_ex_toolwindow or ws_ex_topmost,
  cls.lpszClassName, nil,
  ws_visible or ws_caption or ws_sysmenu,
  0, 0, 250, 50,
  0, 0, hInstance, nil
);
SetCBTHook(1);
while GetMessage(msg, 0, 0, 0) do DispatchMessage(msg);
SetCBTHook(0);
DestroyWindow(frm);
UnregisterClass(cls.lpszClassName, cls.hInstance);
end.
------------------------------------------------------------------------------------------

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sun4sundayAuthor Commented:
Thanks I will test this and let you know..

Is there any direct shell calling procedure?

sun4sunday
ZhaawZSoftware DeveloperCommented:
sorry for delay. and thank you for points.
what did you mean with "shell calling procedure"?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.