C#鼠标拖拽无边框浮动窗体的方法:窗体控制

目录

(1)ReleaseCapture函数

(2)SendMessage函数

(3)实例

1.Resources.Designer.cs

2.Form1.Designer.cs

3.Form1.cs


一般情况下,在标题栏中按住鼠标左键不放即可实现拖动操作。

当做浮动窗体时,如果包含窗体边框,那么界面给使用者的感觉将很不友好,因此浮动窗体没有边框,但对于这种没有边框的窗体,该如何进行拖放操作呢?

主要用Windows的两个API函数,即ReleaseCapture和SendMessage来实现:

(1)ReleaseCapture函数

该函数用来释放被当前线程中某个窗口捕获的光标。语法格式如下:

cs 复制代码
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

(2)SendMessage函数

该函数用来向指定的窗体发送Windows消息。语法格式如下:

cs 复制代码
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);

(3)实例

本实例鼠标落于窗体上,在UP之前,随意拖拽窗体到任意位置。

1.Resources.Designer.cs

cs 复制代码
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace _198.Properties {
    using System;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 StronglyTypedResourceBuilder
    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    // (以 /str 作为命令选项),或重新生成 VS 项目。
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_198.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 CurrentUICulture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _04 {
            get {
                object obj = ResourceManager.GetObject("_04", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
    }
}

2.Form1.Designer.cs

cs 复制代码
namespace _198
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            SuspendLayout();
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources._04;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(311, 218);
            FormBorderStyle = FormBorderStyle.None;
            Name = "Form1";
            Text = "Form1";
            MouseDown += Form1_MouseDown;
            ResumeLayout(false);
        }

        #endregion
    }
}

3.Form1.cs

实例中,使用的API函数 [DllImport("user32.dll")]会提示CA1401和SYSLIB1054的错误消息提示,不要理睬,否则生成的窗体不能实现拖拽。按道理讲,按照提示修改更新为最新的API函数,P/INVOKE应该是能正常工作的,可是我没有心情去调试它了,感兴趣的网友见到后,可以深入研究并攻克它,趟有结果,还请回复。我用的框架是.NET8.0。

cs 复制代码
// 拖拽无边框浮动窗体
using System.Runtime.InteropServices;
namespace _198
{
    public partial class Form1 : Form
    {
        #region 本程序中用到的API函数
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();//用来释放被当前线程中某个窗口捕获的光标
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);//向指定的窗体发送Windows消息
        #endregion

        #region 本程序中需要声明的变量
        public const int WM_SYSCOMMAND = 0x0112;    //该变量表示将向Windows发送的消息类型
        public const int SC_MOVE = 0xF010;          //该变量表示发送消息的附加消息
        public const int HTCAPTION = 0x0002;        //该变量表示发送消息的附加消息
        #endregion

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 鼠标落在窗体上,随意拖拽
        /// </summary>
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();                                                                                       //用来释放被当前线程中某个窗口捕获的光标
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); //向Windows发送拖动窗体的消息
        }
    }
}
相关推荐
与火星的孩子对话38 分钟前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
future141244 分钟前
C#每日学习日记
java·学习·c#
coding随想1 小时前
JavaScript中的BOM:Window对象全解析
开发语言·javascript·ecmascript
念九_ysl1 小时前
Java 使用 OpenHTMLToPDF + Batik 将含 SVG 遮罩的 HTML 转为 PDF 的完整实践
java·开发语言·pdf
yaoxin5211232 小时前
124. Java 泛型 - 有界类型参数
java·开发语言
liulilittle2 小时前
深度剖析:OPENPPP2 libtcpip 实现原理与架构设计
开发语言·网络·c++·tcp/ip·智能路由器·tcp·通信
88号技师2 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟2 小时前
Java 编程之模板方法模式
java·开发语言·模板方法模式
上单带刀不带妹3 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
-凌凌漆-4 小时前
【Qt】QStringLiteral 介绍
开发语言·qt