1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
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
}
}
|