Desktopicons auslesen

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 13 Antworten in diesem Thema. Der letzte Beitrag () ist von Murdock.

    Desktopicons auslesen

    Ich versuche gerade, die Icons auf dem Desktop auszulesen. Dafür muss man das Handle des SysListView auf dem Desktop auslesen. Mein Code hierfür ist dieser:

    C#-Quellcode

    1. this.desktopHandle = Native.User32.FindWindow("Progman", "Program Manager"); //Native.User32 ist eine Klasse, welche die WinApi-Funktionen bereitstellt
    2. this.desktopHandle = Native.User32.FindWindowEx(this.desktopHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
    3. this.desktopHandle = Native.User32.FindWindowEx(this.desktopHandle, IntPtr.Zero, "SysListView32", "FolderView");

    Dieser Code gibt aber am Ende immer IntPtr.Zero aus (bzw. gibt die erste Zeile 65810​ aus, die anderen beiden geben ​0 aus).
    Wo ist denn hier das Problem? An sich sollte er eigentlich funktionsfähig sein, da ich genau diesen Code in mehreren Threads auf den MSDN Forums, mycsharp und stackoverflow gesehen habe. Benutzen tue ich Windows 8.1 auf einem System mit 2 Monitoren - kann es vielleicht daran liegen?

    Hoffe auf hilfreiche Tipps.

    MfG Stefan
    Hey,

    hast mal einen anderen Ansatz probiert, um an die Icons zu kommen? Die Icon-Klasse bietet die statische Funktion .ExtractAssociatedIcon, welche einfach den Pfad entgegen nimmt.
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    Und wie soll das funktionieren? Du willst das handle vom Desktop und suchst nach einem SubElement in einem Programm mit Titel "Progman"? Was hat der Desktop dort drinnen verloren?

    Edit: Wow, ich bin da wohl nicht auf dem Stand von Win7+
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „jvbsl“ ()

    Die Argument sind stimmig, an der besagten Stelle wird der ClassName erwartet nicht der WindowTitle. Bei mir unter Win7 bekomme ich das Handle. Versuch mal mit dem Handle von Programm/Programm Manager und enumchildwindows ob du damit das Handle kriegst.

    C&P Code zum testen.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    2. Dim hwnd As IntPtr = User32.FindWindow("Progman", "Program Manager")
    3. Dim hwnds() As IntPtr = User32.GetChildWindows(hwnd)
    4. For i = 0 To hwnds.Length - 1
    5. Dim classname As New System.Text.StringBuilder("", 256)
    6. User32.GetClassName(hwnds(i), classname, 256)
    7. MessageBox.Show(classname.ToString)
    8. Next
    9. End Sub


    VB.NET-Quellcode

    1. Public Class User32
    2. <System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    3. Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    4. End Function
    5. <System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    6. Public Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    7. End Function
    8. <System.Runtime.InteropServices.DllImport("User32.dll")> _
    9. Public Shared Function EnumChildWindows(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
    10. End Function
    11. Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
    12. Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
    13. Dim ChildrenList As New List(Of IntPtr)
    14. Dim ListHandle As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(ChildrenList)
    15. Try
    16. EnumChildWindows(ParentHandle, AddressOf EnumWindow, System.Runtime.InteropServices.GCHandle.ToIntPtr(ListHandle))
    17. Finally
    18. If ListHandle.IsAllocated Then ListHandle.Free()
    19. End Try
    20. Return ChildrenList.ToArray
    21. End Function
    22. Public Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
    23. Dim ChildrenList As List(Of IntPtr) = CType(System.Runtime.InteropServices.GCHandle.FromIntPtr(Parameter).Target, Global.System.Collections.Generic.List(Of Global.System.IntPtr))
    24. If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
    25. ChildrenList.Add(Handle)
    26. Return True
    27. End Function
    28. <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    29. Public Shared Function GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    30. End Function
    31. End Class


    Funktioniert bei mir tadellos. Win8-64:

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.Runtime.InteropServices;
    11. namespace WindowsFormsApplication4
    12. {
    13. public partial class Form1 : Form
    14. {
    15. private const uint LVM_GETITEMCOUNT = 0x1004;
    16. [DllImport("user32", EntryPoint="FindWindow", SetLastError=true)]
    17. private static extern IntPtr FindWindow(string className, string windowName);
    18. [DllImport("user32", EntryPoint = "FindWindowEx", SetLastError = true)]
    19. private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);
    20. [DllImport("User32", EntryPoint="SendMessage", SetLastError=true)]
    21. private static extern int SendMessage(IntPtr handle, uint message, int wParam, int lParam);
    22. public Form1()
    23. {
    24. InitializeComponent();
    25. }
    26. private void button1_Click(object sender, EventArgs e)
    27. {
    28. IntPtr desktopHandle = FindWindow("Progman", "Program Manager");
    29. desktopHandle = FindWindowEx(desktopHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
    30. desktopHandle = FindWindowEx(desktopHandle, IntPtr.Zero, "SysListView32", "FolderView");
    31. int itemCount = SendMessage(desktopHandle,LVM_GETITEMCOUNT,0,0);
    32. }
    33. }
    34. }
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o