-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCustomShadeTool.xaml.cs
104 lines (90 loc) · 3.13 KB
/
CustomShadeTool.xaml.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace PowerDimmer
{
/// <summary>
/// Interaction logic for CustomShadeTool.xaml
/// </summary>
public partial class CustomShadeTool : Window
{
public IntPtr Handle;
IntPtr _targetHandle;
private Win32.RECT rect;
private CustomShadeToolVM vm = new CustomShadeToolVM();
public Win32.CustomShadeCreatedEventDelegate _createdCallback;
public CustomShadeTool(IntPtr targetHandle, Win32.CustomShadeCreatedEventDelegate createdCallback)
{
_createdCallback = createdCallback;
//ShadeOpacity = shadeOpacity;
InitializeComponent();
this.DataContext = vm;
_targetHandle = targetHandle;
ShowInTaskbar = false;
AllowsTransparency = true;
Background = Brushes.Red;
Opacity = 0.1;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
Topmost = true;
if (_targetHandle != IntPtr.Zero)
{
rect = Win32.GetWindowRectangle(targetHandle);
Left = rect.Left;
Top = rect.Top;
Width = rect.Right - rect.Left;
Height = rect.Bottom - rect.Top;
}
}
protected override void OnSourceInitialized(EventArgs e)
{
Handle = new WindowInteropHelper(this).EnsureHandle();
var style = Win32.GetWindowLong(Handle, Win32.GWL_EXSTYLE);
Win32.SetWindowLong(Handle, Win32.GWL_EXSTYLE, style | Win32.WS_EX_LAYERED);
Win32.ShowWindow(Handle, Win32.SW_SHOWNORMAL);
base.OnSourceInitialized(e);
}
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
{
vm.isDragging = true;
vm.dragStartPos = e.GetPosition(this);
this.CaptureMouse();
e.Handled = true;
}
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (vm.isDragging)
{
Point mousePoint = e.GetPosition(this);
vm.UpdateRect(mousePoint);
e.Handled = true;
}
}
private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
vm.isDragging = false;
e.Handled = true;
if(_createdCallback != null && vm.ShadeWidth > 0 && vm.ShadeHeight > 0)
{
_createdCallback.Invoke(this, _targetHandle, vm.LeftPos, vm.TopPos, vm.ShadeWidth, vm.ShadeHeight);
}
}
}
}