-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegionEditForm.cs
153 lines (120 loc) · 3.49 KB
/
RegionEditForm.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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Xnipper
{
public partial class RegionEditForm : Form
{
public RegionEditForm(Form ParentForm, Rectangle CurrentSelectionRegion)
{
InitializeComponent();
Icon = ParentForm.Icon;
Bitmap iconBmp = new Bitmap(picIcon.Width, picIcon.Height);
using (Graphics g = Graphics.FromImage(iconBmp))
g.DrawIcon(Icon, new Rectangle(0, 0, picIcon.Width, picIcon.Height));
picIcon.Image = iconBmp;
Rectangle total = new Rectangle(0, 0, 0, 0);
foreach (Screen screen in Screen.AllScreens)
{
total.Width += screen.Bounds.Width;
if (screen.Bounds.Y > 0)
total.Height += screen.Bounds.Height;
}
numX.Minimum = total.X;
numY.Minimum = total.Y;
numX.Maximum = total.Width;
numY.Maximum = total.Height;
numW.Minimum = 1;
numH.Minimum = 1;
numW.Maximum = total.Width;
numH.Maximum = total.Height;
numX.Value = CurrentSelectionRegion.X;
numY.Value = CurrentSelectionRegion.Y;
numW.Value = CurrentSelectionRegion.Width;
numH.Value = CurrentSelectionRegion.Height;
}
public bool Confirmed { get; private set; } = false;
public Rectangle ImageRegion { get; private set; }
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void DragForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, 161, 2, 0);
}
}
private void ConfirmRegion(object sender, EventArgs e)
{
Confirmed = true;
ImageRegion = new Rectangle((int)numX.Value, (int)numY.Value, (int)numW.Value, (int)numH.Value);
CancelRegion(sender, e);
}
private void CancelRegion(object sender, EventArgs e)
{
Close();
}
private void Button_MouseEnter(object sender, EventArgs e) => ((Label)sender).BackColor = Color.Gainsboro;
private void Button_MouseLeave(object sender, EventArgs e) => ((Label)sender).BackColor = Color.White;
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmIsCompositionEnabled(ref int pfEnabled);
protected override void WndProc(ref Message m)
{
if (m_aeroEnabled)
{
int v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new MARGINS
{
bottomHeight = 0,
topHeight = 1,
leftWidth = 0,
rightWidth = 0,
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
base.WndProc(ref m);
}
private bool m_aeroEnabled;
private struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
m_aeroEnabled = CheckAeroEnabled();
if (!m_aeroEnabled)
{
const int CS_DROPSHADOW = 0x20000;
cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
}
return cp;
}
}
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}
}
}