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
}
}