using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace NoBorderFormResize
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.Yellow;
this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
this.panel1.Location = new System.Drawing.Point(264, 240);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(20, 20);
this.panel1.TabIndex = 0;
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel1MouseMove);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.LightCyan;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MainForm";
this.Text = "NoBorderFormResize";
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
this.Size = new Size(this.PointToClient(MousePosition).X, this.PointToClient(MousePosition).Y);
}
方法二
#region 控制改变窗体大小
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 = 0x10; //左下角
const int HTBOTTOMRIGHT = 17; //右下角
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
{
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
//判断:仅当当前窗体状态不是最大化时,相关鼠标事件生效
if (this.WindowState != FormWindowState.Maximized)
{
if (vPoint.X <= 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPLEFT;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMLEFT;
}
else
{
m.Result = (IntPtr)HTLEFT;
}
}
else if (vPoint.X >= ClientSize.Width - 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPRIGHT;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
}
else
{
m.Result = (IntPtr)HTRIGHT;
}
}
else if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOP;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOM;
}
}
break;
}
}
}
#endregion
这两个方法我都试过了,
第一个方法似乎只能缩小,无法放大。
第二个方法主要是不知道 状态栏那个小三角形响应鼠标拖拽的消息是哪一个。
#11
能够拖动的无边框窗体
#region 本程序中用到的API函数
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();//用来释放被当前线程中某个窗口捕获的光标
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);//向指定的窗体发送Windows消息
#endregion
#region 本程序中需要声明的变量
public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送的消息类型
public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
public const int HTCAPTION = 0x0002;//该变量表示发送消息的附加消息
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace NoBorderFormResize
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.Yellow;
this.panel1.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
this.panel1.Location = new System.Drawing.Point(264, 240);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(20, 20);
this.panel1.TabIndex = 0;
this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel1MouseMove);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.LightCyan;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MainForm";
this.Text = "NoBorderFormResize";
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
this.Size = new Size(this.PointToClient(MousePosition).X, this.PointToClient(MousePosition).Y);
}
方法二
#region 控制改变窗体大小
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 = 0x10; //左下角
const int HTBOTTOMRIGHT = 17; //右下角
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
{
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
//判断:仅当当前窗体状态不是最大化时,相关鼠标事件生效
if (this.WindowState != FormWindowState.Maximized)
{
if (vPoint.X <= 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPLEFT;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMLEFT;
}
else
{
m.Result = (IntPtr)HTLEFT;
}
}
else if (vPoint.X >= ClientSize.Width - 5)
{
if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOPRIGHT;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
}
else
{
m.Result = (IntPtr)HTRIGHT;
}
}
else if (vPoint.Y <= 5)
{
m.Result = (IntPtr)HTTOP;
}
else if (vPoint.Y >= ClientSize.Height - 5)
{
m.Result = (IntPtr)HTBOTTOM;
}
}
break;
}
}
}
#endregion
这两个方法我都试过了,
第一个方法似乎只能缩小,无法放大。
第二个方法主要是不知道 状态栏那个小三角形响应鼠标拖拽的消息是哪一个。
#11
能够拖动的无边框窗体
#region 本程序中用到的API函数
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();//用来释放被当前线程中某个窗口捕获的光标
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);//向指定的窗体发送Windows消息
#endregion
#region 本程序中需要声明的变量
public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送的消息类型
public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
public const int HTCAPTION = 0x0002;//该变量表示发送消息的附加消息
#endregion