Nachfolgend stelle ich eine Klasse vor, mit der es möglich ist eine Form auf dem Desktop “festzukleben”. Die Klasse stellt damit das Gegenteil von TopMost dar. Die Form bleibt auch auf dem Desktop, wenn der Benutzer “Alles minimieren” oder [Windowstaste]+D drückt.
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 | using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TSQL.DeskCLASS { class Desk { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr FindWindow([MarshalAs(UnmanagedType.LPTStr)] string lpClassName, [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName); [DllImport("user32.dll")] public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll")] public static extern IntPtr GetDC(UInt16 hwnd); IntPtr desktop; Form ParentForm; public Desk(Form senderForm) { ParentForm = senderForm; } public void DockStart() { IntPtr hwndParent = FindWindow("ProgMan", null); SetParent(ParentForm.Handle, hwndParent); desktop = GetDC(0); } public void DockStop() { IntPtr hwndParent = FindWindow("screenClass", null); SetParent(ParentForm.Handle, hwndParent); desktop = GetDC(0); } } } |
Hier noch ein Beispielaufruf aus der Form1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using TSQL.DeskCLASS; namespace WindowsApplication1 { public partial class Form1 : Form { Desk myDesk; public Form1() { InitializeComponent(); myDesk = new Desk(this); myDesk.DockStart(); } } } |
Tags: bottommost, dllimport, FindWindow, GetDC, IntPtr, setparent, topmost