我在winform里面想用用户控件实现win xp那样的具有蓝色立体边框的窗口,我该怎么做
时间: 2023-06-25 20:07:10 浏览: 56
要在WinForm中实现Win XP风格的窗口,你需要使用自定义绘制来绘制窗口的边框和标题栏。下面是一个简单的步骤:
1. 创建一个继承自 System.Windows.Forms.Form 的自定义窗体。
2. 设置窗体的 ControlBox 属性为 false,这将隐藏窗体的标准控件(最小化、最大化、关闭按钮)。
3. 重写 OnPaint 方法并绘制窗体的边框和标题栏。你可以使用 System.Windows.Forms.ControlPaint 类的 DrawBorder3D 和 DrawCaption 方法来帮助你绘制窗口的外观。
4. 在 OnMouseDown、OnMouseMove 和 OnMouseUp 方法中处理用户的鼠标事件,以实现拖动窗口和调整窗口大小的功能。
5. 在 OnResize 方法中重新计算窗体的大小和位置,并调用 Invalidate 方法来触发重新绘制窗口。
下面是一些示例代码,可以帮助你入门:
```csharp
public class CustomForm : System.Windows.Forms.Form
{
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
private const int HTLEFT = 0x10;
private const int HTRIGHT = 0x11;
private const int HTTOP = 0x12;
private const int HTTOPLEFT = 0x13;
private const int HTTOPRIGHT = 0x14;
private const int HTBOTTOM = 0x15;
private const int HTBOTTOMLEFT = 0x16;
private const int HTBOTTOMRIGHT = 0x17;
private Rectangle _captionRect;
private Rectangle _borderRect;
public CustomForm()
{
this.ControlBox = false;
this.DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 绘制窗口边框和标题栏
_captionRect = new Rectangle(0, 0, this.Width, 30);
_borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
ControlPaint.DrawBorder3D(e.Graphics, _borderRect, Border3DStyle.Sunken);
ControlPaint.DrawCaption(e.Graphics, _captionRect, CaptionText, Font,
SystemColors.ActiveCaptionText, SystemColors.ActiveCaption,
TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
// 绘制客户区域
Rectangle clientRect = new Rectangle(1, _captionRect.Height + 1, this.Width - 2, this.Height - _captionRect.Height - 2);
e.Graphics.FillRectangle(Brushes.White, clientRect);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// 处理鼠标按下事件,实现拖动窗口和调整窗口大小的功能
if (e.Button == MouseButtons.Left && _captionRect.Contains(e.Location))
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
else if (e.Button == MouseButtons.Left && _borderRect.Contains(e.Location))
{
if (e.Location.X < 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTLEFT, 0);
}
else if (e.Location.X > this.Width - 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTRIGHT, 0);
}
else if (e.Location.Y < _captionRect.Height + 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOP, 0);
}
else if (e.Location.Y > this.Height - 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOM, 0);
}
else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y < _captionRect.Height + 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOPLEFT, 0);
}
else if (e.Location.X > this.Width - 5 && e.Location.Y < _captionRect.Height + 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOPRIGHT, 0);
}
else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y > this.Height - 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOMLEFT, 0);
}
else if (e.Location.X > this.Width - 5 && e.Location.Y > this.Height - 5)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0);
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// 更改鼠标指针的形状,以指示用户可以拖动窗口或调整窗口大小
if (_captionRect.Contains(e.Location))
{
Cursor = Cursors.SizeAll;
}
else if (_borderRect.Contains(e.Location))
{
if (e.Location.X < 5 || e.Location.X > this.Width - 5)
{
Cursor = Cursors.SizeWE;
}
else if (e.Location.Y < _captionRect.Height + 5 || e.Location.Y > this.Height - 5)
{
Cursor = Cursors.SizeNS;
}
else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y < _captionRect.Height + 5 ||
e.Location.X > this.Width - 5 && e.Location.Y < _captionRect.Height + 5 ||
e.Location.X < _captionRect.Height + 5 && e.Location.Y > this.Height - 5 ||
e.Location.X > this.Width - 5 && e.Location.Y > this.Height - 5)
{
Cursor = Cursors.SizeNWSE;
}
else
{
Cursor = Cursors.Default;
}
}
else
{
Cursor = Cursors.Default;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// 还原鼠标指针的形状
Cursor = Cursors.Default;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// 触发重新绘制窗口
Invalidate();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x20000; // WS_MINIMIZEBOX
return cp;
}
}
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
}
```
这是一个简单的例子,你可以根据自己的需要进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)