コード例 #1
0
        public static ArrayList GetActiveTasks()
        {
            ArrayList returned              = new ArrayList();
            ArrayList SparTaskAppsNames     = new ArrayList();
            ArrayList SparTaskAppshWnd      = new ArrayList();
            ArrayList SparTaskButtohProc    = new ArrayList();
            ArrayList SparTaskButtonInfoTbl = new ArrayList();

            WinAPI.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
            {
                ArrayList     ITEM      = new ArrayList();
                StringBuilder strbTitle = new StringBuilder(255);
                int           nLength   = WinAPI.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                string        strTitle  = strbTitle.ToString();
                long          Style     = (long)WinAPI.GetWindowLong(hWnd, (-16));
                long          ExStyle   = (long)WinAPI.GetWindowLong(hWnd, (-20));

                if (WinAPI.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false && (ExStyle & 0x00000080L) == 0)
                {
                    ITEM.Add(strTitle);
                    ITEM.Add(hWnd);
                    uint pid = 0;
                    WinAPI.GetWindowThreadProcessId(hWnd, out pid);
                    System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById((int)pid);
                    ITEM.Add(proc);
                    SparTaskButtohProc.Add(proc.ProcessName);
                    SparTaskButtonInfoTbl.Add(ITEM);
                }
                return(true);
            };

            returned.Add(SparTaskButtonInfoTbl);
            returned.Add(SparTaskButtohProc);

            if (WinAPI.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
            {
            }
            return(returned);
        }
コード例 #2
0
        bool RefreshWindowList()
        {
            WindowList.Clear();
            WinAPI.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
            {
                StringBuilder strbTitle = new StringBuilder(255);
                int           nLength   = WinAPI.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                string        strTitle  = strbTitle.ToString();

                if (WinAPI.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
                {
                    // get window topmost info
                    bool isTM = WinAPI.isWindowTopMost(hWnd);

                    // Get window opacity
                    bool canSetOpacity = true;
                    byte transparency  = GetWindowTransparency(hWnd, out canSetOpacity);

                    // get process information
                    IntPtr  hProc           = WinAPI.GetProcessHandleFromHwnd(hWnd);
                    int     pid             = WinAPI.GetProcessId(hProc);
                    Process process         = Process.GetProcessById(pid);
                    string  processPath     = "";
                    string  description     = "";
                    string  processFileName = "";
                    try
                    {
                        ProcessModule pModule = process.MainModule;
                        processPath     = pModule.FileName;
                        description     = pModule.FileVersionInfo.FileDescription;
                        processFileName = process.MainModule.ModuleName;
                    }
                    catch {}

                    Bitmap bitmapUWP = null;
                    // 如果是UWP程序,获取UWP程序的图标
                    if (IsWindows10)
                    {
                        UWPProcess.AppxPackage appxPackage = UWPProcess.AppxPackage.FromProcess(process);
                        if (appxPackage != null)
                        {
                            string logoPath = appxPackage.FindHighestScaleQualifiedImagePath(appxPackage.Logo);
                            if (logoPath != null)
                            {
                                bitmapUWP = new Bitmap(logoPath);
                                Bitmap bitmapBackground = new Bitmap(bitmapUWP.Width, bitmapUWP.Height);
                                for (int i = 0; i < bitmapUWP.Width; i++)
                                {
                                    for (int j = 0; j < bitmapUWP.Height; j++)
                                    {
                                        bitmapBackground.SetPixel(i, j, Color.FromArgb(255, 128, 128));
                                    }
                                }
                                using (Graphics gr = Graphics.FromImage(bitmapBackground))
                                {
                                    gr.DrawImage(bitmapUWP, new PointF(0, 0));
                                }
                                bitmapUWP = bitmapBackground;
                            }
                        }
                    }

                    // Get icon
                    IntPtr hIcon  = WinAPI.GetAppIcon(hWnd);
                    Bitmap bitmap = null;

                    if (hIcon != IntPtr.Zero)
                    {
                        bitmap = Icon.FromHandle(hIcon).ToBitmap();
                    }
                    else
                    {
                        bitmap = Icon.FromHandle(WinAPI.LoadIcon(IntPtr.Zero, (IntPtr)WinAPI.SystemIcons.IDI_APPLICATION)).ToBitmap();
                    }
                    // try get application icon
                    if (hIcon == IntPtr.Zero)
                    {
                        hIcon = extractIconFromFile(processPath);
                        if (hIcon != IntPtr.Zero)
                        {
                            bitmap = Icon.FromHandle(hIcon).ToBitmap();
                        }
                    }


                    WindowList.Add(new ProcessHnd()
                    {
                        WindowName          = strTitle,
                        Handle              = hWnd,
                        Icon                = bitmapUWP == null ? bitmap : bitmapUWP,
                        IsTopMost           = isTM,
                        ProcessHandler      = hProc,
                        ProcessFullPath     = processPath,
                        Description         = description,
                        WindowOpacity       = transparency,
                        CanSetWindowOpacity = canSetOpacity,
                        ProcessObject       = process,
                        ProcessID           = pid,
                        ProcessFileName     = processFileName
                    });
                }
                return(true);
            };

            return(WinAPI.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero));
        }