我写了一些java代码。
如何在Windows(32位)中使用JNA运行chrome。
然后我想得到它的重要性。
如您所知,FindWindow是一个简单的解决方案,但如果chrome没有运行,它就不起作用。
下面可能有类似的代码吗?
HWND hwnd = User32.CreateProcess(...);
下面的代码打开chrome。 但是尺寸调整,最大化不起作用。
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
boolean CreateProcessA(
String lpApplicationName
, String lpCommandLine
, Structure lpProcessAttributes
, Structure lpThreadAttributes
, boolean bInheritHandles
, int dwCreationFlags
, Structure lpEnvironment
, String lpCurrentDirectory
, Structure lpStartupInfo
, Structure lpProcessInformation);
}
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class ProcessInformation extends Structure {
public Pointer hProcess;
public Pointer hThread;
public int dwProcessId;
public int dwThreadId;
}
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.WString;
public class StartupInfoA extends Structure {
public int cb;
public WString lpReserved;
public WString lpDesktop;
public WString lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public Pointer lpReserved2;
public Pointer hStdInput;
public Pointer hStdOutput;
public Pointer hStdError;
}
public class Test {
public static void main(String[] args) {
int STARTF_USEPOSITION = 0x00000004;
int STARTF_USESIZE = 0x00000002;
int STARTF_USESHOWWINDOW = 0x00000001;
ProcessInformation processInformation = new ProcessInformation();
StartupInfoA startupInfo = new StartupInfoA();
startupInfo.dwX = 100;
startupInfo.dwY = 100;
startupInfo.dwXSize = 100;
startupInfo.dwYSize = 100;
startupInfo.wShowWindow = (short) SW_MAXIMIZE;
startupInfo.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE;
Kernel32.INSTANCE.CreateProcessA(new String("C:\\Users.....\\Google\\Chrome\\Application\\chrome.exe")
, null
, null
, null
, true
, 0
, null
, null
, startupInfo
, processInformation);
}
}
答案 0 :(得分:2)
如果Chrome没有运行,当然,你无法获得其窗口的句柄,因为这样的窗口不存在。您可能希望使用类似ProcessBuilder的方式运行Chrome,然后调用类似这样的内容:
user32.EnumWindows( new WndEnumProc()
{
@SuppressWarnings ( "AssignmentToMethodParameter" )
public boolean callback ( int hWnd, int lParam )
{
if ( user32.IsWindow( hWnd ) )
{
if ( user32.IsWindowVisible( hWnd ) )
{
RECT r = new RECT();
user32.GetWindowRect( hWnd, r );
// if (r.left > -32000) // is not minimized
//{
String windowTitle = getWindowParentName( hWnd );
String windowClass = getWindowParentClassName( hWnd );
hWnd = user32.GetAncestor( hWnd, 3 );
if ( !windowTitle.toLowerCase().equals( "program manager" ) && !windowClass.toLowerCase().equals( "progman" ) && !windowTitle.equals( "" ) && !windowClass.toLowerCase().equals( "shell_traywnd" ) )
{
listOfWindows.put( hWnd, getWindowParentRectangle( hWnd ) );
}
// }
}
return true;
}
else
{
return false;
}
}
}, 0 );
当然,这已经是一个工作代码,它有一些特定于我的应用程序的条件。但主要的想法是 使用WndEnumProc()调用EnumWindows,将它找到的所有窗口放到一个集合中(在我的例子中是一个HashMap)。然后在EnumWindows返回后,您将在listOfWindows变量中拥有一组窗口,如果在调用EnumWindows期间它正在运行,您将能够获得Chrome的hwnd。
您应该在user32实例中定义EnumWindows,如下所示:
/**
* Enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function.
* @param wndenumproc A pointer to an application-defined callback function.
* @param lParam An application-defined value to be passed to the callback function.
* @return if the function succeeded.
* <a href="http://msdn.microsoft.com/en-us/library/ms633497(v=VS.85).aspx"> <b>Microsoft Reference</b></a><br>
*/
public boolean EnumWindows ( WndEnumProc wndenumproc, int lParam );
您还应该在类中定义您的WndEnumProc结构(我的名为Structures),如下所示:
public static interface WndEnumProc extends StdCallLibrary.StdCallCallback
{
boolean callback ( int hWnd, int lParam );
}
希望有所帮助。请注意,Chrome必须在您完成所有魔法的同时运行。正如我在开始时所说的,运行它应该使用ProcessBuilder相对简单,或者如果你不想打扰并且Chrome在你的道路上,你可以使用
System.getRuntime().exec("chrome.exe")
启动Chrome。