C# Control Drag and Move

 C# Control Drag and Move

主要是设计控件的MouseDown、MouseLeave、MouseMove事件。一步步来吧:
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);
   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
Android Drag and Drop 是 Android 中的一个功能,可以让用户轻松地拖放对象和其他UI元素。 这个功能可以在许多应用程序中使用,例如拖放图像、文件、文本等等。如果你想要学习如何使用 Drag and Drop 功能,可以查阅 Android 的官方文档。这里有一个示例代码可以供参考: 首先在 XML 布局文件中添加两个视图,一个用作拖动源,另一个用作目标: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/source" android:text="Drag Me" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:background="#FFCC33" android:gravity="center" android:textColor="#FFFFFF" android:textSize="20sp" android:layout_margin="10dp" android:longClickable="true" android:tag="Dragabble Text View"/> <TextView android:id="@+id/target" android:text="Drop Here" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:background="#333333" android:gravity="center" android:textColor="#FFFFFF" android:textSize="20sp" android:layout_margin="10dp" android:tag="Droppable Text View"/> </LinearLayout> ``` 然后在 Activity 中实现 drag and drop 的逻辑: ``` public class MainActivity extends Activity implements View.OnLongClickListener, View.OnDragListener { private TextView sourceTextView; private TextView targetTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sourceTextView = (TextView) findViewById(R.id.source); sourceTextView.setOnLongClickListener(this); targetTextView = (TextView) findViewById(R.id.target); targetTextView.setOnDragListener(this); } @Override public boolean onLongClick(View v) { ClipData.Item item = new ClipData.Item((CharSequence) v.getTag()); String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); v.startDrag(data, shadowBuilder, v, 0); return true; } @Override public boolean onDrag(View v, DragEvent event) { int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: // do nothing break; case DragEvent.ACTION_DRAG_ENTERED: // change the target view background color v.setBackgroundColor(Color.YELLOW); break; case DragEvent.ACTION_DRAG_EXITED: // reset the target view background color v.setBackgroundColor(Color.GRAY); break; case DragEvent.ACTION_DROP: // handle the dragged data ClipData.Item item = event.getClipData().getItemAt(0); String dragData = item.getText().toString(); targetTextView.setText(dragData); break; case DragEvent.ACTION_DRAG_ENDED: // reset the source and target view background color sourceTextView.setBackgroundColor(Color.YELLOW); targetTextView.setBackgroundColor(Color.GRAY); break; default: break; } return true; } } ``` 这个代码演示了一个简单的拖放操作。当用户长按源 TextView 时,他/她可以拖动它到目标 TextView 上面。当放手时,目标视图会显示源视图上的文本。在本例中,通过实现 OnLongClickListener 和 OnDragListener 接口来处理拖放操作。如果你需要更高级的 Drag and Drop 操作,可以查阅 Android 的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值