?
Solved

Capture application switching

Posted on 2006-04-05
3
Medium Priority
?
214 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
0
Comment
Question by:sun4sunday
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 1000 total points
ID: 16390679
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
 
LVL 9

Author Comment

by:sun4sunday
ID: 16415744
Thanks I will test this and let you know..

Is there any direct shell calling procedure?

sun4sunday
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16467966
sorry for delay. and thank you for points.
what did you mean with "shell calling procedure"?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question