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

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


//窗口无任何控件


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

    }
}

望互相交流。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值