c#无边框窗口的入门级拖动实现

来源:互联网 发布:身份证被盗用网络贷款 编辑:程序博客网 时间:2024/05/22 00:36

实现了窗口的移动,下一步研究通过菜单拖动窗口


//窗口无任何控件


以下为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;    }}

望互相交流。


0 0
原创粉丝点击