C# 如何让窗体最小化到后台托管(类似QQ托管一样)
2012-07-20 21:50
465 查看
首先,向想要后台运行的 主窗体设计页面添加两个控件
notifyIcon 控件 和 contextMenuStrip控件
notifyIcon 控件 中的 Icon属性 增加图标 (这个图标就是你后台托管时显示的图标)
在 contextMenuStrip 属性中 增加 contextMenuStrip控件的名字 (主要让 托管的图标响应右键,显示右键菜单)
在 contextMenuStrip控件 中增加 让托管中显示的 右键菜单
接着,在主窗体的代码页面中 增加 this.SizeChanged +=new EventHandler(Form1_SizeChanged);
这个为响应窗体上 最大化,最小化,关闭 所触发的事件
事件为
[c-sharp] view
plaincopy
private void Form1_SizeChanged(object sender,EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();//隐藏主窗体
this.notifyIcon1.Visible = true;//让notifyIcon1图标显示
}
}
在 notifyIcon控件 鼠标点击事件中 添加
[c-sharp] view
plaincopy
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) //当鼠标点击托管图标时触发
{
if (e.Button == MouseButtons.Left)//当鼠标点击为左键时
{
this.Show();//显示主窗体
this.WindowState = FormWindowState.Normal;//主窗体的大小为默认
}
}
在contextMenuStrip控件中 设定的 菜单中响应事件
[c-sharp] view
plaincopy
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右击托管图标时触发的事件
{
this.Close();
}
这样就可以让你的窗体 在后台托管了
完整的代码如下:
[c-sharp] view
plaincopy
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 FormBackstageCustody
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SizeChanged +=new EventHandler(Form1_SizeChanged);
}
private void Form1_SizeChanged(object sender,EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();//隐藏主窗体
this.notifyIcon1.Visible = true;//让notifyIcon1图标显示
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) //当鼠标点击托管图标时触发
{
if (e.Button == MouseButtons.Left)//当鼠标点击为左键时
{
this.Show();//显示主窗体
this.WindowState = FormWindowState.Normal;//主窗体的大小为默认
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右击托管图标时触发的事件
{
this.Close();
}
}
}
原文出处:http://blog.csdn.net/zhuimeng11025/article/details/6085312
notifyIcon 控件 和 contextMenuStrip控件
notifyIcon 控件 中的 Icon属性 增加图标 (这个图标就是你后台托管时显示的图标)
在 contextMenuStrip 属性中 增加 contextMenuStrip控件的名字 (主要让 托管的图标响应右键,显示右键菜单)
在 contextMenuStrip控件 中增加 让托管中显示的 右键菜单
接着,在主窗体的代码页面中 增加 this.SizeChanged +=new EventHandler(Form1_SizeChanged);
这个为响应窗体上 最大化,最小化,关闭 所触发的事件
事件为
[c-sharp] view
plaincopy
private void Form1_SizeChanged(object sender,EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();//隐藏主窗体
this.notifyIcon1.Visible = true;//让notifyIcon1图标显示
}
}
在 notifyIcon控件 鼠标点击事件中 添加
[c-sharp] view
plaincopy
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) //当鼠标点击托管图标时触发
{
if (e.Button == MouseButtons.Left)//当鼠标点击为左键时
{
this.Show();//显示主窗体
this.WindowState = FormWindowState.Normal;//主窗体的大小为默认
}
}
在contextMenuStrip控件中 设定的 菜单中响应事件
[c-sharp] view
plaincopy
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右击托管图标时触发的事件
{
this.Close();
}
这样就可以让你的窗体 在后台托管了
完整的代码如下:
[c-sharp] view
plaincopy
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 FormBackstageCustody
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SizeChanged +=new EventHandler(Form1_SizeChanged);
}
private void Form1_SizeChanged(object sender,EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();//隐藏主窗体
this.notifyIcon1.Visible = true;//让notifyIcon1图标显示
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) //当鼠标点击托管图标时触发
{
if (e.Button == MouseButtons.Left)//当鼠标点击为左键时
{
this.Show();//显示主窗体
this.WindowState = FormWindowState.Normal;//主窗体的大小为默认
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右击托管图标时触发的事件
{
this.Close();
}
}
}
原文出处:http://blog.csdn.net/zhuimeng11025/article/details/6085312
相关文章推荐
- C#利用API制作类似QQ一样的右下角弹出窗体
- winForm窗体关闭按钮实现托盘后台运行(类似QQ托盘区运行)
- 用C#代码实现类似QQ窗体的“上、左、右”停靠功能
- 如何让窗体像QQ一样抖动起来
- 用C#开发类似QQ输入法的不规则窗体的程序详解+代码打包分享
- 【转载】winForm窗体关闭按钮 实现提示选择,托盘后台运行或退出(类似QQ托盘区运行)
- c#如何使用webservice、存储过程及存储过程的创建(简单模仿类似QQ统计用户在线时间为例)
- c#如何使用webservice、存储过程及存储过程的创建(简单模仿类似QQ统计用户在线时间为例)
- C# 实现像QQ一样隐藏窗体
- 用C#代码实现类似QQ窗体的“上、左、右”停靠功能【转】
- C#实现类似QQ的窗体停靠
- winForm窗体关闭按钮实现托盘后台运行(类似QQ托盘区运行)
- C# 实现类似QQ的窗体在桌面边缘停靠和隐藏
- C# 如何做类似微博和QQ的授权登录
- [VB.NET]请问类似QQ窗体靠边自动隐藏的效果如何实现?????
- C#Winform窗体如何最小化到系统托盘
- c#制作类似qq安装程序一样的单文件程序安装包
- C#实现类似QQ的隐藏浮动窗体、消息闪动
- winForm窗体关闭按钮实现托盘后台运行(类似QQ托盘区运行)
- 用C#如何实现窗体最小化时,放到任务栏右端的静态区中