1.主窗体下部添加一个Panel乘放ToolStrip控件以实现ToolStrip在窗体下部定位。
2.当ToolStrip控件中子控件超出屏幕时,拖动控件可以实现滑动效果。拖动到控件边缘距窗体边缘1/3宽度时(可设),自动回弹。拖动控件边缘在屏幕内时释放鼠标,控件自动回弹,边缘吸附窗体边缘。
3.当ToolStrip控件中子控件数目较少可以在屏幕上完全显示时,拖动效果不可见。
4.增加 添加、删除 按钮,点击时可增删一个ToolStripButton,方便拖动效果可见(ToolStrip控件中子控件超出屏幕)与不可见(ToolStrip控件中子控件可以在屏幕上完全显示时)的演示。
5.拖动鼠标离开ToolStrip控件再释放,不会触发MouseUp事件,引起控件边缘在屏幕中时释放鼠标自动吸附效果失效。待解决。

源码:http://files.cnblogs.com/files/tobeforever/DragDemo.rar

参考文章:WinForm 实现鼠标拖动控件跟随效果(图文) @SkySoot

http://www.cnblogs.com/SkySoot/archive/2011/12/20/2294733.html

 /*==================================================================================================
** 类 名 称:FrmDragTest
** 创 建 人:liu
** 当前版本:V1.0.0
** CLR 版本:4.0.30319.42000
** 创建时间:2017/5/6 19:53:44 ** 修改人 修改时间 修改后版本 修改内容 ** 功能描述:ToolStrip控件左右拖拽移动效果实现 * 主窗体下部添加一个Panel乘放ToolStrip控件以实现ToolStrip在窗体下部定位。
* 当ToolStrip控件中子控件超出屏幕时,拖动控件可以实现滑动效果。拖动到控件边缘距窗体边缘1/3宽度时(可设),
自动回弹。拖动控件边缘在屏幕内时释放鼠标,控件自动回弹,边缘吸附窗体边缘。
* 当ToolStrip控件中子控件数目较少可以在屏幕上完全显示时,拖动效果不可见。
* 增加 添加、删除 按钮,点击时可增删一个ToolStripButton,方便拖动效果可见(ToolStrip控件中子控件超出屏幕)
与不可见(ToolStrip控件中子控件可以在屏幕上完全显示时)的演示。
* 拖动鼠标离开ToolStrip控件再释放,不会触发MouseUp事件,引起控件边缘在屏幕中时释放鼠标自动吸附效果失效。待解决。 ==================================================================================================
Copyright @2017. liu. All rights reserved.
==================================================================================================*/
using System;
using System.Drawing;
using System.Windows.Forms; namespace DragDemo
{
public partial class FrmDragTest : Form
{
#region 字段 /// <summary>
/// 被拖动的ToolStrip控件
/// </summary>
private ToolStrip _toolStrip; /// <summary>
/// 当前ToolStripButton数目
/// </summary>
private int _itemCount; #endregion #region 构造 public FrmDragTest()
{
InitializeComponent();
} #endregion #region 初始化 private void FrmTest_Load(object sender, EventArgs e)
{
this.Size = new Size(, );
// 窗体大小调整时,检查边缘是否留白
this.Resize += (sender1, e1) => { CheckBlank(); };
// 添加一个Panel乘放ToolStrip控件以实现ToolStrip在窗体下部定位
var pnlmain = new Panel { Dock = DockStyle.Bottom, Height = }; _toolStrip = new ToolStrip
{
// 注意Dock属性必须为None
Dock = DockStyle.None,
AutoSize = true,
//BackgroundImageLayout = ImageLayout.None,
GripStyle = ToolStripGripStyle.Hidden,
// 子项除超出屏幕显示时不溢出显示
LayoutStyle = ToolStripLayoutStyle.Flow,
Height = ,
Location = new Point(, ),
ImageScalingSize = new Size(, )
}; //默认添加些Button以显示效果
for (int j = ; j < ; j++)
{
AddOneButton();
} pnlmain.Controls.Add(_toolStrip);
this.Controls.Add(pnlmain); // 添加键
var btnAdd = new Button { Size = new Size(, ), Text = @"Add", Location = new Point(, ) };
btnAdd.Click += btnAdd_Click;
this.Controls.Add(btnAdd);
// 移除键
var btnRemove = new Button { Size = new Size(, ), Text = @"Remove", Location = new Point(, ) };
btnRemove.Click += btnRemove_Click;
this.Controls.Add(btnRemove);
} #endregion #region 自动添加移除 private void btnAdd_Click(object sender, EventArgs e)
{
AddOneButton();
} private void btnRemove_Click(object sender, EventArgs e)
{
RemoveOneButton();
} /// <summary>
/// 向_toolStrip添加一个Button
/// </summary>
private void AddOneButton()
{
var tsbtn = new ToolStripButton
{
AutoSize = false,
DisplayStyle = ToolStripItemDisplayStyle.Text,
Text = (++_itemCount).ToString(),
Size = new Size(, ),
BackColor = Color.YellowGreen,
Margin = new Padding(, , , )
};
tsbtn.MouseDown += Controls_MouseDown;
tsbtn.MouseMove += Controls_MouseMove;
tsbtn.MouseUp += Controls_MouseUp;
_toolStrip.Items.Add(tsbtn);
} /// <summary>
/// 移除队尾的Button
/// </summary>
private void RemoveOneButton()
{
_toolStrip.Items.RemoveAt(--_itemCount);
} #endregion #region 拖动效果实现 /*
* 理解了下面的几个概念,就能完全明白相对坐标的变化.
* Cursor.Position 获取的是相对于用户屏幕的光标坐标
* PointToClient() 方法可将屏幕坐标 Cursor.Position 换算成工作区的坐标
*/ /// <summary>
/// 保存拖动前鼠标X坐标
/// </summary>
private int _curX;
/// <summary>
/// 保存拖动前_ToolStrip控件X坐标
/// </summary>
private int _oldToolStripX; /// <summary>
/// 按键按下,记录当前光标X坐标与拖动前_ToolStrip控件X坐标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Controls_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 获取当前光标X坐标
_curX = Cursor.Position.X;
// 获取拖动前_ToolStrip控件X坐标
_oldToolStripX = _toolStrip.Location.X;
}
} /// <summary>
/// 鼠标移动,拖动_ToolStrip控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Controls_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// x最小值,对应右侧拖动到边界时x坐标值
int minX = this.Width - _toolStrip.Width;
// 图标当前可以全部显示,拖动效果不可见
if (minX > ) return; // _ToolStrip控件X轴新坐标
// 当前鼠标X坐标-拖动前鼠标X坐标=X轴鼠标偏移量,加上拖动前控件X坐标,即为新坐标
int newToolStripX = Cursor.Position.X - _curX + _oldToolStripX; // 右侧空白超过屏幕宽度1/3,自动回弹
if (newToolStripX < minX - this.Width / )
{
// 左拖动过度,修正
newToolStripX = minX;
}
// 左侧空白超过屏幕宽度1/3,自动回弹
else if (newToolStripX > this.Width / )
{
// 右拖动过多,修正
newToolStripX = ;
} _toolStrip.Location = new Point(newToolStripX, );
}
} /// <summary>
/// 鼠标松开,检查两侧是否留白
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Controls_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CheckBlank();
}
} /// <summary>
/// 检查两侧是否留白
/// </summary>
private void CheckBlank()
{ // x最小值,对应右侧拖动到边界时x坐标值
int minX = this.Width - _toolStrip.Width;
// 图标全部显示,拖动效果不可见
if (minX > ) return; // 左边界限制,左侧不留空
if (_toolStrip.Location.X > )
{
_toolStrip.Location = new Point(, );
}
// 右边界限制,右侧不留空
else if (_toolStrip.Location.X < minX)
{
_toolStrip.Location = new Point(minX, );
} } #endregion
}
}

