Skip to content

Commit

Permalink
增加通过ini文件设置是否允许程序重复启动时的提示信息,在Win10秋季创意者更新以后的版本中默认禁止这个提示信息。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Dec 28, 2017
1 parent 2b3e783 commit 1a9716d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 63 deletions.
43 changes: 41 additions & 2 deletions TrafficMonitor/Common.cpp
Expand Up @@ -327,10 +327,20 @@ bool CCommon::CopyStringToClipboard(const wstring & str)
else return false;
}

bool CCommon::WhenStart()
bool CCommon::WhenStart(int time, bool write_log)
{
//GetTickCount函数用于获取系统启动到现在的时间,但是如果在“控制面板\系统和安全\电源选项\选择电源按钮的功能”中
//勾选了“启用快速启动(推荐)”的话,这其实是一种混合休眠模式,这种情况下,开机时此函数获取到的值就不会被重置,
//也就是说此函数获取到的是上次完全关机后开机到现在的时间,或上次重启到现在的时间(重启就相当于完全关机一次)。
//由于Win10貌似默认开启快速启动功能,所以此函数不适合在Win10上使用,但是我目前还没有找到较好的方法获取开机时间。
int tick_count = GetTickCount();
return (tick_count < 180000);
if (write_log)
{
char buff[128];
sprintf_s(buff, "start time is %dms, no_multistart_warning_time is %d", tick_count, time);
WriteLog(buff, _T(".\\start.log"));
}
return (tick_count < time);
}

CString CCommon::GetMouseTipsInfo(__int64 today_traffic, int cpu_usage, int memory_usage, int used_memory, int total_memory, bool show_cpu_memory)
Expand All @@ -352,3 +362,32 @@ CString CCommon::GetMouseTipsInfo(__int64 today_traffic, int cpu_usage, int memo
}
return tip_info;
}

void CCommon::GetWindowsVersion(int & major_version, int & minor_version, int & build_number)
{
DWORD dwMajorVer{}, dwMinorVer{}, dwBuildNumber{};
HMODULE hModNtdll{};
if (hModNtdll = ::LoadLibraryW(L"ntdll.dll"))
{
typedef void (WINAPI *pfRTLGETNTVERSIONNUMBERS)(DWORD*, DWORD*, DWORD*);
pfRTLGETNTVERSIONNUMBERS pfRtlGetNtVersionNumbers;
pfRtlGetNtVersionNumbers = (pfRTLGETNTVERSIONNUMBERS)::GetProcAddress(hModNtdll, "RtlGetNtVersionNumbers");
if (pfRtlGetNtVersionNumbers)
{
pfRtlGetNtVersionNumbers(&dwMajorVer, &dwMinorVer, &dwBuildNumber);
dwBuildNumber &= 0x0ffff;
}
::FreeLibrary(hModNtdll);
hModNtdll = NULL;
}
major_version = dwMajorVer;
minor_version = dwMinorVer;
build_number = dwBuildNumber;
}

bool CCommon::IsWindows10FallCreatorOrLater()
{
int major_version, minor_version, build_number;
GetWindowsVersion(major_version, minor_version, build_number);
return ((major_version == 10 && build_number >= 16299) || major_version > 10);
}
9 changes: 7 additions & 2 deletions TrafficMonitor/Common.h
Expand Up @@ -98,11 +98,16 @@ class CCommon
//将一个字符串保存到剪贴板
static bool CopyStringToClipboard(const wstring& str);

//判断现在是否刚开机,在刚开机3分钟内返回true,否则返回false
static bool WhenStart();
//判断现在是否刚开机,在刚开机的time毫秒内返回true,否则返回false
static bool WhenStart(int time, bool write_log = false);

//显示鼠标提示
static CString GetMouseTipsInfo(__int64 today_traffic, int cpu_usage, int memory_usage, int used_memory, int total_memory, bool show_cpu_memory);

//获取Windows版本
static void GetWindowsVersion(int& major_version, int& minor_version, int& build_number);

//判断当前Windows版本是否为Win10秋季创意者更新或更新的版本
static bool IsWindows10FallCreatorOrLater();
};

3 changes: 2 additions & 1 deletion TrafficMonitor/HistoryTrafficDlg.cpp
Expand Up @@ -72,7 +72,8 @@ BOOL CHistoryTrafficDlg::OnInitDialog()
m_history_list.SetItemText(i, 1, k_bytes_str);

}

