We help IT Professionals succeed at work.

Capture application switching

sun4sunday
sun4sunday asked
on
Medium Priority
307 Views
Last Modified: 2010-04-05
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
Comment
Watch Question

Software Developer
Commented:
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.
------------------------------------------------------------------------------------------

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks I will test this and let you know..

Is there any direct shell calling procedure?

sun4sunday
ZhaawZSoftware Developer

Commented:
sorry for delay. and thank you for points.
what did you mean with "shell calling procedure"?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.