forked from vpuhoff/Athena-NeuroPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphaForm.cs
1497 lines (1302 loc) · 46.9 KB
/
AlphaForm.cs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Copyright (c) Jeff Anderson
/*
Copyright (c) 2009 Jeff Anderson of Braid Art Labs (www.groboto.com)
This code is released under the Code Project
Open License 1.02. See cpol.html for
license details. NOTE: cpol.html MUST be included
with any source code distribution of this code.
*/
#endregion
#region Version 1.1.3
// Version 1.0 Initial CodeProject Release
// Version 1.1 Initial Commercial Release
// 1. Added AlphaFormTransformer.Fade() for fade in/out functionality
// To use, override OnShown() and OnClosing() and add call to Fade().
// If fading in, main form Opacity property should be set to 0
// in advance AND for fading in you must pass 0 as the
// argument to TransformForm() to set the initial layered form opacity.
// See the examples "TestFormGrobotoTV" or "ControlsOnTopOfSkin" projects
// as they both use Fade().
// 2. Reworked OnPaint override in AlphaFormTransformer (see method summary
// & PowerToys issue)
// 3. Added CanDrag property. If true user can click and drag on
// background of AFT to drag the composite form (see property summary
// for more details)
// 4. Added example project "ControlsOnTopOfSkin" showing how to create
// a form where the controls appear on top of the background image
// (uses the new CanDrag property). The basic idea is to set the
// AFT's background image to one where the alpha channel cuts out
// the control region, and then set the main form's background to the
// same image without the cutout region.
// 5. Fixed issues with main form scaling set to DPI and running under
// different DPI's. Added scaling of main form background image (which
// becomes the AFT's background at runtime).
// We also adjust the region fillBorder for the AFM markers if DPI scaling
// is needed. This is necessary as image rescaling adds or removes to the
// semitransparent border width of the alpha channel.
// 6. Reorganized solution layout with all sample projects in "Test Projects"
// folder.
// Version 1.1.1 - CodeProject License Release
// 1. Changed primary namespace and refactored.
// Version 1.1.2 (beta)
// 1. Fixed SetWindowLong PInvoke signature to be 64 bit compatible, but
// we have not been able to test this component on X64.
// 2. Impulse class made public as it's useful for dampened motion if the user
// wants to animate (either the location or opacity) an alpha transformed window.
// For example see the SlidingPanes project.
// Version 1.1.3 (beta)
// 1. Implementing a Visible change event on the main form and keeping the layered
// window Visible propery in sync was a bad idea. In this component we need to manipulate
// Visible separately for the fade routine so we removed it.
// 2. Added SetAlphaAndParentFormVisible and SetAlphaAndParentFormOpacity utility routines.
// 3. Re-worked the SlidingPanes example project to better illustrate animating the drawer and bug fix.
#endregion
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Imported methods and structs
/// </summary>
internal class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct SIZE
{
public Int32 cx;
public Int32 cy;
public SIZE(Int32 x, Int32 y)
{
cx = x;
cy = y;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
public POINT(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct COLORREF
{
public byte R;
public byte G;
public byte B;
public byte A;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public const byte AC_SRC_OVER = 0;
public const Int32 ULW_ALPHA = 2;
public const byte AC_SRC_ALPHA = 1;
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);
[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int DeleteObject(IntPtr hObj);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr ExtCreateRegion(IntPtr lpXform, uint nCount, IntPtr rgnData);
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags);
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern uint GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out uint pwdFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int InvalidateRgn(IntPtr hWnd, IntPtr hRgn, int bErase);
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int ValidateRgn(IntPtr hWnd, IntPtr hRgn);
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int ValidateRect(IntPtr hWnd, ref RECT rct);
public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}
[SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable")]
[DllImport("user32", EntryPoint = "SetWindowLong")]
public static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
[DllImport("user32", EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
public static IntPtr SetWindowLong(IntPtr hWnd, short nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return SetWindowLongPtr64(hWnd, (int)nIndex, dwNewLong);
}
public static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
{
if (IntPtr.Size == 4)
{
return GetWindowLong32(hWnd, nIndex);
}
return GetWindowLongPtr64(hWnd, nIndex);
}
[DllImport("user32", EntryPoint = "GetWindowLong")]
public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);
[SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
[DllImport("user32", EntryPoint = "GetWindowLongPtr")]
public static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
}
/// <summary>
/// Defines a line segment. Used by AlphaFormTransformer.UpdateRectListFromAlpha()
/// </summary>
internal struct LineSeg
{
public int x1;
public int x2;
public int y;
public int dy;
public LineSeg(int x1,int x2,int y,int dy)
{
this.x1 = x1;
this.x2 = x2;
this.y = y;
this.dy = dy;
}
}
/// <summary>
/// A simple pulse function with exponential dampening
/// input [0..1], output [0..1]
/// Function is steep at beginning and flat at end for Dampening > 2
/// and lesser values not really useful
/// The greater the dampening the steeper the initial ramp
/// If you want it flat at the beginning then call 1.0 - Evaluate(1.0-t)
/// </summary>
public class Impulse {
#region Class Variables
double m_pScale;
double m_pNorm = 1.0;
#endregion
#region Contructors
public Impulse(double s)
{
m_pNorm = 1.0;
m_pScale = s;
}
#endregion
#region Properties
public double Dampening
{
get
{
return m_pScale;
}
set
{
m_pScale = value;
}
}
#endregion
#region Methods
void UpdateScale()
{
m_pNorm = 1.0/EvalInternal(1.0);
}
double EvalInternal(double t)
{
double val;
t = t * m_pScale;
if (t < 1.0)
{
val = t - (1 - Math.Exp(-t));
}
else
{
double start = Math.Exp(-1.0);
t -= 1.0;
double expx = 1 - Math.Exp(-t);
val = start + (expx * (1.0 - start));
}
return val * m_pNorm;
}
public void Reset()
{
m_pNorm = 1.0;
}
public double Evaluate(double t)
{
if (t >= 1.0)
return 1.0;
if (t <= 0.0)
return 0.0;
if (m_pNorm == 1.0)
UpdateScale();
return EvalInternal(t);
}
#endregion
}
/// <summary>
/// This window sets the WS_EX_LAYERED extended style with
/// a method to set the layered window bitmap
/// </summary>
internal class LayeredWindowForm : Form
{
#region Constructors
public LayeredWindowForm()
{
FormBorderStyle = FormBorderStyle.None;
}
#endregion
private
object theobj;
#region Overrides
/// <summary>
/// This window can be the active window when you click
/// to drag it, and it can receive a close event from
/// the system (e.g., user clicks Alt+F4), therefore we
/// instruct the owner to close and cancel the close for
/// this window.
/// </summary>
///
protected void tstlock()
{
lock(theobj) {
theobj = null;
}
}
protected override void OnClosing(CancelEventArgs e)
{
if (Owner != null)
{
e.Cancel = true;
base.OnClosing(e);
Owner.Close();
}
else
base.OnClosing(e);
}
protected override void OnHandleCreated(EventArgs e)
{
InitializeStyles();
base.OnHandleCreated(e);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cParms = base.CreateParams;
cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
return cParms;
}
}
#endregion
#region Methods
public void SetBits(Bitmap bitmap, byte opacity)
{
if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
throw new ApplicationException("The bitmap must be 32 bits per pixel with an alpha channel.");
IntPtr oldBits = IntPtr.Zero;
IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
IntPtr hBitmap = IntPtr.Zero;
IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
Win32.POINT topLoc = new Win32.POINT(Left, Top);
Win32.SIZE bitMapSize = new Win32.SIZE(bitmap.Width, bitmap.Height);
Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
Win32.POINT srcLoc = new Win32.POINT(0, 0);
blendFunc.BlendOp = Win32.AC_SRC_OVER;
blendFunc.SourceConstantAlpha = opacity;
blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
blendFunc.BlendFlags = 0;
try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBits = Win32.SelectObject(memDc, hBitmap);
Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
}
finally
{
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBits);
Win32.DeleteObject(hBitmap);
}
Win32.ReleaseDC(IntPtr.Zero, screenDC);
Win32.DeleteDC(memDc);
}
}
private void InitializeStyles()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
UpdateStyles();
}
#endregion
}
namespace AlphaForm
{
/// <summary>
/// Fade type
/// </summary>
public enum FadeType
{
/// <summary>
/// Fade from 0% to 100% opacity
/// </summary>
FadeIn,
/// <summary>
/// Fade from 100% to 0% opacity
/// </summary>
FadeOut
};
/// <summary>
/// AlphaFormMarker serves as a design time aid for specifying
/// one or more points in the background image that will be used
/// to build the main form's Region. In the designer, it must
/// always be added to the AlphaFormTransformer control (not the main form)
/// </summary>
/// <remarks>
/// At runtime all instances of AlphaFormMarker are made invisible.
/// </remarks>
[ToolboxBitmap(typeof(AlphaFormMarker),"marker.png")]
public class AlphaFormMarker : UserControl
{
#region Class Variables
uint m_fillBorder = 4;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public AlphaFormMarker()
{
Bounds = new Rectangle(Location, new Size(17, 17));
}
#endregion
#region Overrides
/// <summary>
/// Overriden for internal purposes
/// </summary>
protected override void OnHandleCreated(EventArgs e)
{
InitializeStyles();
base.OnHandleCreated(e);
}
/// <summary>
/// Draws the cross-hairs
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
SolidBrush sb = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
Pen p = new Pen(sb, 1);
e.Graphics.DrawEllipse(p, new Rectangle(0, 0, ClientSize.Width - 1, ClientSize.Height - 1));
e.Graphics.DrawLine(p, Bounds.Width / 2, 0, Bounds.Width / 2, Bounds.Height);
e.Graphics.DrawLine(p, 0, Bounds.Height / 2, Bounds.Width, Bounds.Height / 2);
p.Dispose();
sb.Dispose();
}
#endregion
#region Properties
/// <summary>
/// The marker's FillBorder property specifies how far into the non-transparent pixel
/// border the region will be constructed. The composited image will typically have
/// semitransparent edges. Therefore the region that's built from the marker position needs
/// to expand some number of pixels into (and *under* from the compositing point of view)
/// the semitransparent area, otherwise you will see through to the desktop along these borders.
/// You want the border value to be large enough to cover the thickness of the semitransparent
/// edge (typically a couple pixels), but not too large which might cause it to extend past
/// the other side of the image frame.
/// </summary>
/// <value>Sets or gets pixel region fill border for this marker</value>
[Category("Marker Properties"), Description("Fill Border (Pixels)")]
[DefaultValue(4)]
public uint FillBorder
{
get
{
return m_fillBorder;
}
set
{
m_fillBorder = value;
}
}
#endregion
#region Methods
void InitializeStyles()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
UpdateStyles();
}
#endregion
}
/// <summary>
/// A single instance of AlphaFormTransformer is added to the form
/// to be transformed to an alpha channel border window. It hosts all
/// controls to be displayed on the window, the image for the layered
/// window that displays the image border, and one or more AlphaFormMarkers
/// to construct the main form's region.
/// </summary>
[ToolboxBitmap(typeof(AlphaFormTransformer), "Transformer.png")]
public class AlphaFormTransformer : Panel
{
#region Class Variables
bool m_drag = false;
Point m_dragStart;
LayeredWindowForm m_lwin = null;
Bitmap m_alphaBitmap = null;
uint m_dragSleep = 30;
bool m_fading = false;
bool m_transFormerDrags = false;
bool m_powerToysFix = false;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public AlphaFormTransformer()
{
Size = new Size(250, 250);
}
#endregion
#region Overrides
/// <summary>
/// Overriden for internal use.
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_fading) return; // here just to avoid the compiler warning about unused var.
if (m_powerToysFix && m_alphaBitmap != null && m_lwin != null && System.Environment.OSVersion.Version.Major < 6)
m_lwin.SetBits(m_alphaBitmap,(byte)(ParentForm.Opacity*255));
}
#endregion
#region Properties
/// <summary>
/// When moving the form, this control tries to help the desktop and underlying Windows catch up to
/// redrawing invalidated areas (we're actually moving two Windows although the layered
/// Window of the pair is double buffered). If it detects we're dragging the Form, it sleeps the main
/// thread according to this property. A reasonable value like 30 milliseconds makes for less
/// distracting redrawing of the invalidated parts of the desktop. Under Vista this property is
/// ignored as the DWM double buffers everything.
/// </summary>
/// <value>Gets or sets the drag sleep delay in milliseconds</value>
[Category("Alpha Transformer Properties"), Description("Drag Sleep in Milliseconds (Ignored on Vista)")]
[DefaultValue(30)]
public uint DragSleep
{
get
{
return m_dragSleep;
}
set
{
m_dragSleep = value;
}
}
/// <summary>
/// Microsoft's "PowerToys" Alt+Tab extension which replaces the default
/// Alt+Tab functionality has issues with some video cards and layered windows
/// windows and also with any window that has an owner. When
/// the user press Alt+Tab, it shows snapshots for owned windows which
/// it should not (a design flaw really), and in our case that
/// includes our composited frame window which hosts the bitmap. Second in our
/// labs on machines with ATI video cards, it does not
/// capture the composited window contents correctly, and somehow corrupts the
/// buffered contents. So if the user Alt+Tabs to the frame window, it
/// will all of a sudden turn to a semi-transparent white box. As a
/// work-around to the corruption you can set PowerToysFix to true
/// at the cost of unnecessarily refreshing the layered window contents (normally double
/// buffered by the OS) when this control gets a paint message.
/// </summary>
/// <value>Gets or sets the PowerToys fix flag</value>
[Category("Alpha Transformer Properties"), Description("PowerToys fix for XP")]
[DefaultValue(false)]
public bool PowerToysFix
{
get
{
return m_powerToysFix;
}
set
{
m_powerToysFix = value;
}
}
private Form ParentForm
{
get
{
return (Form)TopLevelControl;
}
}
/// <summary>
/// True if mouse drag on this control drags the entire form (meaning the frame window and main
/// form together). This will typically be used when a background image is set to the main form and
/// the user wants controls on top of the bitmap. In this case being able to drag by clicking on the
/// background is an assumption most users will make.
/// </summary>
/// <value>Gets or sets the CanDrag property</value>
[Category("Alpha Transformer Properties"), Description("If true user can drag form by dragging this control")]
[DefaultValue(false)]
public bool CanDrag
{
get
{
return m_transFormerDrags;
}
set
{
m_transFormerDrags = value;
}
}
#endregion
#region Methods
/// <summary>
/// This sets the Visible property of the form and layered window
/// </summary>
/// <param name="v">
/// Visible value
/// </param>
public void SetAlphaAndParentFormVisible(bool v)
{
ParentForm.Visible = v;
m_lwin.Visible = v;
}
/// <summary>
/// This sets the Opacity of the form and layered window
/// </summary>
/// <param name="v">
/// Visible value
/// </param>
public void SetAlphaAndParentFormOpacity(double v)
{
ParentForm.Opacity = v;
m_lwin.SetBits(m_alphaBitmap, Math.Min((byte)(v*255),(byte)255));
}
/// <summary>
/// This methods does the following:
/// <list type="number">
/// <item>
/// <description>Sets the frame window image data from <paramref name="frameBmap"/> and
/// scales as needed.</description>
/// </item>
/// <item>
/// <description>Constructs the main form's Region from the AlphaFormMarkers in
/// this control.</description>
/// </item>
/// <item>
/// <description>Updates the frame window's bitmap.</description>
/// </item>
/// <item>
/// <description>Sets the main form's background image to <paramref name="backBmap"/> with scaling if
/// needed.</description>
/// </item>
/// </list>
/// </summary>
/// <param name="frameBmap">
/// Bitmap to repace current frame image
/// </param>
/// <param name="backBmap">
/// Bitmap to replace the current background image
/// </param>
/// <param name="opacity">
/// Opacity [0..255] of composite form (both the frame and background)
/// </param>
/// <remarks>
/// <para>
/// If this routine is used at runtime to change the alpha channel image,
/// it does nothing to resize the main form, this control, or the
/// the AlphaFormMarkers. If the user intends to update the image
/// with one that has a *different* alpha channel, he'll need to do the
/// following BEFORE calling this routine:
/// </para>
/// <list type="number">
/// <item>
/// <description>If the image size has changed, resize the main form (and this control if it's not docked)</description>
/// </item>
/// <item>
/// <description>Reposition the AlphaFormMarker controls (potentially adding new
/// ones) as needed so the form Region can be calculated.</description>
/// </item>
/// <item>
/// <description>Reposition any other controls on the form.</description>
/// </item>
/// </list>
/// On the other hand, if we're simply swapping in an image with the same
/// size and alpha channel, then simply call this routine.
/// </remarks>
public void UpdateSkin(Bitmap frameBmap, Bitmap backBmap, byte opacity)
{
// From an internal perspective, frameBmap is the layered window contents
// and backBmap becomes (potentially after scaling) the transformer control
// BackgroundImage.
if (frameBmap == null && backBmap == null)
throw new ApplicationException("Must specify at least one bitmap to UpdateSkin()");
if (!Bitmap.IsCanonicalPixelFormat(frameBmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(frameBmap.PixelFormat))
throw new ApplicationException("The frame bitmap must be 32 bits per pixel with an alpha channel.");
double
sizeFactor = 1.0;
if (frameBmap != null)
{
// A prerequisite with this control is that the main form's size
// is set equal to the background image size. However, if the main form's
// AutoScaleMode is set to DPI, and the application is run
// on a system where the font or DPI resolution differs from the design
// time values, then .NET will scale the form and all its controls. However
// the background image is not scaled, so we'll catch that
// condition here and scale the image accordingly. Again, this logic works
// *assuming* you always set the main form's size equal to the image size
// at design time.
if (frameBmap.Size != ParentForm.Size)
{
m_alphaBitmap = new Bitmap(ParentForm.Width, ParentForm.Height);
Graphics gr = Graphics.FromImage(m_alphaBitmap);
gr.SmoothingMode = SmoothingMode.None;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBilinear;
gr.DrawImage(frameBmap, new Rectangle(0, 0, ParentForm.Width, ParentForm.Height),
new Rectangle(0, 0, frameBmap.Width, frameBmap.Height), GraphicsUnit.Pixel);
gr.Dispose();
sizeFactor = Math.Max((double)ParentForm.Size.Width / frameBmap.Size.Width,
(double)ParentForm.Size.Height/frameBmap.Size.Height);
}
else
m_alphaBitmap = frameBmap;
// Need to adjust the region fill border as scaling up will introduce
// semi-transparent pixels along edges
if (frameBmap.Size != ParentForm.Size && ParentForm.BackgroundImage != null)
{
foreach (Control cntrl in Controls)
{
if (typeof(AlphaFormMarker).IsInstanceOfType(cntrl))
{
AlphaFormMarker marker = (AlphaFormMarker)cntrl;
marker.FillBorder = (uint)Math.Round((marker.FillBorder+1.5) * sizeFactor);
}
}
}
}
// Build a array of the alpha values taken from the background image.
// Some of the transparent pixels in this array will be the target of
// what is essentially a seed fill operation in UpdateRectListFromAlpha().
// As some members are set to a non-zero value (i.e., filled), corresponding
// rectangles are added to a running arraylist of rectangles that will
// eventually be used to build the main form's Region.
BitmapData bData = m_alphaBitmap.LockBits(
new Rectangle(0, 0, m_alphaBitmap.Width, m_alphaBitmap.Height),
ImageLockMode.ReadOnly,
m_alphaBitmap.PixelFormat);
byte[,] alphaArr = new byte[m_alphaBitmap.Width, m_alphaBitmap.Height];
#if FAST_ALPHA_BUILD
// Here is an optional way to build the alpha array. It's
// fast but requires pointer access.
unsafe
{
byte *line = (byte*)bData.Scan0;
byte *alpha;
for (int j = 0; j < m_alphaBitmap.Height; j++, line += bData.Stride)
{
// image data is r->g->b->a in memory
alpha = line + 3;
for (int i = 0; i < m_alphaBitmap.Width; i++, alpha += 4)
alphaArr[i,j] = *alpha;
}
}
#else
// Seems faster albeit somewhat wasteful to marshal to a
// managed array than to use Bitmap.GetPixel. If you're
// OK with unsafe code, then define FAST_ALPHA_BUILD and
// enable unsafe compilation
byte[] mngImgData = new byte[m_alphaBitmap.Height * bData.Stride];
Marshal.Copy(bData.Scan0, mngImgData, 0, mngImgData.Length);
for (int j = 0; j < m_alphaBitmap.Height; j++)
{
int ai = j * bData.Stride + 3;
for (int i = 0; i < m_alphaBitmap.Width; i++, ai += 4)
{
alphaArr[i, j] = mngImgData[ai];
}
}
#endif
m_alphaBitmap.UnlockBits(bData);
Rectangle bounds = new Rectangle();
ArrayList rectList = new ArrayList();
// The location of each AlphaFormMarker control serves as a seed point location
// for building the set of rectangles that describe the enclosed transparent region
// (within the background image's alpha channel) around that point.
foreach (Control cntrl in Controls)
{
if (typeof(AlphaFormMarker).IsInstanceOfType(cntrl))
{
AlphaFormMarker marker = (AlphaFormMarker)cntrl;
UpdateRectListFromAlpha(
rectList,
ref bounds,
new Point(marker.Location.X + marker.Width / 2, marker.Location.Y + marker.Height / 2),
alphaArr,
m_alphaBitmap.Width,
m_alphaBitmap.Height,
(int)marker.FillBorder);
// Hide the marker as we don't want to see it at run time.
marker.Visible = false;
}
}
// Build the main form's region
ParentForm.Region = RegionFromRectList(rectList, bounds);
// Set the layered window bitmap
m_lwin.SetBits(m_alphaBitmap,opacity);
// Swap in the main form's background image (if any) and layout
BackgroundImage = backBmap;
BackgroundImageLayout = ParentForm.BackgroundImageLayout;
if (backBmap != null)
{
ParentForm.BackgroundImage = null;
// Like above, we rescale if needed...
if (backBmap.Size != ParentForm.Size)
{
Bitmap backScaled = new Bitmap(ParentForm.Width, ParentForm.Height);
Graphics gr = Graphics.FromImage(backScaled);
gr.SmoothingMode = SmoothingMode.None;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBilinear;
gr.DrawImage(backBmap, new Rectangle(0, 0, ParentForm.Width, ParentForm.Height),
new Rectangle(0, 0, BackgroundImage.Width, BackgroundImage.Height), GraphicsUnit.Pixel);
gr.Dispose();
BackgroundImage = backScaled;
}
}
ParentForm.Opacity = opacity / 255.0;
}
/// <summary>
/// This methods does the following:
/// <list type="number">
/// <item>
/// <description>Constructs a desktop composited frame window and shows it. This window's
/// bitmap is built from this control's <c>BackgroundImage</c>.</description>
/// </item>
/// <item>
/// <description>Calls <c>UpdateSkin</c> to updated the frame window's image data
/// and build the main form's Region.</description>
/// </item>
/// <item>
/// <description>The BackgroundImage is set to the main form's Background image.</description>
/// </item>
/// </list>
/// <para>
/// This method should only be called *once* from the main form's
/// Load event. If the user wants to change the image at runtime,
/// <see cref="AlphaFormTransformer.UpdateSkin" /> should be used.
/// </para>
/// </summary>
/// <param name="opacity">
/// Opacity of form [0..255]
/// </param>
public void TransformForm(byte opacity)
{
if (DesignMode)
return;
#if _DEMO
MessageBox.Show("AlphaForm Transformer Demo Mode.\n\nPlease see www.alpha-forms.com for information","Demo Mode");
#endif
m_lwin = new LayeredWindowForm();
// Setting the layered window's TopMost to the main
// form's value keeps the relative Z order the same for
// the pair of windows
m_lwin.TopMost = ParentForm.TopMost;
// We don't want the layered window form to show in the
// taskbar now do we :-)
m_lwin.ShowInTaskbar = false;
// These will handle dragging for both the layered window
// and the main form.
m_lwin.MouseDown += new MouseEventHandler(LayeredFormMouseDown);
m_lwin.MouseMove += new MouseEventHandler(LayeredFormMouseMove);
m_lwin.MouseUp += new MouseEventHandler(LayeredFormMouseUp);
if (m_transFormerDrags)
{
MouseDown += new MouseEventHandler(FormMouseDown);
MouseMove += new MouseEventHandler(FormMouseMove);
MouseUp += new MouseEventHandler(FormMouseUp);
}
ParentForm.Move += new EventHandler(ParentFormMove);
// Form must be shown by specifying an owner so that activation of
// the layered form activates the main app. It's also needed to keep
// the Z order of the two windows in sync. Note that it's not
// necessary to set the size of the layered window. The call to
// SetBits() does this.
m_lwin.Visible = false;
m_lwin.Show(ParentForm);
m_lwin.Location = ParentForm.Location;
ParentForm.Opacity = opacity/255.0;
if (BackgroundImage != null)
{
// Update the layered window bits and form region
UpdateSkin(
new Bitmap(BackgroundImage),
ParentForm.BackgroundImage != null ? new Bitmap(ParentForm.BackgroundImage) : null,
opacity);
m_lwin.Visible = true;
if (BackColor == Color.Transparent)
{
// user may have it this way in the designer too see the
// main form's background image, but it can't be set to
// this at runtime as the background image will be copied from
// the main form to this control. Even in the absence of a background
// image, it can't be transparent as it won't composite correctly