I am trying to give my application the power to focus another Window (Notepad in this case)
My Class is looking like this
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;
}
}
And I am calling it by using:
User32.INSTANCE.SetFocus(Win32WindowUtils.GetWindowHandle(windowTitle, null));
Note:
public String windowTitle = "Unbennant - Editor";
Sadly nothing is happening and I dont know why
SetFocus()function. - technomage