C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法
介紹
一般情況下,在標(biāo)題欄中按住鼠標(biāo)左鍵不放即可實(shí)現(xiàn)拖動(dòng)操作。
當(dāng)做浮動(dòng)窗體時(shí),如果包含窗體邊框,那么界面給使用者的感覺將很不友好,因此浮動(dòng)窗體沒有邊框,但對(duì)于這種沒有邊框的窗體,該如何進(jìn)行拖放操作呢?
主要用Windows的兩個(gè)API函數(shù),即ReleaseCapture和SendMessage來實(shí)現(xiàn):
(1)ReleaseCapture函數(shù)
該函數(shù)用來釋放被當(dāng)前線程中某個(gè)窗口捕獲的光標(biāo)。語法格式如下:
[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)實(shí)例
本實(shí)例鼠標(biāo)落于窗體上,在UP之前,隨意拖拽窗體到任意位置。
1.Resources.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運(yùn)行時(shí)版本:4.0.30319.42000
//
// 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果
// 重新生成代碼,這些更改將會(huì)丟失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace _198.Properties {
using System;
/// <summary>
/// 一個(gè)強(qiáng)類型的資源類,用于查找本地化的字符串等。
/// </summary>
// 此類是由 StronglyTypedResourceBuilder
// 類通過類似于 ResGen 或 Visual Studio 的工具自動(dòng)生成的。
// 若要添加或移除成員,請(qǐng)編輯 .ResX 文件,然后重新運(yùn)行 ResGen
// (以 /str 作為命令選項(xiàng)),或重新生成 VS 項(xiàng)目。
[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 實(shí)例。
/// </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>
/// 重寫當(dāng)前線程的 CurrentUICulture 屬性,對(duì)
/// 使用此強(qiáng)類型資源類的所有資源查找執(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
實(shí)例中,使用的API函數(shù) [DllImport("user32.dll")]會(huì)提示CA1401和SYSLIB1054的錯(cuò)誤消息提示,不要理睬,否則生成的窗體不能實(shí)現(xiàn)拖拽。按道理講,按照提示修改更新為最新的API函數(shù),P/INVOKE應(yīng)該是能正常工作的,可是我沒有心情去調(diào)試它了,感興趣的網(wǎng)友見到后,可以深入研究并攻克它,趟有結(jié)果,還請(qǐng)回復(fù)。我用的框架是.NET8.0。
// 拖拽無邊框浮動(dòng)窗體
using System.Runtime.InteropServices;
namespace _198
{
public partial class Form1 : Form
{
#region 本程序中用到的API函數(shù)
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();//用來釋放被當(dāng)前線程中某個(gè)窗口捕獲的光標(biāo)
[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>
/// 鼠標(biāo)落在窗體上,隨意拖拽
/// </summary>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture(); //用來釋放被當(dāng)前線程中某個(gè)窗口捕獲的光標(biāo)
SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); //向Windows發(fā)送拖動(dòng)窗體的消息
}
}
}以上就是C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法的詳細(xì)內(nèi)容,更多關(guān)于C#無邊框浮動(dòng)窗體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法
這篇文章主要介紹了C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法,可實(shí)現(xiàn)針對(duì)特定進(jìn)程的關(guān)閉操作,是C#進(jìn)程操作的一個(gè)典型應(yīng)用,需要的朋友可以參考下2014-12-12
C#開發(fā)微信門戶及應(yīng)用(4) 關(guān)注用戶列表及詳細(xì)信息管理
這篇文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第四篇,關(guān)注用戶列表及詳細(xì)信息管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
基于C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼
這篇文章主要介紹了C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼,有需要的朋友可以參考一下2014-01-01
Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)新手引導(dǎo)遮罩鏤空效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02

