CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

winform窗体的拖动问题

楼主superfishmanweb(我也是千百个不愿意呀)2006-03-03 15:31:43 在 .NET技术 / C# 提问

我把窗体的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

相关问题

  • 窗体拖动问题!!
  • 窗体拖动问题
  • 窗体的拖动问题
  • 拖动无标题窗体?
  • 在winform窗体中拖动DataGrid列产生事件,用什么事件,谢谢!
  • 非标题栏窗体拖动问题
  • 如何使用按钮拖动窗体?
  • 如何拖动一个窗体?
  • 拖动透明窗体的问题
  • 在窗体中按下鼠标左键拖动窗体代码???

关键词

  • 代码
  • 鼠标
  • ismousedown
  • 窗体
  • mouseoffset
  • mousepos
  • xoffset
  • 拖动
  • framebordersize
  • yoffset

得分解答快速导航

  • 帖主:superfishmanweb
  • TheRule

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo