zoukankan      html  css  js  c++  java
  • 通过窗口句柄获取程序路径

    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);
    
        }
    }
  • 相关阅读:
    ubuntu分屏终端
    Xcode-5.1.1更改文件盯作者
    Swift——(两)Swift访问元组
    Android Loader使用,屏幕解锁,重复荷载
    医疗信息季节:第二十三届中国国际医疗仪器设备展览会暨研讨会 思考
    UIBarButtonItem 小记边
    L轻松学习inux教程5 知识与学习bash
    Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI
    Android Context 上下文 你必须知道的一切
    Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3251710.html
Copyright © 2011-2022 走看看