c#无边框窗口的入门级拖动实现
2014-12-23 00:42
411 查看
实现了窗口的移动,下一步研究通过菜单拖动窗口
//窗口无任何控件
以下为form1.cs中的全部代码 多出的部分为添加部分(自行寻找)
以下代码为form1.designer.cs中的代码,改动代码已标记
望互相交流。
//窗口无任何控件
以下为form1.cs中的全部代码 多出的部分为添加部分(自行寻找)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication9 { 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); #endregion #region 本程序中需要声明的变量 public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送的消息类型 public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息 public const int HTCAPTION = 0x0002;//该变量表示发送消息的附加消息 #endregion private void ExitContext_Click(object sender, EventArgs e) { Application.Exit();//退出本程序 } private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } } }
以下代码为form1.designer.cs中的代码,改动代码已标记
namespace WindowsFormsApplication9 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { //此处为添加代码********* this.components = new System.ComponentModel.Container(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.ExitContext = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip1.SuspendLayout(); //至此********** this.SuspendLayout(); // // Form1 // //此处为添加代码************* this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ExitContext}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(101, 26); // // ExitContext // this.ExitContext.Name = "ExitContext"; this.ExitContext.Size = new System.Drawing.Size(100, 22); this.ExitContext.Text = "退出"; this.ExitContext.Click += new System.EventHandler(this.ExitContext_Click); // // Frm_Main // //至此**************** this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; //此处为添加代码********** this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; //至此****************** this.ClientSize = new System.Drawing.Size(284, 262); //此处为添加代码*********** this.ContextMenuStrip = this.contextMenuStrip1; //至此******************* this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "Form1"; //此处为添加代码 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; //至此************* this.Text = "Form1"; //此处为添加代码 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.contextMenuStrip1.ResumeLayout(false); //至此************ this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem ExitContext; } }
望互相交流。
相关文章推荐
- C# 无边框窗口实现拖动
- C# 无边框窗口实现拖动
- C# 实现窗口无边框,可拖动效果
- C# 实现无边框窗口拖动
- C#中实现拖动无边框窗体Form
- C# 实现拖动无边框窗体
- C# 如何窗体去掉上面的最大化,最小化合关闭以及实现无边框窗体拖动
- c# 实现用鼠标拖动没有标题栏的窗口(窗体的任何部分都可实现拖动效果)
- C#中实现窗口拖动
- C#下如何实现无标题可拖动窗口
- 实现无边框时拖动窗口
- C#中实现拖动无边框窗体Form
- C#下如何实现无标题可拖动窗口2
- C#实现无边框窗体拖动
- C#实现无边框窗体拖动的两个方案
- C#中实现拖动无边框窗体Form
- java中 实现无标题无边框的窗体能够用鼠标拖动改变窗口大小
- C#中实现拖动无边框窗体Form
- C# 窗口实现拖动
- 手把手教你打造c#透明、自定义边框、可拖动、可以放大缩小的窗口