Sprache: C#
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
}
}
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
}
}
Alte URL:
/snippet/den-bildschirm-um-dialog-herum-abdunkeln-ausgrauen/626
Irgendwie versteh ich es nicht..
Kann mal jemand ein Beispiel machen, wie man das verwendet??
Wenn ich eine neue DialogShadowTopForm per
[code]DialogShadowTopForm s;[/code] erstelle sagt er beim zuweisen der Werte der Eigenschaften zurecht: „Verwendung der nicht zugewiesenen Variablen s“.
Aber man kann auch keine Instanz erstellen.??
[code]DialogShadowTopForm s = new DialogShadowTopForm()[/code]
^^Geht auch nicht was einem aber auch Auffällt, wenn man sich die Klasse mal ansieht. Steh grad echt auf der Leitung. THX
[edit] Anders vormuliert: Mit welchem „Startwet“ muss man s denn Zuweisen??
Hallo.
Wie bereits im eigentlichen Artikel beschrieben ist es eine [b]abstrakte[/b] Form-Klasse deren [b]Ableitung[/b] einen Dialog darstellt. 😉
Hier wurde das mal diskutiert:
http://www.mycsharp.de/wbb2/thread.php?threadid=43292
Thx für die Schnelle Antwort. 🙂