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. httprunner(9)运行测试用例的方式总结

    前言 用过pytest的小伙伴都知道,pytest的运行方式是非常丰富的,可以说是你想怎么运行怎么运行,想运行哪些运行哪些,那httprunner是否同样可以呢? 运行用例的各种方式 运行指定路径的用 ...

  2. 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解

    题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...

  3. Docker项目demo

     Docker数据持久化 4.1 Volume (1)创建mysql数据库的container (2)查看volume (3)具体查看该volume docker volume inspect 485 ...

  4. ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解

    题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...

  5. TypeScript Interface vs Types All In One

    TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...

  6. Express All In One

    Express All In One express.js, node.js web framework # v4.17.1 Latest, on May 26, 2019 $ yarn add ex ...

  7. Bootstrap5 多级dropdown

    <div class="dropdown"> <a class="btn dropdown-toggle"> Dropdown link ...

  8. js 最简单的发布订阅模式

    let _subscriber: any; function autorun(subscriber: Function) { _subscriber = subscriber; _subscriber ...

  9. 画一个PBN大角度飞越转弯保护区

      今天出太阳了,尽管街上的行人依旧很少,但心情开始不那么沉闷了.朋友圈里除了关注疫情的最新变化之外,很多人已经开始选择读书或是和家人一起渡过这个最漫长的春节假期.陕西广电网络春节期间所有点播节目一律 ...

  10. 基于股票大数据分析的Python入门实战(视频教学版)的精彩插图汇总

    在我写的这本书,<基于股票大数据分析的Python入门实战(视频教学版)>里,用能吸引人的股票案例,带领大家入门Python的语法,数据分析和机器学习. 京东链接是这个:https://i ...