码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • WPF 截图控件之文字(七)「仿微信」


    前言

    接着上周写的截图控件继续更新添加 文字。

    1.WPF实现截屏「仿微信」
    2.WPF 实现截屏控件之移动(二)「仿微信」
    3.WPF 截图控件之伸缩(三) 「仿微信」
    4.WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
    5.WPF 截图控件之绘制箭头(五)「仿微信」
    6.WPF 截图控件之绘制箭头禁止越界(六)「仿微信」

    正文

    一、接着ScreenCut继续发电。
    1)添加文字操作只允许在可编辑区域内

    • 在添加文字、使用Border嵌套TextBox

    Border需注意:
    1)当控件的高存放不下内容的时候需要将控件的SetTop设置向上移动,最大不能超过Rect的Top 需要监听宽高发生变化SizeChanged

    TextBox需注意:
    1)需要监听失去光标焦点LostKeyboardFocus后找TextBox的父控件设置边框为零。


    👇


     void DrawText()
            {
                if (pointStart.Value.X < rect.Right
                    &&
                    pointStart.Value.X > rect.Left
                    &&
                    pointStart.Value.Y > rect.Top
                    &&
                    pointStart.Value.Y < rect.Bottom)
                {
                    var currentWAndX = pointStart.Value.X + 40;
                    if (textBorder == null)
                    {
                        textBorder = new Border
                        {
                            BorderBrush = _currentBrush == null ? Brushes.Red : _currentBrush,
                            BorderThickness = new Thickness(1),
                            Tag = _tag
                        };
    
                        var textBox = new TextBox();
                        textBox.Style = null;
                        textBox.Background = null;
                        textBox.BorderThickness = new Thickness(0);
                        textBox.Foreground = textBorder.BorderBrush;
                        textBox.FontFamily = DrawingContextHelper.FontFamily;
                        textBox.FontSize = 16;
                        textBox.TextWrapping = TextWrapping.Wrap;
                        textBox.FontWeight = FontWeights.Bold;
                        textBox.MinWidth = _width;
                        textBox.MaxWidth = rect.Right - pointStart.Value.X;
                        textBox.MaxHeight = rect.Height;
                        textBox.Cursor = Cursors.Hand;
                        
                        textBox.Padding = new Thickness(4);
                        textBox.LostKeyboardFocus += (s, e1) =>
                        {
                            var tb = s as TextBox;
                           
                            var parent = VisualTreeHelper.GetParent(tb);
                            if (parent != null && parent is Border border)
                            {
                                border.BorderThickness = new Thickness(0);
                                if (string.IsNullOrWhiteSpace(tb.Text))
                                    _canvas.Children.Remove(border);
                            }
                                
                        };
                        textBorder.SizeChanged += (s, e1) =>
                        {
                            var tb = s as Border;
                            var y = y1;
                            if (y + tb.ActualHeight > rect.Bottom)
                            {
                                var v = Math.Abs(rect.Bottom - (y + tb.ActualHeight));
                                y1 = y - v;
                                Canvas.SetTop(tb, y1);
                            }
                        };
                        textBorder.PreviewMouseLeftButtonDown += (s, e) =>
                        {
                            SelectElement();
                            var border = s as Border;
                            border.BorderThickness = new Thickness(1);
                            frameworkElement = border;
                            frameworkElement.Opacity = .7;
                        };
                        textBorder.Child = textBox;
                        _canvas.Children.Add(textBorder);
                        textBox.Focus();
                        var x = pointStart.Value.X;
    
                        if (currentWAndX > rect.Right)
                            x = x - (currentWAndX - rect.Right);
                        Canvas.SetLeft(textBorder, x);
                        Canvas.SetTop(textBorder, pointStart.Value.Y);
                    }
                }
            }
    
    折叠

    2)移除焦点 。


     Keyboard.ClearFocus();
    

    完整代码如下

    • ScreenCut GitHub
    • ScreenCut.xaml Gitee
    • ScreenCut Gitee
    • ScreenCut.xaml Gitee

    项目地址

    • 框架名:WPFDevelopers
    • 作者:WPFDevelopers
    • GitHub
    • Gitee
  • 相关阅读:
    RabbitMQ 之 死信队列
    【附源码】计算机毕业设计SSM同城互助平台
    OpenCV实现物体尺寸的测量
    Spark 内核 (二) --------- Spark 部署模式
    数据库的概念-数据库、数据库管理系统、数据库系统、数据库管理员、数据库设计人员、开发管理使用数据库系统的人员
    Vue电影网站构建实战教程
    借势热点:如何快速讲今日热点跟我们的视频结合起来?支招三把斧
    CV基础常用知识点
    JVM实战(二) :关闭dump配置,使服务快速恢复
    mysql caching_sha_password
  • 原文地址:https://www.cnblogs.com/yanjinhua/p/16542711.html
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号