C#实现窗口贴边自动隐藏、任务栏隐藏至托盘并添加双击和右键菜单
开发环境:Windows7系统,Visual Studio 2010专业版,.Net Framework 2.0。
一、Winform窗体贴边自动隐藏
新建Windows窗体应用程序,向Form1窗体中添加一个Timer控件,设定其Interval属性值为50,并为其添加Tick事件。代码为:
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 4) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 4), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 4, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 4));
break;
}
}
}
添加如下变量和函数:
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0 && this.Left <= 0)
{
StopAanhor = AnchorStyles.None;
}
else if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
//else if (this.Left <= 0)
//{
// StopAanhor = AnchorStyles.Left;
//}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
{
StopAanhor = AnchorStyles.Bottom;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
为Form1窗体添加Load事件,代码为:
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.timer1.Start();
}
为Form1窗体添加LocationChangeD事件,代码为:
private void Form1_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}
至此,WinForm窗体的自动贴边隐藏效果完成,该效果可以实现窗体上下右四个方位的贴边隐藏。
(如果想实现窗体上下左右四个方位的贴边隐藏,请将上面的
//else if (this.Left <= 0)
//{
// StopAanhor = AnchorStyles.Left;
//}
注释去掉,但是这样做会影响之后的窗体还原效果)
二、托盘图标、自动隐藏任务栏图标、托盘图标双击事件、托盘图标右键菜单
继续向Form1窗体中添加一个NotifyIcon控件,并为其添加一个图标。
为NotifyIcon控件添加MouseDoubleClick事件,代码如下:
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
(也可添加MouseClick事件,内部代码同上,但是会有我们不希望看到的效果,当你使用右键单击托盘图标想要弹出右键菜单时,也会触发该事件,C#好像并没有区分鼠标的左右键,不知道为什么)
再向Form1窗体添加一个ContentMenuStrip控件,并为其添加两个MenuItem子菜单项,Text分别为“还原”、“关闭”(可为每个子菜单项添加小图标,自便),为两个子菜单项添加Click事件,代码如下:
//还原
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
//关闭
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
this.notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
为Form1窗体添加SizeChanged事件,代码如下:
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
最后向Form1的Load事件中添加如下代码:
this.notifyIcon1.Visible = true;
this.ShowInTaskbar = false;
this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
到此题目所列效果均已实现,但是当窗口处于贴边隐藏状态时,我们双击托盘图标,或者右键托盘菜单还原窗口时,窗口并没有从隐藏状态变为显示状态,与我们的使用习惯不太一致。这是我们可以在NotifyIcon的MouseDoubleClick事件和右键还原菜单项的Click事件中添加如下代码:
this.timer1.Interval = 2000;
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
并且在Timer空间的Tick事件中添加下面一句代码:
this.timer1.Interval = 50;
此种方法定有不妥之处,但限于水平,只能委屈于此了。
现附Form1.cs的完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.timer1.Start();
this.notifyIcon1.Visible = true;
this.ShowInTaskbar = false;
this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Interval = 50;
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 4) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 4), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 4, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 4));
break;
}
}
}
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0 && this.Left <= 0)
{
StopAanhor = AnchorStyles.None;
}
else if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
//else if (this.Left <= 0)
//{
// StopAanhor = AnchorStyles.Left;
//}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
{
StopAanhor = AnchorStyles.Bottom;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
this.timer1.Interval = 2000;
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
//还原-
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
this.timer1.Interval = 2000;
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
case AnchorStyles.Bottom:
this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
}
}
//关闭
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
this.notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
}
}
另附Form1.Designer.cs代码供参考:
namespace WindowsFormsApplication2
{
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.timer1 = new System.Windows.Forms.Timer(this.components);
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1,
this.toolStripMenuItem2});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(101, 48);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(100, 22);
this.toolStripMenuItem1.Text = "还原";
this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(100, 22);
this.toolStripMenuItem2.Text = "关闭";
this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
}
}
项目的环境视图如下:
作者: madebychina 发表于 2011-03-27 11:24 原文链接