IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Discussion :

[débutant] La fenêtre active est un shell?


Sujet :

Windows

  1. #1
    Membre éclairé
    Profil pro
    Ingénieur sécurité
    Inscrit en
    Février 2007
    Messages
    574
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2007
    Messages : 574
    Points : 751
    Points
    751
    Par défaut [débutant] La fenêtre active est un shell?
    Bonjour à tous,
    Hier, je voulais coder un raccourci clavier permettant de créer un nouveau dossier (touche Windows+n). J' arrive à détecter cette combinaison :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    public partial class Form1 : Form
        {
    
            [DllImport("user32", SetLastError = true)]
            private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
            [DllImport("user32", SetLastError = true)]
            private static extern int UnregisterHotKey(IntPtr hwnd, int id);
            [DllImport("kernel32", SetLastError = true)]
            private static extern short GlobalAddAtom(string lpString);
            [DllImport("kernel32", SetLastError = true)]
            private static extern int GlobalDeleteAtom(short nAtom);
    
            private const int MOD_WIN = 0x8;
            private const int WM_HOTKEY = 0x312;
            private short hotKeyId;
            private NewFolderMethods n = null;
    
            public Form1()
            {
                InitializeComponent();
                this.Hide();
                n = new NewFolderMethods();
            }
    
            public void RegisterGlobalHotKey(Keys hotkey, int modifiers)
            {
                try
                {
                    String atomName = AppDomain.GetCurrentThreadId().ToString("X8");
                    hotKeyId = GlobalAddAtom(atomName);
                    if (hotKeyId == 0)
                    {
                        int err = Marshal.GetLastWin32Error();
                        throw new Win32Exception(err, "Unable to generate unique hotkey ID.");
                    }
                    int result = RegisterHotKey(this.Handle, hotKeyId, modifiers, (int)hotkey);
                    if (result == 0)
                    {
                        int err = Marshal.GetLastWin32Error();
                        throw new Win32Exception(err, "Unable to register hotkey");
                    }
                }
                catch (Exception e)
                {
                    UnregisterGlobalHotKey();
                }
            }
    
            public void UnregisterGlobalHotKey()
            {
                if (this.hotKeyId != 0)
                {
                    UnregisterHotKey(this.Handle, hotKeyId);
                    GlobalDeleteAtom(hotKeyId);
                    hotKeyId = 0;
                }
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_HOTKEY)
                {
                    MessageBox.Show(n.GetCurrentActiveDirectory());
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                RegisterGlobalHotKey(Keys.N, MOD_WIN);
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                UnregisterGlobalHotKey();
            }
        }
    Mon problème maintenant c'est de récupérer le chemin abcolu du shell, afin d'y crée un nouveau dossier.
    Je suis tombé sur ce code en delphi. J'ai donc une idée générale.
    Récupérer la fenêtre active avec la fonction GetForegroundWindow() qui renvoi le handle de la fenêtre en cour. Je peux passer ce pointeur à la fonction ShellGetPath qui à l'ait de prendre un handle en paramètre. Mon problème, c'est d'identifier la fenêtre active comme une fenêtre de shell.
    Sivous avez des pistes...
    Merci

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    J'ai du mal à voir ce que tu cherches à faire.
    Tu utilises un raccourci global pour créer un dossier dans la fenêtre shell courante ?
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre éclairé
    Profil pro
    Ingénieur sécurité
    Inscrit en
    Février 2007
    Messages
    574
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2007
    Messages : 574
    Points : 751
    Points
    751
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    Tu utilises un raccourci global pour créer un dossier dans la fenêtre shell courante ?
    Oui.
    Il n'existe pas de raccourci clavier pour créer un nouveau dossier sous windows. Je trouve ça pas pratique. Comme j'ai un peu de temps en ce moment, je voudrais en créer un un. Ca permet de me former un tout petit peu à la programmation Windows que je ne connais pas du tout.
    Si je suis à côté de la plaque, dites le moi, pas de problème...

  4. #4
    Membre éclairé
    Profil pro
    Ingénieur sécurité
    Inscrit en
    Février 2007
    Messages
    574
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2007
    Messages : 574
    Points : 751
    Points
    751
    Par défaut
    Voilà ce que je voulais faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    public class ShellMethods
        {
            [DllImport("user32", SetLastError = true)]
            private static extern IntPtr GetForegroundWindow();
            [DllImport("user32", SetLastError = true)]
            private static extern int SendMessage(IntPtr hwnd, int msg, int pid_info, int extra_info);
            [DllImport("user32", SetLastError = true)]
            private static extern bool IsWindowVisible(IntPtr whnd);
            [DllImport("user32", SetLastError = true)]
            private static extern int GetClassName(IntPtr hwnd, StringBuilder buffer, int bufferSize);
            [DllImport("kernel32", SetLastError = true)]
            private static extern int GetCurrentProcessId();
            //Signature des méthodes de Shell32 (non-signée)
            [DllImport("Shell32", SetLastError = true)]
            private static extern IntPtr SHLockShared(int hMem, int pid);
            [DllImport("Shell32", SetLastError = true)]
            private static extern void SHUnlockShared(IntPtr pv);
            [DllImport("Shell32", SetLastError = true)]
            private static extern void SHFreeShared(int hMem, int pid);
            [DllImport("Shell32", SetLastError = true)]
            private static extern int ILClone(IntPtr pv);
            [DllImport("Shell32", SetLastError = true)]
            private static extern bool SHGetPathFromIDList(int result, StringBuilder buffer);
    
            private const int VM_USER = 0x400;
            private const int CWM_GETPATH = VM_USER + 12; 
            private const int MAX_PATH = 255;
            private StringBuilder pathBuffer = new StringBuilder(MAX_PATH);
            private StringBuilder windowTypeBuffer = new StringBuilder(MAX_PATH);
    
            public String ShellGetPath(IntPtr hwnd)
            {
                int result = 0;
                //On récupère le pid
                int pid = GetCurrentProcessId();
                //On récupère l'adresse de début de la zone mémoire
                int hMem = SendMessage(hwnd, CWM_GETPATH, pid, 0);
                if (hMem != 0)
                {
                    //On lit la mémoire partagée
                    IntPtr pv = SHLockShared(hMem, pid);
                    if (pv != null)
                    {
                        //On copie
                        result = ILClone(pv);
                        SHUnlockShared(pv);
                    }
                    //On libère la mémoire partagée
                    SHFreeShared(hMem, pid);
                }
                //On récupère le chemin du pid (du shell)
                SHGetPathFromIDList(result, pathBuffer);
                return pathBuffer.ToString() ;
            }
    
            /// <summary>
            /// Retourne la fenêtre active (qui a lefocus)
            /// </summary>
            /// <returns>Le handle de la fenêtre active</returns>
            public IntPtr GetActiveWindow()
            {
                return GetForegroundWindow();
            }
    
            /// <summary>
            /// Détermine si la fenêtre est un shell
            /// </summary>
            /// <param name="hwnd">le handle de la fenêtre</param>
            /// <returns>Vrai si la fenêtre est un shell</returns>
            public Boolean ActiveWindowIsShell(IntPtr hwnd)
            {
                //On récupère le type de la fenêtre
                GetClassName(hwnd, windowTypeBuffer, MAX_PATH);
                //Elle doit être visible et de type shell (fenêtre explorer ou structure arborescente)
                if (IsWindowVisible(hwnd) && (windowTypeBuffer.ToString() == "ExploreWClass" || windowTypeBuffer.ToString() == "CabinetWClass"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    Si quelqu'un connait le type du bureau (pour shell, c'est CabinetWClass par exemple).
    Merci...

  5. #5
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Sous mon Windows XP, Spy++ me dit que la ListView du bureau, nommée "FolderView",
    se trouve dans une fenêtre de classe SHELLDLL_DefView, sans nom,
    qui se trouve dans une fenêtre de classe Progman nommée "Program Manager"
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  6. #6
    Membre éclairé
    Profil pro
    Ingénieur sécurité
    Inscrit en
    Février 2007
    Messages
    574
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2007
    Messages : 574
    Points : 751
    Points
    751
    Par défaut
    Merci,
    je vais essayer Spy++. La programmation Windows couplée au DotNet c'est sympa et ça à l'air très puissant.
    A +.

  7. #7
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Tiens, voici une mine d'or de Maître Raymond Chen : http://blogs.msdn.com/oldnewthing/ar...20/188696.aspx

    Une chance pour toi que j'aie exploré les archives de son blog et que je m'en sois souvenu...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  8. #8
    Membre éclairé
    Profil pro
    Ingénieur sécurité
    Inscrit en
    Février 2007
    Messages
    574
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2007
    Messages : 574
    Points : 751
    Points
    751
    Par défaut
    La méthode pour arriver au but est différente. Je vais étudier ça. Merci du lien et de ton aide en tout cas.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant][VE] Comment afficher une fenetre Shell (bean)
    Par antony dans le forum Eclipse Java
    Réponses: 6
    Dernier message: 11/08/2005, 11h46
  2. capture de la fenêtre active
    Par sjprm dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 28/06/2005, 14h15
  3. execution, threads et fenêtre active
    Par inertia dans le forum MFC
    Réponses: 2
    Dernier message: 26/05/2005, 11h05
  4. Comment savoir si une fenêtre s'est fermée
    Par niuniuk36 dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 12/05/2005, 14h49
  5. Fenêtre active
    Par Isa31 dans le forum Composants VCL
    Réponses: 4
    Dernier message: 23/11/2004, 09h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo