在 WinForms 中制作无边框窗体通过鼠标拖动移动和调整大小,难点是我窗体上被标题栏和状态栏dock之后很难选中

项目场景:

在 WinForms 中制作 无边框窗体(FormBorderStyle = None)时,默认失去了窗口的标题栏与边框,因此也失去了通过鼠标拖动移动和调整大小的能力。本例子通过一个标题栏来拖动窗体,通过状态栏右下角的地方来拖动来改变窗体大小。本例子创建了两个窗体。一个baseform用来放标题栏和状态栏。

问题描述

提示:这里描述项目中遇到的问题:

难点是我窗体上被标题栏和状态栏dock之后很难选中

c 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
 
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
            panel1.MouseMove += panel1_MouseMove;
            panel1.MouseDown += panel1_MouseDown;
            panel1.MouseUp += Panel1_MouseUp;
            panel2.MouseDown += PanelTitle_MouseDown;
            this.DoubleBuffered = true;
        }
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;
        private void PanelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
            }
        }
        private void Panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Size = previousRect.Size;
                isResizing = false;
            }
        }

        private bool isResizing = false;
        private Point lastMousePos;
        private Size lastMouseSize;
        private Size NewMouseSize;
        private const int resizeArea = 15;
        private Rectangle previousRect;
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            bool inBottomRight = e.X >= panel1.Width - resizeArea && e.Y >= panel1.Height - resizeArea;
            if (inBottomRight || isResizing)
                panel1.Cursor = Cursors.SizeNWSE;
            else
                panel1.Cursor = Cursors.Default;

            if (isResizing && e.Button == MouseButtons.Left)
            {
                int dx = e.X - lastMousePos.X;
                int dy = e.Y - lastMousePos.Y;
                Size newSize = new Size(lastMouseSize.Width + dx,lastMouseSize.Height + dy);
             
                Rectangle newRect = new Rectangle(this.Location, newSize);
                previousRect = newRect;
                OnUpdateRichTextBox?.Invoke($" lastMousePos.X:{lastMousePos.X} lastMousePos.Y:{lastMousePos.Y} e.Y:{e.Y} e.X:{e.X}  dx:{dx}  dy:{dy}", Color.Black);
            }
        }
        
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left &&
            e.X >= panel1.Width - resizeArea &&
            e.Y >= panel1.Height - resizeArea)
            {
                lastMouseSize = this.Size;
                isResizing = true;
                lastMousePos = e.Location;
            }
        }
        public event Action<string , Color> OnUpdateRichTextBox;

      

        /// <summary>
        /// 改变richtextbox 当前插入行颜色
        /// </summary>
        /// <param name="rtBox"></param>
        /// <param name="addtext"></param>
        /// <param name="color"></param>
        /// <param name="IsaddNewLine"></param
        public void AppendTextColorful(RichTextBox rtBox, string addtext, Color color, bool IsaddNewLine = false)
        {
            if (IsaddNewLine)
            {
                addtext += Environment.NewLine;
            }
            try
            {
                if (rtBox.InvokeRequired)
                {
                    rtBox.Invoke(new Action(() =>
                    {
                        rtBox.ScrollToCaret();
                        rtBox.SelectionStart = rtBox.TextLength;
                        rtBox.SelectionLength = 0;
                        rtBox.SelectionColor = color;
                        rtBox.AppendText(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff") + "  " + addtext + "\r\n");
                        rtBox.SelectionColor = rtBox.ForeColor;
                        if (rtBox.Lines.Length > 1000)
                            rtBox.Clear();
                    }));
                }
                else
                {
                    rtBox.ScrollToCaret();
                    rtBox.SelectionStart = rtBox.TextLength;
                    rtBox.SelectionLength = 0;
                    rtBox.SelectionColor = color;
                    rtBox.AppendText(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff") + addtext + "\r\n");
                    rtBox.SelectionColor = rtBox.ForeColor;
                    if (rtBox.Lines.Length > 1000)
                        rtBox.Clear();
                }
                if (!Directory.Exists($"Log\\"))
                {
                    Directory.CreateDirectory($"Log\\");
                }
                //LoggerUtility.Instance.WriteLog(addtext, "normal");

            }
            catch (Exception ex)
            {
                //LoggerUtility.Instance.WriteException(ex);
            }
        }
    }
}
c 复制代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    class Class1:Form1
    {
        private RichTextBox richTextBox1;

        public Class1()
        {
            InitializeComponent();
            OnUpdateRichTextBox += UpdateTextBox1;
        }
        //RichTextBox richTextBox1;
        private void UpdateTextBox1(string str, Color clr)
        {
            AppendTextColorful(richTextBox1, str, clr);
        }

        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 57);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(1232, 503);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = "";
            // 
            // Class1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.ClientSize = new System.Drawing.Size(1232, 606);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Class1";
            this.Controls.SetChildIndex(this.richTextBox1, 0);
            this.ResumeLayout(false);

        }
    }
}
相关推荐
百锦再1 天前
重新学习Vue中的按键监听和鼠标监听
javascript·vue.js·vue·计算机外设·click·up·down
TESmart碲视2 天前
HKS201-M24 大师版 8K60Hz USB 3.0 适用于 2 台 PC 1台显示器 无缝切换 KVM 切换器
单片机·嵌入式硬件·物联网·游戏·计算机外设·电脑·智能硬件
景行产品分享2 天前
2025年游戏鼠标推荐,游戏鼠标推荐,打CSGO(罗技、雷蛇、卓威、ROG、漫步者、赛睿、达尔优)
计算机外设
jubobolv3692 天前
[深度学习环境踩坑记录]ubuntu22.04安装RTX3060驱动,黑屏、桌面只有壁纸和鼠标,一顿折腾
计算机外设·持续部署
vfvfb3 天前
定时点击二次鼠标 定时点击鼠标
计算机外设·自动点击·定时点击鼠标·倒计时点击鼠标·定时点击
szekl3 天前
HDMI 2.0 4×2矩阵切换器412HN——多信号输入输出的高清解决方案
linux·矩阵·计算机外设·电脑·ekl
zfysis4 天前
键盘第一下无反应
计算机外设
Allen Bright14 天前
【JS-4.4-键盘常用事件】深入理解DOM键盘事件:提升用户交互体验的关键
javascript·计算机外设·交互
小天源15 天前
鼠标自动录制软件下载及使用
自动化测试·自动化·计算机外设·mouse recorder·鼠标录制·鼠标宏
小池先生15 天前
罗技键盘k380 fn按键问题 按f12不能直接打开调试,需要fn+f12
计算机外设