Obtain the icon that has opened the application window (that is, the foreground process in the Task Manager) in the window taskbar. The window

Source: Internet
Author: User

Obtain the icon that has opened the application window (that is, the foreground process in the Task Manager) in the window taskbar. The window

Obtain the icon of the window taskbar that has opened the application window (that is, the foreground process in the task manager ).

1. Function Description

Obtain the window icon of the application that has been opened in the window taskbar. The following figure shows the icons of QQ, browser, and folder, but hidden icons in the taskbar are not displayed)

 

2. Use technologies and tools

JAVA, JNA, Eclipse.

You need to download the JNA package (one is jna. jar, one is the jna-platform.jar); the download of the package provides the address at the end of the article.

3. Implementation ideas

(1).the application program opened in a Windows Task column also has a previous step, so that all tasks obtained through jnaare obtained first (.exe file name, file path, PID) (from. the image can be obtained in the EXE file. In this case, the process also contains some non-window (non-foreground application) processes, which need to be filtered out by the second step.
(2). enumerate all the window handles from JNA and obtain the PID through the window handle.
Obtain the image from the images file.

4. Implementation Code

(1) obtain the PID of the window handle Process

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 (); // put PID 6 user32.EnumWindows (new User32.WNDENUMPROC () {7 public boolean callback (HWND h, Pointer p) {8 user32.GetWindowThreadProcessId (h, i); get the window PID 9 if (user32.IsWindow (h) & user32.is1_wenabled (h) & user32.IsWindowVisible (h) {10 set. add (I. getValue (); 11} 12 return true; 13} 14}, null); 15 return set; // The obtained window PID set 16} 17}

(2) Obtain process images

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 MODULEENTRY3 2 (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 ProcessPathKe Rnel32 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 (); // obtain the PID50 int c = 1 of the window process; 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.creatw.lhelp32snapshot (Tlhelp32.TH32CS _ SNAPPROCESS, new WinDef. DWORD (0); 55 try {56 While (kernel32.Process32Next (processSnapshot, processEntry) {57 // processEntry. PID58 of the th32ProcessID program // Native. toString (processEntry. szExeFile) program name (xx.exe) 59 WinNT. HANDLE moduleSnapshot = kernel32.creatw.lhelp32snapshot (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 is the Start Menu shell, and syntpenh is the touchpad-related program 63 continue; 64} 65 try {66 ProcessPathKernel32.MODULEENTRY32. byReference me = new ProcessPathKernel32.MODULEENTRY32. byReference (); 67 ProcessPathKernel32.INSTANCE. module32First (moduleSna Pshot, me); 68 // me. szExePath () // the path of the program (xx.exe) is 69 Shell32.INSTANCE. extractIconEx (me. szExePath (), 0, a, B, c); 70 if (. length> 0 & Native. toString (processEntry. szExeFile )! = Null & Native. toString (processEntry. szExeFile ). length ()> 0 & Native. toString (processEntry. szExeFile ). indexOf (". exe ")> = 0) {// determine whether there is an icon 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 File 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.

Save the obtained image to the specified folder.

 

6. Put the complete project code on github.

Address: https://github.com/xujinghuan/getWindowTaskIcon

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.