1. 拖拽窗体

使用 System.Windows.Window 自带的 DragMove() 方法即可识别窗体拖动。

DragMove();

2. 拖拽控件:复制、移动控件

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="Rectangle">
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Grid.Resources> <Border Grid.Column="0" BorderBrush="LightSkyBlue" BorderThickness="2" MouseLeftButtonDown="Add_MouseLeftButtonDown">
<StackPanel x:Name="sp">
<Rectangle Fill="#FF113355"/>
<Rectangle Fill="#FF33AA77"/>
<Rectangle Fill="#FFBB2200"/>
<Rectangle Fill="#FFDD0077"/>
</StackPanel>
</Border> <Border Grid.Column="1" BorderBrush="LightSkyBlue" BorderThickness="2" MouseLeftButtonDown="Move_MouseLeftButtonDown">
<Canvas x:Name="cav"> </Canvas>
</Border>
</Grid> ... public partial class VisualWindow : Window
{
public VisualWindow()
{
InitializeComponent();
} Rectangle SelectedRect { get; set; } /// <summary>
/// 如果点击右侧可新增的矩形,则右侧 Canvas 将新增一个矩形
/// </summary>
private void Add_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(this.sp); if (e.Source.GetType() != typeof(Rectangle)) return; InitRectangle((Rectangle)e.Source); this.cav.Children.Add(SelectedRect); this.MouseMove += VisualWindow_MouseMove;
this.MouseLeftButtonUp += VisualWindow_MouseLeftButtonUp;
} /// <summary>
/// 将 SelectedRect 指向一个新的 Rectangle
/// </summary>
private void InitRectangle(Rectangle rect)
{
SelectedRect = new Rectangle();
SelectedRect.Width = rect.Width;
SelectedRect.Height = rect.Height;
SelectedRect.Fill = rect.Fill;
SelectedRect.Opacity = 0;
} /// <summary>
/// 如果目标被拖入指定区域,opacity 设为 0.5,并跟随鼠标,直到鼠标释放或鼠标离开指定区域
/// </summary>
private void VisualWindow_MouseMove(object sender, MouseEventArgs e)
{
if (SelectedRect == null) return; Point point = e.GetPosition(this.cav); if (point.X < 0 || point.Y < 0)
{
SelectedRect.Opacity = 0;
return;
} SelectedRect.Opacity = 0.5; Canvas.SetLeft(SelectedRect, point.X - SelectedRect.Width / 2);
Canvas.SetTop(SelectedRect, point.Y - SelectedRect.Height / 2);
} /// <summary>
/// 如鼠标离开指定区域(这里可以简单以 SelectedRect.Opacity == 0 作为标记),则移除刚新增的矩形
/// 如鼠标在指定区域释放,则将新增的矩形停在鼠标释放的位置
/// </summary>
private void VisualWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(this.cav);
bool isMouseOutOfCav = point.X < 0 || point.Y < 0; if (SelectedRect != null && isMouseOutOfCav)
{
this.cav.Children.Remove(SelectedRect);
} SelectedRect.Opacity = 1; this.MouseMove -= VisualWindow_MouseMove;
this.MouseLeftButtonUp -= VisualWindow_MouseLeftButtonUp;
} /// <summary>
/// 移动矩形
/// </summary>
private void Move_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.Source.GetType() != typeof(Rectangle)) return; SelectedRect = (Rectangle)e.Source; this.MouseMove += VisualWindow_MouseMove;
this.MouseLeftButtonUp += VisualWindow_MouseLeftButtonUp;
}
}

效果:





WPF 应用 - 拖拽窗体、控件的更多相关文章

  1. ios 为什么拖拽的控件为weak 手写的strong

    ib拖拽的控件自动声明为weak  而平时自己手写的为strong 在ios中,对象默认都是强引用,不是强引用赋值后会立即释放 ib声明weak 不立即被释放 简单说就是 1.声明的弱引用指向强引用 ...

  2. swift方法 的写法,ui上拖拽的控件到controller里面的方法

    直接点xcode右上角三个按键中间一下,左右拆分为storyboard和controller, 点击button,按ctrl,然后拖拽到controller里面即可生成对应的点击事件在controll ...

  3. C# WPF可拖拽的TabControl

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. C# WPF可拖拽的TabControl 阅读导航 本文背景 代码实现 本文参考 源码 1. ...

  4. Winfon 页签切换及窗体控件自适应

    由于公司的业务调整,最近不仅开发bs,还有不熟悉的cs,人手也不足,项目还多,对于cs来说,算是小白,虽然是一个人,也是硬着头皮写,拖拽控件,自定义控件.一个项目下来,对cs有了很深的认识,这里好好感 ...

  5. WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)

    Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...

  6. C#窗体控件拖动

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. WPF 使用鼠标拖动一个控件的实现[2018.7.15]

    原文:WPF 使用鼠标拖动一个控件的实现[2018.7.15] Q:已经把一个Shape和一个TextBlock组合起来放到了一个Grid中,现在想要实现用鼠标拖动这个Grid到任意位置的功能,如何做 ...

  8. .NET成人礼 | 还记得20年前一起拖过的控件吗?

    本文是MVP Ediwang写的回忆一个80后的拖控件的感悟,与君共勉: 每一代人都有记忆里的味道.煤球炉.黑白电视机是属于父母的记忆.而“拖控件”式编程,启蒙了无数像我这样的80后(嗯,89也算80 ...

  9. C# winform 跨线程更改窗体控件的属性

    当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...

随机推荐

  1. [Golang]-7 定时器和打点器

    目录 定时器 打点器 After()方法 我们常常需要在未来某个时刻运行 Go 代码,或者在某段时间间隔内重复运行. Go 的内置 定时器 和 打点器 特性让这些很容易实现. 定时器 type Tim ...

  2. Redis 多实例 & 主从复制

    Redis 多实例 多实例目录 [root@db01 ~]# mkdir /service/redis/{6380,6381} 多实例配置文件 # 第一台多实例配置 [root@db01 ~]# vi ...

  3. 弹性伸缩 AS(Auto Scaling)

    根据业务需求和策略设置伸缩规则,在业务需求增长时自动为您增加 ECS 实例以保证计算能力,在业务需求下降时自动减少 ECS 实例以节约成本,弹性伸缩不仅适合业务量不断波动的应用程序,同时也适合业务量稳 ...

  4. Leetcode(29)-两数相除

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...

  5. vi, vim 使用教程

    vim 使用教程 ```shcd lscd ../cd -pwdcprmmkdirtarmvmbtar -zcfchmodsshtopqfree ``` 数字0含空字符到行首,^不含空字符到行首.G移 ...

  6. js script all in one

    js script all in one 你不知道的 js secret https://html.spec.whatwg.org/multipage/scripting.html https://h ...

  7. random array & shuffle 洗牌算法 / 随机算法

    random array & shuffle shuffle 洗牌算法 / 随机算法 https://en.wikipedia.org/wiki/Fisher–Yates_shuffle ES ...

  8. GitHub Classroom

    GitHub Classroom GitHub Education https://classroom.github.com/classrooms https://classroom.github.c ...

  9. Free Serverless

    Free Serverless BFF https://cloud.google.com/functions/ 微服务 Function as a Servcie,FaaS https://segme ...

  10. 为什么说NGK引领全球数字资产财富革命

    进入2020年,区块链万业迸发出巨大的能量,事实上区块链和数字经济正是未来全球财富的新方向.区块链和数字货币的增值空间巨大,是数字时代新的经济增长点.目前,全球衍生品市场价值532万亿美元,全球债务市 ...