zoukankan      html  css  js  c++  java
  • 粘性Snap-to-Center控制

    介绍 本文展示了如何在MouseEnter上将鼠标指针拖动到控件的中心并保持一段时间。在分配的时间(默认是1/4秒)之后,控件释放它对光标的控制。这个方法适用于任何控件-下面的例子有一个按钮和一个PictureBox: 背景 我创建了一个工具,它的窗口可以调整为一个非常小的界面,以避免妨碍(如WinAmp -一个没有标题的小工具条)。这些按钮在最小尺寸时,很难按。我需要一种方法来引导用户到按钮的中心,这样他们就不会意外地点击邻接的按钮。 当这个:扩展到这个:时,很难将鼠标移到按钮上。 通过实现这些事件,用户可以轻松地将鼠标放置在控件上,即使它太小而无法精确地单击。粘着按钮的感觉也会很快结束,所以它不会成为用户的烦恼。 使用的代码 要使控件具有粘性,您需要将其鼠标事件MouseEnter和MouseMove设置为StartSnap和CheckSnap事件处理程序。 c# 隐藏,收缩,复制Code

    public partial class Form1 : Form
    {
        // When the Snap Hold should break
        private DateTime dtSnapEnd;
        // Duration of the Snap Hold - set this value to 
        // determine how long we hold the cursor
        private double dReleaseTime = 250;
    
        private void SnapToCenter(Control control)
        {
            Point pCenter = control.PointToScreen(new Point(0, 0));
            // get the center of the control
            pCenter.X += control.Width / 2;
            pCenter.Y += control.Height / 2;
            // set the cursor position to the center
            Cursor.Position = pCenter;
        }
    
        // Event to handle MouseEnter events
        private void StartSnap(object sender, EventArgs e)
        {
            // start by centering the mouse
            SnapToCenter((Control)sender);
            // set the time when the snap should break
            dtSnapEnd = DateTime.Now.AddMilliseconds(dReleaseTime);
        }
    
        // Event to handle MouseMove events
        private void CheckSnap(object sender, MouseEventArgs e)
        {
            // if we have not passed the Snap End time,
            // then center the mouse
            if (DateTime.Now < dtSnapEnd)
            {
                SnapToCenter((Control)sender);
            }
        }
    
        //... 
    }

    在设计视图: 在窗体中添加控件。在控件属性上,切换到事件,并将MouseEnter事件设置为StartSnap。将MouseMove事件设置为CheckSnap。 VB.NET 在Visual Basic中,我们需要用不同的方式处理事件,因为VB。NET不使用通用事件处理程序(至少从设计接口): 创建窗体并向其添加控件。在表单的Form_Load事件中,为每个控件添加以下内容: 隐藏,复制Code

    AddHandler Button1.MouseEnter, AddressOf StartSnap
    AddHandler Button1.MouseMove, AddressOf CheckSnap
    
    AddHandler YourControlHere.MouseEnter, AddressOf StartSnap
    AddHandler YourControlHere.MouseMove, AddressOf CheckSnap

    这里是完整的VB代码来处理事件: 隐藏,收缩,复制Code

    Public Class Form1
    
        ' When the Snap Hold should break
        Private dSnapEnd As Date
        ' Duration of the Snap Hold - set this value to 
        ' determine how long we hold the cursor
        Private dReleaseTime As Double = 250
    
        Private Sub SnapToCenter(ByVal control As Control)
            Dim pCenter As Point
            ' get the center of the control
            pCenter.X = control.Width / 2
            pCenter.Y = control.Height / 2
            ' Set the cursor to the center
            Cursor.Position = control.PointToScreen(pCenter)
        End Sub
    
        ' Event to handle MouseEnter events
        Private Sub StartSnap(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs)
            ' Start by snapping the cursor to the center
            SnapToCenter(CType(sender, Control))
    
            ' Set the starting and ending snap times
            dSnapEnd = dSnapStart.AddMilliseconds(dReleaseTime)
        End Sub
    
        ' Event to handle MouseMove events
        Private Sub CheckSnap(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs)
            ' If we've not reached the SnapEnd time, center the mouse
            If Date.Now < dSnapEnd Then
                SnapToCenter(CType(sender, Control))
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
            ' Add the event handlers for each control.
            AddHandler Button1.MouseEnter, AddressOf StartSnap
            AddHandler Button1.MouseMove, AddressOf CheckSnap
    
            AddHandler YourControlHere.MouseEnter, AddressOf StartSnap
            AddHandler YourControlHere.MouseMove, AddressOf CheckSnap
        End Sub
    
    End Class

    确认 感谢Karl Moore为控件上的光标定心代码。 历史 2008.09.17 -初始文章与示例应用程序。 本文转载于:http://www.diyabc.com/frontweb/news507.html

  • 相关阅读:
    备忘: Visual Studio 2013 VC++ IDE 使用小贴示。
    获取动态数组指针 所指向数组长度的一种方法
    备忘:C++ 类 (初学之实录)。
    备忘:VC++ 中的异常处理
    备忘: C++中的 vector 容器
    备忘:C++基础 -- 数据类型的学习总结
    Windws Server 2008 R2 WEB环境配置之MYSQL 5.6.22安装配置
    Windows Server 2008 R2 WEB服务器配置系列文章索引
    php学习过程二
    php学习过程一
  • 原文地址:https://www.cnblogs.com/Dincat/p/13450617.html
Copyright © 2011-2022 走看看