目录
一般情况下,在标题栏中按住鼠标左键不放即可实现拖动操作。
当做浮动窗体时,如果包含窗体边框,那么界面给使用者的感觉将很不友好,因此浮动窗体没有边框,但对于这种没有边框的窗体,该如何进行拖放操作呢?
主要用Windows的两个API函数,即ReleaseCapture和SendMessage来实现:
该函数用来释放被当前线程中某个窗口捕获的光标。语法格式如下:
- [DllImport("user32.dll")]
- public static extern bool ReleaseCapture();
该函数用来向指定的窗体发送Windows消息。语法格式如下:
- [DllImport("user32.dll")]
- public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);
本实例鼠标落于窗体上,在UP之前,随意拖拽窗体到任意位置。
- //------------------------------------------------------------------------------
- //
- // 此代码由工具生成。
- // 运行时版本:4.0.30319.42000
- //
- // 对此文件的更改可能会导致不正确的行为,并且如果
- // 重新生成代码,这些更改将会丢失。
- //
- //------------------------------------------------------------------------------
-
- namespace _198.Properties {
- using System;
-
-
- ///
- /// 一个强类型的资源类,用于查找本地化的字符串等。
- ///
- // 此类是由 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() {
- }
-
- ///
- /// 返回此类使用的缓存的 ResourceManager 实例。
- ///
- [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;
- }
- }
-
- ///
- /// 重写当前线程的 CurrentUICulture 属性,对
- /// 使用此强类型资源类的所有资源查找执行重写。
- ///
- [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Globalization.CultureInfo Culture {
- get {
- return resourceCulture;
- }
- set {
- resourceCulture = value;
- }
- }
-
- ///
- /// 查找 System.Drawing.Bitmap 类型的本地化资源。
- ///
- internal static System.Drawing.Bitmap _04 {
- get {
- object obj = ResourceManager.GetObject("_04", resourceCulture);
- return ((System.Drawing.Bitmap)(obj));
- }
- }
- }
- }
- namespace _198
- {
- partial class Form1
- {
- ///
- /// Required designer variable.
- ///
- private System.ComponentModel.IContainer components = null;
-
- ///
- /// Clean up any resources being used.
- ///
- /// true if managed resources should be disposed; otherwise, false.
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
- #region Windows Form Designer generated code
-
- ///
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- ///
- 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
- }
- }
实例中,使用的API函数 [DllImport("user32.dll")]会提示CA1401和SYSLIB1054的错误消息提示,不要理睬,否则生成的窗体不能实现拖拽。按道理讲,按照提示修改更新为最新的API函数,P/INVOKE应该是能正常工作的,可是我没有心情去调试它了,感兴趣的网友见到后,可以深入研究并攻克它,趟有结果,还请回复。我用的框架是.NET8.0。
- // 拖拽无边框浮动窗体
- 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();
- }
- ///
- /// 鼠标落在窗体上,随意拖拽
- ///
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- ReleaseCapture(); //用来释放被当前线程中某个窗口捕获的光标
- SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); //向Windows发送拖动窗体的消息
- }
- }
- }