擷取 window工作列已經開啟應用程式視窗(也就是工作管理員中前台進程)的表徵圖

來源:互聯網
上載者:User

標籤:nta   tps   sentry   xtend   extends      rri   slist   thread   

擷取 window工作列已經開啟應用程式視窗(也就是工作管理員中前台進程)的表徵圖

1.功能描述

   擷取到window工作列已經開啟的應用程式的視窗表徵圖。如下:(要擷取到QQ,瀏覽器,檔案夾的表徵圖,但是工作列中隱藏的表徵圖不顯示)

   

 

2.使用技術及工具

   JAVA,JNA,eclipse。

   需要去下載JNA的包(一個是jna.jar,一個時jna-platform.jar);包的下載在文章結尾提供地址。

   

3.實現思路

(1).一個window工作列開啟的應用程式也就是一個前台進程,所以先通過JNA擷取所有工作列進程(.exe檔案名稱,檔案路徑,PID)(從.EXE檔案中可以獲得圖片),這時獲得進程還包括一些非視窗(非前台應用)進程,需要使用第二步驟過濾掉。
(2).從JNA中枚舉出所有的視窗控制代碼,並且通過視窗控制代碼獲得PID。
(3).遍曆第一步驟獲得的進程獲得元素的PID,當元素的PID在第二步驟獲得的集合中時,則從此元素的.exe檔案中獲得圖片。

4.實現代碼

  (1)擷取視窗的控制代碼進程PID

    

 1 public class EnumWindow { 2     public static Set<Integer> getTaskPID() { 3         User32 user32 = User32.INSTANCE; 4         Set<Integer> set=new HashSet<Integer>(); 5         IntByReference i=new IntByReference();//放PID 6         user32.EnumWindows(new User32.WNDENUMPROC() { 7             public boolean callback(HWND h, Pointer p) { 8                 user32.GetWindowThreadProcessId(h, i);擷取視窗的PID 9                 if(user32.IsWindow(h)&&user32.IsWindowEnabled(h)&&user32.IsWindowVisible(h)){10                     set.add(i.getValue());11                 }12                 return true;13             }14         }, null);15         return set;//獲得到的視窗PID集合16     }17 }

  (2)擷取進程圖片

   

 1 public class Maintest { 2  3     public interface ProcessPathKernel32 extends Kernel32 { 4         class MODULEENTRY32 extends Structure { 5             public static class ByReference extends MODULEENTRY32 implements Structure.ByReference { 6                 public ByReference() {} 7                 public ByReference(Pointer memory) { 8                     super(memory); 9                 }10             }11             public MODULEENTRY32() {12                 dwSize = new WinDef.DWORD(size());13             }14 15             public MODULEENTRY32(Pointer memory) {16                 super(memory);17                 read();18             }19 20             public DWORD dwSize;21             public DWORD th32ModuleID;22             public DWORD th32ProcessID;23             public DWORD GlblcntUsage;24             public DWORD ProccntUsage;25             public Pointer modBaseAddr;26             public DWORD modBaseSize;27             public HMODULE hModule;28             public char[] szModule = new char[255+1]; // MAX_MODULE_NAME3229             public char[] szExePath = new char[MAX_PATH];30             public String szModule() { return Native.toString(this.szModule); }31             public String szExePath() { return Native.toString(this.szExePath); }32             @Override33             protected List<String> getFieldOrder() {34                 return Arrays.asList(new String[] {35                         "dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage", "ProccntUsage", "modBaseAddr", "modBaseSize", "hModule", "szModule", "szExePath"36                 });37             }38         }39 40         ProcessPathKernel32 INSTANCE = (ProcessPathKernel32)Native.loadLibrary(ProcessPathKernel32.class, W32APIOptions.UNICODE_OPTIONS);41         boolean Module32First(HANDLE hSnapshot, MODULEENTRY32.ByReference lpme);42         boolean Module32Next(HANDLE hSnapshot, MODULEENTRY32.ByReference lpme);43     }44 45     public static void main(String[] args) throws IOException {46 47         HICON[] a=new WinDef.HICON[12];48         HICON[] b=new WinDef.HICON[11];49         Set<Integer> Pids=EnumWindow.getTaskPID();//擷取視窗進程的PID50         int c=1;51         Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);52         Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();53         WinNT.HANDLE processSnapshot = 54                 kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));55         try {56             while (kernel32.Process32Next(processSnapshot, processEntry)) {57                 //processEntry.th32ProcessID  程式的PID58                 //Native.toString(processEntry.szExeFile) 程式的名字(xx.exe)59                 WinNT.HANDLE moduleSnapshot =kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, processEntry.th32ProcessID);60                 if(Pids.contains(processEntry.th32ProcessID.intValue())){61                     String exeName=Native.toString(processEntry.szExeFile).substring(0,Native.toString(processEntry.szExeFile).indexOf(".exe"));62                     if(exeName.toLowerCase().equals("shellexperiencehost")||exeName.toLowerCase().equals("syntpenh")){//ShellExperienceHost為開始菜單外殼,syntpenh為觸摸板相關程式63                         continue;64                     }65                     try {66                         ProcessPathKernel32.MODULEENTRY32.ByReference me = new ProcessPathKernel32.MODULEENTRY32.ByReference();67                         ProcessPathKernel32.INSTANCE.Module32First(moduleSnapshot, me);68                         //me.szExePath() //程式(xx.exe)所在路徑69                         Shell32.INSTANCE.ExtractIconEx(me.szExePath(), 0, a, b, c);70                         if(a.length>0&&Native.toString(processEntry.szExeFile)!=null&&Native.toString(processEntry.szExeFile).length()>0&&Native.toString(processEntry.szExeFile).indexOf(".exe")>=0){//判斷是否有表徵圖71                             String fileName=Native.toString(processEntry.szExeFile).substring(0,Native.toString(processEntry.szExeFile).indexOf(".exe"))+".jpg";72                             if (me.szExePath()!=null&&me.szExePath()!="") {73                                 File file=new File(me.szExePath());//.exe檔案74                                 File imgFile=new File("C:\\windowTaskBarIcon\\"+fileName);75                                 if (!imgFile.exists()) {76                                     imgFile.mkdirs();77                                 }78                                 Image image=((ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file)).getImage();79                                 ImageIO.write((RenderedImage) image,"jpg", imgFile);80                             }81                         }82                     }83                     finally {84                         kernel32.CloseHandle(moduleSnapshot);85                     }86                 }87             }88         }89         finally {90             kernel32.CloseHandle(processSnapshot);91         }92     }93 94 }

5.

會把獲得的圖片儲存到指定的檔案夾下面。

 

6.工程完整代碼放在github上

地址:https://github.com/xujinghuan/getWindowTaskIcon

 

 

  

   

擷取 window工作列已經開啟應用程式視窗(也就是工作管理員中前台進程)的表徵圖

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.