Solved

finding last focused application

Posted on 2001-08-07
10
350 Views
Last Modified: 2013-12-03
Hi!

I have to write a program, that allows the user to insert stationaries (text) into every app (word, outlook,..)
What i do is copy the text to the clipboard and paste it in the app via WM_PASTE. The problem is, that the user has to click a button in program, so my program gets the focus.
How do i get the handle of the app/window the focus was before?

Thanks
Bastian (PhrAtoR)
0
Comment
Question by:PhrAtoR
  • 3
  • 2
  • 2
10 Comments
 
LVL 6

Accepted Solution

by:
MichaelS earned 30 total points
Comment Utility
Take a look at WM_ACTIVATE message.

when you get it you have to check wParam. If low-order word is WA_ACTIVE or WA_CLICKACTIVE than lParam is the handle to the window being deactivated. You have to remember it and after inserting text you call SetForegroundWindow, BringWindowToTop, SetWindowPos, SetActiveWindow or whatever to activate a window you did remembered.
0
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
As far as I recall, WM_ACTIVATEs deactivate information is only valid if the previously active window was part of the current process, otherwise it is NULL quite a lot.

I think the only way you will get the info on the old focus window is with a CBT hook which monitors focus changes. If interested heres a sample of mine http://website.lineone.net/~codebox/focuslog.zip
this could easily be adapted to maintain a focus history.

Rob

0
 

Author Comment

by:PhrAtoR
Comment Utility
Hi!

what robpitt says is right. WM_ACTIVATE only works in the current process.

The sample in the zip works fine.
I ported the focusmonitor (focus.dll) from focuslog.zip to Delphi. Now i have the same problem as with WM_ACTIVATE. I'm not a expert in windowshook-programming. So maybe you can help me again. There are Thread or system hooks. But how do i set what type is uses. At the moment it seems that my ported dll only installs a hook for the current
thread.
( has it something to do with the line "#pragma data_seg (".shared")" ? )

Maybe this is a delphi specific question, so if you can't help me this time, i'll give you the points anyway.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
Your dll sould be loaded in each process. Can you show your code where you set hook?
0
 

Author Comment

by:PhrAtoR
Comment Utility
I can't load it in every process, because i'm also going to monitor windows of other applications (ms word, excel,...).

here is the code:


library focus;

uses
  SysUtils,
  Classes,
  windows,
  messages;

{$R *.RES}
var FocusedWindow,
    NotifyWindow    :hwnd;
    NotifyMessage   :uint;
    mhook:HHOOK;
    dllinst:LongWord;

function HookProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
begin
     if code = HCBT_SETFOCUS then begin
        FocusedWindow:= hwnd(wparam);

        if NotifyWindow <> 0 then SendMessage(NotifyWindow,NotifyMessage,wparam,lparam);
     end;
     result:= CallNextHookEx(mhook,code,wparam,lparam);
end;

function startFocusMonitor(notifyw:hwnd; notifym:uint) : Integer; stdcall;
begin
     if mhook <> 0 then UnhookWindowsHookEx(mhook);
     mhook:=SetWindowsHookEx(WH_CBT, HookProc, dllinst, 0);
     NotifyWindow:=notifyw;
     NotifyMessage:=notifym;
     result:=1;
end;

function stopFocusMonitor: Integer; stdcall;
begin
     if mhook <> 0 then UnhookWindowsHookEx(mhook);
     NotifyWindow:=0;
     NotifyMessage:=0;
     result:=1;
end;

function getCurrentFocus: hwnd; stdcall;
begin
     result:=FocusedWindow;

end;

exports
       startFocusMonitor index 10 name 'startMonitor',
       stopFocusMonitor index 11 name 'stopMonitor',
       getCurrentFocus index 12 name 'getCFocus';


// main
begin

   dllinst:=HInstance;

end.
0
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
mhook:=SetWindowsHookEx(WH_CBT, HookProc, dllinst, 0);

sould be
mhook:=SetWindowsHookEx(WH_CALLWNDPROC, HookProc, dllinst, 0);
0
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
The default for a DLL is for all global variables to be allocated on a per process basis. That is each process that loads a DLL gets its own versions of the global variables.

However we actually want some variables (hHook etc) to be common/shared between all DLL instances. In MSVC this is easily achieved using a shared data_seg.

I don't know whether there is an equivalent construct in Delphi.

If not you can do it all manually, see http://msdn.microsoft.com/library/en-us/dllproc/hh/winbase/dll_9khl.asp

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now