Java 如何使用JNA运行chrome?
我写了一些java代码 如何在windows(32位)中使用JNA运行chrome 那我想知道它的名字 正如你们所知,FindWindow是一个简单的解决方案,但若chrome不运行,它就不能工作 下面类似的代码是可能的吗Java 如何使用JNA运行chrome?,java,jna,Java,Jna,我写了一些java代码 如何在windows(32位)中使用JNA运行chrome 那我想知道它的名字 正如你们所知,FindWindow是一个简单的解决方案,但若chrome不运行,它就不能工作 下面类似的代码是可能的吗 HWND hwnd = User32.CreateProcess(...); 下面代码打开chrome。 但是尺寸、最大化不起作用 当然,如果Chrome没有运行,你就无法获得它的窗口句柄,因为这样的窗口并不存在。您可能希望使用ProcessBuilder之类的
HWND hwnd = User32.CreateProcess(...);
下面代码打开chrome。 但是尺寸、最大化不起作用
当然,如果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变量中会有一组窗口,如果Chrome在调用EnumWindows期间运行,则可以获取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 );
public static interface WndEnumProc extends StdCallLibrary.StdCallCallback
{
boolean callback ( int hWnd, int lParam );
}
希望有帮助。请注意,Chrome必须运行,而你正在做所有的魔术。正如我在一开始提到的,使用ProcessBuilder运行它应该相对简单,或者如果您不想太麻烦,而且Chrome在您的道路上,您可以使用
System.getRuntime().exec(“chrome.exe”)
启动Chrome
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);
}
}
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 );
/**
* 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 );
public static interface WndEnumProc extends StdCallLibrary.StdCallCallback
{
boolean callback ( int hWnd, int lParam );
}