Solved

finding last focused application

Posted on 2001-08-07
10
353 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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