2016-02-14 63 views
0

我想給我的應用程序集中另一個窗口(在這種情況下,記事本)電源的Java JNA聚焦於特定的窗口

我的班級正在尋找這樣

public static class Win32WindowUtils { 

    public interface User32 extends StdCallLibrary { 
     User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS); 
     HWND GetParent(HWND hWnd); 
     HWND FindWindow(String lpClassName, String lpWindowName); 
     HWND SetFocus(HWND hWnd); 
     HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter, String lpszClass, String lpszWindow); 
     int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount); 
    } 

    private static final int WIN_TITLE_MAX_SIZE = 512; 

    public static HWND GetWindowHandle(String strSearch, String strClass) { 
     char[] lpString = new char[WIN_TITLE_MAX_SIZE]; 
     String strTitle; 
     int iFind = -1; 
     HWND hWnd = User32.INSTANCE.FindWindow(strClass, null); 
     while(hWnd != null) { 
      User32.INSTANCE.GetWindowText(hWnd, lpString, WIN_TITLE_MAX_SIZE); 
      strTitle = new String(lpString); 
      strTitle = strTitle.toUpperCase(); 
      iFind = strTitle.indexOf(strSearch); 
      if(iFind != -1) { 
       return hWnd; 
      } 
      hWnd = (User32.INSTANCE).FindWindowEx(null, hWnd, strClass, null); 
     } 
     return hWnd; 
    } 
} 

而且我叫它使用:

User32.INSTANCE.SetFocus(Win32WindowUtils.GetWindowHandle(windowTitle, null)); 

注:

public String windowTitle = "Unbennant - Editor"; 

可悲的是什麼也沒有發生,我不知道爲什麼

+0

根據[MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646312(v = vs.85).aspx),「窗口必須連接到調用線程的消息隊列「。這個提示似乎意味着你不能通過'SetFocus()'函數強調不同的應用程序。 – technomage

+0

您可能希望使用[BringWindowToTop()](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632673(v = vs.85).aspx)。 – technomage

+0

您是否想要激活應用程序(將其置於前面)還是隻確保將鍵盤事件定向到所選窗口? – technomage

回答

0

它也許應該通過所有窗口的機器開放閱讀

public String windowTitle = "Unbenannt - Editor"; 
+0

不,我不希望它讀取,公共字符串windowTitle =「Unbenannt - 編輯」;給它一個窗口的名字,在這種情況下,我的語言是未命名的記事本 – durchstarter

+0

@durchstarter看看你的問題:你在字符串windowTitle中有一個錯字:* Unbennant *而不是* Unbenannt * – wero

2

下面的代碼片段迭代,停止在記事本++窗口具有特定標題是找到然後將其帶入焦點。將其聚焦後,按下Enter鍵三次。

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

import com.sun.jna.Native; 
import com.sun.jna.Pointer; 
import com.sun.jna.platform.win32.WinDef; 
import com.sun.jna.platform.win32.WinDef.HWND; 
import com.sun.jna.platform.win32.WinUser; 
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC; 
import com.sun.jna.win32.StdCallLibrary; 

public class TryWithHWND { 
public interface User32 extends StdCallLibrary { 
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); 

    boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg); 

    WinDef.HWND SetFocus(WinDef.HWND hWnd); 

    int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount); 

    boolean SetForegroundWindow(WinDef.HWND hWnd); 
} 

public static void main(String[] args) { 
    final User32 user32 = User32.INSTANCE; 
    user32.EnumWindows(new WNDENUMPROC() { 
     int count = 0; 

     public boolean callback(HWND hWnd, Pointer arg1) { 
      byte[] windowText = new byte[512]; 
      user32.GetWindowTextA(hWnd, windowText, 512); 
      String wText = Native.toString(windowText); 

      // get rid of this if block if you want all windows regardless 
      // of whether 
      // or not they have text 
      if (wText.isEmpty()) { 
       return true; 
      } 

      System.out.println("Found window with text " + hWnd 
        + ", total " + ++count + " Text: " + wText); 
      if (wText 
        .equals("C:\\Users\\Avi.J\\Desktop\\Datasource and Mq setup commands.txt - Notepad++")) { 
       user32.SetForegroundWindow(hWnd); 
       return false; 
      } 
      return true; 
     } 
    }, null); 
    // user32.SetFocus(hWnd); 
    try { 
     Robot r = new Robot(); 
     r.keyPress(KeyEvent.VK_ENTER); 
     r.keyRelease(KeyEvent.VK_ENTER); 
     r.keyPress(KeyEvent.VK_ENTER); 
     r.keyRelease(KeyEvent.VK_ENTER); 
     r.keyPress(KeyEvent.VK_ENTER); 
     r.keyRelease(KeyEvent.VK_ENTER); 
     r.keyPress(KeyEvent.VK_ENTER); 
     r.keyRelease(KeyEvent.VK_ENTER); 
    } catch (AWTException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

希望這可以幫助,我相信發送擊鍵的部分也可以通過User32庫來完成。