//設置自動啟動
string path = Application.StartupPath;
SettingHel.SetAutoRun(path +@"\MostImpressive.DSCJ.DcbInterface.exe", true);

public static class SettingHel { /// <summary> /// 設置應用程序開機自動運行 /// </summary> /// <param name="fileName">應用程序的文件名</param> /// <param name="isAutoRun">是否自動運行,為false時,取消自動運行</param> /// <exception cref="system.Exception">設置不成功時拋出異常</exception> /// <returns>返回1成功,非1不成功</returns> public static String SetAutoRun(string fileName, bool isAutoRun) { string reSet = string.Empty; RegistryKey reg = null; try { if (!System.IO.File.Exists(fileName)) { reSet = "該文件不存在!"; } string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1); reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (reg == null) { reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); } if (isAutoRun) { reg.SetValue(name, fileName); reSet = "1"; } else { reg.SetValue(name, false); } } catch (Exception ex) { //“記錄異常” } finally { if (reg!=null) { reg.Close(); } } return reSet; } }
C# winForm啟動最小化到任務欄右側通知欄並交互操作
一。主要功能:
(1)、程序啟動自動隱藏到任務欄右側通知欄顯示。(與系統托盤同義)
(2)、雙擊系統托盤圖標顯示、隱藏窗口;
(3)、右擊系統托盤圖標提供三個菜單選項,“退出”、“隱藏”、“顯示”;
二。相關控件:
1、建一個WinForm程序—IconForm,將Form屬性ShowInTaskbar改為false,這樣程序將不會在任務欄中顯示。
2、將Form屬性WindowState選擇為 Minimized,以便起來自動最小化隱藏
3、在工具欄中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,這個是程序運行任務欄右側通知區域圖標顯示控件,為控件notifyIcon的屬性Icon添加一個icon圖標,或從代碼中加入。
4、在工具欄中的“菜單和工具欄”里,拖入ContextMenuStrip—contextMenuStrip1,這個控件是右擊時關聯菜單。
5、右鍵notifyIcon1選擇屬性,將其屬性ContextMenuStrip改加為contextMenuStrip1,這個時候notifyIcon1和contextMenuStrip1兩個控件就關聯了。
6、右鍵contextMenuStrip1,選擇屬性,進入Items,然后點擊“添加”,這里添加三個菜單選項:exitMenuItem、hideMenuItem、showMenuItem,同時分別將其Text屬性改為:退出、隱藏和顯示。
三。主要代碼:
1、雙擊IconForm,即添加Load事件然后
//一 右擊窗體,選擇屬性,轉到事件頁面,雙擊 Load,SizeChanged事件,給窗體添加代碼
private void Form1_Load(object sender, EventArgs e)
{
//1.將Form屬性ShowInTaskbar改為false,這樣程序將不會在任務欄中顯示。
//2.將Form屬性WindowState選擇為 Minimized,以便起來自動最小化隱藏。
string startup = Application.ExecutablePath; //取得程序路徑
int pp = startup.LastIndexOf("\\");
startup = startup.Substring(0, pp);
string icon = startup + "\\testIcon.ico";
//3.一定為notifyIcon1其設置圖標,否則無法顯示在通知欄。或者在其屬性中設置
notifyIcon1.Icon = new Icon(icon);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide(); //或者是this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
//二 雙擊窗體上的菜單項,添加相關代碼
private void exitMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你確定要退出程序嗎?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
}
private void hideMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}
private void showMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
//三 轉到窗體設計模式,右擊notifyIcon1 ,選擇屬性,雙擊其中DoubleClick,添加相關代碼
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
四。完整的代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace IconForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//說明,程序運行后自動隱藏到任務欄右側的通知欄里,
//1 右擊選擇退出,隱藏,顯示
//2 雙擊可以隱藏和顯示切換
//一 右擊窗體,選擇屬性,轉到事件頁面,雙擊 Load,SizeChanged事件,給窗體添加代碼
private void Form1_Load(object sender, EventArgs e)
{
//1.將Form屬性ShowInTaskbar改為false,這樣程序將不會在任務欄中顯示。
//2.將Form屬性WindowState選擇為 Minimized,以便起來自動最小化隱藏。
string startup = Application.ExecutablePath; //取得程序路徑
int pp = startup.LastIndexOf("\\");
startup = startup.Substring(0, pp);
string icon = startup + "\\testIcon.ico";
//3.一定為notifyIcon1其設置圖標,否則無法顯示在通知欄。或者在其屬性中設置
notifyIcon1.Icon = new Icon(icon);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide(); //或者是this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
//二 雙擊窗體上的菜單項,添加相關代碼
private void exitMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你確定要退出程序嗎?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
notifyIcon1.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
}
private void hideMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
}
private void showMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
//三 轉到窗體設計模式,右擊notifyIcon1 ,選擇屬性,雙擊其中DoubleClick,添加相關代碼
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
}
}