功能:任意截图,保存至本地,确定截图,否定截图
效果图:
保存:![](https://i-blog.csdnimg.cn/blog_migrate/a652773f1984d04a1df5b7b896f3fb00.png)
fx的资料也太少了,自己也查了资料 找到了一个Swing的然后将它改了改添加到项目里了,上代码
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import static com.sun.jna.platform.win32.WinUser.*;
public class imageScreenshot implements HotkeyListener {
HashMap<Integer, Rectangle> winLayer = null;
static final int shotHotKey = 88;
public volatile boolean enterBusy = false;//防止和全局热键冲突。
private String lastPath = null;
public volatile boolean shotBusy = false;//防止重复截图。
public volatile boolean isProcess = false;//第一次拖拽完成后,需要进行处理,这时候需要重新利用click,drag和release函数。
/**拖拽参数**/
private static final int BREADTH = 7;//边界拉伸范围
private static final int BREADTH2 = 14;//边界拉伸范围
private int dragType;
private static final int DRAG_NONE = 0;
private static final int DRAG_MOVE = 1;
private static final int DRAG_UP = 2;
private static final int DRAG_UPLEFT = 3;
private static final int DRAG_UPRIGHT = 4;
private static final int DRAG_LEFT = 5;
private static final int DRAG_RIGHT = 6;
private static final int DRAG_BOTTOM = 7;
private static final int DRAG_BOTTOMLEFT = 8;
private static final int DRAG_BOTTOMRIGHT = 9;
private Cursor getNewCursor(){
return Toolkit.getDefaultToolkit().createCustomCursor(new ImageIcon(imageScreenshot.class.getResource("cursor.png")).getImage(),new Point(0,0),"myCursor");
}
public void onHotKey(int key) {
switch (key) {
case shotHotKey:
if(shotBusy) return;
shotBusy = true;
saveLayer();//保存窗口层次。
registerESC();//注册窗体全局热键
shotProcess();
break;
default:
System.exit(0);
}
}
/**屏幕窗体层次保存**/
public void saveLayer() {
User32 u32 = User32.INSTANCE;
printAllNextWin(u32.GetWindow(u32.GetForegroundWindow(), new WinDef.DWORD(GW_HWNDFIRST)));
}
public HWND getNextWindow(HWND hWnd) {
return User32.INSTANCE.GetWindow(hWnd, new DWORD(GW_HWNDNEXT));
}
public boolean CheckLegal(HWND hWnd, Rectangle rt) {
WINDOWINFO winInfo = new WINDOWINFO();
User32.INSTANCE.GetWindowInfo(hWnd, winInfo);
int style = winInfo.dwStyle;
if ((style & WS_VISIBLE) == 0) {
return false;//去掉不可视窗口
}
if ((style & WS_DISABLED) != 0) {
return false;//去掉disable窗口
}
if (rt.width < 10 || rt.height < 10) {
return false;//去掉小不点窗口
}
if ((style & WS_MINIMIZE)!=0) {
return false;//去掉最小化状态的窗口
}
if((rt.getWidth() == screenSize.getWidth()) && (rt.getHeight() == screenSize.getHeight())){
return false;//去掉底层屏幕窗口
}
return true;
}
/** 遍历桌面所有窗体,如果是合法的就加入到winLayer **/
public void printAllNextWin(HWND h) {
HashMap<Integer, Rectangle> layerInfo = new HashMap<>();//0是最高层
HWND hWnd = h;
RECT r = new RECT();
int layerPos = 0;
Rectangle rect;
while ((hWnd = getNextWindow(hWnd)) != null) {
User32.INSTANCE.GetWindowRect(hWnd, r);
rect = r.toRectangle();
if (!CheckLegal(hWnd, rect)) continue;
layerInfo.put(layerPos++, rect);
}
winLayer = layerInfo;
}
AWTEventListener al;
private void registerESC() {
al = event -> {
KeyEvent ke = (KeyEvent) event;
if (ke.getID() == KeyEvent.KEY_PRESSED) {
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
//System.out.println("当前AWT事件监听器数量"+ Toolkit.getDefaultToolkit().getAWTEventListeners().length);
clean();
unregisterESC();
jp.setDrag(false);
} else if (ke.getKeyCode() == KeyEvent.VK_