Solved

finding last focused application

Posted on 2001-08-07
10
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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