详解C#创建不规则窗体的几种方式Word下载.docx
《详解C#创建不规则窗体的几种方式Word下载.docx》由会员分享,可在线阅读,更多相关《详解C#创建不规则窗体的几种方式Word下载.docx(12页珍藏版)》请在冰点文库上搜索。
![详解C#创建不规则窗体的几种方式Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/7/adac6aac-8074-4412-8c1c-06cd66169c12/adac6aac-8074-4412-8c1c-06cd66169c121.gif)
BitmapRegion()
4.
{
}
5.
7.
///
summary>
8.
Create
and
apply
the
region
on
supplied
control
9.
创建支持位图区域的控件(目前有button和form)
10.
/summary>
11.
param
name=”control”>
The
Control
object
to
to控件<
/param>
12.
name=”bitmap”>
Bitmap
create
from位图<
13.
static
void
CreateControlRegion(Control
control,
bitmap)
14.
{
15.
//
Return
if
control
bitmap
are
null
16.
//判断是否存在控件和位图
17.
(control
==
null
||
null)
18.
return;
19.
20.
Set
our
control’’s
size
be
same
as
bitmap
21.
//设置控件大小为位图大小
22.
control.Width
bitmap.Width;
23.
control.Height
bitmap.Height;
24.
Check
we
dealing
with
Form
here
25.
//当控件是form时
26.
is
System.Windows.Forms.Form)
27.
28.
Cast
a
object
29.
//强制转换为FORM
30.
form
(Form)control;
31.
form’’s
little
larger
that
just
32.
in
case
border
style
not
set
none
first
place
33.
//当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点
34.
form.Width
control.Width;
35.
form.Height
control.Height;
36.
No
37.
//没有边界
38.
form.FormBorderStyle
FormBorderStyle.None;
39.
background
image
40.
//将位图设置成窗体背景图片
41.
form.BackgroundImage
bitmap;
42.
Calculate
graphics
path
based
43.
//计算位图中不透明部分的边界
44.
GraphicsPath
graphicsPath
CalculateControlGraphicsPath(bitmap);
45.
Apply
46.
//应用新的区域
47.
form.Region
Region(graphicsPath);
48.
49.
Button
50.
//当控件是button时
51.
else
System.Windows.Forms.Button)
52.
53.
button
54.
//强制转换为
button
55.
(Button)control;
56.
Do
show
text
57.
//不显示button
text
58.
button.Text
“”;
59.
60.
Change
cursor
hand
when
over
61.
//改变
cursor的style
62.
button.Cursor
Cursors.Hand;
63.
of
64.
//设置button的背景图片
65.
button.BackgroundImage
66.
67.
68.
69.
70.
71.
72.
button.Region
73.
74.
75.
76.
representing
figure
77.
excluding
transparent
color
which
top
left
pixel.
78.
79.
80.///
calculate
from<
81.
returns>
Calculated
path<
/returns>
82.
private
CalculateControlGraphicsPath(Bitmap
83.
84.
for
calculation
85.
//创建
GraphicsPath
86.
GraphicsPath();
87.
Use
pixel
88.
//使用左上角的一点的颜色作为我们透明色
89.
Color
colorTransparent
bitmap.GetPixel(0,
0);
90.
This
store
column
value
where
an
opaque
found.
91.
will
determine
start
scanning
trailing
pixels.
92.
//第一个找到点的X
93.
int
colOpaquePixel
0;
94.
Go
through
all
rows
(Y
axis)
95.
偏历所有行(Y方向)
96.
(int
row
row++)
97.
98.
Reset
99.
//重设
100.
101.
columns
(X
102.
//偏历所有列(X方向)
103.
col
col++)
104.
105.
If
this
pixel,
mark
it
search
anymore
behind
106.
//如果是不需要透明处理的点则标记,然后继续偏历
107.
(bitmap.GetPixel(col,
row)
colorTransparent)
108.
109.
Opaque
found,
current
position
110.
//记录当前
111.
col;
112.
another
variable
position
113.
//建立新变量来记录当前点
114.
colNext
115.
Starting
from
found
pixels
116.
behind,
until
or
minimum
width
reached
117.
///从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度
118.
(colNext
colOpaquePixel;
colNext++)
119.
(bitmap.GetPixel(colNext,
120.
break;
121.
rectangle
line
add
122.
//将不透明点加到graphics
path
123.
graphicsPath.AddRectangle(new
Rectangle(colOpaquePixel,
row,
-
colOpaquePixel,
1));
124.
need
scan
125.
colNext;
126.
127.
128.
129.
calculated
130.
return
graphicsPath;
131.
132.}
--[endif]-->
三、调用类库实现
主要就是根据一些坐标,然后根据这些坐标绘制窗体
代码如下:
1.public
Form3()
5.
InitializeComponent();
//创建不规则窗体
8.
POINTAPI[]
poin;
10.
poin
POINTAPI[5];
12.
poin[0].x
90;
14.
poin[0].y
16.
poin[1].x
this.Width;
18.
19.
poin[1].y
20.
poin[2].x
Width;
22.
poin[2].y
this.Height
/
2;
24.
poin[3].x
Width
26.
poin[3].y
Height
28.
poin[4].x
30.
poin[4].y
32.
Boolean
flag
true;
34.
IntPtr
hRgn
CreatePolygonRgn(ref
poin[0],
8,
1);
36.
SetWindowRgn(this.Handle,
hRgn,
ref
flag);
38.
this.BackColor
Color.BurlyWood;
40.
42.
[StructLayout(LayoutKind.Sequential)]
44.
struct
POINTAPI
46.
48.
internal
x;
50.
y;
52.
54.
[DllImport("
gdi32.dll"
)]
56.
extern
POINTAPI
lpPoint,int
nCount,int
nPolyFillMode);
58.
59.
user32.dll"
60.
SetWindowRgn(IntPtr
hWnd,IntPtr
bRedraw);
62.
//设置窗体显示状态
64.
67.private
SetWindowPos(IntPtr
hwnd,int
hWndInsertAfter,int
x,int
y,int
cx,int
cy,int
wFlags);
68.
Start_Btn_Click(object
sender,
EventArgs
e)
70.
{//始终显示在前面
72.
SetWindowPos(this.Handle,
-1,
0,
74.
76.
button1_Click(object
78.
80.
//最小化始终显示在前面
82.
84.
}
86.
当然,我们也可以自定义窗体的动作,如按着某个轨迹一定,下面的代码中的BackgroundForm程序中就小试了一下,效果还不错,