ToolStrip控件左右拖拽移动效果实现的更多相关文章

  1. PyQt5控件支持拖拽方法

    让控件支持拖拽动作A.setDragEnable(True) 设置A可以拖动B.setAcceptDrops(True) 设置B可以接受拖动B需要满足两个事件1.dragEnterEvent 将A拖到 ...

  2. 【Android】HorizontalScrollView内子控件横向拖拽

    前言 网上ListView上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下. 声明 欢迎转载,但请保留文章原始出处:)  博客园 ...

  3. 【C#/WPF】UI控件的拖拽/拉伸

    需求①:控件拖拽——按住鼠标,可自由拖拽控件. 方法:目前看到的办法有两种. 使用ZoomableCanvas:http://www.cnblogs.com/gnielee/archive/2011/ ...

  4. C#开发PACS医学影像处理系统(九):序列控件与拖拽

    1.先看结构: 创建WPF用户控件:YourTab 创建WPF用户控件:YourItem 创建选项卡时循环添加item,并设置序列缩略图到控件和异步下载的进度条, 1个病人1个或多个Study检查,1 ...

  5. 怎样在delphi中实现控件的拖拽

    下面这2种方法都能实现对控件和窗体的拖拽 方法1 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift ...

  6. WPF 实现控件间拖拽内容

    想实现这样一个常用功能:在ListBox的一个Item上点住左键,然后拖拽到另外一个控件(如ListView中),松开左键,数据已经拖拽过来. 步骤如下: 1. 设置ListBox 的AllowDro ...

  7. winform上控件的拖拽小结

    这里罗列出几个相关的事件和属性,具体的实现介绍已有非常优秀的文章了,文章末尾我将会给出,大家可以去参考. 属性: AllowDrop: 目标控件必须设定为true,才能接受拖拽来的东西. 事件: It ...

  8. HTML5 02. 多媒体控件、拖拽事件、历史记录、web存储、应用程序缓存、地理定位、网络状态

    多媒体 video:是行内块(text-align: center; 对行内块适用) <figure></figure>: 多媒体标签 : <figcaption> ...

  9. 【转】在delphi中实现控件的拖拽

    提示:可以添加一个布尔来控制可否拖动的状态,这里提供所有都能拖动的方法. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseB ...

