zoukankan      html  css  js  c++  java
  • 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;
            }

        }
    }

  • 相关阅读:
    如何判断两个数组是否相等?
    正则表达式
    开发板 Linux驱动视频 驱动是什么
    关于模型的评估
    Python画图参数设置
    Python图片的横坐标汉字
    矩阵的点成和叉乘
    svd 奇异值分解
    Python的主成分分析PCA算法
    论文参考文献格式
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3251708.html
Copyright © 2011-2022 走看看