Ermitteln von Fenstern und der ZOrder?

28/04/2010 - 16:12 von mimuel | Report spam
Hallo,

wie kann ich Fensternamen und
den Wert der ZOrder ermitteln.

Ich möchte wissen welches Fenster hinter
dem aktiven Fenster liegt?

Danke Michael
 

Lesen sie die antworten

#1 Frank Dzaebel
28/04/2010 - 20:40 | Warnen spam
Hallo Michael,

wie kann ich Fensternamen und
den Wert der ZOrder ermitteln.
Ich möchte wissen welches Fenster hinter
dem aktiven Fenster liegt?



Mit folgenden Methoden kann man u.a. die Reihenfolge (z.T. implizit)
anpassen:

[Control.BringToFront-Methode (System.Windows.Forms)]
http://msdn.microsoft.com/de-de/lib...front.aspx

[Control.UpdateZOrder-Methode (System.Windows.Forms)]
http://msdn.microsoft.com/de-de/lib...order.aspx

[Control.SendToBack-Methode (System.Windows.Forms)]
http://msdn.microsoft.com/de-de/lib...oback.aspx

[Control.ControlCollection.SetChildIndex-Methode (System.Windows.Forms)]
http://msdn.microsoft.com/de-de/lib...index.aspx

[Control.ControlCollection.Add-Methode (System.Windows.Forms)] (und
Remove .. etc.)
http://msdn.microsoft.com/de-de/lib...n.add.aspx
___________

Allgemein dazu:

[Advanced Basics: Windows Forms Controls: Z-order and Copying Collections]
http://msdn.microsoft.com/en-us/mag...64035.aspx
___________


Ansonsten kann man die Z-Order auch über die Windows-API bestimmen,
wie Du im folgendem Beispiel-Code siehst:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public enum GetWindowConstants
{
GW_HWNDFIRST, GW_HWNDLAST, GW_HWNDNEXT,
GW_HWNDPREV, GW_OWNER, GW_CHILD,
GW_ENABLEDPOPUP, GW_MAX = 6
}


/// <summary> [Aus der MSDN]
/// The GetNextWindow function retrieves a handle to
/// the next or previous window in the Z-Order.
/// </summary>
/// <remarks>
/// [GetNextWindow Function (Windows)]
/// http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx
/// </remarks>
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindow")]
public static extern IntPtr GetNextWindow(IntPtr hwnd,
GetWindowConstants wFlag);


/// <summary> [Aus der MSDN]
/// The GetTopWindow function examines the Z order
/// of the child windows associated with the specified
/// parent window and retrieves a handle to the child
/// window at the top of the Z order.
/// </summary>
/// <param name="hWnd">
/// Handle to the parent window whose child windows
/// are to be examined. If this parameter is NULL, the
/// function returns a handle to the window at the top
/// of the Z order.
/// </param>
/// <remarks>
/// [GetTopWindow Function (Windows)]
/// http://msdn.microsoft.com/en-us/library/ms633514(VS.85).aspx
/// </remarks>
[DllImport("user32.dll")]
static extern IntPtr GetTopWindow(IntPtr hWnd);

Timer timer = new Timer();

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 5; i++)
NeueFormAnzeigen("Form" + i);

timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 1000; timer.Enabled = true;
}

void NeueFormAnzeigen(string name)
{
Form f = new Form(); f.Name = name;
f.Text = name; f.Show();
}

void timer_Tick(object sender, EventArgs e)
{
IntPtr window = Process.GetCurrentProcess().MainWindowHandle;
Dictionary<IntPtr, Form> forms = new Dictionary<IntPtr, Form>();
foreach (Form form in Application.OpenForms)
forms.Add(form.Handle, form);

while (forms.ContainsKey(window))
{
Console.Write(forms[window].Name + ", ");
window = GetNextWindow(window, GetWindowConstants.GW_HWNDNEXT);
}
Console.WriteLine();
}
}


ciao Frank
Dipl.Inf. Frank Dzaebel [MCP/MVP C#]
http://Dzaebel.NET

Ähnliche fragen