Beschreibung:
Eine abstrakte Form-Klasse deren Ableitung einen Dialog darstellt, wobei der Bildschirm um den Dialog herum langsam ausgegraut oder verdunkelt wird (ähnlich dem Abmelde-Dialog von Windows-XP).
Properties:*FormMoveable:
Legt fest, ob der Dialog verschoben werden kann oder nicht.
*ActiveShadowMode:
Stellt den Modus auf "Darken" oder "GreyOut" ein.
*GreyOutModeBaseColor:
Die Basisfarbe im Modus "Darken".
*TimerInitWait:
Wartezeit, bis mit dem Abdunkeln/Ausgrauen begonnen wird.
*TimerInterval:
Beeinflusst die Abdunklungs-/Ausgrauungsgeschwindigkeit.
*TimerIntervalSlowDownByOpacityFactor:
Ermöglicht eine abnemmende Geschwindigkeit.
*OpacityStep:
Beeinflusst die Abdunklungs-/Ausgrauungsgeschwindigkeit
*OpacityMax:
Maximal zu verwendende Opacity.
Methoden:*ShowShadow:
Aktiviert den Abdunklungs-/Ausgrauungsvorgang
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace ShadowScreenTest
{
public abstract class DialogShadowTopForm : Form
{
#region --- Fields ---
Timer _shadowTimer = new Timer();
Form _shadowForm = new Form();
bool _formMoveable = false;
ShadowMode _activeShadowMode = ShadowMode.GreyOut;
Color _greyOutModeBaseColor = Color.Black;
int _timerInitWait = 2000;
int _timerInterval = 10;
int _timerIntervalSlowDownByOpacityFactor = 150;
Single _opacityStep = 0.05f;
Single _opacityMax = 0.7f;
#endregion
#region --- Public properties ---
public bool FormMoveable
{
get { return _formMoveable; }
set { _formMoveable = value; }
}
public ShadowMode ActiveShadowMode
{
get { return _activeShadowMode; }
set { _activeShadowMode = value; }
}
public Color GreyOutModeBaseColor
{
get { return _greyOutModeBaseColor; }
set { _greyOutModeBaseColor = value; }
}
public int TimerInitWait
{
get { return _timerInitWait; }
set
{
if (value < 0)
_timerInitWait = 0;
else
_timerInitWait = value;
}
}
public int TimerInterval
{
get { return _timerInterval; }
set
{
if (value < 0)
_timerInterval = 0;
else
_timerInterval = value;
}
}
public int TimerIntervalSlowDownByOpacityFactor
{
get { return _timerIntervalSlowDownByOpacityFactor; }
set
{
if (value < 0)
_timerIntervalSlowDownByOpacityFactor = 0;
else
_timerIntervalSlowDownByOpacityFactor = value;
}
}
public Single OpacityStep
{
get { return _opacityStep; }
set
{
if (value < 0)
_opacityStep = 0.01f;
else
if (value > 1)
_opacityStep = 1.0f;
else
_opacityStep = value;
}
}
public Single OpacityMax
{
get { return _opacityMax; }
set
{
if (value < 0)
_opacityMax = 0;
else
if (value > 1)
_opacityMax = 1.0f;
else
_opacityMax = value;
}
}
#endregion
#region --- Private methods ---
private Image GrayScaleImage(Image image)
{
ImageAttributes imageAttributes = new ImageAttributes();
Single[][] colorMatrixAttributes;
// Greyscale
colorMatrixAttributes = new Single[][]
{
new Single[] { 0.299f, 0.299f, 0.299f, 0, 0 },
new Single[] { 0.587f, 0.587f, 0.587f, 0, 0 },
new Single[] { 0.114f, 0.114f, 0.114f, 0, 0 },
new Single[] { 0, 0, 0, 1, 0 },
new Single[] { 0, 0, 0, 0, 1 }
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixAttributes);
imageAttributes.SetColorMatrix(colorMatrix);
Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
graphics.Dispose();
imageAttributes.Dispose();
return bitmap;
}
#endregion
#region --- Protected methods ---
// Make the form unmoveable.
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x84;
const int HTCAPTION = 0x02;
const int HTCLIENT = 0x01;
base.WndProc(ref m);
if (FormMoveable) return;
if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCAPTION)
{
m.Result = (IntPtr)HTCLIENT;
}
}
#endregion
#region --- Public methods ---
public void ShowShadow ( )
{
_shadowForm.TopMost = true;
_shadowForm.FormBorderStyle = FormBorderStyle.None;
_shadowForm.Opacity = 0;
this.Owner = _shadowForm;
this.TopMost = true;
_shadowForm.Show ( );
_shadowForm.Location = Screen.GetBounds ( new Rectangle ( int.MinValue / 2, int.MaxValue / 2, int.MaxValue, int.MaxValue ) ).Location;
_shadowForm.Size = new Size ( int.MaxValue, int.MaxValue );
switch ( ActiveShadowMode )
{
case ShadowMode.GreyOut:
{
Bitmap bitmap = new Bitmap ( _shadowForm.Width, _shadowForm.Height, PixelFormat.Format32bppArgb );
using ( Graphics graphics = Graphics.FromImage ( bitmap ) )
{
graphics.CopyFromScreen ( _shadowForm.Location, new Point (0,0) , _shadowForm.Size );
}
_shadowForm.BackgroundImage = GrayScaleImage ( (Image)bitmap );
break;
}
case ShadowMode.Darken:
_shadowForm.BackColor = GreyOutModeBaseColor;
break;
}
_shadowTimer.Interval = TimerInitWait;
_shadowTimer.Tick += new EventHandler ( shadowTimer_Tick );
_shadowTimer.Enabled = true;
}
#endregion
#region --- Event handlers ---
private void shadowTimer_Tick(object sender, EventArgs e)
{
_shadowTimer.Interval = TimerInterval + (int)(_shadowForm.Opacity * TimerIntervalSlowDownByOpacityFactor);
_shadowForm.Opacity += OpacityStep;
if (_shadowForm.Opacity >= OpacityMax)
_shadowTimer.Enabled = false;
}
#endregion
#region --- Enumerators ---
public enum ShadowMode : int
{
Darken,
GreyOut
}
#endregion
}
}
Schlagwörter: Bildschirm, Dialog, Modal, verdunkeln, ausgrauen
ich ahb mal versucht das ganze als dialog result zu machen
bildschirm wird grau ... die form ist aktiv ( kann sie mit alt + f4 schließen )
aber die form ist nicht zu sehen
hier diene abgeänderte methode:
public DialogResult ShowShadowDialog()
{
_shadowForm.TopMost = true;
_shadowForm.FormBorderStyle = FormBorderStyle.None;
_shadowForm.Opacity = 0;
this.Owner = _shadowForm;
this.TopMost = true;
//wenn ich scho dialog hier einsetze wo vorher das show war geht gar ncihts .....
//_shadowForm.ShowDialog();
_shadowForm.Location = Screen.GetBounds(new Rectangle(int.MinValue / 2, int.MaxValue / 2, int.MaxValue, int.MaxValue)).Location;
_shadowForm.Size = new Size(int.MaxValue, int.MaxValue);
switch (ActiveShadowMode)
{
case ShadowMode.GreyOut:
{
Bitmap bitmap = new Bitmap(_shadowForm.Width, _shadowForm.Height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(_shadowForm.Location, new Point(0, 0), _shadowForm.Size);
}
_shadowForm.BackgroundImage = GrayScaleImage((Image)bitmap);
break;
}
case ShadowMode.Darken:
_shadowForm.BackColor = GreyOutModeBaseColor;
break;
}
_shadowTimer.Interval = TimerInitWait;
_shadowTimer.Tick += new EventHandler(shadowTimer_Tick);
_shadowTimer.Enabled = true;
return _shadowForm.ShowDialog();
}
wo liegt der fehler ?
Aus der Hüfte geschossen würde ich sagen, du musst
this.ShowDialog();
aufrufen.
Gruß,
AtzeX
Edit:
Ist aber eigentlich auch der falsche Ansatz von dir.
Du willst doch den Dialog weiterhin angezeigt bekommen, nur dass du ihn mit DialogResult.* auswerten kannst, oder?
Also bleibt in meiner Klasse alles beim Alten.
Du musst lediglich dein abgeleitetes Form überarbeiten:
Vorrausgesetzt sind zwei Buttons deren Namen du unten sehen kannst.
private void DialogShadowTopFormTest_Load(object sender, EventArgs e)
{
FormMoveable = false;
ActiveShadowMode = ShadowMode.GreyOut;
GreyOutModeBaseColor = Color.Black;
TimerInitWait = 2000;
TimerInterval = 10;
TimerIntervalSlowDownByOpacityFactor = 150;
OpacityStep = 0.05f;
OpacityMax = 0.7f;
ShowShadow();
}
private void uxButtonOk_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void uxButtonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
Aufgerufen wird dein Dialog dann z.B. so:
(vorrausesetzt dein abgeleitetes Form heißt "DialogShadowTopFormTest")
DialogShadowTopFormTest dialogShadowTopFormTest = new DialogShadowTopFormTest();
MessageBox.Show(dialogShadowTopFormTest.ShowDialog().ToString());
Mit der MessageBox kannst du den Rückgabewert direkt anzeigen lassen.
Gruß,
AtzeX
hallo
hab dass jetzt mal so auf die schnelle probiert und es funktioniert aber der bildschirm bleibt grau und es scheint noch die form offen zu sein obwohl sie nicht mehr da ist
this.Close();
wurde aber ausgeführt
hab leider keine zeit mehr werds mr abends oder morgen nochmal ansehen und die lösung posten wenn ich sie finde g
Tja, das ging mir erst genauso.
Man darf nicht übersehen, dass _shadowForm noch offen ist. Dieses muss auch geschlossen werden.
this.Close() schließt nur das eine Formular, welches von DialogShadowTopForm abgeleitet wurde.
Einfach
public void CloseShadow()
{
_shadowForm.Close();
}
einbauen und an geeigneter Stelle aufrufen.