可以在mouse_down事件中发送WM_NCLBUTTONDOWN来实现你要的效果,可以参考下面的代码.using System.Runtime.InteropServices;
............
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2; [DllImportAttribute ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute ("user32.dll")]
public static extern bool ReleaseCapture(); private void Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
............
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2; [DllImportAttribute ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute ("user32.dll")]
public static extern bool ReleaseCapture(); private void Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
再接着问一个,如何才能使没有标题,边框为None的窗体可以resize?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Data;namespace ResizableForm
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.ComponentModel.Container components = null;
static int frmLastWidth=0;
static int frmLastHeight=0;
static int frmWidth;
static int frmHeight;
static bool frmIsResizing=false;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
System.Drawing.Rectangle frmRectangle = new System.Drawing.Rectangle(); public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Add picture box handler junk
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight);
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
frmIsResizing = false;
frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight);
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
this.Width = frmWidth;
this.Height = frmHeight;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
//this.ResizeRedraw = false;
int sizeageX = (MousePosition.X-this.Location.X);
int sizeageY = (MousePosition.Y-this.Location.Y); // Use this to restrict Width
if (sizeageX < 120)
sizeageX = 120; // Use this to restrict Height
if (sizeageY < 81)
sizeageY = 81; frmWidth = sizeageX;
frmHeight = sizeageY; if (frmLastWidth == 0)
frmLastWidth = frmWidth;
if (frmLastHeight==0)
frmLastHeight = frmHeight;
if (frmIsResizing)
{
frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
frmRectangle.Size = new System.Drawing.Size(frmLastWidth,frmLastHeight);
} frmIsResizing = true; ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
frmLastWidth = frmWidth;
frmLastHeight = frmHeight; frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight); ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
}
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.label1.Location = new System.Drawing.Point(32, 184);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Grab this ->>";
//
// button1
//
this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button1.Location = new System.Drawing.Point(96, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 2;
this.button1.Text = "Close";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.pictureBox1.BackColor = System.Drawing.Color.Blue;
this.pictureBox1.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
this.pictureBox1.Location = new System.Drawing.Point(120, 176);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(152, 208);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.label1,
this.pictureBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing ); }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}