winform窗体的拖动问题
我把窗体的this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;因为这样,我无法拖动窗体了,我程序中应该用什么方法才能在这种状态下拖动窗体呢? 问题点数:20、回复次数:4Top
1 楼sogno(一觞一咏)回复于 2006-03-03 15:34:01 得分 0
自己处理鼠标事件,把微软的轮子重新发明一遍,呵呵Top
2 楼TheRule(绝非偶然)回复于 2006-03-03 15:48:15 得分 20
一个例子,供参考:
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(442, 262);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Changes the isMouseDown field so that the form does
// not move unless the user is pressing the left mouse button.
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
// string UserName=txtname.Text.ToString();
// string UserPwd=txtpwd.Text.ToString();
// CUsers newuser=new CUsers();
// newuser.LogOn(UserName,UserPwd);
// try
// {
// if (newuser.bLogOn )
// {
//MessageBox.Show("aaa");
FrmMain MainForm=new FrmMain();
MainForm.Show();
this.Hide();
//
// }
// else
// {
// MessageBox.Show("bbb");
// }
// }
// catch(Exception EX)
// {
// MessageBox.Show(EX.ToString());
// }
}
}
}
Top
3 楼TheRule(绝非偶然)回复于 2006-03-03 15:50:13 得分 0
注意上面的这部分代码:
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Changes the isMouseDown field so that the form does
// not move unless the user is pressing the left mouse button.
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
Top
4 楼superfishmanweb(我也是千百个不愿意呀)回复于 2006-03-07 14:02:52 得分 0
good!Top




