介绍
一般情况下,在标题栏中按住鼠标左键不放即可实现拖动操作。
当做浮动窗体时,如果包含窗体边框,那么界面给使用者的感觉将很不友好,因此浮动窗体没有边框,但对于这种没有边框的窗体,该如何进行拖放操作呢?
主要用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 类型的本地化资源。
/// </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函数 [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();
}
/// <summary>
/// 鼠标落在窗体上,随意拖拽
/// </summary>
private void form1_mousedown(object sender, mouseeventargs e)
{
releasecapture(); //用来释放被当前线程中某个窗口捕获的光标
sendmessage(handle, wm_syscommand, sc_move + htcaption, 0); //向windows发送拖动窗体的消息
}
}
}以上就是c#实现鼠标拖拽无边框浮动窗体的方法的详细内容,更多关于c#无边框浮动窗体的资料请关注代码网其它相关文章!
发表评论