Windows 플랫폼 설정에서 Windows를 이동하고 크기를 조정 한 방법입니다. 그것은 내가 User32 JNA 윈도우 라이브러리 코드의 일부 작은 부분이다 :
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
@SuppressWarnings("serial")
public class TestMoveWindow extends JPanel {
public static final String[] BOUNDS_NAMES = { "x", "y", "w", "h" };
private JTextField windowNameField = new JTextField(15);
private JTextField[] boundsFields = new JTextField[BOUNDS_NAMES.length];
public TestMoveWindow() {
JPanel winNamePanel = new JPanel();
winNamePanel.add(new JLabel("Window Name:"));
winNamePanel.add(windowNameField);
JPanel boundsPanel = new JPanel(new GridLayout(1, 0, 5, 0));
for (int i = 0; i < BOUNDS_NAMES.length; i++) {
JPanel fieldPanel = new JPanel();
fieldPanel.add(new JLabel(BOUNDS_NAMES[i] + ":"));
boundsFields[i] = new JTextField(3);
fieldPanel.add(boundsFields[i]);
boundsPanel.add(fieldPanel);
}
JPanel btnPanel = new JPanel();
Action doItAction = new AbstractAction("Do it!") {
@Override
public void actionPerformed(ActionEvent e) {
String startOfWindowName = windowNameField.getText().trim();
Pointer hWnd = JnaUtil.getWinHwnd(startOfWindowName);
if (hWnd == null || startOfWindowName.isEmpty()) {
String message = String.format(
"Window named \"%s\" was not found", startOfWindowName);
JOptionPane.showMessageDialog(TestMoveWindow.this, message,
"Window Not Found", JOptionPane.ERROR_MESSAGE);
return;
}
int x = 0;
int y = 0;
int w = 0;
int h = 0;
String xStr = boundsFields[0].getText();
String yStr = boundsFields[1].getText();
String wStr = boundsFields[2].getText();
String hStr = boundsFields[3].getText();
try {
x = Integer.parseInt(xStr);
y = Integer.parseInt(yStr);
w = Integer.parseInt(wStr);
h = Integer.parseInt(hStr);
} catch (NumberFormatException e1) {
String message = String
.format(
"Numbers cannot be parsed: \"%s\", \"%s\", \"%s\", \"%s\"",
xStr, yStr, wStr, hStr);
JOptionPane.showMessageDialog(TestMoveWindow.this, message,
"Numbers Not Parseable", JOptionPane.ERROR_MESSAGE);
return;
}
JnaUtil.moveWindow(hWnd, x, y, w, h);
}
};
btnPanel.add(new JButton(doItAction));
windowNameField.addActionListener(doItAction);
for (JTextField boundField : boundsFields) {
boundField.addActionListener(doItAction);
}
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(winNamePanel);
add(boundsPanel);
add(btnPanel);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Test Move Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestMoveWindow());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class JnaUtil {
private static final User32 user32 = User32.INSTANCE;
private static Pointer callBackHwnd;
public static Pointer getWinHwnd(final String startOfWindowName) {
callBackHwnd = null;
user32.EnumWindows(new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer userData) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) {
callBackHwnd = hWnd;
return false;
}
return true;
}
}, null);
return callBackHwnd;
}
public static boolean moveWindow(Pointer hWnd, int x, int y, int nWidth,
int nHeight) {
boolean bRepaint = true;
return user32.MoveWindow(hWnd, x, y, nWidth, nHeight, bRepaint);
}
}
interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(Pointer hWnd, Pointer arg);
}
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData);
boolean MoveWindow(Pointer hWnd, int x, int y, int nWidth, int nHeight,
boolean bRepaint);
int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);
}
이것은 당신이 당신의 클래스 패스에 JNA 라이브러리에서 jna.jar 및 platform.jar을 가지고 필요합니다. 그리고 이것은 Windows 플랫폼 전용입니다. 코드의 첫 번째 부분 인 TestMoveWindow는 프로그램이 작동하는 방식을 보여주는 테스트 GUI 일 뿐이며 실제로는 JnaUtil.moveWindow(hWnd, x, y, w, h);
을 호출하는 것 이상이지만 코드의 "고기"는 JnaUtil 클래스 및 User32 인터페이스입니다. JNA 라이브러리는이 일을 이므로 쉽습니다. 우스꽝 스럽습니다. 내 코드가 작동하려면 창 텍스트 (종종 웹 페이지의 제목)의 시작을 알아야합니다. 그러면 창에 대한 포인터를 얻을 수 있습니다.
크로스 플랫폼 솔루션이 있는지 솔직히 알 수 없습니다. 나는 그것이 있다고 생각하고 싶습니다.하지만 이것은 항상 시스템 호출을 요구할 것이라고 생각하기 때문에 의심 스럽습니다. 그러나 틀린 것으로 입증되기를 바랍니다!
플랫폼 의존적 인 솔루션을 알고 있지만 Windows에만 해당되며 JNA 또는 JNI가 필요합니다. –
크로스 플랫폼이 필요 하겠지만 당신의 방법에 관심이 있습니다. –
편집 해 주셔서 감사합니다. Dimo. –