Winamp is controlled. Now let us control Windows calculator program using Java code and JNA.
This is similar to controlling Winamp. Same concept is used here. For more details about the concept CLICK.
import java.io.IOException;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import controlled.impl.utils.User32; ==> This interface is defined below in the article: CLICK
/**
* This class controls the Windows Calculator.
* @author Santosh Pai
*/
public class CalculatorControl {
private HWND calcWindowHandle;
/**
* User32 DLL instance
*/
public User32 USER32INST;
/**
* When a number / operator is recognized the value is sent to this method, which takes
* care of performing the whole operation and shows the result.
* @param command Operator / Operand
*/
public void sendCommandToCalculator(String buttonName){
HWND hwndChild = USER32INST.FindWindowEx((HWND)calcWindowHandle,null,"Button",buttonName);
WinDef.WPARAM wParamValue = new WinDef.WPARAM(Long.parseLong("0"));
WinDef.LPARAM lParamValue = new WinDef.LPARAM(Long.parseLong("0"));
USER32INST.PostMessage(hwndChild,245, wParamValue, lParamValue);
}
public void init() {
USER32INST = User32.INSTANCE;
calcWindowHandle = USER32INST.FindWindow(null,"Calculator");
if(calcWindowHandle != null){
System.out.println("CALCULATOR is running.");
return;
}
else{
System.out.println("CALCULATOR is not running.");
System.out.println("Strating the CALCULATOR.");
try {
Runtime.getRuntime().exec("calc");
} catch (IOException e) {
System.out.println("Internal Error. CALCULATOR cannot be started.");
e.printStackTrace();
return;
}
calcWindowHandle = USER32INST.FindWindow(null,"Calculator");
return;
}
}
}
This is similar to controlling Winamp. Same concept is used here. For more details about the concept CLICK.
import java.io.IOException;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import controlled.impl.utils.User32; ==> This interface is defined below in the article: CLICK
/**
* This class controls the Windows Calculator.
* @author Santosh Pai
*/
public class CalculatorControl {
private HWND calcWindowHandle;
/**
* User32 DLL instance
*/
public User32 USER32INST;
/**
* When a number / operator is recognized the value is sent to this method, which takes
* care of performing the whole operation and shows the result.
* @param command Operator / Operand
*/
public void sendCommandToCalculator(String buttonName){
HWND hwndChild = USER32INST.FindWindowEx((HWND)calcWindowHandle,null,"Button",buttonName);
WinDef.WPARAM wParamValue = new WinDef.WPARAM(Long.parseLong("0"));
WinDef.LPARAM lParamValue = new WinDef.LPARAM(Long.parseLong("0"));
USER32INST.PostMessage(hwndChild,245, wParamValue, lParamValue);
}
public void init() {
USER32INST = User32.INSTANCE;
calcWindowHandle = USER32INST.FindWindow(null,"Calculator");
if(calcWindowHandle != null){
System.out.println("CALCULATOR is running.");
return;
}
else{
System.out.println("CALCULATOR is not running.");
System.out.println("Strating the CALCULATOR.");
try {
Runtime.getRuntime().exec("calc");
} catch (IOException e) {
System.out.println("Internal Error. CALCULATOR cannot be started.");
e.printStackTrace();
return;
}
calcWindowHandle = USER32INST.FindWindow(null,"Calculator");
return;
}
}
}
No comments:
Post a Comment