C#實現(xiàn)鼠標拖拽無邊框浮動窗體的方法
介紹
一般情況下,在標題欄中按住鼠標左鍵不放即可實現(xiàn)拖動操作。
當做浮動窗體時,如果包含窗體邊框,那么界面給使用者的感覺將很不友好,因此浮動窗體沒有邊框,但對于這種沒有邊框的窗體,該如何進行拖放操作呢?
主要用Windows的兩個API函數(shù),即ReleaseCapture和SendMessage來實現(xiàn):
(1)ReleaseCapture函數(shù)
該函數(shù)用來釋放被當前線程中某個窗口捕獲的光標。語法格式如下:
[DllImport("user32.dll")] public static extern bool ReleaseCapture();
(2)SendMessage函數(shù)
該函數(shù)用來向指定的窗體發(fā)送Windows消息。語法格式如下:
[DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);
(3)實例
本實例鼠標落于窗體上,在UP之前,隨意拖拽窗體到任意位置。
1.Resources.Designer.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 屬性,對 /// 使用此強類型資源類的所有資源查找執(zhí)行重寫。 /// </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
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函數(shù) [DllImport("user32.dll")]會提示CA1401和SYSLIB1054的錯誤消息提示,不要理睬,否則生成的窗體不能實現(xiàn)拖拽。按道理講,按照提示修改更新為最新的API函數(shù),P/INVOKE應該是能正常工作的,可是我沒有心情去調試它了,感興趣的網(wǎng)友見到后,可以深入研究并攻克它,趟有結果,還請回復。我用的框架是.NET8.0。
// 拖拽無邊框浮動窗體 using System.Runtime.InteropServices; namespace _198 { public partial class Form1 : Form { #region 本程序中用到的API函數(shù) [DllImport("user32.dll")] public static extern bool ReleaseCapture();//用來釋放被當前線程中某個窗口捕獲的光標 [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);//向指定的窗體發(fā)送Windows消息 #endregion #region 本程序中需要聲明的變量 public const int WM_SYSCOMMAND = 0x0112; //該變量表示將向Windows發(fā)送的消息類型 public const int SC_MOVE = 0xF010; //該變量表示發(fā)送消息的附加消息 public const int HTCAPTION = 0x0002; //該變量表示發(fā)送消息的附加消息 #endregion public Form1() { InitializeComponent(); } /// <summary> /// 鼠標落在窗體上,隨意拖拽 /// </summary> private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); //用來釋放被當前線程中某個窗口捕獲的光標 SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); //向Windows發(fā)送拖動窗體的消息 } } }
以上就是C#實現(xiàn)鼠標拖拽無邊框浮動窗體的方法的詳細內容,更多關于C#無邊框浮動窗體的資料請關注腳本之家其它相關文章!
相關文章
C#開發(fā)微信門戶及應用(4) 關注用戶列表及詳細信息管理
這篇文章主要為大家詳細介紹了C#開發(fā)微信門戶及應用第四篇,關注用戶列表及詳細信息管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Unity Shader實現(xiàn)新手引導遮罩鏤空效果
這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)新手引導遮罩鏤空效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02