码迷,mamicode.com
首页 > Windows程序 > 详细

改变窗体的大小(理解鼠标的操作事件和过程)

时间:2015-09-08 01:38:44      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

1.Designer.cs代码

技术分享
namespace 手动改变自制窗体的大小
{
    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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.panel_BR = new System.Windows.Forms.Panel();
            this.panel_All = new System.Windows.Forms.Panel();
            this.panel_TitR = new System.Windows.Forms.Panel();
            this.panel_BL = new System.Windows.Forms.Panel();
            this.panel_TitZ = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.panel_TitL = new System.Windows.Forms.Panel();
            this.panel_Right = new System.Windows.Forms.Panel();
            this.panel_Left = new System.Windows.Forms.Panel();
            this.panel_Title = new System.Windows.Forms.Panel();
            this.panel_Bottom = new System.Windows.Forms.Panel();
            this.panel_Bn = new System.Windows.Forms.Panel();
            this.panel_TitZ.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.panel_Title.SuspendLayout();
            this.panel_Bottom.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel_BR
            // 
            this.panel_BR.BackColor = System.Drawing.Color.Transparent;
            this.panel_BR.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_BR.BackgroundImage")));
            this.panel_BR.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_BR.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            this.panel_BR.Dock = System.Windows.Forms.DockStyle.Right;
            this.panel_BR.Location = new System.Drawing.Point(284, 0);
            this.panel_BR.Name = "panel_BR";
            this.panel_BR.Size = new System.Drawing.Size(8, 10);
            this.panel_BR.TabIndex = 1;
            this.panel_BR.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
            this.panel_BR.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
            // 
            // panel_All
            // 
            this.panel_All.BackColor = System.Drawing.Color.Honeydew;
            this.panel_All.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel_All.Location = new System.Drawing.Point(6, 19);
            this.panel_All.Name = "panel_All";
            this.panel_All.Size = new System.Drawing.Size(280, 96);
            this.panel_All.TabIndex = 13;
            // 
            // panel_TitR
            // 
            this.panel_TitR.BackColor = System.Drawing.Color.Transparent;
            this.panel_TitR.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitR.BackgroundImage")));
            this.panel_TitR.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_TitR.Dock = System.Windows.Forms.DockStyle.Right;
            this.panel_TitR.Location = new System.Drawing.Point(284, 0);
            this.panel_TitR.Name = "panel_TitR";
            this.panel_TitR.Size = new System.Drawing.Size(8, 19);
            this.panel_TitR.TabIndex = 1;
            this.panel_TitR.Tag = "3";
            this.panel_TitR.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
            this.panel_TitR.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
            // 
            // panel_BL
            // 
            this.panel_BL.BackColor = System.Drawing.Color.Transparent;
            this.panel_BL.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_BL.BackgroundImage")));
            this.panel_BL.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_BL.Cursor = System.Windows.Forms.Cursors.SizeNS;
            this.panel_BL.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel_BL.Location = new System.Drawing.Point(0, 0);
            this.panel_BL.Name = "panel_BL";
            this.panel_BL.Size = new System.Drawing.Size(8, 10);
            this.panel_BL.TabIndex = 0;
            // 
            // panel_TitZ
            // 
            this.panel_TitZ.BackColor = System.Drawing.Color.Transparent;
            this.panel_TitZ.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitZ.BackgroundImage")));
            this.panel_TitZ.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_TitZ.Controls.Add(this.pictureBox1);
            this.panel_TitZ.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel_TitZ.Location = new System.Drawing.Point(12, 0);
            this.panel_TitZ.Name = "panel_TitZ";
            this.panel_TitZ.Size = new System.Drawing.Size(272, 19);
            this.panel_TitZ.TabIndex = 2;
            this.panel_TitZ.Tag = "2";
            this.panel_TitZ.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
            this.panel_TitZ.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Hand;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(260, 3);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(11, 11);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Tag = "11";
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            // 
            // panel_TitL
            // 
            this.panel_TitL.BackColor = System.Drawing.Color.Transparent;
            this.panel_TitL.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitL.BackgroundImage")));
            this.panel_TitL.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_TitL.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel_TitL.Location = new System.Drawing.Point(0, 0);
            this.panel_TitL.Name = "panel_TitL";
            this.panel_TitL.Size = new System.Drawing.Size(12, 19);
            this.panel_TitL.TabIndex = 0;
            this.panel_TitL.Tag = "1";
            this.panel_TitL.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
            this.panel_TitL.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
            // 
            // panel_Right
            // 
            this.panel_Right.BackColor = System.Drawing.Color.Transparent;
            this.panel_Right.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Right.BackgroundImage")));
            this.panel_Right.Cursor = System.Windows.Forms.Cursors.SizeWE;
            this.panel_Right.Dock = System.Windows.Forms.DockStyle.Right;
            this.panel_Right.Location = new System.Drawing.Point(286, 19);
            this.panel_Right.Name = "panel_Right";
            this.panel_Right.Size = new System.Drawing.Size(6, 96);
            this.panel_Right.TabIndex = 10;
            this.panel_Right.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
            this.panel_Right.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
            // 
            // panel_Left
            // 
            this.panel_Left.BackColor = System.Drawing.Color.Transparent;
            this.panel_Left.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Left.BackgroundImage")));
            this.panel_Left.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel_Left.Location = new System.Drawing.Point(0, 19);
            this.panel_Left.Name = "panel_Left";
            this.panel_Left.Size = new System.Drawing.Size(6, 96);
            this.panel_Left.TabIndex = 9;
            this.panel_Left.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
            this.panel_Left.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
            // 
            // panel_Title
            // 
            this.panel_Title.BackColor = System.Drawing.Color.Transparent;
            this.panel_Title.Controls.Add(this.panel_TitZ);
            this.panel_Title.Controls.Add(this.panel_TitR);
            this.panel_Title.Controls.Add(this.panel_TitL);
            this.panel_Title.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel_Title.Location = new System.Drawing.Point(0, 0);
            this.panel_Title.Name = "panel_Title";
            this.panel_Title.Size = new System.Drawing.Size(292, 19);
            this.panel_Title.TabIndex = 7;
            // 
            // panel_Bottom
            // 
            this.panel_Bottom.BackColor = System.Drawing.Color.Transparent;
            this.panel_Bottom.Controls.Add(this.panel_Bn);
            this.panel_Bottom.Controls.Add(this.panel_BR);
            this.panel_Bottom.Controls.Add(this.panel_BL);
            this.panel_Bottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel_Bottom.Location = new System.Drawing.Point(0, 115);
            this.panel_Bottom.Name = "panel_Bottom";
            this.panel_Bottom.Size = new System.Drawing.Size(292, 10);
            this.panel_Bottom.TabIndex = 8;
            // 
            // panel_Bn
            // 
            this.panel_Bn.BackColor = System.Drawing.Color.Transparent;
            this.panel_Bn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Bn.BackgroundImage")));
            this.panel_Bn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.panel_Bn.Cursor = System.Windows.Forms.Cursors.SizeNS;
            this.panel_Bn.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel_Bn.Location = new System.Drawing.Point(8, 0);
            this.panel_Bn.Name = "panel_Bn";
            this.panel_Bn.Size = new System.Drawing.Size(276, 10);
            this.panel_Bn.TabIndex = 2;
            this.panel_Bn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
            this.panel_Bn.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 125);
            this.Controls.Add(this.panel_All);
            this.Controls.Add(this.panel_Right);
            this.Controls.Add(this.panel_Left);
            this.Controls.Add(this.panel_Title);
            this.Controls.Add(this.panel_Bottom);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel_TitZ.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.panel_Title.ResumeLayout(false);
            this.panel_Bottom.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel_BR;
        private System.Windows.Forms.Panel panel_All;
        private System.Windows.Forms.Panel panel_TitR;
        private System.Windows.Forms.Panel panel_BL;
        private System.Windows.Forms.Panel panel_TitZ;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Panel panel_TitL;
        private System.Windows.Forms.Panel panel_Right;
        private System.Windows.Forms.Panel panel_Left;
        private System.Windows.Forms.Panel panel_Title;
        private System.Windows.Forms.Panel panel_Bottom;
        private System.Windows.Forms.Panel panel_Bn;
    }
}
View Code

