无标题窗体的拖动,在MouseMove中实现,非MouseUP!

 

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Point mouseOffset;
InBlock.gif        
private bool isMouseDown = false;
InBlock.gif
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(208224);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.Size = new System.Drawing.Size(8040);
InBlock.gif            
this.button1.TabIndex = 0;
InBlock.gif            
this.button1.Text = "button1";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(292273);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
InBlock.gif            
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
InBlock.gif            
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private void button1_Click(object sender, System.EventArgs e) dot.gif{
InBlock.gif            
this.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) dot.gif{
InBlock.gif            
int xOffset;
InBlock.gif            
int yOffset;
InBlock.gif            
//先记得窗体内部的作标,标以负数是因为:当前的Location=当前屏幕的光标位置+(-1*窗体内部作标)
InBlock.gif
            xOffset = -e.X;
InBlock.gif            yOffset 
= -e.Y;
InBlock.gif
InBlock.gif            mouseOffset 
= new Point(xOffset, yOffset);
InBlock.gif            isMouseDown 
= true;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (isMouseDown) dot.gif{
InBlock.gif                Point mousePos 
= Control.MousePosition;
InBlock.gif                
//移动后,用新的屏幕作标+(-1*窗体内部作标)=窗体的Location+增量N,而增量N对于三个数来讲都是相对的,
InBlock.gif                
//所以Form的新作标为原Location+增量N.
InBlock.gif
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
InBlock.gif                Location 
= mousePos;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (e.Button == MouseButtons.Left) dot.gif{
InBlock.gif         &nb#

转载于:https://www.cnblogs.com/fxwdl/archive/2006/09/27/516173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现C#窗体的任意位置拖动效果,可以通过以下步骤进行: 1. 在窗体上添加一个Panel控件,并将其Dock属性设置为Top。这个Panel控件将作为窗体的标题栏。 2. 在Panel控件上添加一个Label或者其他控件,用来显示窗体的标题。 3. 在Panel控件的MouseDown事件,获取鼠标当前的位置,并将窗体的Location属性设置为这个位置。同时设置一个变量记录鼠标按下时的位置。 4. 在Panel控件的MouseMove事件,如果鼠标左键被按下,计算鼠标当前位置和按下时位置的偏移量,然后将窗体的Location属性加上这个偏移量。 下面是示例代码: ``` public partial class Form1 : Form { private Point mouseOffset; public Form1() { InitializeComponent(); } private void panelTitle_MouseDown(object sender, MouseEventArgs e) { mouseOffset = new Point(-e.X, -e.Y); this.Cursor = Cursors.SizeAll; } private void panelTitle_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); this.Location = mousePos; } } private void panelTitle_MouseUp(object sender, MouseEventArgs e) { this.Cursor = Cursors.Default; } } ``` 在上述代码,panelTitle是我们添加的Panel控件,用来作为窗体的标题栏。在MouseDown事件,我们记录鼠标按下时的位置,并将鼠标的光标设置为SizeAll,以便用户知道窗体可以被拖动。在MouseMove事件,如果鼠标左键被按下,我们计算鼠标当前位置和按下时位置的偏移量,并将窗体的Location属性设置为鼠标当前位置加上偏移量。在MouseUp事件,我们将光标设置回默认状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值