将判断Windows版本的处理封装成一个类

This commit is contained in:
Zhong Yang
2018-07-21 12:00:32 +08:00
parent ea2394544e
commit ef2447b764
13 changed files with 112 additions and 75 deletions
+1 -1
View File
@@ -172,7 +172,7 @@ BOOL CAppearanceSettingDlg::OnInitDialog()
#ifdef COMPILE_IN_WIN_XP
m_follow_system_color_check.EnableWindow(FALSE);
#else
if (IsWindows8OrGreater())
if (theApp.m_win_version.IsWindows8OrLater())
m_follow_system_color_check.SetCheck(m_data.theme_color_follow_system);
else
m_follow_system_color_check.EnableWindow(FALSE); //Win8以下系统禁用此复选按钮
-54
View File
@@ -416,60 +416,6 @@ int CCommon::GetListWidth(CListBox & list)
return nMaxExtent;
}
//void CCommon::FillStaticColor(CStatic & static_ctr, COLORREF color)
//{
// CDC* pDC = static_ctr.GetDC();
// CRect rect;
// static_ctr.GetClientRect(&rect);
// CBrush BGBrush, *pOldBrush;
// BGBrush.CreateSolidBrush(color);
// pOldBrush = pDC->SelectObject(&BGBrush);
// pDC->FillRect(&rect, &BGBrush);
// pDC->SelectObject(pOldBrush);
// BGBrush.DeleteObject();
//}
//COLORREF CCommon::ConvertToItemColor(COLORREF color)
//{
// int r, g, b, l;
// int r1, g1, b1, l1;
// r = GetRValue(color);
// g = GetGValue(color);
// b = GetBValue(color);
// l = ((r * 299) + (g * 587) + (b * 114)) / 1000; //计算亮度值
// if (l == 0) return color;
//
// l1 = (l - 80)*3 / 5 + 80; //计算新亮度
// r1 = r * l1 / l;
// g1 = g * l1 / l;
// b1 = b * l1 / l;
// return RGB(r1, g1, b1);
//}
bool CCommon::IsWindows10OrLater()
{
#ifdef COMPILE_IN_WIN_XP
return false;
#else
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;
}
return (dwMajorVer >= 10u);
#endif
}
int CCommon::DeleteAFile(HWND hwnd, _tstring file)
{
-5
View File
@@ -121,11 +121,6 @@ public:
//获取一个列表控件最大长度项目宽度的像素值
static int GetListWidth(CListBox& list);
//判断当前操作系统是否是Windows10或以上版本
//VersionHelper中的函数无法判断Windows8.1和Windows10,
//这个函数调用了RtlGetNtVersionNumbers获取系统版本信息
static bool IsWindows10OrLater();
//删除一个文件
static int DeleteAFile(HWND hwnd, _tstring file);
//删除多个文件
-3
View File
@@ -112,9 +112,6 @@ BOOL CMusicPlayerApp::InitInstance()
LoadSongData();
LoadConfig();
//判断当前操作系统是否是Windows10。结果储存在CMusicPlayerApp类中的成员变量中,以便在全局范围内可访问
m_is_windows10 = CCommon::IsWindows10OrLater();
//启动时检查更新
if (m_general_setting_data.check_update_when_start)
{
+2 -1
View File
@@ -14,6 +14,7 @@
#include "ColorConvert.h"
#include "DrawCommon.h"
#include "IniHelper.h"
#include "WinVersionHelper.h"
//选项设置数据
struct PlaySettingData
@@ -79,7 +80,7 @@ public:
CMusicPlayerApp();
int m_dpi;
bool m_is_windows10; //当前系统是否是Windows10
CWinVersionHelper m_win_version; //当前Windows的版本
CPlayer m_player;
wstring m_module_dir; //当前程序exe文件所在目录
+2
View File
@@ -200,6 +200,7 @@
<ClInclude Include="CoverDownloadCommon.h" />
<ClInclude Include="CoverDownloadDlg.h" />
<ClInclude Include="IniHelper.h" />
<ClInclude Include="WinVersionHelper.h" />
<ClInclude Include="WmaEncodeCfgDlg.h" />
<ClInclude Include="FilePathHelper.h" />
<ClInclude Include="ColorConvert.h" />
@@ -252,6 +253,7 @@
<ClCompile Include="CoverDownloadCommon.cpp" />
<ClCompile Include="CoverDownloadDlg.cpp" />
<ClCompile Include="IniHelper.cpp" />
<ClCompile Include="WinVersionHelper.cpp" />
<ClCompile Include="WmaEncodeCfgDlg.cpp" />
<ClCompile Include="FilePathHelper.cpp" />
<ClCompile Include="ColorConvert.cpp" />
@@ -144,6 +144,9 @@
<Filter Include="头文件和源文件\公共的类\IniHelper">
<UniqueIdentifier>{42734b69-66a6-4f02-b9f2-8c4675336e95}</UniqueIdentifier>
</Filter>
<Filter Include="头文件和源文件\公共的类\WinVersionHelper">
<UniqueIdentifier>{a08082b3-9202-4b64-9941-d4ff6059ae9b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
@@ -303,6 +306,9 @@
<ClInclude Include="IniHelper.h">
<Filter>头文件和源文件\公共的类\IniHelper</Filter>
</ClInclude>
<ClInclude Include="WinVersionHelper.h">
<Filter>头文件和源文件\公共的类\WinVersionHelper</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FindDlg.cpp">
@@ -446,6 +452,9 @@
<ClCompile Include="IniHelper.cpp">
<Filter>头文件和源文件\公共的类\IniHelper</Filter>
</ClCompile>
<ClCompile Include="WinVersionHelper.cpp">
<Filter>头文件和源文件\公共的类\WinVersionHelper</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="MusicPlayer2.rc">
+4 -4
View File
@@ -1113,14 +1113,14 @@ BOOL CMusicPlayerDlg::OnInitDialog()
#ifdef COMPILE_IN_WIN_XP
theApp.m_app_setting_data.theme_color_follow_system = false;
#else
if (!IsWindows8OrGreater())
if (!theApp.m_win_version.IsWindows8OrLater())
theApp.m_app_setting_data.theme_color_follow_system = false;
#endif
//只有Win10以上的系统才能在Cortana搜索框中显示歌词
if (!theApp.m_is_windows10)
if (!theApp.m_win_version.IsWindows10OrLater())
theApp.m_play_setting_data.show_lyric_in_cortana = false;
m_cortana_lyric.SetEnable(theApp.m_is_windows10);
m_cortana_lyric.SetEnable(theApp.m_win_version.IsWindows10OrLater());
//设置窗口不透明度
SetTransparency();
@@ -1574,7 +1574,7 @@ void CMusicPlayerDlg::OnTimer(UINT_PTR nIDEvent)
COLORREF color;
//如果系统是Win10就取窗口最边缘像素的颜色,因为当win10设置成不显示标题栏颜色时,只有窗口边框最外面的一圈像素是主题颜色
//如果系统是Win8/8.1,则取窗口边缘住下2个像素的颜色,因为win8窗口标题栏总是当前主题色,而边框最外侧的颜色比主题色要深。
if (theApp.m_is_windows10)
if (theApp.m_win_version.IsWindows10OrLater())
color = ::GetPixel(hDC, rect.TopLeft().x + DPI(8), rect.TopLeft().y);
else
color = ::GetPixel(hDC, rect.TopLeft().x + DPI(8), rect.TopLeft().y + DPI(2));
+1 -1
View File
@@ -71,7 +71,7 @@ BOOL CPlaySettingsDlg::OnInitDialog()
m_show_album_cover_in_cortana_check.SetCheck(m_data.cortana_show_album_cover);
m_cortana_icon_beat_check.SetCheck(m_data.cortana_icon_beat);
//m_cortana_icon_beat_check.EnableWindow(!m_data.cortana_show_album_cover);
if (theApp.m_is_windows10)
if (theApp.m_win_version.IsWindows10OrLater())
{
m_show_lyric_in_cortana_check.SetCheck(m_data.show_lyric_in_cortana);
}
+3 -3
View File
@@ -82,9 +82,9 @@ void CProgressStatic::OnPaint()
this->GetClientRect(&rect);
int max_length = rect.Width();
//由于非windows10时为主窗口设置了WS_CLIPCHILDREN属性,所以这里需要手动重绘控件区域
if (!theApp.m_is_windows10)
DrawThemeParentBackground(m_hWnd, dc.GetSafeHdc(), &rect); //重绘控件区域
////由于非windows10时为主窗口设置了WS_CLIPCHILDREN属性,所以这里需要手动重绘控件区域
//if (!theApp.m_win_version.IsWindows10OrLater())
// DrawThemeParentBackground(m_hWnd, dc.GetSafeHdc(), &rect); //重绘控件区域
//将矩形的高度更改为进度条显示的高度
if (m_progress_height < rect.Height() && m_progress_height > 0)
+65
View File
@@ -0,0 +1,65 @@
#include "stdafx.h"
#include "WinVersionHelper.h"
CWinVersionHelper::CWinVersionHelper()
{
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;
}
m_major_version = dwMajorVer;
m_minor_version = dwMinorVer;
m_build_number = dwBuildNumber;
}
CWinVersionHelper::~CWinVersionHelper()
{
}
bool CWinVersionHelper::IsWindows10FallCreatorOrLater() const
{
if (m_major_version > 10)
return true;
else if (m_major_version == 10 && m_minor_version > 0)
return true;
else if (m_major_version == 10 && m_minor_version == 0 && m_build_number >= 16299)
return true;
else return false;
}
bool CWinVersionHelper::IsWindows7() const
{
return (m_major_version == 6 && m_minor_version == 1);
}
bool CWinVersionHelper::IsWindows8Or8point1() const
{
return (m_major_version == 6 && m_minor_version > 1);
}
bool CWinVersionHelper::IsWindows8OrLater() const
{
if (m_major_version > 6)
return true;
else if (m_major_version == 6 && m_minor_version > 1)
return true;
else return false;
}
bool CWinVersionHelper::IsWindows10OrLater() const
{
return (m_major_version >= 10);
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
class CWinVersionHelper
{
public:
CWinVersionHelper();
~CWinVersionHelper();
bool IsWindows10FallCreatorOrLater() const; //判断当前Windows版本是否为Win10秋季创意者更新或更新的版本
bool IsWindows7() const; //判断Windows版本是否为Windows7
bool IsWindows8Or8point1() const; //判断Windows版本是否为Windows8或Windows8.1
bool IsWindows8OrLater() const;
bool IsWindows10OrLater() const;
int GetMajorVersion() const { return m_major_version; }
int GetMinorVersion() const { return m_minor_version; }
int GetBuildNumber() const { return m_build_number; }
protected:
int m_major_version{};
int m_minor_version{};
int m_build_number{};
};
+3 -3
View File
@@ -47,9 +47,9 @@
#include <tuple>
#include <deque>
#include <algorithm>
#ifndef COMPILE_IN_WIN_XP
#include <VersionHelpers.h>
#endif
//#ifndef COMPILE_IN_WIN_XP
//#include <VersionHelpers.h>
//#endif
#include <iomanip>
#include <map>
#include <cmath>