//LVITEMW item;
//m_history_list.GetSub;
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
Binary file modified TrafficMonitor/TrafficMonitor.aps
Binary file not shown.
86 changes: 33 additions & 53 deletions TrafficMonitor/TrafficMonitor.cpp
Expand Up @@ -29,51 +29,20 @@ CTrafficMonitorApp::CTrafficMonitorApp()
// 将所有重要的初始化放置在 InitInstance 中
}

//void CTrafficMonitorApp::_OnOptions(int tab)
//{
// COptionsDlg optionsDlg(tab);
// //将选项设置数据传递给选项设置对话框
// optionsDlg.m_tab1_dlg.m_font_name = theApp.m_font_name;
// optionsDlg.m_tab1_dlg.m_font_size = theApp.m_font_size;
// optionsDlg.m_tab1_dlg.m_text_color = theApp.m_text_color;
// optionsDlg.m_tab1_dlg.m_up_string = theApp.m_up_string;
// optionsDlg.m_tab1_dlg.m_down_string = theApp.m_down_string;
// optionsDlg.m_tab1_dlg.m_cpu_string = theApp.m_cpu_string;
// optionsDlg.m_tab1_dlg.m_memory_string = theApp.m_memory_string;
// optionsDlg.m_tab1_dlg.m_swap_up_down = theApp.m_swap_up_down;
//
// optionsDlg.m_tab2_dlg.m_font_name = theApp.m_tbar_font_name;
// optionsDlg.m_tab2_dlg.m_font_size = theApp.m_tbar_font_size;
// optionsDlg.m_tab2_dlg.m_text_color = theApp.m_tbar_text_color;
// optionsDlg.m_tab2_dlg.m_back_color = theApp.m_tbar_back_color;
// optionsDlg.m_tab2_dlg.m_up_string = theApp.m_tbar_up_string;
// optionsDlg.m_tab2_dlg.m_down_string = theApp.m_tbar_down_string;
// optionsDlg.m_tab2_dlg.m_cpu_string = theApp.m_tbar_cpu_string;
// optionsDlg.m_tab2_dlg.m_memory_string = theApp.m_tbar_memory_string;
// optionsDlg.m_tab2_dlg.m_swap_up_down = theApp.m_tbar_swap_up_down;
//
// if (optionsDlg.DoModal() == IDOK)
// {
// theApp.m_font_name = optionsDlg.m_tab1_dlg.m_font_name;
// theApp.m_font_size = optionsDlg.m_tab1_dlg.m_font_size;
// theApp.m_text_color = optionsDlg.m_tab1_dlg.m_text_color;
// theApp.m_up_string = optionsDlg.m_tab1_dlg.m_up_string;
// theApp.m_down_string = optionsDlg.m_tab1_dlg.m_down_string;
// theApp.m_cpu_string = optionsDlg.m_tab1_dlg.m_cpu_string;
// theApp.m_memory_string = optionsDlg.m_tab1_dlg.m_memory_string;
// theApp.m_swap_up_down = optionsDlg.m_tab1_dlg.m_swap_up_down;
//
// theApp.m_tbar_font_name = optionsDlg.m_tab2_dlg.m_font_name;
// theApp.m_tbar_font_size = optionsDlg.m_tab2_dlg.m_font_size;
// theApp.m_tbar_text_color = optionsDlg.m_tab2_dlg.m_text_color;
// theApp.m_tbar_back_color = optionsDlg.m_tab2_dlg.m_back_color;
// theApp.m_tbar_up_string = optionsDlg.m_tab2_dlg.m_up_string;
// theApp.m_tbar_down_string = optionsDlg.m_tab2_dlg.m_down_string;
// theApp.m_tbar_cpu_string = optionsDlg.m_tab2_dlg.m_cpu_string;
// theApp.m_tbar_memory_string = optionsDlg.m_tab2_dlg.m_memory_string;
// theApp.m_tbar_swap_up_down = optionsDlg.m_tab2_dlg.m_swap_up_down;
// }
//}
void CTrafficMonitorApp::LoadConfig()
{
bool is_windows10_fall_creator = CCommon::IsWindows10FallCreatorOrLater();

m_no_multistart_warning_time = GetPrivateProfileInt(_T("other"), _T("no_multistart_warning_time"), 300000, m_config_path.c_str());
//如果当前Windows版本是Win10秋季创意者更新,则m_no_multistart_warning默认为true,否则默认为false
m_no_multistart_warning = (GetPrivateProfileInt(_T("other"), _T("no_multistart_warning"), is_windows10_fall_creator ? 1 : 0, m_config_path.c_str()) != 0);
}

