标题:c# 控件缩放拖动和开关问题
只看楼主
wangtong0213
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-12-18
结帖率:50%
已结贴  问题点数:2 回复次数:2 
c# 控件缩放拖动和开关问题
想用dotnetbar里边的tabcontrol或者grouppanel控件,执行程序后能让这些控件放缩,和拖动,和显隐性的设置,就跟咱们用的一般软件里视图功能差不多。
从网上找到的代码针对tabcontrol没用啊,grouppanel的缩放只能缩放左边和上边,不能拖动下边和右边,不知道什么问题,求大神啊


1、定义一个枚举类型,描述光标状态

private enum EnumMousePointPosition   
   {   
    MouseSizeNone   = 0, //'无   
    MouseSizeRight   = 1, //'拉伸右边框   
    MouseSizeLeft   = 2, //'拉伸左边框   
    MouseSizeBottom   = 3, //'拉伸下边框   
    MouseSizeTop   = 4, //'拉伸上边框   
    MouseSizeTopLeft = 5, //'拉伸左上角   
    MouseSizeTopRight = 6, //'拉伸右上角   
    MouseSizeBottomLeft = 7, //'拉伸左下角   
    MouseSizeBottomRight= 8, //'拉伸右下角   
    MouseDrag    = 9   // '鼠标拖动   
   }  
2、定义几个变量

const int Band = 5;   
   const int MinWidth=10;   
   const int MinHeight=10;   
   private EnumMousePointPosition m_MousePointPosition;   
   private Point p,p1;  
3、定义自己的MyMouseDown事件

private void MyMouseDown(object sender,System.Windows.Forms.MouseEventArgs e)   
{   
 p.X=e.X;   
 p.Y=e.Y;   
 p1.X=e.X;   
 p1.Y=e.Y;      
}  
4、定义自己的MyMouseLeave事件

private void MyMouseLeave(object sender, System.EventArgs e)   
{   
 m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;   
 this.Cursor=Cursors.Arrow;   
}  
5、设计一个函数,确定光标在控件不同位置的样式

