Skip to content

Commit 5da8e97

Browse files
committed
新功能:自定义窗口
1、自定义窗口类添加标题栏按键高亮; 2、窗口缩放功能; 3、鼠标移动窗口功能。
1 parent c7dac1c commit 5da8e97

26 files changed

+1383
-29
lines changed

MControl.sln

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30804.86
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MControl", "MControl\MControl.csproj", "{D9609C84-77EA-4127-899C-49CE6DA6BB5E}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{147D460A-6F89-40D3-A793-26A622AD6E8E}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{D9609C84-77EA-4127-899C-49CE6DA6BB5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{D9609C84-77EA-4127-899C-49CE6DA6BB5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{D9609C84-77EA-4127-899C-49CE6DA6BB5E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{147D460A-6F89-40D3-A793-26A622AD6E8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{147D460A-6F89-40D3-A793-26A622AD6E8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{147D460A-6F89-40D3-A793-26A622AD6E8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{147D460A-6F89-40D3-A793-26A622AD6E8E}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

MControl/Class1.cs

-11
This file was deleted.

MControl/Forms/FormWithTitle.Designer.cs

+165
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MControl/Forms/FormWithTitle.cs

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Data;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Windows.Forms;
9+
10+
namespace MControl.Forms
11+
{
12+
public partial class FormWithTitle : Form
13+
{
14+
public FormWithTitle()
15+
{
16+
InitializeComponent();
17+
}
18+
19+
#region 按键高亮
20+
/// <summary>
21+
/// 鼠标进入控件后控件高亮
22+
/// </summary>
23+
/// <param name="p_Control"></param>
24+
private void ButHighlight(Control p_Control)
25+
{
26+
Color color = TitleBar.BackColor;
27+
p_Control.BackColor = Color.FromArgb(color.R + 20, color.G + 20, color.B + 20);
28+
}
29+
30+
/// <summary>
31+
/// 鼠标离开控件范围后还原控件颜色
32+
/// </summary>
33+
/// <param name="p_Control"></param>
34+
private void ButRestoreColor(Control p_Control)
35+
{
36+
p_Control.BackColor = TitleBar.BackColor;
37+
}
38+
39+
private void ButClose_MouseEnter(object sender, EventArgs e)
40+
{
41+
ButHighlight(ButClose);
42+
}
43+
44+
private void ButClose_MouseLeave(object sender, EventArgs e)
45+
{
46+
ButRestoreColor(ButClose);
47+
}
48+
49+
private void ButMaxmum_MouseEnter(object sender, EventArgs e)
50+
{
51+
ButHighlight(ButMaxmum);
52+
}
53+
54+
private void ButMaxmum_MouseLeave(object sender, EventArgs e)
55+
{
56+
ButRestoreColor(ButMaxmum);
57+
}
58+
59+
private void ButMinmum_MouseEnter(object sender, EventArgs e)
60+
{
61+
ButHighlight(ButMinmum);
62+
}
63+
64+
private void ButMinmum_MouseLeave(object sender, EventArgs e)
65+
{
66+
ButRestoreColor(ButMinmum);
67+
}
68+
69+
#endregion
70+
71+
#region 窗口缩放
72+
73+
const int WM_NCHITTEST = 0x0084;
74+
const int HTLEFT = 10; //左边界
75+
const int HTRIGHT = 11; //右边界
76+
const int HTTOP = 12; //上边界
77+
const int HTTOPLEFT = 13; //左上角
78+
const int HTTOPRIGHT = 14; //右上角
79+
const int HTBOTTOM = 15; //下边界
80+
const int HTBOTTOMLEFT = 0x10; //左下角
81+
const int HTBOTTOMRIGHT = 17; //右下角
82+
protected override void WndProc(ref Message m)
83+
{
84+
switch (m.Msg)
85+
{
86+
case WM_NCHITTEST:
87+
base.WndProc(ref m);
88+
Point vPoint = new Point((int)m.LParam & 0xFFFF,
89+
(int)m.LParam >> 16 & 0xFFFF);
90+
vPoint = PointToClient(vPoint);
91+
if (vPoint.X <= 5)
92+
if (vPoint.Y <= 5)
93+
m.Result = (IntPtr)HTTOPLEFT;
94+
else if (vPoint.Y >= ClientSize.Height - 5)
95+
m.Result = (IntPtr)HTBOTTOMLEFT;
96+
else m.Result = (IntPtr)HTLEFT;
97+
else if (vPoint.X >= ClientSize.Width - 5)
98+
if (vPoint.Y <= 5)
99+
m.Result = (IntPtr)HTTOPRIGHT;
100+
else if (vPoint.Y >= ClientSize.Height - 5)
101+
m.Result = (IntPtr)HTBOTTOMRIGHT;
102+
else m.Result = (IntPtr)HTRIGHT;
103+
else if (vPoint.Y <= 5)
104+
m.Result = (IntPtr)HTTOP;
105+
else if (vPoint.Y >= ClientSize.Height - 5)
106+
m.Result = (IntPtr)HTBOTTOM;
107+
break;
108+
default:
109+
base.WndProc(ref m);
110+
break;
111+
}
112+
}
113+
#endregion
114+
115+
#region 窗口移动
116+
private Point m_PointMouse; //保存鼠标移动前的坐标
117+
/// <summary>
118+
/// 鼠标在标题栏按下后开始监听鼠标移动事件
119+
/// </summary>
120+
/// <param name="sender"></param>
121+
/// <param name="e"></param>
122+
private void TitleBar_MouseMove(object sender, MouseEventArgs e)
123+
{
124+
Point l_Point = this.Location;
125+
this.Location = new Point(l_Point.X + (MousePosition.X - m_PointMouse.X), l_Point.Y + (MousePosition.Y - m_PointMouse.Y));
126+
m_PointMouse = MousePosition;
127+
}
128+
129+
/// <summary>
130+
/// 鼠标移动事件,获取鼠标当前坐标
131+
/// </summary>
132+
/// <param name="sender"></param>
133+
/// <param name="e"></param>
134+
private void TitleBar_MouseDown(object sender, MouseEventArgs e)
135+
{
136+
if(e.Button == MouseButtons.Left)
137+
{
138+
m_PointMouse = MousePosition;
139+
this.TitleBar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TitleBar_MouseMove);
140+
}
141+
}
142+
143+
/// <summary>
144+
/// 鼠标按键释放后停止监听鼠标坐标
145+
/// </summary>
146+
/// <param name="sender"></param>
147+
/// <param name="e"></param>
148+
private void TitleBar_MouseUp(object sender, MouseEventArgs e)
149+
{
150+
this.TitleBar.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.TitleBar_MouseMove);
151+
}
152+
153+
#endregion
154+
}
155+
}

0 commit comments

Comments
 (0)