void CTrafficMonitorApp::SaveConfig()
{
CCommon::WritePrivateProfileIntW(_T("other"), _T("no_multistart_warning_time"), m_no_multistart_warning_time, m_config_path.c_str());
CCommon::WritePrivateProfileIntW(_T("other"), _T("no_multistart_warning"), m_no_multistart_warning, m_config_path.c_str());
}


// 唯一的一个 CTrafficMonitorApp 对象
Expand All @@ -85,10 +54,20 @@ CTrafficMonitorApp theApp;

BOOL CTrafficMonitorApp::InitInstance()
{
//设置配置文件的路径
wstring exe_path{ CCommon::GetExePath() };
m_config_path = exe_path + L"config.ini";
m_history_traffic_path = exe_path + L"history_traffic.dat";
m_log_path = exe_path + L"error.log";
m_skin_path = exe_path + L"skins";

//从ini文件载入设置
LoadConfig();

#ifndef _DEBUG
wstring cmd_line{ m_lpCmdLine };
bool is_restart{ cmd_line.find(L"RestartByRestartManager") != wstring::npos }; //如果命令行参数中含有字符串“RestartByRestartManager”则说明程序是被Windows重新启动的
bool when_start{ CCommon::WhenStart() };
bool when_start{ CCommon::WhenStart(m_no_multistart_warning_time) };
if (is_restart && when_start) //如果在开机时程序被重新启动,则直接退出程序
{
//AfxMessageBox(_T("调试信息:程序已被Windows的重启管理器重新启动。"));
Expand All @@ -101,19 +80,20 @@ BOOL CTrafficMonitorApp::InitInstance()
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
if (!when_start)
//char buff[128];
//string cmd_line_str{ CCommon::UnicodeToStr(cmd_line.c_str()) };
//sprintf_s(buff, "when_start=%d, m_no_multistart_warning=%d, cmd_line=%s", when_start, m_no_multistart_warning, cmd_line_str.c_str());
//CCommon::WriteLog(buff, _T(".\\start.log"));
if (!when_start && !m_no_multistart_warning)
{
AfxMessageBox(_T("已经有一个程序正在运行。"));
}
return FALSE;
}
}
#endif

//设置配置文件的路径
wstring exe_path{ CCommon::GetExePath() };
m_config_path = exe_path + L"config.ini";
m_history_traffic_path = exe_path + L"history_traffic.dat";
m_log_path = exe_path + L"error.log";
m_skin_path = exe_path + L"skins";
SaveConfig();

// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
Expand Down
9 changes: 7 additions & 2 deletions TrafficMonitor/TrafficMonitor.h
Expand Up @@ -63,8 +63,13 @@ class CTrafficMonitorApp : public CWinApp

CTrafficMonitorApp();

//打开“选项”对话框的处理,参数为打开时切换的标签
//void _OnOptions(int tab);
bool WhenStart() const { return CCommon::WhenStart(m_no_multistart_warning_time); }
void LoadConfig();
void SaveConfig();

private:
int m_no_multistart_warning_time{}; //用于设置在开机后多长时间内不弹出“已经有一个程序正在运行”的警告提示
bool m_no_multistart_warning{}; //如果为false,则永远都不会弹出“已经有一个程序正在运行”的警告提示

// 重写
public:
Expand Down
2 changes: 1 addition & 1 deletion TrafficMonitor/TrafficMonitor.vcxproj
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{09483BED-B1E9-4827-8120-A18302C84AA8}</ProjectGuid>
<RootNamespace>TrafficMonitor</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion>
<Keyword>MFCProj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down
4 changes: 2 additions & 2 deletions TrafficMonitor/TrafficMonitorDlg.cpp
Expand Up @@ -764,7 +764,7 @@ void CTrafficMonitorDlg::OnTimer(UINT_PTR nIDEvent)
if (theApp.m_hide_main_window) //设置隐藏主窗口
ShowWindow(SW_HIDE);

if (!CCommon::WhenStart())
if (!theApp.WhenStart())
{
if (m_show_task_bar_wnd && m_tBarDlg == nullptr)
OpenTaskBarWnd();
Expand All @@ -774,7 +774,7 @@ void CTrafficMonitorDlg::OnTimer(UINT_PTR nIDEvent)
}

//开机启动2秒后显示任务栏窗口,防止开机启动时由于任务栏还没有加载导致嵌入任务栏失败的问题
if (CCommon::WhenStart() && m_timer_cnt == 2)
if (theApp.WhenStart() && m_timer_cnt == 2)
{
if (m_show_task_bar_wnd && m_tBarDlg == nullptr)
OpenTaskBarWnd();
Expand Down

0 comments on commit 1a9716d

Please sign in to comment.