Solved

finding last focused application

Posted on 2001-08-07
10
354 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
ID: 6362692
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
ID: 6362916
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
ID: 6379633
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Expert Comment

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

Author Comment

by:PhrAtoR
ID: 6379795
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
ID: 6379841
mhook:=SetWindowsHookEx(WH_CBT, HookProc, dllinst, 0);

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

Expert Comment

by:robpitt
ID: 6381837
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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.…
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…

828 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