当前位置:网站首页>C # minimize the WinForm software to the system tray, and start up automatically
C # minimize the WinForm software to the system tray, and start up automatically
2022-07-19 15:28:00 【sam-zy】
original text :https://www.cnblogs.com/GT_Andy/archive/2010/03/16/1921836.html
1. Minimize to system tray
add to notifyIcon and contextMenuStrip
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized; // Minimize to system tray
}
// To minimize the
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized) // Minimize to system tray
{
Visible = false;
notifyIcon1.Visible = true;
}
}
1.1.notifyIcon Set up :


1.2.NotifyIcon Double click to pop up the software
//NotifyIcon Double click to pop up the software
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetF(); // Get the handle of this form
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetF(IntPtr hWnd); // Set this form as the active form
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Visible = true;
WindowState = FormWindowState.Normal;
Show();
if (this.Handle != GetF()) // If this window does not get focus
SetF(this.Handle); // Set this window to get focus
}
1.3.contextMenuStrip Set up :

// Right click the small icon menu - sign out
private void sign out ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
2. Boot up
// Boot up
private void checkBoxReboot_CheckedChanged(object sender, EventArgs e)
{
string R_startPath = Application.ExecutablePath;
if (checkBoxReboot.Checked == true)
{
RegistryKey R_local = Registry.LocalMachine;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.SetValue("BirthdayTipF", 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("BirthdayTipF", false);
R_run.Close();
R_local.Close();
}
catch (Exception ex)
{
MessageBox.Show(" You need administrator privileges to modify ", " Tips ", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
}
边栏推荐
- Discussion on ISP image noise model 2
- Is it safe for Hengtai securities to open an account online?
- Impact analysis: rubygems unauthorized access vulnerability (cve-2022-29176)
- 浅谈ISP-CCM
- 初来乍到,多多关照(其实不是初来了^_^,Hello CSDN,我来了)
- [flower carving hands-on] interesting music visualization project (11) --ws2812 magic ribbon
- Code compliance: five reasons why developers use helix QAC
- MMRotate从零开始训练自己的数据集
- wpa_supplicant @authenticate
- Questions about Alibaba cloud's classic network
猜你喜欢

Face technology: the picture of unclear people is repaired into a high-quality and high-definition image framework (with source code download)

State machine exercise

A - trees on the level

OSPF学习笔记(四)

浅谈ISP-噪声模型1
![[gym103660] the 19th Zhejiang University City College Programming Contest vp/s](/img/97/655aa436d42340db4fc12c7c79b863.png)
[gym103660] the 19th Zhejiang University City College Programming Contest vp/s
![[user article] examples of P4 consolidation practice guide disassemble resolve](/img/43/fd68171e5223d0ec4af55fc208a7ac.png)
[user article] examples of P4 consolidation practice guide disassemble resolve

【Renesas RA6M4开发板之I2C读取mpu6050】

FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card

【C语言进阶】---常见内存函数
随机推荐
证券账户上买基金安全吗。可以做短线吗
I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)
Reflector使用详解,把DLL文件转换为.cs文件
初来乍到,多多关照(其实不是初来了^_^,Hello CSDN,我来了)
PostgreSQL installation et démarrage des tutoriels de base sous Linux et Windows
FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card
Graph Cuts学习
Arkui FAQ summary [Series 2]
GYM103660H. Distance
2022/7/17
【C语言进阶】---常见内存函数
Selenium三种等待方式以及Alert弹窗的三种处理方式
Using flex layout to realize local scroll bar
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
wpa_supplicant @authenticate
人脸技术:不清楚人照片修复成高质量高清晰图像框架(附源代码下载)
232. 用栈实现队列
数字IC-1.11.1 静态时序分析 - 单周期静态时序分析
Wechat applet 7 cloud storage
【codeforces round#800 D题 Fake Plastic Trees】树上贪心