0

I am trying to create a JWindow that sits on top of an external application.

I want the window to resize whenever the external application resizes (to match it) and be only on top of the external application and not everything else. (I can't use setAlwaysOnTop(), because it would be always on top of everything).

By modifying code from the first answer from here I am able to find the external window title (for example Untitled - Notepad) and its handle if needed.

Using code from the first answer from here, I can get the dimensions of an external window from it's window title.

The problem with finding the dimensions from the code at the second link is that I have to always loop and check them (which is not optimal) and it isn't helpful in putting my window only on top of the external one.

Is there a way to implement what I want in Java? Ideally, I would (using JNA?) make a listener to the messages (WinEvents?) the OS sends to the external program to resize it, gain/lose focus etc and handle them somehow.

Community
  • 1
  • 1
  • So you want a program that fools the user into thinking she is using application A (for example, Notepad) but in reality she is using Application B (your malicious program made to look like Notepad) ? – FredK Jul 14 '16 at 19:05
  • hahaha no I am doing a poker hud (google it). I want to make small windows under each player to show stats. – dimitris_2315 Jul 14 '16 at 19:07
  • You could conceivably subclass the target window procedure (`SetWindowLong`/`GWL_WNDPROC`), and snoop on the `WM_WINDOWPOSCHANGING` message to track its size and position updates. As far as making your application always on top of a specific window use `SetWindowPos` and specify the `hwndInsertAfter` parameter. – theB Jul 14 '16 at 19:23
  • @theB Can you explain why the following code (it's compiling) doesn't work? (i can't fit the whole code here), the code is the one from THE QUESTION here with modifications. – dimitris_2315 Jul 14 '16 at 20:08
  • Nevermind... I'm an idiot. I read the question and 4 seconds later wrote a comment which completely forgets the fact that the target application is _external_ to your process. The solution I suggested won't work at all. – theB Jul 14 '16 at 20:08
  • @theB No, your suggestion would work, I have the hWnd for the external application – dimitris_2315 Jul 14 '16 at 20:10
  • You can't replace the window procedure of another process. That would be a bad thing™. To do it you'd have to create a inject a thread into the window's process, which unless you're particularly masochistic, you probably don't want to do from java. – theB Jul 14 '16 at 20:14
  • @theB :/ SetWindowPos would work though, right? – dimitris_2315 Jul 14 '16 at 20:16
  • On your own process' window, absolutely. You just can't snoop on the other application's `WM_WINDOWPOSCHANGING` messages the way I said. (That comment is too old to edit, but ignore the first sentence. The second sentence still is ok.) – theB Jul 14 '16 at 20:22
  • About the second sentence, I think the syntax should be `SetWindowPos(hWndEXTERNAL, hWndMYWINDOW, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); `, right? do you know how I write this in java? Just declare it in User32 interface and then use it normally? :) – dimitris_2315 Jul 14 '16 at 20:30
  • Reverse the two `HWND` parameters. The first is the window you're moving, the second is the window you want to be on top of. As far as how to do it in Java, I've got no clue. Not really my area of expertise. – theB Jul 15 '16 at 11:36
  • @theB I did that but it had some problems. For anyone that finds this thread, the best solution I came up with is with `SetParent()`, while also having an infinite loop to change the size of the child to match the parents' size every few milliseconds. – dimitris_2315 Jul 15 '16 at 16:11

0 Answers0