Avatar billede thomas_fogh Nybegynder
04. august 2006 - 14:46 Der er 4 kommentarer og
1 løsning

Få fat på icon fra anden process?

Kan man få fat i icon'et fra en anden process?
Fx. for notepad:

Process[] processes = Process.GetProcessesByName("notepad");
...
[get icon]
[display icon in list]
...
Avatar billede mcgoat Nybegynder
04. august 2006 - 19:38 #1
hmm. hvis du kan få fat på filnavnet kan det vel nok lade sig gøre
Avatar billede mcgoat Nybegynder
04. august 2006 - 19:46 #2
Få alle moduler som processen bruger: (den første må være stien til selve programmet, som vi skal hente ikonet ud fra)

//Get all modules inside the process
Process[] ObjModulesList = Process.GetProcessesByName("devenv");

// Populate the module collection.
ProcessModuleCollection ObjModules = ObjModulesList[0].Modules;

// Iterate through the module collection.
foreach (ProcessModule objModule in ObjModules)
{
//Get valid module path
strModulePath =GetValidString(objModule.FileName.ToString());
//If the module exists
if (File.Exists(objModule.FileName.ToString()))
  {
    //Get version
    string strFileVersion = GetValidString(objModule.
                  FileVersionInfo.FileVersion.ToString());
    //Get File size
    string strFileSize    = GetValidString
                (objModule.ModuleMemorySize.ToString());
    //Get Modification date
    FileInfo objFileInfo = new
                FileInfo(objModule.FileName.ToString());
    string strFileModificationDate = GetValidString
        (objFileInfo.LastWriteTime.ToShortDateString()); 
    //Get File description
    string strFileDescription  = GetValidString
                  (objModule.FileVersionInfo.
                  FileDescription.ToString());
    //Get Product Name
    string strProductName  = GetValidString
          (objModule.FileVersionInfo.ProductName.ToString());
    //Get Product Version
    string strProductVersion  = GetValidString
          (objModule.FileVersionInfo.ProductVersion.ToString());
  }
}


Hente ikon fra fil:

GetFileIcon is used to obtain icons for files, and uses three parameters:

name - Complete file and path names to read.
size - Whether to obtain 16x16 or 32x32 pixels, uses the IconSize enumeration.
linkOverlay - Specify whether the returned icon should include the small link overlay.
It is a static member function since it doesn't need to store any state, and is intended primarily as an added layer of abstraction. If I needed to obtain a file's icon in the future (and not store it in an ImageList etc.) then I could do so using this class. Once I had a type that wrapped up the necessary API functions to obtain file icons I would then build another type to manage large and small ImageLists that would enable me to make a single call to add an icon, and if it was already added, return the index that the icon was in the ImageList.

public static System.Drawing.Icon GetFileIcon(string name, IconSize size,
                                              bool linkOverlay)
{
    Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
   
    if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;


    /* Check the size specified for return. */
    if (IconSize.Small == size)
    {
        flags += Shell32.SHGFI_SMALLICON ; // include the small icon flag
    }
    else
    {
        flags += Shell32.SHGFI_LARGEICON ;  // include the large icon flag
    }
   
    Shell32.SHGetFileInfo( name,
        Shell32.FILE_ATTRIBUTE_NORMAL,
        ref shfi,
        (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),
        flags );


    // Copy (clone) the returned icon to a new object, thus allowing us
    // to call DestroyIcon immediately
    System.Drawing.Icon icon = (System.Drawing.Icon)
                        System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
    User32.DestroyIcon( shfi.hIcon ); // Cleanup
    return icon;
}
Avatar billede mcgoat Nybegynder
04. august 2006 - 20:16 #3
Har lige lavet og testet:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("Shell32.dll")]
        public static extern IntPtr SHGetFileInfo(
            string pszPath,
            uint dwFileAttributes,
            ref SHFILEINFO psfi,
            uint cbFileInfo,
            uint uFlags
        );

        [StructLayout(LayoutKind.Sequential)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        };
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
        extern static bool DestroyIcon(IntPtr handle);

        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0;    // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1;    // 'Small icon
        public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
        public const uint SHGFI_LINKOVERLAY = 0x8000;
        public const uint FILE_ATTRIBUTE_NORMAL = 0x80;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] ObjModulesList = Process.GetProcessesByName("notepad");
            ProcessModuleCollection ObjModules = ObjModulesList[0].Modules;
            Form1.ActiveForm.Text = ObjModules[0].FileName;
            Form1.ActiveForm.Icon = GetFileIcon(ObjModules[0].FileName,false);
        }

        public static System.Drawing.Icon GetFileIcon(string name, bool linkOverlay)
        {

            SHFILEINFO shfi = new SHFILEINFO();
            uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;

            if (true == linkOverlay) flags += SHGFI_LINKOVERLAY;

                flags += SHGFI_SMALLICON;
                SHGetFileInfo(name,
                FILE_ATTRIBUTE_NORMAL,
                ref shfi,
                (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                flags);

            System.Drawing.Icon icon = (System.Drawing.Icon)
                                System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
            DestroyIcon(shfi.hIcon); // Cleanup
            return icon;
        }
    }
}
Avatar billede mcgoat Nybegynder
04. august 2006 - 20:19 #4
hvor den sætter Form1 text til det fulde filnavn og ikonet på fomen til ikonet på den process du har fat på
Avatar billede thomas_fogh Nybegynder
05. august 2006 - 13:59 #5
øh, okay. Mange tak!
Meget detaljeret svar ;)
Så skal jeg bare finde ud af hvordan man ændre position og størrelse på en anden proces's vindue, men det laver jeg lige et andet spørgsmål til...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester