当用户去除窗体边框时,自行设置窗体外观,用户就不能对窗体的大小进行随意改变了,下面,济南网站建设小编就来为大家介绍,怎么在c#编程语言中手动改天无边框窗体的大小,有需要的朋友可以过来参考一下。
关键代码:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class FrmMain : Form
{
private const int RESIZE_HANDLE_SIZE = 10;
private Timer resizeTimer;
private Size targetSize;
public FrmMain()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.MinimumSize = new Size(400, 300);
resizeTimer = new Timer();
resizeTimer.Interval = 10;
resizeTimer.Tick += ResizeTimer_Tick;
// 添加测试按钮
Button btnMax = new Button();
btnMax.Text = "最大化";
btnMax.Location = new Point(10, 10);
btnMax.Click += BtnMax_Click;
this.Controls.Add(btnMax);
Button btnRestore = new Button();
btnRestore.Text = "恢复";
btnRestore.Location = new Point(120, 10);
btnRestore.Click += BtnRestore_Click;
this.Controls.Add(btnRestore);
}
private void BtnMax_Click(object sender, EventArgs e)
{
AnimateResize(Screen.PrimaryScreen.WorkingArea.Size);
}
private void BtnRestore_Click(object sender, EventArgs e)
{
AnimateResize(new Size(800, 600));
}
private void ResizeTimer_Tick(object sender, EventArgs e)
{
Size current = this.Size;
int step = 10;
if (Math.Abs(current.Width - targetSize.Width) < step &&
Math.Abs(current.Height - targetSize.Height) < step)
{
this.Size = targetSize;
resizeTimer.Stop();
}
else
{
int newWidth = current.Width + Math.Sign(targetSize.Width - current.Width) * step;
int newHeight = current.Height + Math.Sign(targetSize.Height - current.Height) * step;
this.Size = new Size(newWidth, newHeight);
}
}
private void AnimateResize(Size newSize)
{
targetSize = newSize;
resizeTimer.Start();
}
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x0084;
const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 16;
const int HTBOTTOMRIGHT = 17;
if (m.Msg == WM_NCHITTEST)
{
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
pos = this.PointToClient(pos);
if (pos.X < RESIZE_HANDLE_SIZE && pos.Y < RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTTOPLEFT;
else if (pos.X >= this.ClientSize.Width - RESIZE_HANDLE_SIZE && pos.Y < RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTTOPRIGHT;
else if (pos.X < RESIZE_HANDLE_SIZE && pos.Y >= this.ClientSize.Height - RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTBOTTOMLEFT;
else if (pos.X >= this.ClientSize.Width - RESIZE_HANDLE_SIZE && pos.Y >= this.ClientSize.Height - RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTBOTTOMRIGHT;
else if (pos.X < RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTLEFT;
else if (pos.X >= this.ClientSize.Width - RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTRIGHT;
else if (pos.Y < RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTTOP;
else if (pos.Y >= this.ClientSize.Height - RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)HTBOTTOM;
else
m.Result = (IntPtr)HTCLIENT;
return;
}
base.WndProc(ref m);
}
protected override void onPaint(PaintEventArgs e)
{
base.onPaint(e);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
this.ClientSize.Width - RESIZE_HANDLE_SIZE,
this.ClientSize.Height - RESIZE_HANDLE_SIZE,
RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
}
}