zoukankan      html  css  js  c++  java
  • Silverlight中按钮实现简单的拖放功能注意点

       <Canvas Background="#46461F">
            <Button
               MouseLeftButtonDown="OnMouseDown"
               MouseMove="OnMouseMove"
               MouseLeftButtonUp="OnMouseUp"
               Canvas.Left="50" Canvas.Top="50" Background="Red"
               FontSize="18"         
                ClickMode="Hover"
               Width="160" Height="80">
                <Button.Content>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
                                VerticalAlignment="Center">
                    
                        <TextBlock Text="拖动我" VerticalAlignment="Center" Margin="10"></TextBlock>
                    </StackPanel>
                </Button.Content>
            </Button>
        </Canvas>

    红色为注意点,否则按钮触发不了MouseLeftButtonDown与MouseLeftButtonUp

     

     

     

     以下为事件 

      bool trackingMouseMove = false;
            Point mousePosition;

            void OnMouseDown(object sender, MouseButtonEventArgs e)
            {
                FrameworkElement element = sender as FrameworkElement;
                mousePosition = e.GetPosition(null);
                trackingMouseMove = true;
                if (null != element)
                {
                    element.CaptureMouse();
                    element.Cursor = Cursors.Hand;
                }
            }

            void OnMouseMove(object sender, MouseEventArgs e)
            {
                FrameworkElement element = sender as FrameworkElement;
                if (trackingMouseMove)
                {
                    double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                    double deltaH = e.GetPosition(null).X - mousePosition.X;
                    double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);

                    element.SetValue(Canvas.TopProperty, newTop);
                    element.SetValue(Canvas.LeftProperty, newLeft);

                    mousePosition = e.GetPosition(null);
                }
            }

            void OnMouseUp(object sender, MouseButtonEventArgs e)
            {
                FrameworkElement element = sender as FrameworkElement;
                trackingMouseMove = false;
                element.ReleaseMouseCapture();

                mousePosition.X = mousePosition.Y = 0;
                element.Cursor = null;
            }

     

  • 相关阅读:
    错误:IIS Admin Service 服务因 2147549465 (0x80010119) 服务性错误而停止(转)
    学习:Using STSADM o migrateuser on a recreated account(转)
    记录:MOSS里中英文权限对照表
    错误:用stsdev创建的解决方案:Makecab.exe — error MSB3073
    记录:MOSS:EventHandler部署和使用
    学习:C#中的String、string(转)
    学习:SQL数据库日志收缩(转)
    学习:SharePoint 使用 SPQuery.Folder 查询文件夹中的数据(转)
    学习:双机热备、集群、负载均衡、SQL故障转移群集简单理解(转)
    学习:[SharePoint]HTTP 500 Internal Server Error (转)
  • 原文地址:https://www.cnblogs.com/lbg280/p/1639249.html
Copyright © 2011-2022 走看看