BasePnl.cs
3.7 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asa.Face
{
public partial class BasePnl : Panel
{
private bool _inside = false;
private int _borderW = 2;
public BasePnl()
{
InitializeComponent();
Padding = new Padding(6);
}
/// <summary>
/// 鼠标焦点在控件里面
/// </summary>
[Browsable(false)]
public bool Inside
{
set
{
if (_inside != value)
{
_inside = value;
Refresh();
}
}
get
{
return _inside;
}
}
[Browsable(false)]
public Rectangle BorderRect { set; get; }
[Browsable(false)]
public Pen BorderInsidePen { set; get; }
[Browsable(false)]
public Pen BorderOutsidePen { set; get; }
[Browsable(true), DefaultValue(2)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int BorderWidth
{
set
{
if (value < 0)
_borderW = 0;
else
_borderW = value;
CalcSize();
Refresh();
}
get
{
return _borderW;
}
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
}
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
CalcSize();
Refresh();
}
}
[Browsable(false)]
public Bitmap ToImage()
{
IntPtr hSrce = API.GetWindowDC(Handle);
IntPtr hDest = API.CreateCompatibleDC(hSrce);
IntPtr hBmp = API.CreateCompatibleBitmap(hSrce, Width, Height);
IntPtr hOldBmp = API.SelectObject(hDest, hBmp);
if (API.BitBlt(hDest, 0, 0, Width, Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
{
Bitmap bmp = Image.FromHbitmap(hBmp);
API.SelectObject(hDest, hOldBmp);
API.DeleteObject(hBmp);
API.DeleteDC(hDest);
API.ReleaseDC(Handle, hSrce);
return bmp;
}
return null;
}
/// <summary>
/// 计算大小
/// </summary>
protected virtual void CalcSize()
{
BorderRect = new Rectangle(_borderW / 2, _borderW / 2, Width - _borderW, Height - _borderW);
BorderInsidePen = new Pen(Common.BLUE_COLOR, _borderW);
BorderOutsidePen = new Pen(Common.BORDER_COLOR, _borderW);
}
private void BasePnl_Resize(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}
}
}