近段时间忙于写一个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
在注册表的"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"中
存储应用程序名和路径可以实现程序的自启动。代码如下:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoRun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 设置开机启动
private void btnSet_Click( object sender, EventArgs e)
{
Microsoft.Win32.Registry.SetValue( @" HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run "
, Application.ProductName, Application.StartupPath + Application.ProductName);
}
// C# WinForm打开超链接
private void Form1_Load( object sender, EventArgs e)
{
System.Diagnostics.Process.Start( " iexplore.exe " , " http://revit.5d6d.com " );
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoRun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 设置开机启动
private void btnSet_Click( object sender, EventArgs e)
{
Microsoft.Win32.Registry.SetValue( @" HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run "
, Application.ProductName, Application.StartupPath + Application.ProductName);
}
// C# WinForm打开超链接
private void Form1_Load( object sender, EventArgs e)
{
System.Diagnostics.Process.Start( " iexplore.exe " , " http://revit.5d6d.com " );
}
}
}