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?
 
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.
------------------------------------------------------------------------------------------
0
 
sun4sundayAuthor Commented:
Thanks I will test this and let you know..

Is there any direct shell calling procedure?

sun4sunday
0
 
ZhaawZSoftware DeveloperCommented:
sorry for delay. and thank you for points.
what did you mean with "shell calling procedure"?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.