2.cs代码

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 手动改变自制窗体的大小
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static int Example_X = 0;
        public static int Example_Y = 0;
        public static int Example_W = 0;
        public static Point CPoint;

        #region  利用窗体上的控件移动窗体
        /// <summary>
        /// 利用控件移动窗体
        /// </summary>
        /// <param Frm="Form">窗体</param>
        /// <param e="MouseEventArgs">控件的移动事件</param>
        public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空间using System.Windows.Forms;
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        #region  获取鼠标的当前位置
        /// <summary>
        /// 获取鼠标的当前位置
        /// </summary>
        /// <param Frm="Form">窗体</param>
        /// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param>
        public void FrmScreen_SizeInfo(Form Frm, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Example_X = e.X;
                Example_Y = e.Y;
                Example_W = Frm.Width;
            }
        }
        #endregion

        #region  改变窗体的大小(用于鼠标的移动事件)
        /// <summary>
        /// 改变窗体的大小(用于鼠标的移动事件)
        /// </summary>
        /// <param Frm="Form">窗体</param>
        /// <param Pan="Panel">设置窗体边框的控件</param>
        /// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param>
        public void FrmScreen_EnlargeSize(Form Frm, Panel Pan, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                switch (Pan.Name)
                {
                    case "panel_Right":                        //如果移动的是窗体的右边框
                        {
                            if (this.Width <= 70)                //如果窗体的宽度小于等于70
                            {
                                Frm.Width = 70;                //设置窗体的宽度为70
                                //如果用鼠标向右移动窗体的右边框
                                if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width)
                                {
                                    //根据鼠标的移动值,增加窗体的宽度
                                    Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
                                }
                                break;
                            }
                            //根据鼠标的移动值,增加窗体的宽度
                            Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
                            break;
                        }
                    case "panel_BR":                        //如果移动的是窗体的右下角
                        {
                            //如果窗体的大小不为窗体大小的最小值
                            if (this.Width > 70 && this.Height > (panel_Title.Height + panel_Bn.Height + 1))
                            {
                                //根据鼠标的移动改变窗体的大小
                                Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);
                                Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
                            }
                            else
                            {
                                if (this.Width <= 70)            //如果窗体的宽度小于等于最小值
                                {
                                    Frm.Width = 70;            //设置窗体的宽度为70
                                    if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高小于最小值
                                    {
                                        Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的最小高度
                                        //如果用鼠标向下移动窗体的底边框
                                        if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
                                        {
                                            //根据鼠标的移动值,增加窗体的高度
                                            Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);
                                        }
                                        break;
                                    }
                                    //如果用鼠标向右移动窗体
                                    if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width)
                                    {
                                        //增加窗体的宽度
                                        Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
                                    }
                                    break;
                                }
                                if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高度小于等于最小值
                                {
                                    Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的高度为最小值
                                    Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);//改变窗体的宽度
                                    //如果用鼠标向下移动窗体的边框
                                    if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
                                    {
                                        Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);//增加窗体的高度
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    case "panel_Bn"://如果移动的是窗体的底边框
                        {
                            if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高度小于等于最小值
                            {
                                Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的高度为最小值
                                //如果用鼠标向下移动窗体的下边框
                                if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
                                {
                                    Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);    //增加窗体的高度
                                }
                                break;
                            }
                            Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);            //增加窗体的高度
                            break;
                        }
                }
            }
        }
        #endregion

        private void panel_Right_MouseDown(object sender, MouseEventArgs e)
        {
            FrmScreen_SizeInfo(this, e);//获取鼠标的当前位置
        }

        private void panel_Right_MouseMove(object sender, MouseEventArgs e)
        {
            FrmScreen_EnlargeSize(this, (Panel)sender, e);//改变窗体的大小
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel_TitL_MouseDown(object sender, MouseEventArgs e)
        {
            int Tem_X = -e.X;
            if (Convert.ToInt16(((Panel)sender).Tag.ToString()) == 2)//如果移动的是标题栏的中间部分
                Tem_X = -e.X - panel_TitL.Width;
            if (Convert.ToInt16(((Panel)sender).Tag.ToString()) == 3)//如果移动的是标题栏的尾端
                Tem_X = -(this.Width - ((Panel)sender).Width) - e.X;
            CPoint = new Point(Tem_X, -e.Y);
        }

        private void panel_TitL_MouseMove(object sender, MouseEventArgs e)
        {
            FrmMove(this, e);
        }

    }
}
View Code

 为了更好的理解鼠标事件,再来看一下下面这个鼠标穿透domo,仿Windows 8.1右下角的Calendar:

