|
| 1 | +package cn.lcy9.qqmsg4l; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UnsupportedEncodingException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import com.sun.jna.Native; |
| 9 | +import com.sun.jna.Pointer; |
| 10 | +import com.sun.jna.win32.StdCallLibrary; |
| 11 | + |
| 12 | +import jodd.http.HttpRequest; |
| 13 | + |
| 14 | +public class Main { |
| 15 | + |
| 16 | + static interface User32 extends StdCallLibrary { |
| 17 | + User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); |
| 18 | + |
| 19 | + interface WNDENUMPROC extends StdCallCallback { |
| 20 | + boolean callback(Pointer hWnd, Pointer arg); |
| 21 | + } |
| 22 | + |
| 23 | + boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData); |
| 24 | + |
| 25 | + int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount); |
| 26 | + |
| 27 | + int GetClassNameA(Pointer hWnd, byte[] lpString, int nMaxCount); |
| 28 | + |
| 29 | + //Pointer GetWindow(Pointer hWnd, int uCmd); |
| 30 | + } |
| 31 | + |
| 32 | + public static List<String> getAllWindowNames() { |
| 33 | + |
| 34 | + System.setProperty("jna.encoding", "GBK"); |
| 35 | + |
| 36 | + final List<String> windowNames = new ArrayList<>(); |
| 37 | + final User32 user32 = User32.INSTANCE; |
| 38 | + user32.EnumWindows(new User32.WNDENUMPROC() { |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean callback(Pointer hWnd, Pointer arg) { |
| 42 | + final byte[] windowText1 = new byte[512]; |
| 43 | + final byte[] windowText2 = new byte[512]; |
| 44 | + |
| 45 | + user32.GetWindowTextA(hWnd, windowText1, 512); |
| 46 | + final String wText1 = Native.toString(windowText1).trim(); |
| 47 | + |
| 48 | + user32.GetClassNameA(hWnd, windowText2, 512); |
| 49 | + final String wText2 = Native.toString(windowText2).trim(); |
| 50 | + |
| 51 | + if (!wText1.isEmpty()) { |
| 52 | + try { |
| 53 | + windowNames.add(new String(wText1.getBytes(), "gbk")+ "\t" + wText2); |
| 54 | + } catch (UnsupportedEncodingException e) { |
| 55 | + // TODO Auto-generated catch block |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + //System.out.println(wText1 + "\t" + wText2); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + }, null); |
| 65 | + |
| 66 | + return windowNames; |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 70 | + |
| 71 | + //final List<String> winNameList = getAllWindowNames(); |
| 72 | + |
| 73 | + //final List<String> windownames = FileUtils.readLines(new File(args[0]), "utf-8"); |
| 74 | + |
| 75 | + if(args.length != 1){ |
| 76 | + System.out.println("Usage:\n\tjava -jar qq_notice.jar <server url>\n\nhttps://github.com/binaryer/qqmsg4l"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + while(true){ |
| 81 | + Thread.sleep(3000L); |
| 82 | + |
| 83 | + final StringBuffer sbtitles = new StringBuffer(); |
| 84 | + for (final String winName : getAllWindowNames()) { |
| 85 | + |
| 86 | + final String[] winNames = winName.trim().split("\t"); |
| 87 | + |
| 88 | + final String title = winNames[0].trim(); |
| 89 | + final String classname = winNames[1].trim(); |
| 90 | + if(title.equals("")) continue; |
| 91 | + if(!classname.equals("TXGuiFoundation")) continue; |
| 92 | + if(title.equals("QQ")) continue; |
| 93 | + if(title.equals("TXMenuWindow")) continue; |
| 94 | + sbtitles.append(title).append('\n'); |
| 95 | + //System.out.println(title); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + try{ |
| 100 | + HttpRequest.post(args[0]) |
| 101 | + .charset("gbk") |
| 102 | + .form("action", "notice", "titles", sbtitles.toString()) |
| 103 | + .send(); |
| 104 | + }catch(Throwable t){ |
| 105 | + t.printStackTrace(); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments