Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Get the Active window

807599Jan 10 2007 — edited Feb 18 2008
I used the following code.. but i didn't get activewindow..

int hwnd = OS.GetDesktopWindow();
it returns value and working..
but

int hwnd = OS.GetActiveWindow();
it returns 0 only.. i didn't get correct value...

how can i get Activewindow .... is there any alternate method....

import java.awt.image.*;
import javax.imageio.*;
import java.awt.Rectangle;
import java.lang.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.Tracker;

try
{
//in hwnd=OS.GetDesktopWindow();
int hwnd =OS.GetActiveWindow();
System.out.println(hwnd);
RECT clientRect= new RECT();
POINT pt1 = new POINT ();
POINT pt2 = new POINT ();

OS.GetClientRect(hwnd, clientRect);

pt1.x = clientRect.left;
pt1.y = clientRect.top;
pt2.x = clientRect.right;
pt2.y = clientRect.bottom;

System.out.println(clientRect.left);
System.out.println(clientRect.top);
System.out.println(clientRect.right);
System.out.println(clientRect.bottom);

OS.ClientToScreen (hwnd, pt1);
OS.ClientToScreen (hwnd, pt2);

clientRect.left = pt1.x;
clientRect.top = pt1.y;
clientRect.right = pt2.x;
clientRect.bottom = pt2.y;

System.out.println(clientRect.left);
System.out.println(clientRect.top);
System.out.println(clientRect.right);
System.out.println(clientRect.bottom);

if((clientRect.right!=0)&&(clientRect.bottom!=0))
{
BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(clientRect.left,clientRect.top,
clientRect.right,clientRect.bottom));

File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);

System.out.println("Image Saved Successfully");
}
else
System.out.println("Failed");

}
catch(AWTException ae){}
catch(IOException ie){}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 17 2008
Added on Jan 10 2007
2 comments
318 views