-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
158 lines (127 loc) · 4.12 KB
/
MainForm.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
using CSDeskBand;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace HDF.DeskBand
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetShadows(this);
}
#region 边框阴影
[EditorBrowsable(EditorBrowsableState.Never)]
public struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
[DllImport("dwmapi.dll")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
public static void SetShadows(Form form)
{
if (form.FormBorderStyle != FormBorderStyle.None)
return;
var v = 2;
DwmSetWindowAttribute(form.Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 0,
rightWidth = 0,
topHeight = 0
};
DwmExtendFrameIntoClientArea(form.Handle, ref margins);
}
#endregion
#region 窗口控制
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
//case 0x4e:
//case 0xd:
//case 0xe:
//case 0x14:
// base.WndProc(ref m);
// break;
case 0x84://鼠标点任意位置后可以拖动窗体
this.DefWndProc(ref m);
if (m.Result.ToInt32() == 0x01)
{
m.Result = new IntPtr(0x02);
}
break;
case 0xA3://禁止双击最大化
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
if (!IsFixed)
this.Visible = false;
}
#endregion
public bool IsFixed => cb_Fixed.Checked;
public TextBox TxtKey => txt_Key;
public TextBox TxtDetail => txt_Target;
public void ShowOrHide(Edge edge, Point p, Size s)
{
this.Visible = !this.Visible;
if (this.Visible)
{
switch (edge)
{
case Edge.Left:
this.Left = s.Width;
this.Top = p.Y - this.Height;
break;
case Edge.Top:
this.Left = p.X + s.Width - this.Width;
this.Top = s.Height;
break;
case Edge.Right:
this.Left = p.X - this.Width;
this.Top = p.Y - this.Height;
break;
case Edge.Bottom:
this.Left = p.X + s.Width - this.Width;
this.Top = p.Y - this.Height;
break;
}
this.Activate();
this.Focus();
this.TxtKey.Focus();
}
}
private void txt_Key_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
var str = txt_Key.Text.Trim();
if (string.IsNullOrEmpty(str))
return;
Clipboard.SetText(str);
e.Handled = true;
}
else if (e.KeyCode == Keys.Escape)
{
this.Visible = !this.Visible;
}
}
}
}