怀化职院网站,品牌策划书模板范文,公司网站开发费计入,更新wordpress目录
(1)ReleaseCapture函数
(2)SendMessage函数
(3)实例
1.Resources.Designer.cs
2.Form1.Designer.cs
3.Form1.cs 一般情况下#xff0c;在标题栏中按住鼠标左键不放即可实现拖动操作。 当做浮动窗体时#xff0c;如果包含窗体边框#xff0c;那么界面给使用者的感…目录
(1)ReleaseCapture函数
(2)SendMessage函数
(3)实例
1.Resources.Designer.cs
2.Form1.Designer.cs
3.Form1.cs 一般情况下在标题栏中按住鼠标左键不放即可实现拖动操作。 当做浮动窗体时如果包含窗体边框那么界面给使用者的感觉将很不友好因此浮动窗体没有边框但对于这种没有边框的窗体该如何进行拖放操作呢? 主要用Windows的两个API函数即ReleaseCapture和SendMessage来实现
(1)ReleaseCapture函数 该函数用来释放被当前线程中某个窗口捕获的光标。语法格式如下
[DllImport(user32.dll)]
public static extern bool ReleaseCapture();
(2)SendMessage函数 该函数用来向指定的窗体发送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 属性对/// 使用此强类型资源类的所有资源查找执行重写。/// /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 类型的本地化资源。/// /summaryinternal 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./// /summaryprivate System.ComponentModel.IContainer components null;/// summary/// Clean up any resources being used./// /summary/// param namedisposingtrue if managed resources should be disposed; otherwise, false./paramprotected 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./// /summaryprivate 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。
// 拖拽无边框浮动窗体
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; //该变量表示发送消息的附加消息#endregionpublic Form1(){InitializeComponent();}/// summary/// 鼠标落在窗体上随意拖拽/// /summaryprivate void Form1_MouseDown(object sender, MouseEventArgs e){ReleaseCapture(); //用来释放被当前线程中某个窗口捕获的光标SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE HTCAPTION, 0); //向Windows发送拖动窗体的消息}}
}