说这么多看上去好像很难,来让我们看看实现的代码吧。
创建透明窗体的代码
01 |
/// <summary> |
02 |
/// Initializes the surrounding windows. |
03 |
/// </summary> |
04 |
private void InitializeSurrounds() |
05 |
{ |
06 |
// Top. |
07 |
m_wndT = CreateTransparentWindow(); |
08 |
|
09 |
// Left. |
10 |
m_wndL = CreateTransparentWindow(); |
11 |
|
12 |
// Bottom. |
13 |
m_wndB = CreateTransparentWindow(); |
14 |
|
15 |
// Right. |
16 |
m_wndR = CreateTransparentWindow(); |
17 |
|
18 |
SetSurroundShadows(); |
19 |
} |
20 |
|
21 |
/// <summary> |
22 |
/// Creates an empty window. |
23 |
/// </summary> |
24 |
/// <returns></returns> |
25 |
private static Window CreateTransparentWindow() |
26 |
{ |
27 |
Window wnd = new Window(); |
28 |
wnd.AllowsTransparency = true; |
29 |
wnd.ShowInTaskbar = false; |
30 |
wnd.WindowStyle = WindowStyle.None; |
31 |
wnd.Background = null; |
32 |
|
33 |
return wnd; |
34 |
} |
35 |
|
36 |
/// <summary> |
37 |
/// Sets the artificial drop shadow. |
38 |
/// </summary> |
39 |
/// <param name="active">if set to <c>true</c> [active].</param> |
40 |
private void SetSurroundShadows(Boolean active = true) |
41 |
{ |
42 |
if (active) |
43 |
{ |
44 |
Double cornerRadius = 1.75; |
45 |
|
46 |
m_wndT.Content = GetDecorator( |
47 |
"Images/ACTIVESHADOWTOP.PNG"); |
48 |
m_wndL.Content = GetDecorator( |
49 |
"Images/ACTIVESHADOWLEFT.PNG", cornerRadius); |
50 |
m_wndB.Content = GetDecorator( |
51 |
"Images/ACTIVESHADOWBOTTOM.PNG"); |
52 |
m_wndR.Content = GetDecorator( |
53 |
"Images/ACTIVESHADOWRIGHT.PNG", cornerRadius); |
54 |
} |
55 |
else |
56 |
{ |
57 |
m_wndT.Content = GetDecorator( |
58 |
"Images/INACTIVESHADOWTOP.PNG"); |
59 |
m_wndL.Content = GetDecorator( |
60 |
"Images/INACTIVESHADOWLEFT.PNG"); |
61 |
m_wndB.Content = GetDecorator( |
62 |
"Images/INACTIVESHADOWBOTTOM.PNG"); |
63 |
m_wndR.Content = GetDecorator( |
64 |
"Images/INACTIVESHADOWRIGHT.PNG"); |
65 |
} |
66 |
} |
67 |
|
68 |
[DebuggerStepThrough] |
69 |
private Decorator GetDecorator(String imageUri, Double radius = 0) |
70 |
{ |
71 |
Border border = new Border(); |
72 |
border.CornerRadius = new CornerRadius(radius); |
73 |
border.Background = new ImageBrush( |
74 |
new BitmapImage( |
75 |
new Uri(BaseUriHelper.GetBaseUri(this), |
76 |
imageUri))); |
77 |
|
78 |
return border; |
79 |
} |
计算位置高度的代码
01 |
/// <summary> |
02 |
/// Raises the <see cref="FrameworkElement.Initialized"/> event. |
03 |
/// This method is invoked whenever |
04 |
/// <see cref="FrameworkElement.IsInitialized"/> |
05 |
/// is set to true internally. |
06 |
/// </summary> |
07 |
/// <param name="e">The <see cref="T:RoutedEventArgs"/> |
08 |
/// that contains the event data.</param> |
09 |
protected override void OnInitialized(EventArgs e) |
10 |
{ |
11 |
// ... |
12 |
|
13 |
LocationChanged += HandleLocationChanged; |
14 |
SizeChanged += HandleLocationChanged; |
15 |
StateChanged += HandleWndStateChanged; |
16 |
|
17 |
InitializeSurrounds(); |
18 |
ShowSurrounds(); |
19 |
|
20 |
base.OnInitialized(e); |
21 |
} |
22 |
|
23 |
/// <summary> |
24 |
/// Handles the location changed. |
25 |
/// </summary> |
26 |
/// <param name="sender">The sender.</param> |
27 |
/// <param name="e">The <see cref="System.EventArgs"/> |
28 |
/// instance containing the event data.</param> |
29 |
private void HandleLocationChanged(Object sender, EventArgs e) |
30 |
{ |
31 |
m_wndT.Left = Left - c_edgeWndSize; |
32 |
m_wndT.Top = Top - m_wndT.Height; |
33 |
m_wndT.Width = Width + c_edgeWndSize * 2; |
34 |
m_wndT.Height = c_edgeWndSize; |
35 |
|
36 |
m_wndL.Left = Left - m_wndL.Width; |
37 |
m_wndL.Top = Top; |
38 |
m_wndL.Width = c_edgeWndSize; |
39 |
m_wndL.Height = Height; |
40 |
|
41 |
m_wndB.Left = Left - c_edgeWndSize; |
42 |
m_wndB.Top = Top + Height; |
43 |
m_wndB.Width = Width + c_edgeWndSize * 2; |
44 |
m_wndB.Height = c_edgeWndSize; |
45 |
|
46 |
m_wndR.Left = Left + Width; |
47 |
m_wndR.Top = Top; |
48 |
m_wndR.Width = c_edgeWndSize; |
49 |
m_wndR.Height = Height; |
50 |
} |
51 |
|
52 |
/// <summary> |
53 |
/// Handles the windows state changed. |
54 |
/// </summary> |
55 |
/// <param name="sender">The sender.</param> |
56 |
/// <param name="e">The <see cref="System.EventArgs"/> |
57 |
/// instance containing the event data.</param> |
58 |
private void HandleWndStateChanged(Object sender, EventArgs e) |
59 |
{ |
60 |
if (WindowState == WindowState.Normal) |
61 |
{ |
62 |
ShowSurrounds(); |
63 |
} |
64 |
else |
65 |
{ |
66 |
HideSurrounds(); |
67 |
} |
68 |
} |
