• C# 通过窗口句柄获取程序路径 图标


    转自:http://qqhack8.blog.163.com/blog/static/11414798520113363829505/

    C# 通过窗口句柄获取程序路径 图标
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Drawing;

    namespace K8加强版任务管理器
    {
        class k8taskmgr
        {
            private const int MAX_PATH = 260;
            public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
            [DllImport("coredll.dll")]
            public extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
            [DllImport("coredll.dll")]
            public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
            [DllImport("coredll.dll")]
            public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);
            [DllImport("coredll.dll")]
            public extern static bool CloseHandle(IntPtr hObject);
            [DllImport("Coredll.dll", EntryPoint = "GetModuleFileName")]
            private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);
            [DllImport("coredll.dll", SetLastError = true)]
            private static extern IntPtr ExtractIconEx(string fileName, int index, ref IntPtr hIconLarge, ref IntPtr hIconSmall, uint nIcons);

            //通过句柄获取运行程序路径

            public static String GetAppRunPathFromHandle(IntPtr hwnd)
            {
                int pId = 0;
                IntPtr pHandle = IntPtr.Zero;
                GetWindowThreadProcessId(hwnd, ref pId);
                pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pId);
                StringBuilder sb = new StringBuilder(MAX_PATH);
                GetModuleFileName(pHandle, sb, sb.Capacity);
                CloseHandle(pHandle);
                return sb.ToString();
            }

            //根据句柄获取运行程序小图标 //当然也可以获取大图标

            private Icon GetSmallIconFromHandle(IntPtr hwnd)
            {
                IntPtr hLargeIcon = IntPtr.Zero;
                IntPtr hSmallIcon = IntPtr.Zero;
                String filePath = GetAppRunPathFromHandle(hwnd);

                ExtractIconEx(filePath, 0, ref hLargeIcon, ref hSmallIcon, 1);
                Icon icon = null;
                try
                {
                    icon = (Icon)Icon.FromHandle(hSmallIcon);
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.Message);
                }
                return icon;
            }

        }
    }

  • 相关阅读:
    Nodejs与ES6系列3:generator对象
    Nodejs与ES6系列2:Promise对象
    Nodejs与ES6系列1:变量声明
    Nodejs与ES6系列4:ES6中的类
    angular单元测试与自动化UI测试实践
    javascript 异步模块加载 简易实现
    javascript模式 (3)——工厂模式和装饰模式
    TP手册学习第四内置天
    TP手册学习第三天
    tp5命令行基础
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3251708.html
Copyright © 2020-2023  润新知