随机推荐

  1. .netCore+Vue 搭建的简捷开发框架--目录

    .netCore+Vue 搭建的简捷开发框架 .netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用 .netCore+Vue 搭建的简捷开发框架 (3)-- Ser ...

  2. drf框架中所有视图及用法

    0909自我总结 drf框架中所有视图及用法 一.drf框架中的所有视图类 from django.views import View from rest_framework import views ...

  3. JAVA中JDK开发环搭的搭建,jvm jre

    1.JDK的下载与安装: www.oracle.com 安装需要注意的是:不能把jdk安装到有空格或中文的文件夹中,建议大家在某个目录下创建一个JavaWeb的文件夹,然后把所学的java所有内容(后 ...

  4. 使用java语言实现一个动态数组(详解)(数据结构)

    废话不多说,上代码 1.从类名开始(我真是太贴心了) public class Array<E> 首先数组类需要带有泛型,这个不多说.需要注意的是在java中,数组只能存放同一个类型的. ...

  5. 朋友外包干了5年java,居然不知道dubbo-monitor是怎么用的?

    Dubbo工具--dubbo-monitor监控平台的发布和使用 1)下载 https://github.com/alibaba/dubbo/archive/dubbo-2.5.8.zip 2)编译 ...

  6. HTML5 lufylegend引擎学习(一) -- 剪刀石头布小游戏

    网址:http://www.lufylegend.com/ <!DOCTYPE html> <html> <head> <title>A Little ...

  7. Making Dishes (P3243 [HNOI2015]菜肴制作)

    Background\text{Background}Background I've got that Luogu Dialy has been \text{I've got that Luogu D ...

  8. bcache 状态/配置 文件详细介绍(翻译自官网)

    声明: 文中 斜体带下划线  的段落为翻译不够准确的段落 原文:https://www.kernel.org/doc/Documentation/bcache.txt 官网:https://bcach ...

  9. golang会取代php吗

    看看PHP和Golang如何在开发速度,性能,安全性,可伸缩性等方面展开合作. PHP与Golang比较是一个艰难的比较. PHP最初创建于1994年,已有24年.自那时起,由于PHP的开源格式,易用 ...

  10. 16.Nginx HTTPS实践

    1.不做任何修改实现http跳转https(协议间的跳转): return [root@web01 conf.d]# cat url.cheng.com.conf server { listen 80 ...