Winform开机启动与托盘显示

近段时间忙于写一个winform小程序,对于擅长写web程序的我谈不上是在分享,这里所写的相当于我的备忘录,呵呵。。
1)开机启动

string R_startPath = Application.ExecutablePath; if (checkBox1.Checked == true) { RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run"); R_run.SetValue("MT128Stock", R_startPath); R_run.Close(); R_local.Close(); } else { try { RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run"); R_run.DeleteValue("MT128Stock", false); R_run.Close(); R_local.Close(); } catch (Exception ex) { MessageBox.Show("您需要管理員權權限修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } }

2)托盘显示
向winfom中插入NotifyIcon控件notifyIcon1
protected override void OnSizeChanged(EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.Width = 920; this.Height = 530; } base.OnSizeChanged(e); } protected override void OnClosing(CancelEventArgs e) { //base.OnClosing(e); e.Cancel = true; HideMainForm(); } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.Visible) { HideMainForm(); } else { ShowMainForm(); } } private void HideMainForm() { this.Hide(); } private void ShowMainForm() { this.WindowState = FormWindowState.Normal; this.Show(); this.Activate(); } private void ExitMainForm() { if (MessageBox.Show("您確認要退出股票接收程序嗎?", "確認退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { this.notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } }

 

3)托盘显示的右击菜单
向winfom中插入ContextMenuStrip控件contextMenuStrip1,再将notifyIcon1的ContextMenuStrip属性值设为contextMenuStrip1,并添加多项相关Items
#region 右键菜单处理,显示 隐藏 退出 private void MinShowItem_Click(object sender, EventArgs e) { HideMainForm(); } private void ShowItem_Click(object sender, EventArgs e) { ShowMainForm(); } private void ExitItem_Click(object sender, EventArgs e) { ExitMainForm(); } #endregion

你可能感兴趣的:(exception,Web,object,String,WinForm)