标签:
1.Designer.cs部分代码
namespace QQFrm { 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.timer1 = new System.Windows.Forms.Timer(this.components); this.timer2 = new System.Windows.Forms.Timer(this.components); this.panel1 = new System.Windows.Forms.Panel(); this.panel_Title = new System.Windows.Forms.Panel(); this.panel_Close = new System.Windows.Forms.Panel(); this.panel2 = new System.Windows.Forms.Panel(); this.panel_Title.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // timer1 // this.timer1.Enabled = true; this.timer1.Interval = 2; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // timer2 // this.timer2.Interval = 1; this.timer2.Tick += new System.EventHandler(this.timer2_Tick); // // panel1 // this.panel1.BackgroundImage = global::QQFrm.Properties.Resources._01; this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom; this.panel1.Location = new System.Drawing.Point(0, 443); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(174, 4); this.panel1.TabIndex = 1; // // panel_Title // this.panel_Title.BackColor = System.Drawing.Color.DarkBlue; this.panel_Title.BackgroundImage = global::QQFrm.Properties.Resources._1; this.panel_Title.Controls.Add(this.panel_Close); this.panel_Title.Location = new System.Drawing.Point(5, 0); this.panel_Title.Name = "panel_Title"; this.panel_Title.Size = new System.Drawing.Size(314, 27); this.panel_Title.TabIndex = 0; this.panel_Title.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseDown); this.panel_Title.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseMove); this.panel_Title.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_Title_MouseUp); // // panel_Close // this.panel_Close.BackColor = System.Drawing.Color.Red; this.panel_Close.BackgroundImage = global::QQFrm.Properties.Resources.Close; this.panel_Close.Location = new System.Drawing.Point(150, 3); this.panel_Close.Name = "panel_Close"; this.panel_Close.Size = new System.Drawing.Size(18, 18); this.panel_Close.TabIndex = 0; this.panel_Close.Click += new System.EventHandler(this.panel1_Click); // // panel2 // this.panel2.BackgroundImage = global::QQFrm.Properties.Resources._02; this.panel2.Controls.Add(this.panel_Title); this.panel2.Location = new System.Drawing.Point(0, 4); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(174, 443); this.panel2.TabIndex = 2; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(174, 447); this.Controls.Add(this.panel2); this.Controls.Add(this.panel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.panel_Title.ResumeLayout(false); this.panel2.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel_Title; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Timer timer2; private System.Windows.Forms.Panel panel_Close; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Panel panel2; } }
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; using System.Runtime.InteropServices; namespace QQFrm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 公共变量 IntPtr Tem_Handle;//获取控件及窗体的句柄 Point CPoint;//获取控件中鼠标的坐标 static int Tem_place = 0; int Frm_Height = 0; int FrmHeight = 0; #endregion #region API声明 //获取当前鼠标下可视化控件的句柄 [DllImport("user32.dll")] public static extern int WindowFromPoint(int xPoint, int yPoint); //获取指定句柄的父级句柄 [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd); //获取屏幕的大小 [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")] private static extern int GetSystemMetrics(int mVal); #endregion #region 获取当前鼠标下可视化控件的句柄 /// <summary> /// 获取当前鼠标下可视化控件的句柄 /// </summary> /// <param x="int">当前鼠标的X坐标</param> /// <param y="int">当前鼠标的Y坐标</param> public IntPtr FormNameAt(int x, int y) { IntPtr Tem_hWnd;//设置存储句柄的变量 Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//获取当前鼠标下可视化控件的句柄 Tem_hWnd = Tem_Handle;//记录原始句柄 while (Tem_hWnd != ((IntPtr)0))//遍历该句柄的父级句柄 { Tem_Handle = Tem_hWnd;//记录当前句柄 Tem_hWnd = GetParent(Tem_hWnd);//获取父级句柄 } return Tem_Handle;//返回最底层的父级句柄 } #endregion private void timer1_Tick(object sender, EventArgs e) { if (this.Top < 3 && Tem_place==0)//如果窗体被移到屏幕的顶部 { if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 { panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部 timer2.Enabled = false;//不对窗体进行拉伸操作 this.Top = 0;//使窗体致顶 } else { panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部 timer2.Enabled = true;//将窗体在顶部进行隐藏 } } else { if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的左端或右端 { if (this.Left < 3)//如果窗体被移到屏幕的左端 { if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 { panel_Title.Tag = 2;//设置标识,用于判断窗体在屏幕左端 timer2.Enabled = false; Frm_Height = this.Height; this.Left = 0;//使窗体致左 this.Top = 0; this.Height = Screen.AllScreens[0].Bounds.Height; Tem_place = 1; } else { panel_Title.Tag = 2; timer2.Enabled = true;//将窗体在左端进行隐藏 } } if (this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的右端 { if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 { panel_Title.Tag = 3;//设置标识,用于判断窗体在屏幕右端 timer2.Enabled = false; Frm_Height = this.Height; this.Left = GetSystemMetrics(0) - this.Width;//使窗体致右 this.Top = 0; this.Height = Screen.AllScreens[0].Bounds.Height; Tem_place = 1; } else { panel_Title.Tag = 3; timer2.Enabled = true;//将窗体在右端进行隐藏 } } } } } private void timer2_Tick(object sender, EventArgs e) { switch (Convert.ToInt16(panel_Title.Tag.ToString()))//对标识进行判断 { case 1://顶端隐藏 { if (this.Top < 5) this.Top = -(this.Height - 2); break; } case 2://左端隐藏 { if (this.Left < 5) this.Left = -(this.Width - 2); break; } case 3://右端隐藏 { if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5)) this.Left = GetSystemMetrics(0) - 2; break; } } } private void panel1_Click(object sender, EventArgs e) { this.Close(); } #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;//设置当前窗体在屏幕上的位置 Tem_place = 0; this.Height = FrmHeight; } } #endregion private void panel_Title_MouseDown(object sender, MouseEventArgs e) { timer1.Enabled = false; CPoint = new Point(-e.X, -e.Y); } private void panel_Title_MouseMove(object sender, MouseEventArgs e) { FrmMove(this, e); } private void panel_Title_MouseUp(object sender, MouseEventArgs e) { timer1.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { Frm_Height = this.Height; FrmHeight = this.Height; this.TopMost = true; } } }
标签:
原文地址:http://www.cnblogs.com/haizhibin1989/p/4790249.html