using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace WpfApplication1 { class WindowListItem : DragItem { private TaskBarWindow taskBarWindow; public WindowListItem(TaskBarWindow taskBarWindow) { InitializeComponent(); this.Content.Content = taskBarWindow.title; this.taskBarWindow = taskBarWindow; BitmapImage image = new BitmapImage(new Uri(@"F: emp.png", UriKind.Absolute)); this.Icon.Source = GetIcon(); } public override void onItemClick() { MainWindow.mainWindow.Hide(); Win32.ShowWindow(this.taskBarWindow.hwnd.ToInt32(), 9); Win32.SetForegroundWindow(this.taskBarWindow.hwnd.ToInt32()); } [DllImport("psapi.dll")] static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize); [System.Runtime.InteropServices.DllImport("shell32.dll")] private static extern int ExtractIconEx(string lpszFile, int niconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, int nIcons); public static IntPtr getAppIconFromWindowHandle(IntPtr hwnd) { int pId = 0; IntPtr pHandle = IntPtr.Zero; Win32.GetWindowThreadProcessId(hwnd, ref pId); pHandle = Win32.OpenProcess(1040, 0, pId); StringBuilder sb = new StringBuilder(512); GetModuleFileNameEx(pHandle, IntPtr.Zero, sb, 512); Win32.CloseHandle(pHandle); IntPtr[] largeIcons, smallIcons ; int IconCount = ExtractIconEx(sb.ToString(), -1, null, null, 0); largeIcons = new IntPtr[IconCount]; smallIcons = new IntPtr[IconCount]; ExtractIconEx(sb.ToString(), 0, largeIcons, smallIcons, IconCount); if (largeIcons.Length > 0) { return largeIcons[0]; } else { return IntPtr.Zero; } } public ImageSource GetIcon() { IntPtr icon = new IntPtr(0); icon = getAppIconFromWindowHandle(this.taskBarWindow.hwnd); if (icon != IntPtr.Zero) { return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( icon, new Int32Rect(0, 0, 32, 32), BitmapSizeOptions.FromEmptyOptions()); } else { return null; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace WpfApplication1 { public class Win32 { [DllImport("user32.dll", EntryPoint = "GetClassName")] public static extern int GetClassName(int hwnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll", EntryPoint = "GetWindow")] public static extern int GetWindow(int hwnd, int uCmd); [DllImport("user32.dll", EntryPoint = "GetWindowLong")] public static extern long GetWindowLong(int hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "IsWindowVisible")] public static extern bool IsWindowVisible(int hwnd); [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] public static extern bool SetForegroundWindow(int hwnd); [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern bool ShowWindow(int hwnd, int nCmdShow); [DllImport("user32.dll", EntryPoint = "GetWindowText")] public static extern int GetWindowText(int hwnd, StringBuilder lpString, int cch); public delegate bool EnumWindowsProc(int hWnd, int lParam); [DllImport("user32.dll")] public static extern int EnumWindows(EnumWindowsProc ewp, int lParam); public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF; [DllImport("User32.dll")] public extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId); [DllImport("Kernel32.dll")] public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess); [DllImport("Kernel32.dll")] public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode); [DllImport("Kernel32.dll")] public extern static bool CloseHandle(IntPtr hObject); [DllImport("Kernel32.dll", EntryPoint = "GetModuleFileName")] public static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize); [DllImport("SHELL32.DLL", CharSet = CharSet.Auto)] public static extern int ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, IntPtr phiconSmall, int nIcons); } }