无边框窗体和可调整大小的窗体是Windows应用程序中常见的两种窗体类型。无边框窗体去掉了标准窗体的边框和标题栏,提供了更现代的外观。可调整大小的窗体允许用户通过拖动窗体的边缘来改变其大小。
FormBorderStyle
属性为None
来实现。FormBorderStyle
属性为SizableToolWindow
或Sizable
来实现。OnResize
方法来处理窗体大小变化时的逻辑。以下是一个简单的C#示例,展示如何创建一个无边框且可调整大小的窗体:
using System;
using System.Drawing;
using System.Windows.Forms;
public class CustomForm : Form
{
public CustomForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ResizeRedraw = true;
this.DoubleBuffered = true;
this.ClientSize = new Size(800, 600);
this.BackColor = Color.White;
// 添加鼠标事件处理
this.MouseDown += CustomForm_MouseDown;
this.MouseMove += CustomForm_MouseMove;
this.MouseUp += CustomForm_MouseUp;
}
private bool isDragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;
private void CustomForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
dragCursorPoint = Cursor.Position;
dragFormPoint = this.Location;
}
}
private void CustomForm_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
this.Location = Point.Add(dragFormPoint, new Size(dif));
}
}
private void CustomForm_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CustomForm());
}
}
ResizeRedraw
属性为true
,并在OnPaint
方法中进行优化。通过以上方法和示例代码,你可以创建一个无边框且可调整大小的窗体,并解决常见的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云