Search code examples
javawinapiwindowjna

JNA finding window, but not minimizing it


I'm trying to manipulate an external window on Java, with JNA. I want to find a specific window and then minimize it. Every window that I've tried worked well but one (obviously the one that I need). That one can be found and set to the foreground, but can not be minimized programmatically. I've also tried to move it with User32.INSTANCE.MoveWindow, but that command failed too. I was wondering if there are applications that do not allow JNA window manipulations. If so, what can I do? My code is shown below.

    HWND test = User32.INSTANCE.FindWindow(null,"windowname");
    User32.INSTANCE.SetForegroundWindow(test);
    User32.INSTANCE.ShowWindow(test, WinUser.SW_MINIMIZE);

Solution

  • From the API for ShowWindow:

    nCmdShow

    Type: int

    Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values.

    It would seem to be that your minimize command may work if you call it a second time.