通过窗口句柄获取程序路径
2013-08-11 17:59
537 查看
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:\temp\b.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); } }
相关文章推荐
- C# 通过窗口句柄获取程序运行路径 通过程序运行路径获取程序图标
- C# 通过窗口句柄获取程序路径 图标
- Delphi~通过程序窗体句柄获取程序路径
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
- VC通过窗口句柄获取应用程序路径
- 获取当前焦点所在窗口句柄,通过 句柄获取执行路径
- VC++通过类名获取同一程序的多个实例窗口句柄
- VB6: 通过窗口句柄得到窗口所在程序的名称和路径(实例)
- 根据窗口句柄获取所在程序路径
- Delphi~通过程序窗体句柄获取程序路径
- 根据窗口句柄获取所在程序路径 - 回复 "Haiwen" 的问题
- Delphi~通过程序窗体句柄获取程序路径
- VB.NET 通过窗口句柄,获取webbrowser控件HTML内容
- 通过Windows进程ID获取窗口句柄
- Delphi通过IE窗口句柄获取网页接口(IWebBrowser2)
- 通过窗口句柄获取窗口图标的几种办法
- Windows程序获取鼠标下窗口句柄的例子
- 如何使用vs对程序进行打包?怎么通过注册表获取安装路径?
- Delphi 获取其他程序窗口句柄
- vs不支持通过afxgetmainwnd()获取窗口句柄