隐藏窗体

Lumier 2004-03-30 08:50:33
请问在.NET中如何让窗体在最小化的时候不在任务栏中显示最小化的窗体并且像OICQ那样在任务栏的最右边显示程序的小图标?还有,如何在这个小图标上添加快捷菜单?如何让程序在最小化后响应用户的键盘操作?比如,当我按下某组组合键后让窗体最大化
...全文
56 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
reformer 2004-03-30
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;
using System.Drawing;

public class SystemTray:Form
{
private NotifyIcon notifyIcon1;
private Icon m_Icon1;
private Icon m_Icon2;
private Button button1;
private Timer myTimer;
private bool flag;
public static void Main()
{
SystemTray aSystemTray=new SystemTray();
Application.Run(aSystemTray);
}
public SystemTray()
{
flag=true;
try
{
m_Icon1 = new Icon("Icon1.ico");
m_Icon2 = new Icon("Icon2.ico");
}
catch ( Exception e )
{
MessageBox.Show("Error " + e.Message,"Animate Tray - Error");
//menuItem2.Enabled = false;
//menuItem3.Enabled = false;
}
InitializeComponent();
}
private void InitializeComponent()
{
myTimer=new Timer();
myTimer.Tick+=new EventHandler(this.TimerEventProcessor);
myTimer.Interval=1000;
myTimer.Start();

this.Size=new Size(200,122);
this.notifyIcon1 = new NotifyIcon();
this.notifyIcon1.Icon = m_Icon1;
this.notifyIcon1.Text = "效果怎么样?";
this.notifyIcon1.Visible = true;
this.button1=new Button();
AddControl(button1,new Point(50,50),new Size(90,22),"隐藏窗口",0,"button1");
MenuItem menuItem1=new MenuItem("显示窗口");
MenuItem menuItem2=new MenuItem("隐藏窗口");
MenuItem menuItem3=new MenuItem("执行程序");
MenuItem menuItem4=new MenuItem("退出程序");
menuItem1.Click+=new System.EventHandler(this.menuItem1_Click);
menuItem2.Click+=new System.EventHandler(this.menuItem2_Click);
menuItem3.Click+=new System.EventHandler(this.menuItem3_Click);
menuItem4.Click+=new System.EventHandler(this.menuItem4_Click);
notifyIcon1.ContextMenu=new ContextMenu(new MenuItem[]{menuItem1,menuItem2,menuItem3,menuItem4});
notifyIcon1.DoubleClick+=new System.EventHandler(this.notifyIcon_DBClick);
button1.Click+=new System.EventHandler(this.button1_Click);
}
private void AddControl(Control aControl,Point Location,Size Size,string strText,int TabIndex,string strName)
{
aControl.Location=Location;
aControl.Size=Size;
aControl.Text=strText;
aControl.TabIndex=TabIndex;
aControl.Name=strName;
this.Controls.Add(aControl);
}
private void menuItem1_Click(object sender,System.EventArgs e)//“显示窗口”菜单的响应方法
{
if(this.Visible==false)
this.Visible=true;
}
private void menuItem2_Click(object sender,System.EventArgs e)//"隐藏窗口"菜单的响应方法
{
if(this.Visible==true)
this.Visible=false;
}
private void menuItem3_Click(object sender,System.EventArgs e)//"执行程序"菜单的响应方法
{
MessageBox.Show("此处功能就是显示一个提示框!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void menuItem4_Click(object sender,System.EventArgs e)//“退出程序”菜单的响应方法
{
this.Close();
Application.Exit();
}
private void notifyIcon_DBClick(object sender, System.EventArgs e)//用户双击应用程序图标进的响应方法
{
this.Visible=true;
}
private void button1_Click(object sender, System.EventArgs e)//用户单击按钮时的响应方法
{
this.Visible=false;
}
private void TimerEventProcessor(Object myObject,EventArgs e) //间隔触发事件
{
if ( m_Icon1 != null && m_Icon2 != null )
{
if ( flag == true )
{
this.notifyIcon1.Icon = m_Icon2;
flag = false;
}
else
{
this.notifyIcon1.Icon = m_Icon1;
flag = true;
}
}
}
}

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