3

I have a java application that will run on Windows 7 (using Swing, App #1) that runs as full screen (but not in exclusive mode). I have another application (App #2) that displays a GUI to configure an external device over a serial port that I do not have the source to and cannot change at all.

I want to embed App #2 inside of App #1 so that it looks like it is part of the parent java application (hiding the file --> exit button and hiding the minimize, maximize, and close buttons).

If this kind of integration is not possible inside the Java application, I would be fine with opening the process using Java and just monitoring it to keep it open . It would need to keep the window set to "always on top" because App #1 is fullscreen and hide as much of the external MS Windows UI as possible to trick the user into thinking it isn't an external application. Is there some kind of method whether using JNI or something else to manage another processes window (screen location, title bar, minimize, maximize, close button visibility) and process state from inside my Java application?

I will be happy to provide more information if needed.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85

1 Answers1

6

The following scheme is language independent, I've managed to embed IE window into a Ruby application this way:

  • First of all, change the style of the external application window (you can use JNA for calling WinAPI):

    style = GetWindowLongPtr(APP_HWND, GWL_STYLE);

    style |= WS_CHILD;

    style &= ~WS_CAPTION;

    style &= ~WS_POPUP;

    SetWindowLongPtr(HWND, GWL_STYLE, style);

  • Define parent-child relationship between windows:

    SetParent(APP_HWND, JAVA_HWND);

  • Listen to Move/Resize events of your Java window, and apply new positions on a child window.

Michael Spector
  • 36,723
  • 6
  • 60
  • 88
  • I've never used the Windows API's before or JNA. How would I get the hWnd pointer of an application I opened using ProcessBuilder? . Here is a reference I found on SO, but it talks about passing a component which I don't think I have because its an external application. http://stackoverflow.com/questions/386792/in-java-swing-how-do-you-get-a-win32-window-handle-hwnd-reference-to-a-window – Austyn Mahoney Jul 22 '10 at 18:47
  • 1
    Here are some examples of using JNA for detecting a window handle in Java: http://forums.sun.com/thread.jspa?threadID=5423994 In general, you call WinAPI's method `EnumWindows`, which returns all existing windows. Then you find the relevant one by window title (`GetWindowText` method) – Michael Spector Jul 22 '10 at 19:07
  • @spektom Do you have the source code for the Java application that you described above? I'm trying to embed a desktop application window inside a web page using a Java applet. – Anderson Green Nov 20 '12 at 05:15
  • @spektom Also, is it possible to do this without using the Windows API (which isn't cross-platform)? – Anderson Green Nov 20 '12 at 05:21
  • @AndersonGreen if you are developing for a cross-platform, you'll have to implement all platform-specific parts separately, i.e. for GTK, Windows, etc. I don't have a source code for Java, but if you look into browser embedding section of Eclipse sources you'll find all the needed information for how to do that: http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5/org.eclipse.swt.win32.win32/x86/3.5.0/org/eclipse/swt/browser/IE.java?av=f – Michael Spector Nov 20 '12 at 08:56