3.Designer.cs代码

技术分享
namespace CalendarPenetrate
{
    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();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.ToolS_P = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolS_Close = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // monthCalendar1
            // 
            this.monthCalendar1.Location = new System.Drawing.Point(2, 2);
            this.monthCalendar1.Name = "monthCalendar1";
            this.monthCalendar1.TabIndex = 0;
            this.monthCalendar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.monthCalendar1_MouseMove);
            this.monthCalendar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.monthCalendar1_MouseDown);
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Text = "notifyIcon1";
            this.notifyIcon1.Visible = true;
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ToolS_P,
            this.ToolS_Close});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(167, 70);
            // 
            // ToolS_P
            // 
            this.ToolS_P.Name = "ToolS_P";
            this.ToolS_P.Size = new System.Drawing.Size(166, 22);
            this.ToolS_P.Text = "是否进行穿透效果";
            this.ToolS_P.Click += new System.EventHandler(this.ToolS_P_Click);
            // 
            // ToolS_Close
            // 
            this.ToolS_Close.Name = "ToolS_Close";
            this.ToolS_Close.Size = new System.Drawing.Size(166, 22);
            this.ToolS_Close.Text = "退出";
            this.ToolS_Close.Click += new System.EventHandler(this.ToolS_Close_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(272, 148);
            this.Controls.Add(this.monthCalendar1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.MonthCalendar monthCalendar1;
        private System.Windows.Forms.NotifyIcon notifyIcon1;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem ToolS_P;
        private System.Windows.Forms.ToolStripMenuItem ToolS_Close;
    }
}
View Code

4.cs代码

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CalendarPenetrate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Point CPoint;
        private void monthCalendar1_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void monthCalendar1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }

        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_EXSTYLE = (-20);
        private string Var_genre = "";//记录当前操作的类型

        #region 在窗口结构中为指定的窗口设置信息
        /// <summary>
        /// 在窗口结构中为指定的窗口设置信息
        /// </summary>
        /// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
        /// <param name="nIndex">欲取回的信息</param>
        /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
        #endregion

        #region 从指定窗口的结构中取得信息
        /// <summary>
        /// 从指定窗口的结构中取得信息
        /// </summary>
        /// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
        /// <param name="nIndex">欲取回的信息</param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
        #endregion

        #region 使窗口有鼠标穿透功能
        /// <summary>
        /// 使窗口有鼠标穿透功能
        /// </summary>
        private void CanPenetrate()
        {
            uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
            uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
        }
        #endregion

        private void ToolS_P_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.7;
            CanPenetrate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中
            this.TopMost = true;//使窗体始终在其它窗体之上
        }

        private void ToolS_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
View Code

以及下面的鼠标穿透雾状薄膜的代码:

5.Designer.cs代码

技术分享
namespace 鼠标穿透窗体
{
    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();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.ToolColor = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolColor_Gainsboro = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolColor_DarkOrchid = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolColor_RoyalBlue = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolColor_Gold = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolColor_LightGreen = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_10 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_20 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_30 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_40 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_50 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_60 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_70 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_80 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClarity_90 = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolAcquiescence = new System.Windows.Forms.ToolStripMenuItem();
            this.ToolClose = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
            this.notifyIcon1.Text = "notifyIcon1";
            this.notifyIcon1.Visible = true;
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ToolColor,
            this.ToolClarity,
            this.ToolAcquiescence,
            this.ToolClose});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(137, 92);
            // 
            // ToolColor
            // 
            this.ToolColor.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ToolColor_Gainsboro,
            this.ToolColor_DarkOrchid,
            this.ToolColor_RoyalBlue,
            this.ToolColor_Gold,
            this.ToolColor_LightGreen});
            this.ToolColor.Name = "ToolColor";
            this.ToolColor.Size = new System.Drawing.Size(136, 22);
            this.ToolColor.Text = "颜色设置";
            // 
            // ToolColor_Gainsboro
            // 
            this.ToolColor_Gainsboro.Name = "ToolColor_Gainsboro";
            this.ToolColor_Gainsboro.Size = new System.Drawing.Size(100, 22);
            this.ToolColor_Gainsboro.Tag = "1";
            this.ToolColor_Gainsboro.Text = "玻璃";
            this.ToolColor_Gainsboro.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolColor_DarkOrchid
            // 
            this.ToolColor_DarkOrchid.Name = "ToolColor_DarkOrchid";
            this.ToolColor_DarkOrchid.Size = new System.Drawing.Size(100, 22);
            this.ToolColor_DarkOrchid.Tag = "2";
            this.ToolColor_DarkOrchid.Text = "炫紫";
            this.ToolColor_DarkOrchid.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolColor_RoyalBlue
            // 
            this.ToolColor_RoyalBlue.Name = "ToolColor_RoyalBlue";
            this.ToolColor_RoyalBlue.Size = new System.Drawing.Size(100, 22);
            this.ToolColor_RoyalBlue.Tag = "3";
            this.ToolColor_RoyalBlue.Text = "海洋";
            this.ToolColor_RoyalBlue.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolColor_Gold
            // 
            this.ToolColor_Gold.Name = "ToolColor_Gold";
            this.ToolColor_Gold.Size = new System.Drawing.Size(100, 22);
            this.ToolColor_Gold.Tag = "4";
            this.ToolColor_Gold.Text = "金属";
            this.ToolColor_Gold.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolColor_LightGreen
            // 
            this.ToolColor_LightGreen.Name = "ToolColor_LightGreen";
            this.ToolColor_LightGreen.Size = new System.Drawing.Size(100, 22);
            this.ToolColor_LightGreen.Tag = "5";
            this.ToolColor_LightGreen.Text = "翠绿";
            this.ToolColor_LightGreen.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity
            // 
            this.ToolClarity.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ToolClarity_10,
            this.ToolClarity_20,
            this.ToolClarity_30,
            this.ToolClarity_40,
            this.ToolClarity_50,
            this.ToolClarity_60,
            this.ToolClarity_70,
            this.ToolClarity_80,
            this.ToolClarity_90});
            this.ToolClarity.Name = "ToolClarity";
            this.ToolClarity.Size = new System.Drawing.Size(136, 22);
            this.ToolClarity.Text = "透明度设置";
            // 
            // ToolClarity_10
            // 
            this.ToolClarity_10.Name = "ToolClarity_10";
            this.ToolClarity_10.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_10.Tag = "1";
            this.ToolClarity_10.Text = "10%";
            this.ToolClarity_10.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_20
            // 
            this.ToolClarity_20.Name = "ToolClarity_20";
            this.ToolClarity_20.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_20.Tag = "2";
            this.ToolClarity_20.Text = "20%";
            this.ToolClarity_20.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_30
            // 
            this.ToolClarity_30.Name = "ToolClarity_30";
            this.ToolClarity_30.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_30.Tag = "3";
            this.ToolClarity_30.Text = "30%";
            this.ToolClarity_30.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_40
            // 
            this.ToolClarity_40.Name = "ToolClarity_40";
            this.ToolClarity_40.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_40.Tag = "4";
            this.ToolClarity_40.Text = "40%";
            this.ToolClarity_40.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_50
            // 
            this.ToolClarity_50.Name = "ToolClarity_50";
            this.ToolClarity_50.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_50.Tag = "5";
            this.ToolClarity_50.Text = "50%";
            this.ToolClarity_50.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_60
            // 
            this.ToolClarity_60.Name = "ToolClarity_60";
            this.ToolClarity_60.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_60.Tag = "6";
            this.ToolClarity_60.Text = "60%";
            this.ToolClarity_60.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_70
            // 
            this.ToolClarity_70.Name = "ToolClarity_70";
            this.ToolClarity_70.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_70.Tag = "7";
            this.ToolClarity_70.Text = "70%";
            this.ToolClarity_70.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_80
            // 
            this.ToolClarity_80.Name = "ToolClarity_80";
            this.ToolClarity_80.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_80.Tag = "8";
            this.ToolClarity_80.Text = "80%";
            this.ToolClarity_80.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClarity_90
            // 
            this.ToolClarity_90.Name = "ToolClarity_90";
            this.ToolClarity_90.Size = new System.Drawing.Size(101, 22);
            this.ToolClarity_90.Tag = "9";
            this.ToolClarity_90.Text = "90%";
            this.ToolClarity_90.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolAcquiescence
            // 
            this.ToolAcquiescence.Name = "ToolAcquiescence";
            this.ToolAcquiescence.Size = new System.Drawing.Size(136, 22);
            this.ToolAcquiescence.Text = "默认效果";
            this.ToolAcquiescence.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // ToolClose
            // 
            this.ToolClose.Name = "ToolClose";
            this.ToolClose.Size = new System.Drawing.Size(136, 22);
            this.ToolClose.Text = "退出";
            this.ToolClose.Click += new System.EventHandler(this.ToolColor_Glass_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.Gainsboro;
            this.ClientSize = new System.Drawing.Size(472, 284);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.Opacity = 0.6D;
            this.Text = "鼠标穿透窗体";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NotifyIcon notifyIcon1;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem ToolColor;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity;
        private System.Windows.Forms.ToolStripMenuItem ToolAcquiescence;
        private System.Windows.Forms.ToolStripMenuItem ToolClose;
        private System.Windows.Forms.ToolStripMenuItem ToolColor_Gainsboro;
        private System.Windows.Forms.ToolStripMenuItem ToolColor_DarkOrchid;
        private System.Windows.Forms.ToolStripMenuItem ToolColor_RoyalBlue;
        private System.Windows.Forms.ToolStripMenuItem ToolColor_Gold;
        private System.Windows.Forms.ToolStripMenuItem ToolColor_LightGreen;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_10;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_20;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_30;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_40;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_50;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_60;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_70;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_80;
        private System.Windows.Forms.ToolStripMenuItem ToolClarity_90;

    }
}
View Code

6.cs代码

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 鼠标穿透窗体
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_EXSTYLE = (-20);
        private string Var_genre = "";//记录当前操作的类型

        #region 在窗口结构中为指定的窗口设置信息
        /// <summary>
        /// 在窗口结构中为指定的窗口设置信息
        /// </summary>
        /// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
        /// <param name="nIndex">欲取回的信息</param>
        /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
        #endregion

        #region 从指定窗口的结构中取得信息
        /// <summary>
        /// 从指定窗口的结构中取得信息
        /// </summary>
        /// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
        /// <param name="nIndex">欲取回的信息</param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
        #endregion

        #region 使窗口有鼠标穿透功能
        /// <summary>
        /// 使窗口有鼠标穿透功能
        /// </summary>
        private void CanPenetrate()
        {
            uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
            uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;//窗体不出现在Windows任务栏中
            CanPenetrate();
            this.TopMost = true;//使窗体始终在其它窗体之上
        }

        #region 设置颜色和透明度的状态
        /// <summary>
        /// 设置颜色和透明度的状态
        /// </summary>
        private void SetEstate(Form Frm, object sender)
        {
            Var_genre = ((ToolStripMenuItem)sender).Name;
            string Tem_Str = Var_genre;
            if (Var_genre.IndexOf(_) >= 0)
            {
                Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf(_));
            }

            switch (Var_genre)
            {
                case "ToolColor":
                    {
                        Color Tem_Color=Color.Gainsboro;
                        switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                        {
                            case 1: Tem_Color = Color.Gainsboro; break;
                            case 2: Tem_Color = Color.DarkOrchid; break;
                            case 3: Tem_Color = Color.RoyalBlue; break;
                            case 4: Tem_Color = Color.Gold; break;
                            case 5: Tem_Color = Color.LightGreen; break;
                        }
                        Frm.BackColor = Tem_Color;
                        break;
                    }
                case "ToolClarity":
                    {
                        double Tem_Double = 0.0;
                        switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
                        {
                            case 1: Tem_Double = 0.1; break;
                            case 2: Tem_Double = 0.2; break;
                            case 3: Tem_Double = 0.3; break;
                            case 4: Tem_Double = 0.4; break;
                            case 5: Tem_Double = 0.5; break;
                            case 6: Tem_Double = 0.6; break;
                            case 7: Tem_Double = 0.7; break;
                            case 8: Tem_Double = 0.8; break;
                            case 9: Tem_Double = 0.9; break;

                        }
                        Frm.Opacity = Tem_Double;
                        break;
                    }
                case "ToolAcquiescence":
                    {
                        Frm.BackColor = Color.Gainsboro;
                        Frm.Opacity = 0.6;
                        break;
                    }
                case "ToolClose":
                    {
                        Close();
                        break;
                    }

            }
        }
        #endregion

        private void ToolColor_Glass_Click(object sender, EventArgs e)
        {
            SetEstate(this, sender);
        }
    }
}
View Code

 

改变窗体的大小(理解鼠标的操作事件和过程)

标签:

原文地址:http://www.cnblogs.com/haizhibin1989/p/4790379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!