文章目录
- 1.将主窗口隐藏起来,并移除任务栏图标显示
- 2. 制作系统托盘
- 3. 右键托盘得到信息
- 4. 选择信息栏触发事件
- 5. 添加开机自启动与关闭开机自启动OnSetPowerBoot与OnCancelPowerBoot
右键点击托盘,弹出如下的图标
1.将主窗口隐藏起来,并移除任务栏图标显示
在CMFCApplication2Dlg::OnInitDialog进行初始化
int nFullWidth = GetSystemMetrics(SM_CXSCREEN);
int nFullHeight = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(NULL, nFullWidth, nFullHeight, 0, 0, SWP_NOZORDER);
//设置0像素,移到最角落 或者:MoveWindow(0,0,0,0);
ShowWindow(SW_HIDE);
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); //移除任务栏图标显示
2. 制作系统托盘
在CMFCApplication2Dlg类中增加两个成员与一个函数
NOTIFYICONDATA NotifyIcon; //系统托盘类
CMenu menu;
afx_msg LRESULT OnSystemtray(WPARAM wParam, LPARAM lParam);
在BEGIN_MESSAGE_MAP(CMFCApplication2Dlg, CDialogEx) 与END_MESSAGE_MAP()之间添加自定义消息
#define WM_SYSTEMTRAY WM_USER+100
ON_MESSAGE(WM_SYSTEMTRAY, &CMFCApplication2Dlg::OnSystemtray)
OnSysCommand的实现
void CMFCApplication2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
在CMFCApplication2Dlg::OnInitDialog进行初始化
//设置系统托盘
NotifyIcon.cbSize = sizeof(NOTIFYICONDATA);
//NotifyIcon.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
NotifyIcon.hIcon = m_hIcon; //上面那句也可以
NotifyIcon.hWnd = m_hWnd;
lstrcpy(NotifyIcon.szTip, _T("更改端口"));
NotifyIcon.uCallbackMessage = WM_SYSTEMTRAY;
NotifyIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &NotifyIcon); //添加系统托盘
//声明一个弹出式菜单
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING, WM_DESTROY, _T("退出"));
menu.AppendMenu(MF_STRING, WM_SETPOWERBOOT, _T("开启开机自启动"));
menu.AppendMenu(MF_STRING, WM_CANCELPOWERBOOT, _T("关闭开机自启动"));
3. 右键托盘得到信息
afx_msg LRESULT CMFCApplication2Dlg::OnSystemtray(WPARAM wParam, LPARAM lParam)
{
switch (lParam)
{
case WM_RBUTTONDOWN://右键起来时弹出快捷菜单
{
//menu.LoadMenuW(IDR_MENU1);//加载菜单资源
LPPOINT lpoint = new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN, lpoint->x, lpoint->y, this);
break;
}
case WM_LBUTTONDOWN://左键单击的处理
{
//....
}
}
return 0;
}
4. 选择信息栏触发事件
BOOL CMFCApplication2Dlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
switch (LOWORD(wParam))
{
case WM_DESTROY:
{
OnDestroy();
break;
}
case WM_SETPOWERBOOT:
{
OnSetPowerBoot(wParam, lParam);
break;
}
case WM_CANCELPOWERBOOT:
{
OnCancelPowerBoot(wParam, lParam);
break;
}
}
return CDialogEx::OnCommand(wParam, lParam);
}
5. 添加开机自启动与关闭开机自启动OnSetPowerBoot与OnCancelPowerBoot
在BEGIN_MESSAGE_MAP(CMFCApplication2Dlg, CDialogEx) 与END_MESSAGE_MAP()之间添加自定义消息
#define WM_SETPOWERBOOT WM_USER+101
#define WM_CANCELPOWERBOOT WM_USER+102
ON_MESSAGE(WM_SETPOWERBOOT, &CMFCApplication2Dlg::OnSetPowerBoot)
ON_MESSAGE(WM_CANCELPOWERBOOT, &CMFCApplication2Dlg::OnCancelPowerBoot)
LRESULT CMFCApplication2Dlg::OnSetPowerBoot(WPARAM wParam, LPARAM lParam)
{
HKEY hKey;
//找到系统的启动项
CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
//打开启动项
long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);
//找到程序自身路径
TCHAR pFileName[MAX_PATH] = {};
GetModuleFileName(NULL, pFileName, MAX_PATH);
//判断是否已经设置开机启动
TCHAR PowerBoot[MAX_PATH] = {};
DWORD nLongth = MAX_PATH;
long result = RegGetValue(hKey, NULL, _T("PowerBoot"), RRF_RT_REG_SZ, 0, PowerBoot, &nLongth);
if (result == ERROR_SUCCESS) //自启状态
{
if (lRet == ERROR_SUCCESS)
{
MessageBox(_T("已是自启状态"), _T("PanTum"));
}
}
else //未自启状态
{
//设置自启
lRet = RegSetValueEx(hKey, _T("PowerBoot"), 0, REG_SZ, (LPBYTE)pFileName, (lstrlen(pFileName) + 1) * sizeof(TCHAR));
if (lRet == ERROR_SUCCESS)
{
MessageBox(_T("设置自启成功"), _T("PanTum"));
}
}
//关闭注册表
RegCloseKey(hKey);
return 0;
}
LRESULT CMFCApplication2Dlg::OnCancelPowerBoot(WPARAM wParam, LPARAM lParam)
{
HKEY hKey;
//找到系统的启动项
CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
//打开启动项
long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);
//找到程序自身路径
TCHAR pFileName[MAX_PATH] = {};
GetModuleFileName(NULL, pFileName, MAX_PATH);
//判断是否已经设置开机启动
TCHAR PowerBoot[MAX_PATH] = {};
DWORD nLongth = MAX_PATH;
long result = RegGetValue(hKey, NULL, _T("PowerBoot"), RRF_RT_REG_SZ, 0, PowerBoot, &nLongth);
if (result == ERROR_SUCCESS) //自启状态
{
//取消自动启动
lRet = RegDeleteValue(hKey, _T("PowerBoot"));
if (lRet == ERROR_SUCCESS)
{
MessageBox(_T("关闭自启成功"), _T("PanTum"));
}
}
else //未自启状态
{
//设置自启
MessageBox(_T("已关闭自启状态"), _T("PanTum"));
}
//关闭注册表
RegCloseKey(hKey);
return 0;
}