private EnumMousePointPosition MousePointPosition(Size size,System.Windows.Forms.MouseEventArgs e)   
{   
  
 if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))   
 {   
  if (e.X < Band)   
  {   
   if (e.Y < Band) {return EnumMousePointPosition.MouseSizeTopLeft;}   
   else   
   {   
    if (e.Y > -1 * Band + size.Height)   
    {return EnumMousePointPosition.MouseSizeBottomLeft;}   
    else   
    {return EnumMousePointPosition.MouseSizeLeft;}   
   }   
  }   
  else  
  {   
   if (e.X > -1 * Band + size.Width)   
   {   
    if (e.Y < Band)   
    {return EnumMousePointPosition.MouseSizeTopRight;}   
    else   
    {   
     if (e.Y > -1 * Band + size.Height)   
     {return EnumMousePointPosition.MouseSizeBottomRight;}   
     else  
     {return EnumMousePointPosition.MouseSizeRight;}   
    }   
   }   
   else  
   {   
    if (e.Y < Band)   
    {return EnumMousePointPosition.MouseSizeTop;}   
    else  
    {   
     if (e.Y > -1 * Band + size.Height)   
     {return EnumMousePointPosition.MouseSizeBottom;}   
     else   
     {return EnumMousePointPosition.MouseDrag;}   
    }   
   }   
  }   
 }   
 else   
 {return EnumMousePointPosition.MouseSizeNone;}   
}  
6、定义自己的MyMouseMove事件,在这个事件里,会使用上面设计的函数

   private void MyMouseMove(object sender,System.Windows.Forms.MouseEventArgs e)   
   {   
    Control lCtrl =(sender as Control);   
  
    if (e.Button==MouseButtons.Left)   
    {   
     switch (m_MousePointPosition)   
     {   
      case EnumMousePointPosition.MouseDrag:         
       lCtrl.Left =lCtrl.Left+ e.X - p.X;   
       lCtrl.Top =lCtrl.Top+ e.Y - p.Y;   
       break;   
      case EnumMousePointPosition.MouseSizeBottom:   
       lCtrl.Height = lCtrl.Height + e.Y - p1.Y;   
       p1.X=e.X;   
       p1.Y=e.Y; //'记录光标拖动的当前点   
       break;   
      case EnumMousePointPosition.MouseSizeBottomRight:   
       lCtrl.Width   = lCtrl.Width + e.X - p1.X;   
       lCtrl.Height = lCtrl.Height + e.Y - p1.Y;   
       p1.X=e.X;   
       p1.Y=e.Y; //'记录光标拖动的当前点   
       break;   
      case EnumMousePointPosition.MouseSizeRight:   
       lCtrl.Width   = lCtrl.Width + e.X - p1.X;         
//       lCtrl.Height = lCtrl.Height + e.Y - p1.Y;   
       p1.X=e.X;   
       p1.Y=e.Y; //'记录光标拖动的当前点   
       break;   
      case EnumMousePointPosition.MouseSizeTop:   
       lCtrl.Top   = lCtrl.Top + (e.Y - p.Y);   
       lCtrl.Height = lCtrl.Height - (e.Y - p.Y);   
       break;   
      case EnumMousePointPosition.MouseSizeLeft:   
       lCtrl.Left   = lCtrl.Left + e.X - p.X;   
       lCtrl.Width   = lCtrl.Width - (e.X - p.X);   
       break;   
      case EnumMousePointPosition.MouseSizeBottomLeft:   
       lCtrl.Left   = lCtrl.Left + e.X - p.X;   
       lCtrl.Width   = lCtrl.Width - (e.X - p.X);   
       lCtrl.Height = lCtrl.Height+ e.Y - p1.Y;   
       p1.X=e.X;   
       p1.Y=e.Y; //'记录光标拖动的当前点   
       break;   
      case EnumMousePointPosition.MouseSizeTopRight:   
       lCtrl.Top   = lCtrl.Top + (e.Y - p.Y);   
       lCtrl.Width   = lCtrl.Width + (e.X - p1.X);   
       lCtrl.Height = lCtrl.Height - (e.Y - p.Y);   
       p1.X=e.X;   
       p1.Y=e.Y; //'记录光标拖动的当前点   
       break;   
      case EnumMousePointPosition.MouseSizeTopLeft:   
       lCtrl.Left   = lCtrl.Left + e.X - p.X;   
       lCtrl.Top   = lCtrl.Top + (e.Y - p.Y);   
       lCtrl.Width   = lCtrl.Width - (e.X - p.X);   
       lCtrl.Height = lCtrl.Height - (e.Y - p.Y);   
       break;   
      default:   
       break;   
     }   
     if (lCtrl.Width<MinWidth) lCtrl.Width=MinWidth;   
     if (lCtrl.Height<MinHeight) lCtrl.Height=MinHeight;         
  
    }   
    else  
    {   
     m_MousePointPosition = MousePointPosition(lCtrl.Size, e);   //'判断光标的位置状态   
     switch (m_MousePointPosition)   //'改变光标   
     {   
      case EnumMousePointPosition.MouseSizeNone:   
       this.Cursor = Cursors.Arrow;        //'箭头   
       break;   
      case EnumMousePointPosition.MouseDrag:   
       this.Cursor = Cursors.SizeAll;      //'四方向   
       break;   
      case EnumMousePointPosition.MouseSizeBottom:   
       this.Cursor = Cursors.SizeNS;       //'南北   
       break;   
      case EnumMousePointPosition.MouseSizeTop:   
       this.Cursor = Cursors.SizeNS;       //'南北   
       break;   
      case EnumMousePointPosition.MouseSizeLeft:   
       this.Cursor = Cursors.SizeWE;       //'东西   
       break;   
      case EnumMousePointPosition.MouseSizeRight:   
       this.Cursor = Cursors.SizeWE;       //'东西   
       break;   
      case EnumMousePointPosition.MouseSizeBottomLeft:   
       this.Cursor = Cursors.SizeNESW;     //'东北到南西   
       break;   
      case EnumMousePointPosition.MouseSizeBottomRight:   
       this.Cursor = Cursors.SizeNWSE;     //'东南到西北   
       break;   
      case EnumMousePointPosition.MouseSizeTopLeft:   
       this.Cursor = Cursors.SizeNWSE;     //'东南到西北   
       break;   
      case EnumMousePointPosition.MouseSizeTopRight:   
       this.Cursor = Cursors.SizeNESW;     //'东北到南西   
       break;   
      default:   
       break;   
     }   
    }   
  
   }   
7、制作一个初始化过程,将界面panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,记得在Form初始化或者Form_Load时先执行它。

private void initProperty()   
{   
 for(int i = 0; i < this.panel1.Controls.Count; i++)   
 {   
  this.panel1.Controls[i].MouseDown+= new System.Windows.Forms.MouseEventHandler(MyMouseDown);   
  this.panel1.Controls[i].MouseLeave+= new System.EventHandler(MyMouseLeave);   
  this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);   


                this.groupPanel1.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
                this.groupPanel1.MouseLeave += new System.EventHandler(MyMouseLeave);
                this.groupPanel1.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
            
 }   
  
}
搜索更多相关主题的帖子: private 开关 软件 网上 
2014-04-23 11:09
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
得分:2 
上传完整程序也也许有朋友帮你看看,这个代码太多了

站在春哥的肩膀上
2014-04-25 16:27
wangtong0213
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-12-18
得分:0 
回复 2 楼 xydddaxia
完整程序更多哇~~更木有看了
tabcontrol可能太复杂了,我在他后边加了个panel就有反应能执行了~~~
2014-04-26 20:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-430516-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.700231 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved