|
package com.thkmon.hwndtest;
import java.awt.MouseInfo; import java.awt.Point; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.HashMap;
import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinDef.RECT; import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
public class MainClass {
public static Robot robot = null;
public static void main(String[] args) {
double mx = 1000; double my = 1000;
try { robot = new Robot();
// 마우스가 화면 좌상단 100, 100 이내이면 프로그램 종료. while (mx > 100 || my > 100) {
Point point = MouseInfo.getPointerInfo().getLocation(); mx = point.getLocation().getX(); my = point.getLocation().getY();
Thread.sleep(100);
HashMap hwndMap = checkHwnd();
if (hwndMap != null) { HWND hWnd = castHwnd(hwndMap.get(“HNC_DIALOG”)); if (hWnd != null) { System.err.println(“찾음.”); Thread.sleep(100);
// 셋포커스(동작하지 않네…) User32.INSTANCE.SetForegroundWindow(hWnd); HWND curHwnd = User32.INSTANCE.GetForegroundWindow(); if (curHwnd != null) { String curWinName = getClassNameFromHandle(curHwnd); System.out.println(“curWinName : “ + curWinName);
if (curWinName != null && curWinName.equals(“HNC_DIALOG”)) { // 종료 AltF4(200); } } }
} }
} catch (Exception e) { e.printStackTrace(); }
System.err.println(“사용자 명령으로 종료.”); System.exit(0); }
public static HWND castHwnd(Object obj) {
try { return (HWND) obj; } catch (Exception e) { e.printStackTrace(); return null; } }
public static void AltF4(int delay) throws Exception { robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_F4); robot.keyRelease(KeyEvent.VK_F4); robot.keyRelease(KeyEvent.VK_ALT); robot.delay(delay); }
public static HashMap checkHwnd() {
final HashMap hwndMap = new HashMap();
User32.INSTANCE.EnumWindows(new WNDENUMPROC() { int count = 0;
public boolean callback(HWND hWnd, Pointer arg1) { char[] windowText = new char[512]; User32.INSTANCE.GetWindowText(hWnd, windowText, 512); String wText = Native.toString(windowText); RECT rectangle = new RECT(); User32.INSTANCE.GetWindowRect(hWnd, rectangle); // get rid of this if block if you want all windows // regardless // of whether // or not they have text // second condition is for visible and non minimised windows if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hWnd) && rectangle.left > -32000)) { return true; }
String clsName = getClassNameFromHandle(hWnd); if (clsName != null && clsName.length() > 0) {
// StringBuffer buff = new StringBuffer(); // // buff.append(“번호:” + (++count)); // buff.append(“,텍스트:” + wText); // buff.append(“,” + “위치:(“); // buff.append(rectangle.left + “,” + rectangle.top + “)~(“); // buff.append(rectangle.right + “,” + rectangle.bottom); // buff.append(“),” + “클래스네임:” + clsName); // // System.out.println(buff.toString());
hwndMap.put(clsName, hWnd); }
return true; } }, null);
return hwndMap; }
public static String getClassNameFromHandle(HWND hWnd) { if (hWnd == null) { return “”; }
// 핸들의 클래스 네임 얻기 char[] c = new char[512]; User32.INSTANCE.GetClassName(hWnd, c, 512); String clsName = String.valueOf(c).trim(); return clsName; } }
|