无标题窗体的拖动,在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值