winForm程序开机启动和托盘显示,并允许一个程序运行

1、建一个WinForm程序—TestIconForm,将其属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示;将MaximizeBox属性设置为false,屏蔽掉最大化按钮;把StartPosition属性改为CerternScreen,这样程序运行后,窗口将会居中显示。

2、在工具栏中的公共控件里,拖入NotifyIcon控件—NotifyIcon1,这个是程序运行任务栏右侧通知区域图标显示控件。

3、在工具栏中的菜单和工具栏里,拖入ContextMenuStrip—ContextMenuStrip1,这个控件是右击时关联菜单。

4、右键NotifyIcon1选择属性,将其属性ContextMenuStrip改加为testContextMenuStrip,这个时候1和2两个步骤的两个控件就关联了,用于完成上面(3)功能。

5、右键testContextMenuStrip选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。

 private void OPCValue_Load(object sender, EventArgs e)
        {
            this.Hide();   
        }

#region 托盘处理
        private void OPCValue_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            HideMainForm();
        }

        private void OPCValue_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }

        }

        private void ShowMenuItem_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }

        private void HideMenuItem_Click(object sender, EventArgs e)
        {
            HideMainForm();
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            ExitMainForm();
        }

        #region 处理窗体的 显示 隐藏 关闭(退出)
        private void ExitMainForm()
        {
            if (MessageBox.Show("您确定要退出OPC数据接收程序吗?", "确认退出 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();

            }
        }

        private void HideMainForm()
        {
            this.Hide();
        }

        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        #endregion

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                HideMainForm();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                ShowMainForm();
            }
        }
        #endregion

只运行一个程序运行代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OPCValues
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            int processCount = 0;
            Process[] pa = Process.GetProcesses();//获取当前进程数组。
            foreach (Process PTest in pa)
            {
                if (PTest.ProcessName == Process.GetCurrentProcess().ProcessName)
                {
                    processCount += 1;
                }
            }
            if (processCount > 1)
            {
                MessageBox.Show(null, "相同的程序已经在运行了,请不要同时运行多个程序!\n\n这个程序即将退出!",
                    Application.ProductName + "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               // Application.Exit();

                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new OPCValue());
        }
    }
}

 

posted on 2014-09-24 19:41  泰山脚下 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/godxiangyu/p/3991406.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值