Wurde Form minimiert?
-
ich habe ein Objekt vom Typ System.Windows.Forms.Form - ich frage mich gerade welches Event man abfangen muss um festzustellen, dass dieses Objekt minimiert wurde
ich möchte, dass meine Anwendung nur Berechnungen durchführt, wenn sie auf dem Bildschirm sichtbar ist, wenn sie im Hintergrund bzw. Minimiert ist sollen keine Berechnungen durchgeführt werden
-
bin gerade darauf gekommen das Resize Event abzufangen:
this.Resize += new EventHandler(this.FormResize);
...
protected void FormResize (Object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
MessageBox.Show(this, "Ich wurde minimiert!");
}
}Funktioniert recht gut - jedoch weiß ich nicht, wie ich feststellen soll, ob mein Fenster im Hintergrund/Sichtbar ist...?
-
problem gelöst
public class WinAPI { [StructLayout(LayoutKind.Explicit)] public struct Rect { [FieldOffset(0)] public int left; [FieldOffset(4)] public int top; [FieldOffset(8)] public int right; [FieldOffset(12)] public int bottom; } [DllImport("gdi32")] public static extern int GetClipBox(System.IntPtr hDC,ref Rect r); } // stellt fest ob ein Control sichtbar ist oder nicht (z. B. durch ein Fenster verdeckt) public bool isVisible(Control control) { Rectangle dummy = GetViewableRect(this); if( dummy.X == 0 && dummy.Y == 0 && dummy.Height == 0 && dummy.Width == 0) { return false; } return true; } public static Rectangle GetViewableRect( Control control ) { //! Get a graphics from the control, we need the HDC Graphics graphics = Graphics.FromHwnd(control.Handle); //! Get the hDC ( remember to call ReleaseHdc() when finished ) IntPtr hDC = graphics.GetHdc(); //! Create a rect to receive the viewable area of the control WinAPI.Rect r = new WinAPI.Rect(); //! Call the Win32 method which recieves the viewable area WinAPI.GetClipBox(hDC, ref r); //! Convert that to a .NET Rectangle Rectangle rectangle = new Rectangle(r.left, r.top, r.right - r.left, r.bottom - r.top ); //! Release the HDC (if you don't, the CLR throws an // exception when it tries to Finalize/Dispose it) graphics.ReleaseHdc(hDC); //! Dispose of the graphics, we don' need it any more graphics.Dispose(); graphics = null; return rectangle; }
-
Servus,
Frage: Erfüllt Graphics.IsVisible() nicht den gleichen Zweck, wie dieses Beispiel?
mfg
Hellsgore
-
edit: schwachsinn
-
hab folgende Methode geschrieben und getestet - isVisible2 liefert mir auch true von das Fenster komplett verdeckt ist - aus meinen VB6 Zeiten kenne ich die Visible Eigenschaft noch, wobei diese nur die generelle Sichtbarkeit auf einer Form festgelegt hat unabhängig davon ob sie gerade im Moment sichtbar (z. B. von einem Fenster verdeckt) ist
public bool isVisible2(Control control) { //! Get a graphics from the control, we need the HDC Graphics graphics = Graphics.FromHwnd(control.Handle); //! Get the hDC ( remember to call ReleaseHdc() when finished ) bool result = graphics.IsVisible(0,0,1024, 768); //! Release the HDC (if you don't, the CLR throws an // exception when it tries to Finalize/Dispose it) //! Dispose of the graphics, we don' need it any more graphics.Dispose(); return result; }
-
WindowState?