博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
美女跟我走
阅读量:6239 次
发布时间:2019-06-22

本文共 2892 字,大约阅读时间需要 9 分钟。

hot3.png

前台:

<UserControl x:Class="SilverlightApplication1.SilverlightControl1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid Background="White" Name="LayOutGrid">
        <Button  Height="70" HorizontalAlignment="Left"  ClickMode="Hover"
                   Margin="120,120,0,0" Name="button1" VerticalAlignment="Top" Width="164" MouseLeftButtonDown="button1_MouseLeftButtonDown" MouseLeftButtonUp="button1_MouseLeftButtonUp" MouseMove="button1_MouseMove">
            <Button.Content>
                <StackPanel Orientation="Horizontal" Width="157">
                    <Image Source="/SilverlightApplication1;component/img/1.jpg" Width="62"></Image>
                    <TextBlock FontSize="18" Text="跟我走"  Height="46" Margin="20" Width="64"></TextBlock>
                </StackPanel>
            </Button.Content>
        </Button>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="171,240,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Canvas.Left="-25" Canvas.Top="-8" />
    </Grid>
</UserControl>

后台:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
    public partial class SilverlightControl1 : UserControl
    {
        public SilverlightControl1()
        {
            InitializeComponent();
           
        }
        Point mousePosition;
        bool trackingMouseMove = false;
        //开始拖动对象
        private void button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            textBlock1.Text = "Test";
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = true;
            mousePosition = e.GetPosition(null);
            if (null != element)
            {
                //激活button拖动
                element.CaptureMouse();
                element.Cursor = Cursors.Hand;
            }
        }
        //拖动完成
        private void button1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            //停止button拖动
            element.ReleaseMouseCapture();
            element.Cursor = null;
            trackingMouseMove = false;
            mousePosition.X = mousePosition.Y = 0;
        }
        //移动对象
        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double daltaV = e.GetPosition(null).X - mousePosition.X;
                double daltaH = e.GetPosition(null).Y - mousePosition.Y;
                Thickness tk = button1.Margin;
                double newTop = daltaH + tk.Top;
                double newLeft = daltaV + tk.Left;
              
                tk.Top = newTop;
                tk.Left = newLeft;
                element.SetValue(Grid.MarginProperty, tk);
               
                mousePosition = e.GetPosition(null);
                textBlock1.Text = "X=" + tk.Top + ",Y=" + tk.Left + "," + tk.Bottom + "," + tk.Right;
            }
        }
    }
}

原文链接:

转载于:https://my.oschina.net/changpinghu/blog/92487

你可能感兴趣的文章
Sql Server内置函数实现MD5加密
查看>>
2017-2018-1 期中教学检查教师自查表
查看>>
Attention[Content]
查看>>
docker下部署spring boot
查看>>
【Android Studio安装部署系列】十九、Android studio使用SVN
查看>>
java 按概率产生
查看>>
设计模式(26)-----创建型模式-----建造者模式
查看>>
excel读写技术-:ADO.NET 如何读取 Excel
查看>>
纯前端表格控件SpreadJS与Java结合,实现模板上传和下载等功能
查看>>
推荐 5 款超好用的 Chrome 浏览器插件,文末有从别人的电脑移植插件的方法
查看>>
几种实现延时任务的方式(二)
查看>>
ReactNative:require & import
查看>>
MaxCompute新功能发布
查看>>
decorator(修饰器)的业务应用
查看>>
ES6系列-- 8. Symbol
查看>>
要点提炼| Gradle指南
查看>>
Hexo Next底部powered by的logo栏更改以及注意事项(附官方文档,文末有福利链)
查看>>
我是如何进入阿里巴巴的-面向春招应届生Java面试指南(七)
查看>>
Android Studio 打包生成的 apk 安装包装到手机上闪退
查看>>
Mybatis技术内幕:初始化之加载 mybatis-config
查看>>