1

I'm looking for a solution, where my app would follow another external app with it's WindowState.

The basic idea is, that I'd have actions made on next changes: When external app is normalized: Move my form to specific coordinates When external app is maximized: Move my form to specific coordinates when external app is minimized: Minimize my form to taskbar.

So said, I want my app to look and behave like it was an actual part of certain external app.

So far here's what I have:

I get the external window handle ok. Using the next function case I get the results as follows:

  case WindowPlacement.showCmd of
   SW_HIDE: Result := 0;          // hidden
   SW_SHOWNORMAL: Result := 1;    // show normal
   SW_SHOWMINIMIZED: Result := 2; // minimized
   SW_SHOWMAXIMIZED: Result := 3; // maximized
  end;

This works fine. However, at the moment, I'm using timer event to trigger every 100mS, which gets this function result and then takes actions as follows:

procedure TPre2.Timer1Timer(Sender: TObject);
var t: integer;
begin
t:=  CheckWindowState(AvoHandle);
 case t of
 0: Application.Minimize;
 1: Application.MainForm.WindowState:=wsNormal; // add position later
 2: Application.Minimize;
 3: Application.MainForm.WindowState:=wsNormal; // add position later
 end;

which gets things randomly working or not. A few times it minimizes and restores just as it should, then the next moment after the external app minimization my app stays visible instead of hiding. Timer is still looping though. Clicking on the app icon in the taskbar causes it to minimize instantly.

Is there a better approach to what I'm trying to achieve? Or, is there any plausible reason that could cause this not working always?

thx.

1 Answer 1

3

This kind of task can be done using a CBT Hook. To implement this type of hook follow these steps.

  1. Use the SetWindowsHookEx function to install a global CBT hook. (In order to install a global hook you should call this method and the callback procedure from a dll, check the MSDN Documentation for details)

  2. In the CBTProc callback function check for the HCBT_MINMAX code.

  3. The LParam of the CBTProc callback function will contain a show-window value (SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE and so on)

  4. From you application use the LoadLibrary method to load the dll with the global hook implementation (you can export a method from the dll to enable or disable the hook).

  5. To communicate your App with the dll you can use a shared memory block (CreateFileMapping, MapViewOfFile) and a custom message (RegisterWindowMessage).

4
  • 6
    Alternatively, you can use SetWinEventHook() instead to receive specific events (EVENT_OBJECT_SHOW/HIDE, EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_STATECHANGE, etc) for the specific thread that owns the window that you are interested in. No DLL needed, less filtering, and less overhead than a global CBT hook. May 27, 2015 at 2:29
  • 1
    @RemyLebeau Sounds like that should be its own answer!
    – David
    May 27, 2015 at 11:09
  • Minor point: it doesn't matter where do you call SetWindowsHookEx. May 27, 2015 at 15:13
  • I just used a hook for the first time for checking mouse clicks, so this now looks familiar to me (yey..). Though, I didn't manage to figure where do I set which app am I "listening" to for the MinMax events....?
    – That Marc
    May 28, 2015 at 0:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.