CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

hbxtlhx(下着春雨的天) 请进

楼主hanbinghai(海宁)2004-11-03 14:45:21 在 .NET技术 / C# 提问

当Form的formBorderStyle设为none之后,实现窗体托拽,边框移动窗体的功能。  
  看了你实现的代码,原贴见http://community.csdn.net/Expert/topic/3401/3401961.xml?temp=.3063928感觉不错,现在我在窗体上加一个label,它的Dock   =   DockStyle.Top之后窗体的上部,左上角和右上角的边框不能实现移动窗体,请问该如何实现?  
  问题点数:100、回复次数:16Top

1 楼hanbinghai(海宁)回复于 2004-11-03 15:10:01 得分 0

自己upTop

2 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-03 15:17:03 得分 90

这样的话,只能重写一个panel的控件来实现,因为这时panel是当前接爱鼠标事件,当然你可以做成当panel有鼠标事件时让窗体得到,但是重写一个panel控件有时可能更有意思,我没有重写panel,但我重写了一个button的,代码我想只是它们继承的基类不一样罢了:代码大致如下:  
  using   System;  
  using   System.Windows.Forms;  
  using   System.Drawing;  
   
  namespace   MoveControlLocation  
  {  
  ///   <summary>  
  ///    
  ///   </summary>  
  public   class   CusLabel:   System.Windows.Forms.Button  
  {  
  public   CusLabel()  
  {  
  //    
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
  private   const   int   WM_NCHITTEST   =   0x84;  
  private   const   int   WM_SIZING   =   532;  
   
  private   const   int   HTERROR   =   (-2);  
  private   const   int   HTTRANSPARENT   =   (-1);  
  private   const   int   HTNOWHERE   =   0;  
  private   const   int   HTCLIENT   =     1;  
  private   const   int   HTCAPTION   =   2;  
  private   const   int   HTSYSMENU   =   3;  
  private   const   int   HTGROWBOX   =   4;  
  private   const   int   HTSIZE   =   HTGROWBOX;  
  private   const   int   HTMENU   =   5;  
  private   const   int   HTHSCROLL   =   6;  
  private   const   int   HTVSCROLL   =   7;  
  private   const   int   HTMINBUTTON   =   8;  
  private   const   int   HTMAXBUTTON   =   9;  
  private   const   int   HTLEFT   =   10;  
  private   const   int   HTRIGHT   =   11;  
  private   const   int   HTTOP   =   12;  
  private   const   int   HTTOPLEFT   =   13;  
  private   const   int   HTTOPRIGHT   =   14;  
  private   const   int   HTBOTTOM   =   15;  
  private   const   int   HTBOTTOMLEFT   =   16;  
  private   const   int   HTBOTTOMRIGHT   =   17;  
  private   const   int   HTBORDER   =   18;  
  private   const   int   HTREDUCE   =   HTMINBUTTON;  
  private   const   int   HTZOOM   =   HTMAXBUTTON;  
  private   const   int   HTSIZEFIRST   =   HTLEFT;  
  private   const   int   HTSIZELAST   =   HTBOTTOMRIGHT;  
  /*   WINVER   >=   0x0400   */  
  private   const   int   HTOBJECT   =   19;  
  private   const   int   HTCLOSE   =   20;  
  private   const   int   HTHELP   =   21;  
  /*   WINVER   >=   0x0400   */  
   
  private   const   int   BorderWidth   =   3;  
  private   int   CaptionHeight   =   17;  
   
  //可以调整窗体的大小和移动窗体的位置  
  protected   override   void   WndProc(ref   System.Windows.Forms.Message   m)  
  {  
  switch(m.Msg)  
  {  
  case   WM_NCHITTEST:  
  base.WndProc(ref   m);  
  if   (DesignMode)  
  {  
  return;  
  }  
  if   ((int)m.Result   ==   HTCLIENT)  
  {  
  m.HWnd   =   this.Handle;  
  int   pLeft=0;  
  int   pTop=0;  
   
  bool   IsControl   =   true;  
  if   (IsControl)  
  {  
  if   ((this.Parent   as   Form).FormBorderStyle==FormBorderStyle.None)  
  {  
  pLeft=this.Parent.Left;  
  pTop=this.Parent.Top;  
  }  
  else  
  {  
  pLeft=this.Parent.Left+2;  
  CaptionHeight   =   this.Parent.Height-this.Parent.ClientRectangle.Height-2   *   BorderWidth;  
  pTop=this.Parent.Top   +   CaptionHeight   +   BorderWidth-2;  
  }  
  }  
   
  if   ((Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth)   &&   (Cursor.Position.Y   <=   pTop   +   this.Top   +   BorderWidth))  
  m.Result   =   (IntPtr)HTTOPLEFT;//左上  
  else   if   ((Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y<=pTop   +   this.Top   +BorderWidth))  
  m.Result   =   (IntPtr)HTTOPRIGHT;//右上  
  else   if   ((Cursor.Position.X   <=   pLeft   +   this.Left   +   BorderWidth)   &&   (Cursor.Position.Y>=pTop   +   this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)HTBOTTOMLEFT;//左下  
  else   if   ((Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y>=pTop   +   this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)HTBOTTOMRIGHT;//右下  
  else   if   (Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth-1)  
  m.Result   =   (IntPtr)HTLEFT;//左  
  else   if   (Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)  
  m.Result   =   (IntPtr)HTRIGHT;//右  
  else   if   (Cursor.Position.Y<=pTop   +   this.Top   +   BorderWidth-1)  
  m.Result   =   (IntPtr)HTTOP;//上  
  else   if   (Cursor.Position.Y>=pTop   +   this.Top   +   this.Height-BorderWidth)  
  m.Result   =   (IntPtr)HTBOTTOM;//下  
  else   if   (Cursor.Position.Y<=pTop   +   this.Top   +   CaptionHeight   +   BorderWidth   )  
  {  
  if   (Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth   +   CaptionHeight)  
  {  
  m.Result   =   (IntPtr)HTSYSMENU;  
  }  
  else  
  {  
  m.Result   =   (IntPtr)HTCAPTION;  
  }  
  }  
  }  
  return;  
  default:  
  base.WndProc(ref   m);  
  break;  
  }  
  }    
   
  }  
  }Top

3 楼sywcf(wcf)回复于 2004-11-03 15:30:58 得分 5

楼上的兄弟真的不错,有发展。  
  学习。。Top

4 楼xiaoslong(龙哥)回复于 2004-11-03 15:36:20 得分 5

帮你顶Top

5 楼hanbinghai(海宁)回复于 2004-11-03 16:00:07 得分 0

鼠标形状已经改变了符合要求,但是窗体大小怎么控制呢?半天也没整出来:(  
  代码如下:  
  using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
   
  namespace   WindowsApplication5  
  {  
  ///   <summary>  
  ///   Form1   的摘要说明。  
  ///   </summary>  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   CusLabel   label1;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
  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()  
  {  
  this.label1   =   new   WindowsApplication5.Form1.CusLabel();  
  this.SuspendLayout();  
  //    
  //   label1  
  //    
  this.label1.BackColor   =   System.Drawing.SystemColors.ActiveCaption;  
  this.label1.Dock   =   System.Windows.Forms.DockStyle.Top;  
  this.label1.Location   =   new   System.Drawing.Point(0,   0);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(292,   23);  
  this.label1.TabIndex   =   0;  
  //    
  //   Form1  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.ClientSize   =   new   System.Drawing.Size(292,   273);  
  this.Controls.Add(this.label1);  
  this.FormBorderStyle   =   System.Windows.Forms.FormBorderStyle.None;  
  this.Name   =   "Form1";  
  this.Text   =   "Form1";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  ///   <summary>  
  ///   应用程序的主入口点。  
  ///   </summary>  
  [STAThread]  
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
  public   class   CusLabel:   System.Windows.Forms.Label  
  {  
   
  private   const   int   WM_NCHITTEST   =   0x84;  
  private   const   int   WM_SIZING   =   532;  
   
  private   const   int   HTERROR   =   (-2);  
  private   const   int   HTTRANSPARENT   =   (-1);  
  private   const   int   HTNOWHERE   =   0;  
  private   const   int   HTCLIENT   =     1;  
  private   const   int   HTCAPTION   =   2;  
  private   const   int   HTSYSMENU   =   3;  
  private   const   int   HTGROWBOX   =   4;  
  private   const   int   HTSIZE   =   HTGROWBOX;  
  private   const   int   HTMENU   =   5;  
  private   const   int   HTHSCROLL   =   6;  
  private   const   int   HTVSCROLL   =   7;  
  private   const   int   HTMINBUTTON   =   8;  
  private   const   int   HTMAXBUTTON   =   9;  
  private   const   int   HTLEFT   =   10;  
  private   const   int   HTRIGHT   =   11;  
  private   const   int   HTTOP   =   12;  
  private   const   int   HTTOPLEFT   =   13;  
  private   const   int   HTTOPRIGHT   =   14;  
  private   const   int   HTBOTTOM   =   15;  
  private   const   int   HTBOTTOMLEFT   =   16;  
  private   const   int   HTBOTTOMRIGHT   =   17;  
  private   const   int   HTBORDER   =   18;  
  private   const   int   HTREDUCE   =   HTMINBUTTON;  
  private   const   int   HTZOOM   =   HTMAXBUTTON;  
  private   const   int   HTSIZEFIRST   =   HTLEFT;  
  private   const   int   HTSIZELAST   =   HTBOTTOMRIGHT;  
  /*   WINVER   >=   0x0400   */  
  private   const   int   HTOBJECT   =   19;  
  private   const   int   HTCLOSE   =   20;  
  private   const   int   HTHELP   =   21;  
  /*   WINVER   >=   0x0400   */  
   
  private   const   int   BorderWidth   =   2;  
   
  private   int   CaptionHeight   =   17;  
   
  //可以调整窗体的大小和移动窗体的位置  
  protected   override   void   WndProc(ref   System.Windows.Forms.Message   m)  
  {  
  switch(m.Msg)  
  {  
  case   WM_NCHITTEST:  
  base.WndProc(ref   m);  
  if   (DesignMode)  
  {  
  return;  
  }  
  if   ((int)m.Result   ==   HTCLIENT)  
  {  
  m.HWnd   =   this.Handle;  
  int   pLeft=0;  
  int   pTop=0;  
   
  bool   IsControl   =   true;  
  if   (IsControl)  
  {  
  if   ((this.Parent   as   Form).FormBorderStyle==FormBorderStyle.None)  
  {  
  pLeft=this.Parent.Left;  
  pTop=this.Parent.Top;  
  }  
  else  
  {  
  pLeft=this.Parent.Left+2;  
  CaptionHeight   =   this.Parent.Height-this.Parent.ClientRectangle.Height-2   *   BorderWidth;  
  pTop=this.Parent.Top   +   CaptionHeight   +   BorderWidth-2;  
  }  
  }  
   
  if   ((Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth)   &&   (Cursor.Position.Y   <=   pTop   +   this.Top   +   BorderWidth))  
  m.Result   =   (IntPtr)HTTOPLEFT;//左上  
  else   if   ((Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y<=pTop   +   this.Top   +BorderWidth))  
  m.Result   =   (IntPtr)HTTOPRIGHT;//右上  
  else   if   ((Cursor.Position.X   <=   pLeft   +   this.Left   +   BorderWidth)   &&   (Cursor.Position.Y>=pTop   +   this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)HTLEFT;//左下-》左  
  else   if   ((Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y>=pTop   +   this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)HTRIGHT;//右下-》右  
  else   if   (Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth-1)  
  m.Result   =   (IntPtr)HTLEFT;//左  
  else   if   (Cursor.Position.X>=pLeft   +   this.Left   +   this.Width-BorderWidth)  
  m.Result   =   (IntPtr)HTRIGHT;//右  
  else   if   (Cursor.Position.Y<=pTop   +   this.Top   +   BorderWidth-1)  
  m.Result   =   (IntPtr)HTTOP;//上  
  else   if   (Cursor.Position.Y<=pTop   +   this.Top   +   CaptionHeight   +   BorderWidth   )  
  {  
  if   (Cursor.Position.X<=pLeft   +   this.Left   +   BorderWidth   +   CaptionHeight)  
  {  
  m.Result   =   (IntPtr)HTSYSMENU;  
  }  
  else  
  {  
  m.Result   =   (IntPtr)HTCAPTION;  
  }  
  }  
  else    
  m.Result   =   (IntPtr)HTCAPTION;//移动窗体    
  }  
  return;  
  default:  
  base.WndProc(ref   m);  
  break;  
  }  
  }    
  }  
   
   
  private   const   int   WM_NCHITTEST   =   0x84;  
  private   const   int   HTCLIENT   =   0x1;  
  private   const   int   HTSIZE   =   0x4;  
  private   const   int   HTCAPTION   =   0x2;    
  private   const   int   WM_SIZING   =   532;  
  private   const   int   HTMINBUTTON   =   8;  
   
   
  private   const   int   BorderWidth   =   5;  
   
  // m.Result   =   (IntPtr)20;//关闭按纽  
  // m.Result   =   (IntPtr)21;//help按纽  
   
  //可以调整窗体的大小和移动窗体的位置  
  protected   override   void   WndProc(ref   Message   m)  
  {  
  System.Diagnostics.Debug.WriteLine(m.ToString());  
  switch(m.Msg)  
  {  
  case   WM_NCHITTEST:  
  base.WndProc(ref   m);  
  if   (DesignMode)  
  {  
  return;  
  }  
  if   (((int)m.Result   ==   HTCLIENT   ))  
  {  
  if   ((Cursor.Position.X<=this.Left   +   BorderWidth)   &&   (Cursor.Position.Y   <=   this.Top   +   BorderWidth))  
  m.Result   =   (IntPtr)13;//左上  
  else   if   ((Cursor.Position.X>=this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y<=this.Top   +BorderWidth))  
  m.Result   =   (IntPtr)14;//右上  
  else   if   ((Cursor.Position.X   <=   this.Left   +   BorderWidth)   &&   (Cursor.Position.Y>=this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)16;//左下  
  else   if   ((Cursor.Position.X>=this.Left   +   this.Width-BorderWidth)   &&   (Cursor.Position.Y>=this.Top   +   this.Height-BorderWidth))  
  m.Result   =   (IntPtr)17;//右下  
  else   if   (Cursor.Position.X<=this.Left   +   BorderWidth)  
  m.Result   =   (IntPtr)10;//左  
  else   if   (Cursor.Position.X>=this.Left   +   this.Width-BorderWidth)  
  m.Result   =   (IntPtr)11;//右  
  else   if   (Cursor.Position.Y<=this.Top   +   BorderWidth)  
  m.Result   =   (IntPtr)12;//上  
  else   if   (Cursor.Position.Y>=this.Top   +   this.Height-BorderWidth)  
  m.Result   =   (IntPtr)15;//下  
  else    
  m.Result   =   (IntPtr)HTCAPTION;//移动窗体      
  }  
  return;  
  default:  
  base.WndProc(ref   m);  
  break;  
  }  
  }    
   
  }  
  }  
  Top

6 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-03 16:06:01 得分 0

明白你的意思了,你可以再加这一句话试试:  
  private   void   panel1_Resize(object   sender,   System.EventArgs   e)  
  {  
  this.Size   =   this.panel1.Size;  
  }Top

7 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-03 16:12:42 得分 0

还有,就是像你这种情你可以给窗体留出一个"边"来,让你的窗体在这个"边"上调整窗体的大小位置等.  
  也就是说你可以不用  
  this.panel1.Dock   =   System.Windows.Forms.DockStyle.Fill;来填满整个窗体.而是给你的窗体留出一个可以操作窗体的边来,你也应注意到一个正常的窗体由其是在XP下看到它是有一个边的,正是这个边才能操作窗体的大小位置,如果把这个边设为None则不能再对它操作了,我想应该是这个道理的.  
  Top

8 楼hanbinghai(海宁)回复于 2004-11-03 16:26:18 得分 0

to   hbxtlhx(下着春雨的天)    
  this.Size   =   this.panel1.Size这个方法是行不通的  
  你说给窗体留出一个"边"来是没有办法的办法  
  ^_^Top

9 楼hanbinghai(海宁)回复于 2004-11-03 16:48:59 得分 0

to   hbxtlhx(下着春雨的天)    
  要是有更好的办法请发短消息告诉我  
  结贴了Top

10 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-03 17:06:04 得分 0

没问题!有好的思路我一定告知.Top

11 楼hanbinghai(海宁)回复于 2004-11-05 11:28:42 得分 0

不是办法的办法:  
  可以给窗体添加Panel控件,调整好与form的上下左右的距离,给窗体留出一个"边"  
  效果也不错,^_^Top

12 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-05 11:45:39 得分 0

问题是怎样来给窗体这个边?  
  我的一般的做法是得写如下的方法,在里面放上要调整的控件的位置或大小等信息.  
  protected   override   void   OnLayout(LayoutEventArgs   levent)  
  {  
  base.OnLayout   (levent);  
  }  
  Top

13 楼hanbinghai(海宁)回复于 2004-11-05 11:58:48 得分 0

对Pannel操作,刚才忘说了,要把panel的Anchor设置为上下左右  
  this.Panel1.Anchor   =   ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top   |   System.Windows.Forms.AnchorStyles.Bottom)    
  |   System.Windows.Forms.AnchorStyles.Left)    
  |   System.Windows.Forms.AnchorStyles.Right)));Top

14 楼hbxtlhx(平民百姓-自已动手,丰衣足食)回复于 2004-11-05 13:48:22 得分 0

对啊,你那个方法也是可的啊.Top

15 楼hanbinghai(海宁)回复于 2004-11-19 09:01:24 得分 0

最简单的方法:  
  新建一个项目,在窗体Load事件中添加  
  private   void   Form1_Load(object   sender,   System.EventArgs   e)  
  {  
  this.ControlBox   =   false;  
  this.Text   =   string.Empty;  
  }  
  就把以上的问题全解决了,就这么简单的两行代码  
  ^_^  
  Top

16 楼hanbinghai(海宁)回复于 2004-12-02 12:14:23 得分 0

问题是怎样来给窗体这个边?  
  this.DockPadding.Bottom   =   2;  
  this.DockPadding.Left   =   2;  
  this.DockPadding.Right   =   2;  
  this.DockPadding.Top   =   2;  
  真简单啊Top

相关问题

  • 请wangyi03(小楼一夜听春雨) 快来领分(内容空)
  • 请wangyi03(小楼一夜听春雨)来领分(内容空)
  • 请 wangyi03(小楼一夜听春雨)领分 内容空 
  • 天下太平.
  • 《《《《《《《天下无贼》》》》》》》
  • 天下无贼
  • 倒分天天有,今天下午特别多1
  • 贱人天天有,昨天下午特别多2
  • 帅哥天天有,今天下午特别多3
  • 接分的天天有,今天下午特别多4

关键词

  • 代码
  • 窗体
  • borderwidth
  • hbxtlhx
  • ptop
  • pleft
  • cuslabel
  • htright
  • htbottomright
  • htgrowbox

得分解答快速导航

  • 帖主:hanbinghai
  • hbxtlhx
  • sywcf
  • xiaoslong

相关链接

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

广告也精彩

反馈

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