惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - AskWeee

基于 Raphael 的 Web UI 设计 - 初稿 Bootstrap变形记 silverlight页面设计,记录之,也算是成长的过程 WPF 还有多少人在用? WPF教程尝试(修正部分格式) 系统管理工具集锦 - AskWeee - 博客园 有关XXX产品研发管理约定 有关软件版本管理 取之于民用之于民,分享一个云计算PPT(普及版) 学习笔记 - 002 学习笔记 - 001 JIRA + FishEye + Crucible + Confluence + PostgreSQL 测试,无意中我和你遭遇 温习:DIV CSS JS 导航菜单制作 - AskWeee 程序人生的后半生 专利之家:让灵感冒冒泡 转帖:专利之家 转帖:曲线路标北京前门上岗 中国移动:引领3G生活
WPF教程.002
AskWeee · 2012-01-12 · via 博客园 - AskWeee

让窗口动起来:

背景:因为使用了WindowStyle = “None”,所以窗口无法移动了;

需求:现在让我们为K3WindowHeader添加一些代码,从而让用户可以延续传统的鼠标拖拽窗口标题方式移动窗口;

实现:

捕获三个鼠标事件:

this.borderRoot.MouseLeftButtonDown += new MouseButtonEventHandler(BorderRoot_MouseLeftButtonDown);
this.borderRoot.MouseMove += new MouseEventHandler(BorderRoot_MouseMove);
this.borderRoot.MouseLeftButtonUp += new MouseButtonEventHandler(BorderRoot_MouseLeftButtonUp);

添加事件处理代码:

#region >>>>> 内部变量 ...
bool _isMouseLeftButtonDown = false;
Point _pointMouseLeftButtonOriginal = new Point(0, 0);
#endregion

void BorderRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _isMouseLeftButtonDown = true;
    _pointMouseLeftButtonOriginal = PointToScreen(e.GetPosition(this));
    this.borderRoot.CaptureMouse();
}

void BorderRoot_MouseMove(object sender, MouseEventArgs e)
{
    if (_isMouseLeftButtonDown)
    {
        Window winParent = (this.Parent as Grid).Parent as Window;

        Point pointCurrent = PointToScreen(e.GetPosition(this));
        winParent.Left += pointCurrent.X - _pointMouseLeftButtonOriginal.X;
        winParent.Top += pointCurrent.Y - _pointMouseLeftButtonOriginal.Y;
        _pointMouseLeftButtonOriginal = pointCurrent;
    }
}

void BorderRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    _isMouseLeftButtonDown = false;
    this.borderRoot.ReleaseMouseCapture();
}

注:

我将最外层 Border 命名为borderRoot。逻辑不多说了。CaptureMouse() 和 ReleaseMouseCapture() 也很重要!

F5,看看结果!

上面为了尽快看到结果,再一次使用了hard code方式找到父窗口,现在换个通用的方式,否则总有点“鱼鲠在喉,芒刺在背”的感觉。

记得我们还有个K3Helper工程吧,在这里我们创建一个K3Common类,并在其中实现一个静态方法,由它来沿着指定对象的 Visual Tree 上行,找到符合条件的Parent。

using System.Windows;  // 这个需要引入WindowsBase
using System.Windows.Media; // 这个需要引入PresentationCore

namespace K3Helper
{
    public class K3Common
    {
        public static DependencyObject GetVisualParent<T>(DependencyObject source)
        {
            while ((source != null) && (source.GetType() != typeof(T)) && !(source is T))
            {
                source = VisualTreeHelper.GetParent(source);
            }

            return source;
        }
    }
}

好了,重新修正一下MouseMove部分代码:

void BorderRoot_MouseMove(object sender, MouseEventArgs e)
{
    if (_isMouseLeftButtonDown)
    {
        DependencyObject objParent = K3Helper.K3Common.GetVisualParent<Window>(this);

        if (objParent != null)
        {
            Window winParent = objParent as Window;
            Point pointCurrent = PointToScreen(e.GetPosition(this));
            winParent.Left += pointCurrent.X - _pointMouseLeftButtonOriginal.X;
            winParent.Top += pointCurrent.Y - _pointMouseLeftButtonOriginal.Y;
            _pointMouseLeftButtonOriginal = pointCurrent;
        }
    }
}

F5,看看结果!

写东西好累,好在今天的电视节目还不错(江苏卫视),边看边写,^_^