Gets the icon for the Windows taskbar that has the application window open (that is, the foreground process in Task Manager)

Source: Internet
Author: User

Gets the icon for the Windows taskbar that has the application window open (that is, the foreground process in Task Manager)

1. Function description

Gets the window icon for an application that has been opened to the window taskbar. as follows: (to get the icon to QQ, browser, folder, but the hidden icon in the taskbar does not show)

2. Use of technology and tools

JAVA,JNA, Eclipse.

Need to download JNA package (one is Jna.jar, one time Jna-platform.jar); The download of the package provides an address at the end of the article.

3. Realization of Ideas

(1). A window taskbar opens an application that is a foreground process, so get all the taskbar processes (. exe file name, file path, PID) (from the. exe file) by JNA first, and then the get process also includes some non-window (non-foreground app) processes, Need to filter out using the second step.
(2). Enumerates all the window handles from the JNA, and obtains the PID through the window handle.
(3). The process obtained by traversing the first step gets the pid of the element, and when the pid of the element is in the collection obtained in the second step, the picture is obtained from the. exe file of this element.

4. Implementing the Code

(1) Get window handle process PID

1  Public classEnumwindow {2      Public StaticSet<integer>Gettaskpid () {3User32 User32 =user32.instance;4Set<integer> set=NewHashset<integer>();5Intbyreference i=NewIntbyreference ();//put PID6User32. EnumWindows (NewUser32.wndenumproc () {7              Public BooleanCallback (HWND H, Pointer p) {8User32. GetWindowThreadProcessId (H, i);gets the PID of the window9                 if(User32. IsWindow (h) &&user32. IsWindowEnabled (h) &&User32. IsWindowVisible (h)) {Ten Set.add (I.getvalue ()); One                 } A                 return true; -             } -},NULL); the         returnset;//get to the window PID set -     } -}

(2) Get process picture

1  Public classMaintest {2 3      Public InterfaceProcessPathKernel32extendsKernel32 {4         classMODULEENTRY32extendsStructure {5              Public Static classByreferenceextendsMODULEENTRY32ImplementsStructure.byreference {6                  Publicbyreference () {}7                  Publicbyreference (Pointer memory) {8                     Super(memory);9                 }Ten             } One              PublicMODULEENTRY32 () { Adwsize =NewWindef.dword (Size ()); -             } -  the              PublicMODULEENTRY32 (Pointer memory) { -                 Super(memory); - read (); -             } +  -              PublicDWORD dwsize; +              PublicDWORD Th32moduleid; A              PublicDWORD Th32processid; at              PublicDWORD glblcntusage; -              PublicDWORD proccntusage; -              PublicPointer modbaseaddr; -              PublicDWORD modbasesize; -              Publichmodule hmodule; -              Public Char[] Szmodule =New Char[255+1];//max_module_name32 in              Public Char[] Szexepath =New Char[MAX_PATH]; -              PublicString Szmodule () {returnNative.tostring ( This. Szmodule); } to              PublicString Szexepath () {returnNative.tostring ( This. Szexepath); } + @Override -             protectedList<string>Getfieldorder () { the                 returnArrays.aslist (Newstring[] { *"Dwsize", "Th32moduleid", "Th32processid", "Glblcntusage", "Proccntusage", "modbaseaddr", "Modbasesize", "HModule", " Szmodule "," Szexepath " $                 });Panax Notoginseng             } -         } the  +ProcessPathKernel32 INSTANCE = (ProcessPathKernel32) native.loadlibrary (ProcessPathKernel32.class, w32apioptions.unicode_options); A         BooleanModule32first (HANDLE hsnapshot, MODULEENTRY32. Byreference LPME); the         BooleanModule32next (HANDLE hsnapshot, MODULEENTRY32. Byreference LPME); +     } -  $      Public Static voidMain (string[] args)throwsIOException { $  -Hicon[] A=NewWindef.hicon[12]; -Hicon[] B=NewWindef.hicon[11]; theSet<integer> Pids=enumwindow.gettaskpid ();//gets the PID of the window process -         intC=1;WuyiKernel32 kernel32 = (Kernel32) native.loadlibrary (Kernel32.class, w32apioptions.default_options); theTlhelp32.PROCESSENTRY32.ByReference Processentry =NewTlhelp32.PROCESSENTRY32.ByReference (); -Winnt.handle Processsnapshot = WuKernel32. CreateToolhelp32Snapshot (Tlhelp32.th32cs_snapprocess,NewWindef.dword (0)); -         Try { About              while(KERNEL32. Process32Next (Processsnapshot, Processentry)) { $                 //PID of the PROCESSENTRY.TH32PROCESSID program -                 //name of the native.tostring (processentry.szexefile) program (Xx.exe) -Winnt.handle Modulesnapshot =KERNEL32. CreateToolhelp32Snapshot (Tlhelp32.th32cs_snapmodule, processentry.th32processid); -                 if(Pids.contains (ProcessEntry.th32ProcessID.intValue ())) { AString exename=native.tostring (processentry.szexefile). substring (0,native.tostring (processentry.szexefile). IndexOf (". exe")); +                     if(Exename.tolowercase (). Equals ("Shellexperiencehost") | | Exename.tolowercase (). Equals ("Syntpenh")) {//shellexperiencehost for Start menu shell, syntpenh for touchpad related program the                         Continue; -                     } $                     Try { theProcessPathKernel32.MODULEENTRY32.ByReference me =NewProcessPathKernel32.MODULEENTRY32.ByReference (); the ProcessPathKernel32.INSTANCE.Module32First (Modulesnapshot, ME); the                         //Me.szexepath ()//The path where the program (Xx.exe) resides theShell32.INSTANCE.ExtractIconEx (Me.szexepath (), 0, A, b, c); -                         if(A.length>0&&native.tostring (processentry.szexefile)! =NULL&&native.tostring (processentry.szexefile). Length () >0&&native.tostring ( Processentry.szexefile). IndexOf (". exe") >=0) {//determine if there is an icon inString filename=native.tostring (processentry.szexefile). substring (0,native.tostring (processentry.szexefile). IndexOf (". exe")) + ". jpg"; the                             if(Me.szexepath ()! =NULL&&me.szexepath ()! = "") { theFile file=NewFile (Me.szexepath ());//. exe file AboutFile imgfile=NewFile ("c:\\windowtaskbaricon\\" +fileName); the                                 if(!imgfile.exists ()) { the imgfile.mkdirs (); the                                 } +Image image=((ImageIcon) Filesystemview.getfilesystemview (). Getsystemicon (file)). GetImage (); -Imageio.write ((renderedimage) image, "JPG", imgfile); the                             }Bayi                         } the                     } the                     finally { - KERNEL32. CloseHandle (modulesnapshot); -                     } the                 } the             } the         } the         finally { - KERNEL32. CloseHandle (processsnapshot); the         } the     } the 94}

5.

Saves the acquired picture to the specified folder.

6. Project complete code on GitHub

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

Gets the icon for the Windows taskbar that has the application window open (that is, the foreground process in Task Manager)

Related Article

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.