paktrick
paktrick

Reputation: 93

jna getDesktop bringWindowToTop

I'm having problems activating desktop window.

I have taken the following approach

1: GetDesktopWindow to retrieve the handle of desktop (This works) I have tried the following methods to bring desktop window to top, but they did not work.

   SetForegroundWindow 
   SwitchToThisWindow
   ShowWindow
   BringWindowToTop

Is there something i'm doing wrong? Or is not possible to show desktop with jna?

Upvotes: 5

Views: 3615

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

One way is to do get the taskbar's handle and send it a message to hide all windows, perhaps something like this which has worked for me on Windows 7:

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.W32APIOptions;

public class ToggleDesktop3 {
   public interface User32 extends W32APIOptions {
      public static final String SHELL_TRAY_WND = "Shell_TrayWnd";
      public static final int WM_COMMAND = 0x111;
      public static final int MIN_ALL = 0x1a3;
      public static final int MIN_ALL_UNDO = 0x1a0;

      User32 instance = (User32) Native.loadLibrary("user32", User32.class,
            DEFAULT_OPTIONS);

      HWND FindWindow(String winClass, String title);

      long SendMessageA(HWND hWnd, int msg, int num1, int num2);
   }

   public static void main(String[] args) {
      // get the taskbar's window handle
      HWND shellTrayHwnd = User32.instance.FindWindow(User32.SHELL_TRAY_WND,
            null);

      // use it to minimize all windows
      User32.instance.SendMessageA(shellTrayHwnd, User32.WM_COMMAND,
            User32.MIN_ALL, 0);

      // sleep for 3 seconds
      try {
         Thread.sleep(3000);
      } catch (InterruptedException e) {
      }

      // then restore previously minimized windows
      User32.instance.SendMessageA(shellTrayHwnd, User32.WM_COMMAND,
            User32.MIN_ALL_UNDO, 0);
   }
}

There looks to be another way to do this via Shell32 library calls (something involving the ToggleDesktop function -- for a C# version, check out this SO link), but I've not gotten it to work yet.

Upvotes: 6

Related Questions