转一个 WPF 拖动 BUTTON 的代码

程序不大, 以前写过, 现在网上找个样码, 做得不错

Simple Drag And Drop a Wpf button Control - MSDN - Microsoft

这里面最后的一个代码

<Window x:Class="ouyLightUdpPWM.ouyMoveButtonWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ouyMoveButtonWin" Height="357" Width="648" Activated="Window_Activated" ContentRendered="Window_ContentRendered">
    <Grid x:Name="mainGrd">
        <Button x:Name="btnButton1" Content="Button" HorizontalAlignment="Left" Height="123" Margin="35,64,0,0" VerticalAlignment="Top" Width="192" MouseDown="btnButton_MouseDown" MouseMove="btnButton_MouseMove" MouseUp="btnButton_MouseUp" Click="btnButton_Click"/>

        <Button x:Name="btnButton2" Content="Button" HorizontalAlignment="Left" Height="123" Margin="294,64,0,0" VerticalAlignment="Top" Width="192" MouseDown="btnButton_MouseDown" MouseMove="btnButton_MouseMove" MouseUp="btnButton_MouseUp" Click="btnButton_Click"/>
        <Label x:Name="lbCoordinate" Content="Label" HorizontalAlignment="Left" Height="57" Margin="89,229,0,0" VerticalAlignment="Top" Width="269"/>

    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ouyLightUdpPWM
{
    /// <summary>
    /// Interaction logic for ouyMoveButtonWin.xaml
    /// </summary>
    public partial class ouyMoveButtonWin : Window
    {
        public ouyMoveButtonWin()
        {
            InitializeComponent();

            btnButton1.PreviewMouseUp += new MouseButtonEventHandler(btnButton_MouseUp);
            btnButton1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(btnButton_MouseDown);
            btnButton1.PreviewMouseMove += new MouseEventHandler(btnButton_MouseMove);

            btnButton2.PreviewMouseUp += new MouseButtonEventHandler(btnButton_MouseUp);
            btnButton2.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(btnButton_MouseDown);
            btnButton2.PreviewMouseMove += new MouseEventHandler(btnButton_MouseMove);

        }

        private void Window_Activated(object sender, EventArgs e)
        {
           
        }



        double m_MouseX;
        double m_MouseY;
        private void btnButton_MouseDown(object sender, MouseButtonEventArgs e)
        {

            m_MouseX = e.GetPosition(this).X;
            m_MouseY = e.GetPosition(this).Y;
        }

        private void btnButton_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.LeftButton == MouseButtonState.Pressed)
            {


                // Capture the mouse for border
                Button bb = (Button)  sender;
                e.MouseDevice.Capture(bb);
                System.Windows.Thickness _margin = new System.Windows.Thickness();
                int _tempX = Convert.ToInt32(e.GetPosition(this).X);
                int _tempY = Convert.ToInt32(e.GetPosition(this).Y);
                _margin = bb.Margin;

               

                // when While moving _tempX get greater than m_MouseX relative to usercontrol 
                if (m_MouseX > _tempX)
                {
                    // add the difference of both to Left
                    _margin.Left += (_tempX - m_MouseX);
                    // subtract the difference of both to Left
                    _margin.Right -= (_tempX - m_MouseX);

                    Console.WriteLine("========================>>>>>>>>>  ++++++++++++++ xxxxxxx");
                }
                else
                {
                    _margin.Left -= (m_MouseX - _tempX);
                    _margin.Right -= (_tempX - m_MouseX);
                    Console.WriteLine("========================>>>>>>>>>  ------------- xxxxxxx");

                }
                if (m_MouseY > _tempY)
                {
                    _margin.Top += (_tempY - m_MouseY);
                    _margin.Bottom -= (_tempY - m_MouseY);

                    Console.WriteLine("========================>>>>>>>>>  ++++++++++++++ yyyyyyyyyyyy");

                }
                else
                {
                    _margin.Top -= (m_MouseY - _tempY);
                    _margin.Bottom -= (_tempY - m_MouseY);

                    Console.WriteLine("========================>>>>>>>>>  --------- yyyyyyyyyyyy");

                }

                bb.Margin = _margin;
                m_MouseX = _tempX;
                m_MouseY = _tempY;

                lbCoordinate.Content = "x: " + _margin.Left + "\n" + "y : " + _margin.Top; 
 
            }
        }

        private void btnButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            e.MouseDevice.Capture(null);
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {

    
            Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxx  --------- yyyyyyyyyyyy");
           
        }

        private void btnButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

 

转载于:https://www.cnblogs.com/ouyang800/articles/7086610.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值