C#十字线小工具

小工具:实现了一个无边框、置顶显示的透明窗体,具备自定义拖动功能和十字准星绘制效果。

1. 窗体属性配置

this.FormBorderStyle = FormBorderStyle.None; // 移除默认边框

this.TopMost = true; // 窗体置顶显示

this.BackColor = Color.Magenta; // 设置背景色

this.TransparencyKey = Color.Magenta; // 指定透明色

  • FormBorderStyle.None‌:创建无边框窗体
  • TopMost‌:确保窗体始终显示在最上层
  • TransparencyKey‌:将洋红色设为透明,实现特殊显示效果
2. 拖动机制实现

代码提供了两种拖动方式:

方式一:通过拖动手柄

private Label dragHandle; // 创建红色拖动标签
dragHandle.MouseDown += (s, e) => { isDragging = true; mouseOffset = e.Location; };

  • 在窗体顶部中央创建15x15像素的红色拖动区域
  • 通过MouseDown、MouseMove、MouseUp事件实现拖动逻辑

方式二:直接拖动窗体

protected override void OnMouseDown(MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

isDragging = true;

mouseOffset = e.Location;

}

}

  • 重写窗体鼠标事件方法
  • 通过计算鼠标偏移量更新窗体位置
3. 十字准星绘制

protected override void OnPaint(PaintEventArgs e)

{

using (Pen pen = new Pen(Color.Red, 1))

{

int cx = this.Width / 2;

int cy = this.Height / 2;

e.Graphics.DrawLine(pen, 0, cy, this.Width, cy); // 水平线

e.Graphics.DrawLine(pen, cx, 0, cx, this.Height); // 垂直线

}

}

  • 重写OnPaint方法实现自定义绘制
  • 使用Graphics对象在窗体中心绘制红色十字线
4. 上下文菜单

var menu = new ContextMenuStrip();

menu.Items.Add("最小化", null, (s, e) => this.WindowState = FormWindowState.Minimized);

menu.Items.Add("退出", null, (s, e) => Application.Exit());

  • 提供"最小化"和"退出"功能选项
  • 增强无边框窗体的可操作性

技术亮点

  1. 双重拖动机制‌:既可通过专用手柄拖动,也可直接拖动窗体
  2. 透明效果‌:利用TransparencyKey实现特殊显示需求
  3. 自定义绘制‌:灵活重写OnPaint实现个性化界面
  4. 事件处理优化‌:合理使用鼠标事件实现流畅交互

附:源码

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CrossTool
{
    public partial class Form1 : Form
    {
        private Point mouseOffset;
        private bool isDragging = false;
        private Label dragHandle;
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.TopMost = true;
            this.ShowInTaskbar = false;
            this.BackColor = Color.Magenta;
            this.TransparencyKey = Color.Magenta;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Size = new Size(800, 700); // 小巧尺寸

            dragHandle = new Label
            {
                Size = new Size(15, 15),
                BackColor = Color.Red,
                Cursor = Cursors.Hand,
                Location = new Point(this.Width / 2 - 7, 0)
            };

            dragHandle.MouseDown += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    isDragging = true;
                    mouseOffset = e.Location;
                }
            };

            dragHandle.MouseMove += (s, e) =>
            {
                if (isDragging)
                {
                    Point newLoc = this.Location;
                    newLoc.X += e.X - mouseOffset.X;
                    newLoc.Y += e.Y - mouseOffset.Y;
                    this.Location = newLoc;
                }
            };

            dragHandle.MouseUp += (s, e) => isDragging = false;

            this.Controls.Add(dragHandle);

            var menu = new ContextMenuStrip();
            menu.Items.Add("最小化", null, (s, e) => this.WindowState = FormWindowState.Minimized);
            menu.Items.Add("退出", null, (s, e) => Application.Exit());
            this.ContextMenuStrip = menu;
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                mouseOffset = e.Location;
            }
        }


        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isDragging)
            {
                Point newLocation = this.Location;
                newLocation.X += e.X - mouseOffset.X;
                newLocation.Y += e.Y - mouseOffset.Y;
                this.Location = newLocation;
            }
        }


        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                isDragging = false;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (Pen pen = new Pen(Color.Red, 1))
            {
                int cx = this.Width / 2;
                int cy = this.Height / 2;
                e.Graphics.DrawLine(pen, 0, cy, this.Width, cy);
                e.Graphics.DrawLine(pen, cx, 0, cx, this.Height);
            }
        }



    }
}
相关推荐
宝桥南山6 小时前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong1239 小时前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp
星辰徐哥10 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
leoufung10 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
hacker70710 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
SKY -dada10 小时前
Understand 使用教程
开发语言·c#·流程图·软件构建·敏捷流程·代码复审·源代码管理
William_cl13 小时前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄13 小时前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf
武藤一雄13 小时前
WPF进阶:万字详解WPF如何性能优化
windows·性能优化·c#·.net·wpf·.netcore·鲁棒性
xiaogutou11211 天前
2026年历史课件PPT模板选购指南:教师备课效率与精度的平衡方案
开发语言·c#