Repository: peng-zhihui/TrafficMonitor
Branch: master
Commit: 5ac1625134bf
Files: 205
Total size: 1.1 MB
Directory structure:
gitextract_vr76zmf_/
├── .github/
│ └── workflows/
│ └── main.yml
├── .gitignore
├── Help.md
├── Help_en-us.md
├── LICENSE
├── LICENSE_CN
├── OpenHardwareMonitorApi/
│ ├── LibreHardwareMonitorLib.xml
│ ├── OpenHardwareMonitorApi.vcxproj
│ ├── OpenHardwareMonitorApi.vcxproj.filters
│ ├── OpenHardwareMonitorImp.cpp
│ ├── OpenHardwareMonitorImp.h
│ ├── ReadMe.txt
│ ├── Stdafx.cpp
│ ├── Stdafx.h
│ ├── UpdateVisitor.cpp
│ ├── UpdateVisitor.h
│ ├── app.rc
│ └── resource.h
├── PluginDemo/
│ ├── CustomDrawItem.cpp
│ ├── CustomDrawItem.h
│ ├── DataManager.cpp
│ ├── DataManager.h
│ ├── OptionsDlg.cpp
│ ├── OptionsDlg.h
│ ├── PluginDemo.cpp
│ ├── PluginDemo.h
│ ├── PluginDemo.rc
│ ├── PluginDemo.vcxproj
│ ├── PluginDemo.vcxproj.filters
│ ├── PluginSystemDate.cpp
│ ├── PluginSystemDate.h
│ ├── PluginSystemTime.cpp
│ ├── PluginSystemTime.h
│ ├── framework.h
│ ├── pch.cpp
│ ├── pch.h
│ └── resource.h
├── README.md
├── README_en-us.md
├── TrafficMonitor/
│ ├── AboutDlg.cpp
│ ├── AboutDlg.h
│ ├── AdapterCommon.cpp
│ ├── AdapterCommon.h
│ ├── AppAlreadyRuningDlg.cpp
│ ├── AppAlreadyRuningDlg.h
│ ├── BaseDialog.cpp
│ ├── BaseDialog.h
│ ├── CAutoAdaptSettingsDlg.cpp
│ ├── CAutoAdaptSettingsDlg.h
│ ├── CMFCColorDialogEx.cpp
│ ├── CMFCColorDialogEx.h
│ ├── CPUUsage.cpp
│ ├── CPUUsage.h
│ ├── CSkinPreviewView.cpp
│ ├── CSkinPreviewView.h
│ ├── CTabCtrlEx.cpp
│ ├── CTabCtrlEx.h
│ ├── CVariant.cpp
│ ├── CVariant.h
│ ├── CalendarHelper.cpp
│ ├── CalendarHelper.h
│ ├── ColorSettingListCtrl.cpp
│ ├── ColorSettingListCtrl.h
│ ├── ColorStatic.cpp
│ ├── ColorStatic.h
│ ├── ComboBox2.cpp
│ ├── ComboBox2.h
│ ├── Common.cpp
│ ├── Common.h
│ ├── CommonData.cpp
│ ├── CommonData.h
│ ├── DisplayTextSettingDlg.cpp
│ ├── DisplayTextSettingDlg.h
│ ├── DonateDlg.cpp
│ ├── DonateDlg.h
│ ├── DrawCommon.cpp
│ ├── DrawCommon.h
│ ├── DrawCommonEx.cpp
│ ├── DrawCommonEx.h
│ ├── FilePathHelper.cpp
│ ├── FilePathHelper.h
│ ├── GeneralSettingsDlg.cpp
│ ├── GeneralSettingsDlg.h
│ ├── HighResolutionTimer.h
│ ├── HistoryTrafficCalendarDlg.cpp
│ ├── HistoryTrafficCalendarDlg.h
│ ├── HistoryTrafficDlg.cpp
│ ├── HistoryTrafficDlg.h
│ ├── HistoryTrafficFile.cpp
│ ├── HistoryTrafficFile.h
│ ├── HistoryTrafficListCtrl.cpp
│ ├── HistoryTrafficListCtrl.h
│ ├── HistoryTrafficListDlg.cpp
│ ├── HistoryTrafficListDlg.h
│ ├── IconSelectDlg.cpp
│ ├── IconSelectDlg.h
│ ├── IniHelper.cpp
│ ├── IniHelper.h
│ ├── LinkStatic.cpp
│ ├── LinkStatic.h
│ ├── ListCtrlEx.cpp
│ ├── ListCtrlEx.h
│ ├── MainWndColorDlg.cpp
│ ├── MainWndColorDlg.h
│ ├── MainWndSettingsDlg.cpp
│ ├── MainWndSettingsDlg.h
│ ├── MessageDlg.cpp
│ ├── MessageDlg.h
│ ├── NetworkInfoDlg.cpp
│ ├── NetworkInfoDlg.h
│ ├── OptionsDlg.cpp
│ ├── OptionsDlg.h
│ ├── PictureStatic.cpp
│ ├── PictureStatic.h
│ ├── PluginInfoDlg.cpp
│ ├── PluginInfoDlg.h
│ ├── PluginManager.cpp
│ ├── PluginManager.h
│ ├── PluginManagerDlg.cpp
│ ├── PluginManagerDlg.h
│ ├── ReadMe.txt
│ ├── SelectConnectionsDlg.cpp
│ ├── SelectConnectionsDlg.h
│ ├── SetItemOrderDlg.cpp
│ ├── SetItemOrderDlg.h
│ ├── SimpleXML.cpp
│ ├── SimpleXML.h
│ ├── SkinDlg.cpp
│ ├── SkinDlg.h
│ ├── SkinFile.cpp
│ ├── SkinFile.h
│ ├── SpinEdit.cpp
│ ├── SpinEdit.h
│ ├── StaticEx.cpp
│ ├── StaticEx.h
│ ├── TabDlg.cpp
│ ├── TabDlg.h
│ ├── TaskBarDlg.cpp
│ ├── TaskBarDlg.h
│ ├── TaskBarSettingsDlg.cpp
│ ├── TaskBarSettingsDlg.h
│ ├── TaskbarColorDlg.cpp
│ ├── TaskbarColorDlg.h
│ ├── TaskbarDefaultStyle.cpp
│ ├── TaskbarDefaultStyle.h
│ ├── TaskbarItemOrderHelper.cpp
│ ├── TaskbarItemOrderHelper.h
│ ├── Test.cpp
│ ├── Test.h
│ ├── TinyXml2Helper.cpp
│ ├── TinyXml2Helper.h
│ ├── TrafficMonitor.cpp
│ ├── TrafficMonitor.h
│ ├── TrafficMonitor.rc
│ ├── TrafficMonitor.vcxproj
│ ├── TrafficMonitor.vcxproj.filters
│ ├── TrafficMonitorDlg.cpp
│ ├── TrafficMonitorDlg.h
│ ├── UpdateHelper.cpp
│ ├── UpdateHelper.h
│ ├── WIC.cpp
│ ├── WIC.h
│ ├── WinVersionHelper.cpp
│ ├── WinVersionHelper.h
│ ├── WindowsSettingHelper.cpp
│ ├── WindowsSettingHelper.h
│ ├── auto_start_helper.cpp
│ ├── auto_start_helper.h
│ ├── crashtool.cpp
│ ├── crashtool.h
│ ├── print_compile_time.bat
│ ├── res/
│ │ ├── Acknowledgement.txt
│ │ ├── Acknowledgement_en.txt
│ │ └── TrafficMonitor.rc2
│ ├── resource.h
│ ├── skins/
│ │ ├── 0默认皮肤/
│ │ │ └── skin.ini
│ │ ├── xml_test/
│ │ │ └── skin.xml
│ │ ├── 皮肤01/
│ │ │ └── skin.ini
│ │ ├── 皮肤02/
│ │ │ └── skin.ini
│ │ ├── 皮肤03/
│ │ │ └── skin.ini
│ │ ├── 皮肤04/
│ │ │ └── skin.ini
│ │ ├── 皮肤05/
│ │ │ └── skin.ini
│ │ ├── 皮肤06/
│ │ │ └── skin.ini
│ │ ├── 皮肤07/
│ │ │ └── skin.ini
│ │ ├── 皮肤08/
│ │ │ └── skin.ini
│ │ ├── 皮肤09/
│ │ │ └── skin.ini
│ │ ├── 皮肤10/
│ │ │ └── skin.ini
│ │ ├── 皮肤10(竖排)/
│ │ │ └── skin.ini
│ │ ├── 皮肤11/
│ │ │ └── skin.ini
│ │ └── 默认皮肤2/
│ │ └── skin.ini
│ ├── stdafx.cpp
│ ├── stdafx.h
│ ├── targetver.h
│ └── tinyxml2/
│ ├── tinyxml2.cpp
│ └── tinyxml2.h
├── TrafficMonitor.sln
├── UpdateLog/
│ ├── update_log.md
│ ├── update_log_en-us.md
│ └── update_log_zh-tw.md
├── include/
│ ├── OpenHardwareMonitor/
│ │ ├── OpenHardwareMonitorApi.h
│ │ └── OpenHardwareMonitorGlobal.h
│ └── PluginInterface.h
├── version.info
├── version_utf8.info
└── 皮肤制作教程.md
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/workflows/main.yml
================================================
name: Release CI
on: push
jobs:
x64_build:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1
- name: Run msbuild
run: msbuild -p:configuration=release -p:platform=x64 -p:platformToolset=v142
- name: Get current time
uses: 1466587594/current-time@v1
id: current-time
with:
format: YYYYMMDD_HHmmss
utcOffset: "+08:00"
- name : Upload artifact
uses: actions/upload-artifact@v2
with:
name: x64_${{ steps.current-time.outputs.formattedTime }}_TrafficMonitor
path: |
Bin/x64/Release/TrafficMonitor.exe
Bin/x64/Release/*.dll
- name : Upload pdb files
uses: actions/upload-artifact@v2
with:
name: x64_${{ steps.current-time.outputs.formattedTime }}_pdb
path: Bin/x64/Release/*.pdb
x86_build:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1
- name: Run msbuild
run: msbuild -p:configuration=release -p:platform=x86 -p:platformToolset=v142
- name: Get current time
uses: 1466587594/current-time@v1
id: current-time
with:
format: YYYYMMDD_HHmmss
utcOffset: "+08:00"
- name : Upload artifact
uses: actions/upload-artifact@v2
with:
name: x86_${{ steps.current-time.outputs.formattedTime }}_TrafficMonitor
path: |
Bin/Release/TrafficMonitor.exe
Bin/Release/*.dll
- name : Upload pdb files
uses: actions/upload-artifact@v2
with:
name: x86_${{ steps.current-time.outputs.formattedTime }}_pdb
path: Bin/Release/*.pdb
# winXP_build:
# runs-on: windows-latest
# steps:
# - uses: actions/checkout@v2
# - name: Add msbuild to PATH
# uses: microsoft/setup-msbuild@v1
# - name: Run msbuild
# run: |
# set ExternalCompilerOptions=/DCOMPILE_FOR_WINXP
# msbuild -p:configuration=release -p:platform=x86 -p:platformToolset=v140_xp
# shell: cmd
# - name: Get current time
# uses: 1466587594/current-time@v1
# id: current-time
# with:
# format: YYYYMMDD_HHmmss
# utcOffset: "+08:00"
# - name : Upload artifact
# uses: actions/upload-artifact@v1
# with:
# name: winXP_${{ steps.current-time.outputs.formattedTime }}_TrafficMonitor
# path: Bin/Release/TrafficMonitor.exe
================================================
FILE: .gitignore
================================================
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Typescript v1 declaration files
typings/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
*.ini
!skin.ini
*.dat
*.bak
/TrafficMonitor/skins/test*
[Dd]ebug (without temperature)/
[Rr]elease (without temperature)/
[Dd]ebug (lite)/
[Rr]elease (lite)/
/TrafficMonitor/compile_time.txt
================================================
FILE: Help.md
================================================
**简体中文 | [English](./Help_en-us.md)**
# TrafficMonitor 常见问题
这里是关于TrafficMonitor常见问题的页面,如果你想查看关于TrafficMonitor的各项功能和使用方法的详细介绍,请[点击这里](https://github.com/zhongyang219/TrafficMonitor/wiki)转到TraffinMonitor Wiki页面。
### 1. 如何显示CPU和内存利用率?
在主窗口点击右弹出键菜单,勾选“显示更多信息”。如果需要在任务栏窗口中也显示CPU和内存利用率,则在任务栏窗口中点击右键弹出菜单,在“显示设置 ”子菜单下勾选“CPU和内存利用率”即可。
### 2. 如何单独设置任务栏窗口中每个项目的颜色?
在右键菜单中选择“选项”,切换到“任务栏窗口设置”,勾选“指定每个项目的颜色”,此时再点击“文本颜色”右边的颜色块,就会弹出“任务栏窗口颜色设置”的对话框了。
如果不勾选“指定每个项目的颜色”,则只能为文本设置统一的颜色。
### 3. 设置了开机自动运行仍然无法开机自启。
从1.80版本开始,包含温度监控的版本和不含温度监控的版本采用了不同的方式来实现开机自启动。
* 不含温度监控的版本和1.80以前的版本:
不含温度监控的版本和1.80以前的版本的开机启动功能是通过在注册表“计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run”中创建“TrafficMonitor”的注册表项来实现的,如果你遇到无法开机启动的问题,请先检查该注册表项是否存在,再检查程序的路径是否正确。如果你移动了程序的位置,则会因为路径无效导致开机自启动失效,此时只需要在选项设置中取消“开机自动运行”的勾选,再将其勾选上就可以了。
注意,某些第三方安全软件可能会阻止TrafficMonitor的开机自启动,请尝试在安全软件中允许TrafficMonitor开机自启动。
如果设置了以管理员身份运行也会出现开机无法自启动的问题,请尝试去掉以管理员身份运行。
* 包含温度监控的版本:
包含温度监控的版本是通过创建任务计划来实现开机自启动的。可以通过`控制面板\系统和安全\管理工具`来打开任务计划程序。
如下图所示:
如果你遇到无法开机自启动的情况,请到“任务计划程序”中检查TrafficMonitor的计划任务是否正常创建,exe文件的路径是否正确。
开机无法自启动的一个常见原因是你可能移动了TrafficMonitor主程序的位置。如果你为TrafficMonitor设置好了开机自动运行,但是你将TrafficMonitor移动到了其他位置,那么开机自启动肯定就失效了。你需要打开TrafficMonitor的“选项设置”——“常规设置”,如果“开机时自动运行”处于勾选状态,先去掉勾选,然后再次打开“选项设置”——“常规设置”,重新勾选“开机时自动运行”即可。
需要注意的是,如果你使用不含温度监控的版本在注册表中创建了开机自启动项,然后再使用包含温度监控的版本开启开机自启动功能,它会自动将注册表中的开机自启动项删除,再在任务计划中创建开机自启动项。反之亦然。
### 4. 程序弹出“无法保存设置”的对话框。
如果遇到这种情况,说明程序没有向其所在目录下写入数据的权限,导致设置数据无法保存。尝试将程序移动到其他有写入权限的文件夹中可以解决这个问题。
你也可以通过以下步骤将配置文件的保存路径改为C:\Users\\<用户名\>\AppData\Roaming\TrafficMonitor目录。
* 退出TrafficMonitor,以管理员身份重新启动TrafficMonitor。
* 在右键菜单中选择“选项”,切换到“常规设置”选项卡,在“数据和配置文件”中选择“保存到Appdata目录”。
如果此时仍然提示“无法保存设置”,请打开应用程序所在目录,打开`global_cfg.ini`文件,如果不存在请新建一个,在里面添加如下内容:
```
[config]
portable_mode = true
```
如果无法新建,可以在其他位置(比如桌面)新建该文件,然后移动到程序所在目录。
如果`global_cfg.ini`文件已存在,就把`portable_mode `的值改成`true`,保存后重新启动TrafficMonitor。
如果`global_cfg.ini`没有写入权限,可以尝试把该文件复制到桌面,改好后复制回原来的路径将原文件覆盖。
执行以上步骤后理论上应该不会出现这种问题了。如果这个问题仍然出现,请尝试把C:\Users\\<用户名\>\AppData\Roaming\TrafficMonitor\config.ini删除,该文件会在删除后重新生成。
在1.79以后的版本中,如果程序所在目录无法写入数据,会自动将配置和数据文件保存到C:\Users\\<用户名\>\AppData\Roaming\TrafficMonitor目录,此时,“选项”——“常规设置”——“数据和配置文件”中“保存到程序所在目录”将不可用。
### 5. 多显示器时悬浮窗只能在主显示器中显示。
默认情况下做了让悬浮窗无法超出屏幕边界的处理,如果需要将悬浮窗移动到其他显示器,请在悬浮窗中点击右键弹出菜单,选择“其他功能”,勾选“允许超出屏幕边界”,此时悬浮窗不再限制在屏幕内,也能移动到任意显示器中了。
如果移除额外的显示器,那么悬浮窗可能会出现在屏幕区域外导致不可见,此时只需要在通知区域图标上点击鼠标右键,选择“其他功能”,去掉“允许超出屏幕边界”的勾选,悬浮窗就会重新出现屏幕范围内了。
### 6. 网速数值显示不全。
由于不同字体每个字符的宽度并不一样,在某些情况下,确实会出现网速数值显示不全的问题。如果出现了这样的问题,请打开“选项”——“任务栏窗口设置”,在“数据位数”下拉列表中选择一个更大的值。
### 7. 设置了鼠标穿透后如何取消?
在通知区的TrafficMonitor的图标上点击鼠标右键,去掉“鼠标穿透”的勾选即可。
设置了鼠标穿透后,悬浮窗将无法响应任何鼠标消息,也无法弹出右键菜单,但是可以通过通知区图标来弹出右键菜单。主窗口的右键菜单和通知区图标的右键是完全一样的。
另外,即使你之前设置了隐藏通知区图标,开启鼠标穿透后,通知区图标也会自动显示出来,防止无法弹出右键菜单。
说明:以下几种情况下通知区图标会自动显示出来:
* 开启鼠标穿透后;
* 不显示任务栏窗口的情况下隐藏主窗口后;
* 隐藏主窗口的情况下关闭任务栏窗口后;
* 开启鼠标穿透的情况下关闭任务栏窗口后。
### 8. Windows 10 白色任务栏主题时任务栏窗口颜色的问题
在使用白色任务栏主题时,你可以在在“任务栏窗口设置”点击“预设方案”按钮,选择“浅色模式”,可以一键设置浅色模式任务栏颜色。如图所示:
同时,你还可能勾选“自动适应Windows10深色/浅色主题”,程序会在Windows10深色/浅色主题更换时自动切换颜色方案。你可以点击“自动适应设置”按钮来配置在深色和浅色主题时分别使用哪个颜色预设。
### 9. 在Windows7/Windows8/8.1下任务栏窗口有个背景色,无法完全透明
这个问题确实存在,但是在Win10下是正常的。这个问题暂时无法解决。
在1.79以后的版本中,Windows8/8.1下可以在“选项”——“任务栏窗口设置”中勾选“背景色透明”,再勾选“根据任务栏颜色自动设置背景色”即可获得较好的显示效果。
### 10. 任务栏窗口有时会显示不全,比如单位被覆盖了
这确实是一个BUG,但是我目前还没有找到一个好的解决方法,这个问题通常出现在任务栏右侧通知区域宽度变化的时候,主要在在切换输入法的时候,如果出现了这个问题,可以将通知区任意一个图标向上拖动将其隐藏,再将其拖下来即可恢复正常。
出现这个问题原因在于,由于系统任务栏通知区的图标数量可能会发生变化,导致通知区的宽度也会时常变化,当通知区的宽度发生变化时,TrafficMonitor的任务栏窗口需要实时调整其位置。但是由于我无法知道通知区的宽度在什么时候变化,因此只能每隔一段时间判断是否需要调整位置,如果任务栏通知区域的宽度变化得太快,就会导致TrafficMonitor的任务栏无法及时调整其位置,从而导致了这个BUG。
#### 以下步骤或许可以解决这个问题:
* 打开“设置”
* 点击“时间和语言”——“区域和语言”
* 点击右侧“高级键盘设置”
* 勾选“使用桌面语言栏”
* 右键点击任务栏,选择“任务栏设置”
* 点击“打开或关闭系统图标”,关闭“输入指示”
方法来自知乎 [win10的任务栏为何一点击就乱动?](https://www.zhihu.com/question/312032145/answer/627965084)
### 11. Windows10中开启HDR后任务栏窗口无法显示
部分用户反馈,在Windows10中开始HDR功能会导致任务栏窗口无法显示。如果遇到这个问题,可以尝试在[“选项设置”——“任务栏窗口设置”](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#任务栏窗口设置)中关闭“背景透明”选项的勾选。
### 12. CPU利用率显示和任务管理器不一致
在Windows10及以上操作系统中,如果你需要让TrafficMonitor显示的CPU利用率和任务管理器一致,请到[“选项设置”——“常规设置”——“高级”](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#高级)——“CPU使用率获取方式”中选择“使用性能计数器”。
由于Windows10以上操作系统中任务管理器获取CPU利用率的方式发生了改变,因此选择“基于CPU使用时间”的方式获取到的CPU利用率会和任务管理器中显示的不一致。
### 13. 关于TrafficMonitor温度监控的问题
由于温度监控功能在某些电脑中存在一些问题,因此温度监控功能默认是关闭的,如果你要使用TrafficMonitor的温度监控功能,请到[“选项设置”-“常规设置”-“硬件监控”](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#硬件监控)中开启。开启后,任务栏右键菜单中的“显示设置”子菜单下才会出现温度相关的项目。
TrafficMonitor的温度监控功能依赖第三方开源库[LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor)。如果你遇到硬件温度无法显示,或者显示的温度异常的问题,请先下载LibreHardwareMonitor,并查看LibreHardwareMonitor是否能正常显示对应的温度。
如果LibreHardwareMonitor也无法显示对应硬件的温度,那么我也无法解决这个问题,你可以向LibreHardwareMonitor作者反馈你的问题。
如果LibreHardwareMonitor可以正常显示对应硬件的温度,请向我反馈这个问题,同时提供LibreHardwareMonitor的截图,以便我调查你的问题。
**注意:硬件监控功能(包括温度监控和显卡使用率监控)可能存在一些问题,它可能会占用更多的CPU和内存。据部分用户反馈,开启温度功能后会导致程序崩溃和系统死机等问题,请在知晓以上风险后再决定开启硬件监控功能。否则,请不要使用硬件监控功能。**
## 14. 程序启动时提示找不到“MSVC\*.dll”或“mfc\*.dll”
点击以下链接下载并安装Microsoft Visual C++ 运行环境。
[最新支持的 Visual C++ 可再发行程序包下载 | Microsoft Docs](https://docs.microsoft.com/zh-CN/cpp/windows/latest-supported-vc-redist?view=msvc-170)
>如果还遇到其他问题,请点击“关于”对话框中的“联系作者”,或者直接[点击此处](mailto:zhongyang219@hotmail.com)向我发送电子邮件。但我由于作者的能力有限,我并不能保证可以解决所有问题,但是你的反馈也许可以帮助我更好的改进这个软件。
请在邮件中尽可能详细地描述你遇到的问题,出现了什么错误提示,你尝试过哪些操作等,最好能够附上截图和配置文件(“选项”——“常规设置”——“打开配置文件所在路径”)。
注意,发送前请先确认一下你发送时使用的电子邮件地址,如果你的邮件地址是形如“outlook_随机字符串@outlook.com”的格式,那么这样的邮箱地址是无法回复的。
这可能是由于你使用了第三方邮箱地址作为Microsoft账号登录Windows导致的。如果有这样的情况,请务必在邮件中附上正确的电子邮件地址。
================================================
FILE: Help_en-us.md
================================================
**[简体中文](./Help.md) | English**
# TrafficMonitor Frequently Asked Questions
This is the page about the Frequently Asked Questions of TrafficMonitor. If you want to view the detailed introduction of the functions and usage of TrafficMonitor, please [click here](https://github.com/zhongyang219/TrafficMonitor/wiki) to go the the TraffinMonitor Wiki page (Only Chinese Available).
### 1. How to show the CPU and memory usage?
Right click the main window and check "Show More Info". If you also need to display the CPU and memory usage in the taskbar window, right click the taskbar window and check "CPU and Memory Usage" in the "Display Settings " sub menu.
### 2. How do I set the color of each item in the taskbar window individually?
Select "Options" In the right-click menu, switch to "Taskbar Window Settings" tab, check "Specify colors for each item", and then click the color block on the right side of "Text Color " to pop up the dialog box for taskbar window color settings.
If you do not check "Specify colors for each item", you can only set the uniform color for the text.
### 3. "Auto run when Windows start" dose not work
Starting from version 1.80, the version with temperature monitoring and the version without temperature monitoring have adopted different methods to realize "auto run when Windows start".
* Versions without temperature monitoring and versions before 1.80:
The auto run function of the versions without temperature monitoring and versions before 1.80 is achieved by creating the "TrafficMonitor" key in the registry path of "Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run".
If you encounter the problem of auto run dose not work, please check that the registry exists, and then check the program path is correct. If you move the location of the program, the auto run will be invalid because of the program location being invalid. In this case, you only need to uncheck "Auto run when Windows start" in the option settings, and then check it on.
Note that some third-party security software may prevent TrafficMonitor from booting up automatically. Please try to allow TrafficMonitor to boot up automatically in the security software.
If you set the program to run as an administrator, the auto run function will also not work. Please try to remove running as an administrator.
* Version with temperature monitoring:
The version that includes temperature monitoring implements auto run function by creating a task plan. The task scheduler can be opened through `Control Panel\System and Security\Administrative Tools`. As shown below:
If you encounter that situation that the program cannot be started automatically after booting, please go to "Task Scheduler" to check whether the scheduled task of TrafficMonitor is created normally and the path of the exe file is correct.
A common reason why the "Auto run when Windows start" dose not work is that you may have moved the location of the TrafficMonitor main program. If you have set up TrafficMonitor to run automatically at startup, but you move TrafficMonitor to another location, then the auto run at startup will definitely fail. You need to open "Option Settings"-"General Settings" of TrafficMonitor, if "Auto run when Windows start" is checked, remove the check, then open "Option Settings"-"General Settings" again, and check again "Auto run when Windows start" will be fine.
It should be noted that if you use the version without temperature monitoring to create a auto-start item in the registry, and then use the version that with temperature monitoring to turn on the "Auto run when Windows start" function, it will automatically delete the auto-start item in the registry, and then create a auto-start item in the task plan. vice versa.
### 4. The program pops up the "Unable to Save Settings" message box.
If you encountered this problem, that means the program does not have permission to write data to its directory, causing the settings data cannot be saved. Try to move the program to another folder that has write permissions will save this problem.
You can also change the save path of the configuration and data file to the C:\Users\\\AppData\Roaming\TrafficMonitor directory by the following steps.
* Exit TrafficMonitor and restart TrafficMonitor as an administrator.
* Select "Options" in the right-click menu, switch to the "General Settings" tab, and select "Save to Appdata Directory" in "Configuration and data files".
If it still pops up the "Unable to Save Settings" message box, open the directory where the application is located, open the `global_cfg.ini` file. If it doesn't exist, create a new one, adding the following:
```
[config]
portable_mode = true
```
If the file cannot be created, you can create the file in a different location, such as the desktop, and then move to the directory where the program is located.
If the `global_cfg.ini` file already exists, change the value of `portable_mode` to `true` and save, then restart TrafficMonitor.
If `global_cfg.ini` does not have write permission, you can try copying the file to the desktop, and then copying it back to the original path to overwrite the original file after modified.
After these steps, this problem should not theoretically occur. If this problem still occurs, try to delete the file C:\Users\\\AppData\Roaming\TrafficMonitor\config.ini. This file will be regenerated after it is deleted.
In version 1.79 and later, if the directory where the program located is not writable, the configuration and data files will be automatically saved to the "C:\Users\\\AppData\Roaming\TrafficMonitor" directory. At this time, "Options" -“General Settings”-“Save to the program directory” in “Configuration and data files” will not be available.
### 5. The suspension window can only be displayed on the main monitor when multiple monitors are present.
By default, the suspension window cannot be moved out of the screen boundaries. If you need to move the suspension window to other monitors, please right click the suspension window, select "Other Functions", check "Allow Out of Screen Boundaries". At this time the suspension window is no longer limited to the screen, it can be also move to any monitors.
If you remove the extra monitor, then the suspension window may appear outside the screen area to cause the invisible. At this time, you only need to right click the the icon in the notification area, select "Other function", uncheck "Allow Out of Screen Boundaries", the suspension window will appear in the screen area again.
### 6. The net speed value is not fully displayed.
Because the width of each character in different fonts is not the same, in some cases, it does appear the problem of the net speed value is not fully displayed. If this problem occurs, open "Options"-"Taskbar Window Settings", and select a larger value in the "Number of digits" drop down list.
### 7. How to cancel after setting the mouse penetrate?
Right click the TrafficMonitor icon in the notification area, uncheck the "Mouse Penetrate".
After the mouse penetrate is set, the suspension window will not be able to respond to any mouse messages, or pop-up right-click menu. But the menu can be poped up by right click the icon in notification area. The right-click menu of the main window and which of the notification area icon is exactly the same.
In addition, even if you have previously set the hidden notify icon, when the "mouse penetrate" is set, the notify icon will also automatically appear to prevent the right-click menu cannot be displayed.
Note: The notification area icon will be automatically displayed in the following situations:
* After the "Mouse Penetrate" is set;
* Hide the main window without displaying the taskbar window;
* Close the taskbar window when the main window is hidden;
* Close the taskbar window when the "Mouse Penetrate" is set.
### 8. Problems with the taskbar window color in Windows 10 white taskbar theme
When using the white taskbar theme, you can click the "Preset" button in the "Taskbar Window Settings" and select "Light Mode" to set the taskbar color in light mode. As the picture shows:
At the same time, you may also check "Auto adapt to Windows 10 dark/light theme", the program will automatically switch the color preset when Windows 10 dark/light theme is changed. You can click the "Auto Adapt settings" button to configure which color preset to use for dark and light themes.
### 9. The taskbar windows cannot be displayed when the HDR is turned on in Windows 10
Some users have reported that turning on the HDR function in Windows 10 will cause the taskbar window to fail to display. If you encounter this problem, you can try turning off the "Background Transparent" option in ["Option Settings" - "Taskbar Window Settings"](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#任务栏窗口设置).
### 13. About the temperature monitoring of TrafficMonitor
Due to some problems occurred in some computers caused by the temperature monitoring function, the temperature monitoring function is turned off by default. If you want to use the temperature monitoring function of TrafficMonitor, please go to ["Option Settings"-"General Settings"-"Hardware Monitoring"](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#硬件监控) to enable it. After it is turned on, temperature-related items will appear under the "Display Settings" submenu in the taskbar right-click menu.
The temperature monitoring function of TrafficMonitor relies on a third-party open source library [LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor). If you encounter the problem that the hardware temperature cannot be displayed, or the displayed temperature is incorrect, then please download LibreHardwareMonitor first, and check whether LibreHardwareMonitor can display the corresponding temperature normally.
If LibreHardwareMonitor cannot display the temperature of the corresponding hardware, then I cannot solve this problem. You can report your problem to the author of LibreHardwareMonitor.
If LibreHardwareMonitor can display the temperature of the corresponding hardware normally, please report this problem to me, and provide a screenshot of LibreHardwareMonitor so that I can investigate your problem.
**Note: The hardware monitoring function (including temperature monitoring and GPU usage monitoring) may have some problems, which may cause more CPU and memory usage. According to feedback from some users, turning on the temperature function will cause problems such as program crashes and system crashes. Please decide to turn on the hardware monitoring function after you are aware of the above risks. Otherwise, please do not use the hardware monitoring function.**
## 14. The program prompts that "MSVC\*.dll" or "mfc\*.dll" cannot be found when the program starts
Click the link below to download and install the Microsoft Visual C++ runtime environment.
[Latest supported Visual C++ Redistributable downloads | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170)
>If you have encountered any other problems, please click "Contact Author" in the "About" dialog box, or directly [click here](mailto:zhongyang219@hotmail.com) to send me an email.
Please describe the problems you have encountered in detail, the error prompts, what operations you have tried, it is better to attach the screenshots and configuration files ("Options"-"General Settings"-"Open configuration file path").
Note: Please confirm the email address that you used before sending the email. If your email address is form like "Outlook_\@outlook.com", then such email address is unable to reply.
This may be due to the fact that you are using a third-party e-mail address as the Microsoft account to log on to Windows. If this is the case, be sure to enclose the correct email address in the message.
================================================
FILE: LICENSE
================================================
Copyright (c) <2019>
"Anti 996" License Version 1.0 (Draft)
Permission is hereby granted to any individual or legal entity
obtaining a copy of this licensed work (including the source code,
documentation and/or related items, hereinafter collectively referred
to as the "licensed work"), free of charge, to deal with the licensed
work for any purpose, including without limitation, the rights to use,
reproduce, modify, prepare derivative works of, distribute, publish
and sublicense the licensed work, subject to the following conditions:
1. The individual or the legal entity must conspicuously display,
without modification, this License and the notice on each redistributed
or derivative copy of the Licensed Work.
2. The individual or the legal entity must strictly comply with all
applicable laws, regulations, rules and standards of the jurisdiction
relating to labor and employment where the individual is physically
located or where the individual was born or naturalized; or where the
legal entity is registered or is operating (whichever is stricter). In
case that the jurisdiction has no such laws, regulations, rules and
standards or its laws, regulations, rules and standards are
unenforceable, the individual or the legal entity are required to
comply with Core International Labor Standards.
3. The individual or the legal entity shall not induce, suggest or force
its employee(s), whether full-time or part-time, or its independent
contractor(s), in any methods, to agree in oral or written form, to
directly or indirectly restrict, weaken or relinquish his or her
rights or remedies under such laws, regulations, rules and standards
relating to labor and employment as mentioned above, no matter whether
such written or oral agreements are enforceable under the laws of the
said jurisdiction, nor shall such individual or the legal entity
limit, in any methods, the rights of its employee(s) or independent
contractor(s) from reporting or complaining to the copyright holder or
relevant authorities monitoring the compliance of the license about
its violation(s) of the said license.
THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.
================================================
FILE: LICENSE_CN
================================================
版权所有(c)<2019>
反996许可证版本1.0
在符合下列条件的情况下,特此免费向任何得到本授权作品的副本(包括源代码、文件和/或相关内容,以
下统称为“授权作品”)的个人和法人实体授权:被授权个人或法人实体有权以任何目的处置授权作品,包括
但不限于使用、复制,修改,衍生利用、散布,发布和再许可:
1. 个人或法人实体必须在许可作品的每个再散布或衍生副本上包含以上版权声明和本许可证,不得自行修
改。
2. 个人或法人实体必须严格遵守与个人实际所在地或个人出生地或归化地、或法人实体注册地或经营地(
以较严格者为准)的司法管辖区所有适用的与劳动和就业相关法律、法规、规则和标准。如果该司法管辖区
没有此类法律、法规、规章和标准或其法律、法规、规章和标准不可执行,则个人或法人实体必须遵守国际
劳工标准的核心公约。
3. 个人或法人不得以任何方式诱导、暗示或强迫其全职或兼职员工或其独立承包人以口头或书面形式同意
直接或间接限制、削弱或放弃其所拥有的,受相关与劳动和就业有关的法律、法规、规则和标准保护的权利
或补救措施,无论该等书面或口头协议是否被该司法管辖区的法律所承认,该等个人或法人实体也不得以任
何方法限制其雇员或独立承包人向版权持有人或监督许可证合规情况的有关当局报告或投诉上述违反许可证
的行为的权利。
该授权作品是"按原样"提供,不做任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵
权性的保证。在任何情况下,无论是在合同诉讼、侵权诉讼或其他诉讼中,版权持有人均不承担因本软件或
本软件的使用或其他交易而产生、引起或与之相关的任何索赔、损害或其他责任。
================================================
FILE: OpenHardwareMonitorApi/LibreHardwareMonitorLib.xml
================================================
LibreHardwareMonitorLib
Stores all hardware groups and decides which devices should be enabled and updated.
Creates a new instance with basic initial .
Creates a new instance with additional .
Computer settings that will be transferred to each .
Contains computer information table read in accordance with System Management BIOS (SMBIOS) Reference Specification .
Triggers the method for the given observer.
Observer who call to devices.
Triggers the method with the given visitor for each device in each group.
Observer who call to devices.
If hasn't been opened before, opens , , and triggers the private method depending on which categories are enabled.
If opened before, removes all and triggers , and .
If opened before, removes all and recreates it.
specific additional settings passed to its .
Support for the Kraken X3 devices from NZXT
Initializes a new instance of the class.
The group.
The thread.
The affinity.
Gets the specified .
The group.
The thread.
.
Gets the CPUID.
Gets the CPU index.
Sets the default fan speed.
Gets the OverdriveN temperature.
The type.
The sensor.
The minimum temperature.
The scale.
If set to true , resets the sensor value to null .
Gets a PMLog sensor value.
The data.
Type of the sensor.
The sensor.
The factor.
Gets the Overdrive6 power.
The type.
The sensor.
This structure describes a group-specific affinity.
Initializes a new instance of the struct.
The group.
The mask.
Gets a single group affinity.
The group.
The index.
.
Gets the group.
Gets the mask.
Determines whether the specified is equal to this instance.
The to compare with this instance.
true if the specified is equal to this instance; otherwise, false .
Returns a hash code for this instance.
A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
Implements the == operator.
The a1.
The a2.
The result of the operator.
Implements the != operator.
The a1.
The a2.
The result of the operator.
Object representing a component of the computer.
Individual information can be read from the .
Creates a new instance based on the data provided.
Component name.
Identifier that will be assigned to the device. Based on
Additional settings passed by the .
Event triggered when is closing.
Collection of identifiers representing the purpose of the hardware.
Handler that will trigger the actions assigned to it when the event occurs.
Component returned to the assigned action(s).
Basic abstract with methods for the class which can store all hardware and decides which devices are to be checked and updated.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about:
devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about or devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets or sets a value indicating whether collecting information about devices should be enabled and updated.
if a given category of devices is already enabled.
Gets a list of all known .
Can be updated by .
List of all enabled devices.
Triggered when a new is registered.
Triggered when a is removed.
Generates full LibreHardwareMonitor report for devices that have been enabled.
A formatted text string with library, OS and hardware information.
Represents a unique / identifier in text format with a / separator.
Creates a new identifier instance based on the base and additional elements.
Base identifier being the beginning of the new one.
Additional parts by which the base will be extended.
Abstract parent with logic for the abstract class that stores data.
Accepts the observer for this instance.
Computer observer making the calls.
Call the method for all child instances (called only from visitors).
Computer observer making the calls.
A group of devices from one category in one list.
Gets a list that stores information about in a given group.
Report containing most of the known information about all in this .
A formatted text string with hardware information.
Stop updating this group in the future.
Handler that will trigger the actions assigned to it when the event occurs.
Component returned to the assigned action(s).
Abstract object that stores information about a device. All sensors are available as an array of .
Can contain .
Type specified in .
Gets a unique hardware ID that represents its location.
Gets or sets device name.
Gets the device that is the parent of the current hardware. For example, the motherboard is the parent of SuperIO.
Gets an array of all sensors such as , , etc.
Gets child devices, e.g. of the .
Report containing most of the known information about the current device.
A formatted text string with hardware information.
Refreshes the information stored in array.
An that will be triggered when a new sensor appears.
An that will be triggered when one of the sensors is removed.
Gets rarely changed hardware properties that can't be represented as sensors.
Abstract object that represents additional parameters included in .
Gets a parameter default value defined by library.
Gets a parameter description defined by library.
Gets a unique parameter ID that represents its location.
Gets or sets information whether the given is the default for .
Gets a parameter name defined by library.
Gets the sensor that is the data container for the given parameter.
Gets or sets the current value.
Category of what type the selected sensor is.
Stores the readed value and the time in which it was recorded.
of the sensor.
The time code during which the was recorded.
Gets the value of the sensor
Gets the time code during which the was recorded.
Stores information about the readed values and the time in which they were collected.
Gets the unique identifier of this sensor for a given .
Gets a maximum value recorded for the given sensor.
Gets a minimum value recorded for the given sensor.
Gets or sets a sensor name.
By default determined by the library.
Gets the last recorded value for the given sensor.
Gets a list of recorded values for the given sensor.
Resets a value stored in .
Resets a value stored in .
Abstract object that stores information about the limits of .
Upper limit of value.
Lower limit of value.
Abstract object that stores information about the critical limits of .
Critical upper limit of value.
Critical lower limit of value.
Abstract object that stores settings passed to , and .
Returns information whether the given collection of settings contains a value assigned to the given key.
Key to which the setting value is assigned.
Assigns a setting option to a given key.
Key to which the setting value is assigned.
Text setting value.
Gets a setting option assigned to the given key.
Key to which the setting value is assigned.
Default value.
Removes a setting with the specified key from the settings collection.
Key to which the setting value is assigned.
Base interface for creating observers who call to devices.
Refreshes the values of all in all on selected .
Instance of the computer to be revisited.
Refreshes the values of all on selected .
Instance of the hardware to be revisited.
Refreshes the values on selected .
Instance of the sensor to be revisited.
Refreshes the values on selected .
Instance of the parameter to be revisited.
An unsafe but universal implementation for the ACPI Embedded Controller IO interface for Windows
It is unsafe because of possible race condition between this application and the PC firmware when
writing to the EC registers. For a safe approach ACPI/WMI methods have to be used, but those are
different for each motherboard model.
Represents the motherboard of a computer with its and as .
Creates motherboard instance by retrieving information from and creates a new based on data from and .
table containing motherboard data.
Additional settings passed by .
Gets the name obtained from .
Always
Gets the information.
Motherboard itself cannot be updated. Update instead.
Closes using .
Composite class containing information about the selected .
Creates a new instance and assigns values.
Name of the selected component.
Description of the selected component.
Default value of the selected component.
Gets a name of the parent .
Gets a description of the parent .
Gets a default value of the parent .
Observer making calls to selected component 's.
Creates a new observer instance.
Instance of the that triggers events during visiting the .
Goes through all the components of the specified with its .
Computer class instance that is derived from the interface.
Goes through all the components of the specified with its .
Hardware class instance that is derived from the interface.
Goes through all the components of the specified using .
Sensor class instance that is derived from the interface.
Goes through all the components of the specified .
Parameter class instance that is derived from the interface.
Chassis security status based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.4.3 .
Chassis state based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.4.2 .
Chassis type based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.4.1 .
Processor family based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.5.2 .
Processor type based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.5.1 .
Processor socket based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.5.5 .
System wake-up type based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.2.2 .
Cache associativity based on DMTF SMBIOS Reference Specification v.3.3.0, Chapter 7.8.5 .
Processor cache level.
Initializes a new instance of the class.
The data.
The strings.
Gets the byte.
The offset.
.
Gets the word.
The offset.
.
Gets the string.
The offset.
.
Motherboard BIOS information obtained from the SMBIOS table.
Gets the BIOS release date.
Gets the size of the physical device containing the BIOS.
Gets the string number of the BIOS Vendor’s Name.
Gets the string number of the BIOS Version. This value is a free-form string that may contain Core and OEM version information.
Gets the size.
.
Gets the date.
The bios date.
.
System information obtained from the SMBIOS table.
Gets the family associated with system.
This text string identifies the family to which a particular computer belongs. A family refers to a set of computers that are similar but not identical from a hardware or software point of
view. Typically, a family is composed of different computer models, which have different configurations and pricing points. Computers in the same family often have similar branding and cosmetic
features.
Gets the manufacturer name associated with system.
Gets the product name associated with system.
Gets the serial number string associated with system.
Gets the version string associated with system.
Gets
Chassis information obtained from the SMBIOS table.
Gets the asset tag associated with the enclosure or chassis.
Gets
Gets
Gets or sets the chassis lock.
Chassis lock is present if . Otherwise, either a lock is not present or it is unknown if the enclosure has a lock.
Gets the string describing the chassis or enclosure manufacturer name.
Gets the number of power cords associated with the enclosure or chassis.
Gets the state of the enclosure’s power supply (or supplies) when last booted.
Gets the height of the enclosure, in 'U's. A U is a standard unit of measure for the height of a rack or rack-mountable component and is equal to 1.75 inches or 4.445 cm. A value of 0
indicates that the enclosure height is unspecified.
Gets the physical security status of the enclosure when last booted.
Gets the string describing the chassis or enclosure serial number.
Gets the string describing the chassis or enclosure SKU number.
Gets the thermal state of the enclosure when last booted.
Gets the number of null-terminated string representing the chassis or enclosure version.
Motherboard information obtained from the SMBIOS table.
Gets the value that represents the manufacturer's name.
Gets the value that represents the motherboard's name.
Gets the value that represents the motherboard's serial number.
Gets the value that represents the motherboard's revision number.
Processor information obtained from the SMBIOS table.
Gets the value that represents the number of cores per processor socket.
Gets the value that represents the number of enabled cores per processor socket.
Gets the value that represents the current processor speed (in MHz).
Gets the external Clock Frequency, in MHz. If the value is unknown, the field is set to 0.
Gets
Gets the string number of Processor Manufacturer.
Gets the value that represents the maximum processor speed (in MHz) supported by the system for this processor socket.
Gets
Gets the value that represents the string number for the serial number of this processor.
This value is set by the manufacturer and normally not changeable.
Gets
Gets the string number for Reference Designation.
Gets the value that represents the number of threads per processor socket.
Gets the value that represents the string number describing the Processor.
Processor cache information obtained from the SMBIOS table.
Gets
Gets
Gets the value that represents the installed cache size.
Gets the cache designation.
.
Memory information obtained from the SMBIOS table.
Gets the string number of the string that identifies the physically labeled bank where the memory device is located.
Gets the string number of the string that identifies the physically-labeled socket or board position where the memory device is located.
Gets the string number for the manufacturer of this memory device.
Gets the string number for the part number of this memory device.
Gets the string number for the serial number of this memory device.
Gets the size of the memory device. If the value is 0, no memory device is installed in the socket.
Gets the the value that identifies the maximum capable speed of the device, in mega transfers per second (MT/s).
Reads and processes information encoded in an SMBIOS table.
Gets
Gets
Gets
Gets
Gets
Gets
Gets
Report containing most of the information that could be read from the SMBIOS table.
A formatted text string with computer information and the entire SMBIOS table.
Helper to calculate the disk performance with base timestamps
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-perfrawdata
Gets the SMART data.
Gets the SMART attributes.
Gets the SMART data.
Initializes a new instance of the class.
The SMART id of the attribute.
The name of the attribute.
Initializes a new instance of the class.
The SMART id of the attribute.
The name of the attribute.
A delegate for converting the raw byte
array into a value (or null to use the attribute value).
Initializes a new instance of the class.
The SMART id of the attribute.
The name of the attribute.
A delegate for converting the raw byte
array into a value (or null to use the attribute value).
Type of the sensor or null if no sensor is to
be created.
If there exists more than one attribute with
the same sensor channel and type, then a sensor is created only for the
first attribute.
The name to be used for the sensor, or null if
no sensor is created.
True to hide the sensor initially.
Description for the parameters of the sensor
(or null).
Gets the SMART identifier.
Localization class for SMART attribute names.
Reads Smart health status of the drive
True, if drive is healthy; False, if unhealthy; Null, if it cannot be read
Initializes static members of the class.
Gets the processor group count.
Returns true if the is valid.
The affinity.
true if the specified affinity is valid; otherwise, false .
Sets the processor group affinity for the current thread.
The processor group affinity.
The previous processor group affinity.
All OK, but need to wait.
All OK, but need restart.
All OK but need mode change.
All OK, but with warning.
ADL function completed successfully.
Generic Error. Most likely one or more of the Escape calls to the driver
failed!
ADL not initialized.
One of the parameter passed is invalid.
One of the parameter size is invalid.
Invalid ADL index passed.
Invalid controller index passed.
Invalid display index passed.
Function not supported by the driver.
Null Pointer error.
Call can't be made due to disabled adapter.
Invalid Callback.
Display Resource conflict.
Failed to update some of the values. Can be returned by set request that
include multiple values if not all values were successfully committed.
There's no Linux XDisplay in Linux Console environment.
If set to 1, then the available spare space has fallen below the threshold.
If set to 1, then a temperature is above an over temperature threshold or below an under temperature threshold.
If set to 1, then the device reliability has been degraded due to significant media related errors or any internal error that degrades device reliability.
If set to 1, then the media has been placed in read only mode
If set to 1, then the volatile memory backup device has failed. This field is only valid if the controller has a volatile memory backup solution.
Create a instance from a struct with zero initialized memory arrays
no need to init every inner array with the correct sizes
type of struct that is needed
SMART data requested.
Identify data is requested.
Read SMART data.
Read SMART thresholds.
obsolete
Autosave SMART data.
Save SMART attributes.
Set SMART to offline immediately.
Read SMART log.
Write SMART log.
Write SMART thresholds.
obsolete
Enable SMART.
Disable SMART.
Get SMART status.
Set SMART to offline automatically.
bit 0:15 Maximum Power (MP) in centiwatts
bit 16:23
bit 24 Max Power Scale (MPS), bit 25 Non-Operational State (NOPS)
bit 32:63 Entry Latency (ENLAT) in microseconds
bit 64:95 Exit Latency (EXLAT) in microseconds
bit 96:100 Relative Read Throughput (RRT)
bit 104:108 Relative Read Latency (RRL)
bit 112:116 Relative Write Throughput (RWT)
bit 120:124 Relative Write Latency (RWL)
bit 128:143 Idle Power (IDLP)
bit 150:151 Idle Power Scale (IPS)
bit 152:159
bit 160:175 Active Power (ACTP)
bit 176:178 Active Power Workload (APW), bit 182:183 Active Power Scale (APS)
bit 184:255.
byte 0:1 M - PCI Vendor ID (VID)
byte 2:3 M - PCI Subsystem Vendor ID (SSVID)
byte 4: 23 M - Serial Number (SN)
byte 24:63 M - Model Number (MN)
byte 64:71 M - Firmware Revision (FR)
byte 72 M - Recommended Arbitration Burst (RAB)
byte 73:75 M - IEEE OUI Identifier (IEEE). Controller Vendor code.
byte 76 O - Controller Multi-Path I/O and Namespace Sharing Capabilities (CMIC)
byte 77 M - Maximum Data Transfer Size (MDTS)
byte 78:79 M - Controller ID (CNTLID)
byte 80:83 M - Version (VER)
byte 84:87 M - RTD3 Resume Latency (RTD3R)
byte 88:91 M - RTD3 Entry Latency (RTD3E)
byte 92:95 M - Optional Asynchronous Events Supported (OAES)
byte 96:239.
byte 240:255. Refer to the NVMe Management Interface Specification for definition.
byte 256:257 M - Optional Admin Command Support (OACS)
byte 258 M - Abort Command Limit (ACL)
byte 259 M - Asynchronous Event Request Limit (AERL)
byte 260 M - Firmware Updates (FRMW)
byte 261 M - Log Page Attributes (LPA)
byte 262 M - Error Log Page Entries (ELPE)
byte 263 M - Number of Power States Support (NPSS)
byte 264 M - Admin Vendor Specific Command Configuration (AVSCC)
byte 265 O - Autonomous Power State Transition Attributes (APSTA)
byte 266:267 M - Warning Composite Temperature Threshold (WCTEMP)
byte 268:269 M - Critical Composite Temperature Threshold (CCTEMP)
byte 270:271 O - Maximum Time for Firmware Activation (MTFA)
byte 272:275 O - Host Memory Buffer Preferred Size (HMPRE)
byte 276:279 O - Host Memory Buffer Minimum Size (HMMIN)
byte 280:295 O - Total NVM Capacity (TNVMCAP)
byte 296:311 O - Unallocated NVM Capacity (UNVMCAP)
byte 312:315 O - Replay Protected Memory Block Support (RPMBS)
byte 316:511
byte 512 M - Submission Queue Entry Size (SQES)
byte 513 M - Completion Queue Entry Size (CQES)
byte 514:515
byte 516:519 M - Number of Namespaces (NN)
byte 520:521 M - Optional NVM Command Support (ONCS)
byte 522:523 M - Fused Operation Support (FUSES)
byte 524 M - Format NVM Attributes (FNA)
byte 525 M - Volatile Write Cache (VWC)
byte 526:527 M - Atomic Write Unit Normal (AWUN)
byte 528:529 M - Atomic Write Unit Power Fail (AWUPF)
byte 530 M - NVM Vendor Specific Command Configuration (NVSCC)
byte 531
byte 534:535
byte 536:539 O - SGL Support (SGLS)
byte 540:703
byte 704:2047
byte 2048:3071 Power State Descriptors
byte 3072:4095 Vendor Specific
This field indicates critical warnings for the state of the controller.
Each bit corresponds to a critical warning type; multiple bits may be set.
Composite Temperature: Contains the temperature of the overall device (controller and NVM included) in units of Kelvin.
Available Spare: Contains a normalized percentage (0 to 100%) of the remaining spare capacity available
Available Spare Threshold: When the Available Spare falls below the threshold indicated in this field,
an asynchronous event completion may occur. The value is indicated as a normalized percentage (0 to 100%).
Percentage Used: Contains a vendor specific estimate of the percentage of NVM subsystem life used based on
the actual usage and the manufacturer’s prediction of NVM life. A value of 100 indicates that the estimated endurance of
the NVM in the NVM subsystem has been consumed, but may not indicate an NVM subsystem failure. The value is allowed to exceed 100.
Data Units Read: Contains the number of 512 byte data units the host has read from the controller;
this value does not include metadata. This value is reported in thousands
(i.e., a value of 1 corresponds to 1000 units of 512 bytes read) and is rounded up.
Data Units Written: Contains the number of 512 byte data units the host has written to the controller;
this value does not include metadata. This value is reported in thousands
(i.e., a value of 1 corresponds to 1000 units of 512 bytes written) and is rounded up.
Host Read Commands: Contains the number of read commands completed by the controller.
For the NVM command set, this is the number of Compare and Read commands.
Host Write Commands: Contains the number of write commands completed by the controller.
For the NVM command set, this is the number of Write commands.
Controller Busy Time: Contains the amount of time the controller is busy with I/O commands.
Power Cycles: Contains the number of power cycles.
Power On Hours: Contains the number of power-on hours.
This does not include time that the controller was powered and in a low power state condition.
Unsafe Shutdowns: Contains the number of unsafe shutdowns.
This count is incremented when a shutdown notification is not received prior to loss of power.
Media Errors: Contains the number of occurrences where the controller detected an unrecoverable data integrity error.
Errors such as uncorrectable ECC, CRC checksum failure, or LBA tag mismatch are included in this field.
Number of Error Information Log Entries: Contains the number of Error Information log entries over the life of the controller
Warning Composite Temperature Time: Contains the amount of time in minutes that the controller is operational and the Composite Temperature is greater than or equal to the Warning Composite
Temperature Threshold.
Critical Composite Temperature Time: Contains the amount of time in minutes that the controller is operational and the Composite Temperature is greater than the Critical Composite Temperature
Threshold.
Contains the current temperature reported by temperature sensor 1-8.
Gets the resulting IO control code.
Initializes a new instance of the struct.
Type of the device.
The function.
The access.
Initializes a new instance of the struct.
Type of the device.
The function.
The method.
The access.
The operation was successful
NvidiaML was not first initialized with nvmlInit()
A supplied argument is invalid
The requested operation is not available on target device
The current user does not have permission for operation
A query to find an object was unsuccessful
An input argument is not large enough
A device's external power cables are not properly attached
NVIDIA driver is not loaded
User provided timeout passed
NVIDIA Kernel detected an interrupt issue with a GPU
NvidiaML Shared Library couldn't be found or loaded
Local version of NvidiaML doesn't implement this function
infoROM is corrupted
The GPU has fallen off the bus or has otherwise become inaccessible
The GPU requires a reset before it can be used again
The GPU control device has been blocked by the operating system/cgroups
RM detects a driver/library version mismatch
An operation cannot be performed because the GPU is currently in use
An public driver error occurred
Driver with access at kernel level.
Contains Win32 definitions for Windows NT.
Describes a local identifier for an adapter.
Represents a 64-bit signed integer value.
Contains basic information about the operating system.
Statically checks if the current system and .
Gets information about whether the current system is 64 bit.
Gets information about whether the current system is Unix based.
================================================
FILE: OpenHardwareMonitorApi/OpenHardwareMonitorApi.vcxproj
================================================
Debug
Win32
Release
Win32
Debug
x64
Release
x64
{C0A42F4A-ABB3-4575-B4D5-CEDD8379AC26}
v4.5.2
ManagedCProj
OpenHardwareMonitorApi
10.0
DynamicLibrary
true
v142
true
Unicode
DynamicLibrary
false
v142
true
Unicode
DynamicLibrary
true
v142
true
Unicode
DynamicLibrary
false
v142
true
Unicode
true
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Configuration)\
true
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Platform)\$(Configuration)\
false
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Configuration)\
false
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Platform)\$(Configuration)\
Level3
Disabled
WIN32;_DEBUG;%(PreprocessorDefinitions);OPENHARDWAREMONITOR_EXPORTS
Use
Level3
Disabled
_DEBUG;%(PreprocessorDefinitions);OPENHARDWAREMONITOR_EXPORTS
Use
Level3
WIN32;NDEBUG;%(PreprocessorDefinitions);OPENHARDWAREMONITOR_EXPORTS
Use
Level3
NDEBUG;%(PreprocessorDefinitions);OPENHARDWAREMONITOR_EXPORTS
Use
LibreHardwareMonitorLib.dll
Create
Create
Create
Create
================================================
FILE: OpenHardwareMonitorApi/OpenHardwareMonitorApi.vcxproj.filters
================================================
{4FC737F1-C7A5-4376-A066-2A32D752A2FF}
cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
{93995380-89BD-4b04-88EB-625FBE52EBFB}
h;hh;hpp;hxx;hm;inl;inc;xsd
{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
头文件
头文件
头文件
头文件
头文件
头文件
源文件
源文件
源文件
资源文件
================================================
FILE: OpenHardwareMonitorApi/OpenHardwareMonitorImp.cpp
================================================
// 这是主 DLL 文件。
#include "stdafx.h"
#include "OpenHardwareMonitorImp.h"
#include
namespace OpenHardwareMonitorApi
{
static std::wstring error_message;
//将CRL的String类型转换成C++的std::wstring类型
static std::wstring ClrStringToStdWstring(System::String^ str)
{
if (str == nullptr)
{
return std::wstring();
}
else
{
const wchar_t* chars = (const wchar_t*)(Runtime::InteropServices::Marshal::StringToHGlobalUni(str)).ToPointer();
std::wstring os = chars;
Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr((void*)chars));
return os;
}
}
std::shared_ptr CreateInstance()
{
std::shared_ptr pMonitor;
try
{
MonitorGlobal::Instance()->Init();
pMonitor = std::make_shared();
}
catch (System::Exception^ e)
{
error_message = ClrStringToStdWstring(e->Message);
}
return pMonitor;
}
std::wstring GetErrorMessage()
{
return error_message;
}
float COpenHardwareMonitor::CpuTemperature()
{
return m_cpu_temperature;
}
float COpenHardwareMonitor::GpuTemperature()
{
if (m_gpu_nvidia_temperature >= 0)
return m_gpu_nvidia_temperature;
else
return m_gpu_ati_temperature;
}
float COpenHardwareMonitor::HDDTemperature()
{
return m_hdd_temperature;
}
float COpenHardwareMonitor::MainboardTemperature()
{
return m_main_board_temperature;
}
float COpenHardwareMonitor::GpuUsage()
{
if (m_gpu_nvidia_usage >= 0)
return m_gpu_nvidia_usage;
else
return m_gpu_ati_usage;
}
float COpenHardwareMonitor::CpuFreq()
{
return m_cpu_freq;
}
const std::map& COpenHardwareMonitor::AllHDDTemperature()
{
return m_all_hdd_temperature;
}
const std::map& COpenHardwareMonitor::AllCpuTemperature()
{
return m_all_cpu_temperature;
}
const std::map& COpenHardwareMonitor::AllHDDUsage()
{
return m_all_hdd_usage;
}
void COpenHardwareMonitor::SetCpuEnable(bool enable)
{
MonitorGlobal::Instance()->computer->IsCpuEnabled = enable;
}
void COpenHardwareMonitor::SetGpuEnable(bool enable)
{
MonitorGlobal::Instance()->computer->IsGpuEnabled = enable;
}
void COpenHardwareMonitor::SetHddEnable(bool enable)
{
MonitorGlobal::Instance()->computer->IsStorageEnabled = enable;
}
void COpenHardwareMonitor::SetMainboardEnable(bool enable)
{
MonitorGlobal::Instance()->computer->IsMotherboardEnabled = enable;
}
bool COpenHardwareMonitor::GetCPUFreq(IHardware^ hardware, float& freq) {
for (int i = 0; i < hardware->Sensors->Length; i++)
{
if (hardware->Sensors[i]->SensorType == SensorType::Clock)
{
String^ name = hardware->Sensors[i]->Name;
if (name != L"Bus Speed")
m_all_cpu_clock[ClrStringToStdWstring(name)] = Convert::ToDouble(hardware->Sensors[i]->Value);
}
}
float sum{};
for (auto i : m_all_cpu_clock)
sum += i.second;
freq = sum / m_all_cpu_clock.size() / 1000.0;
return true;
}
bool COpenHardwareMonitor::GetHardwareTemperature(IHardware^ hardware, float& temperature)
{
temperature = -1;
std::vector all_temperature;
float core_temperature{ -1 };
System::String^ temperature_name;
switch (hardware->HardwareType)
{
case HardwareType::Cpu:
temperature_name = L"Core Average";
break;
case HardwareType::GpuNvidia: case HardwareType::GpuAmd:
temperature_name = L"GPU Core";
break;
default:
break;
}
for (int i = 0; i < hardware->Sensors->Length; i++)
{
//找到温度传感器
if (hardware->Sensors[i]->SensorType == SensorType::Temperature)
{
float cur_temperture = Convert::ToDouble(hardware->Sensors[i]->Value);
all_temperature.push_back(cur_temperture);
if (hardware->Sensors[i]->Name == temperature_name) //如果找到了名称为temperature_name的温度传感器,则将温度保存到core_temperature里
core_temperature = cur_temperture;
}
}
if (core_temperature >= 0)
{
temperature = core_temperature;
return true;
}
if (!all_temperature.empty())
{
//如果有多个温度传感器,则取平均值
float sum{};
for (auto i : all_temperature)
sum += i;
temperature = sum / all_temperature.size();
return true;
}
//如果没有找到温度传感器,则在SubHardware中寻找
for (int i = 0; i < hardware->SubHardware->Length; i++)
{
if (GetHardwareTemperature(hardware->SubHardware[i], temperature))
return true;
}
return false;
}
bool COpenHardwareMonitor::GetCpuTemperature(IHardware^ hardware, float& temperature)
{
temperature = -1;
m_all_cpu_temperature.clear();
for (int i = 0; i < hardware->Sensors->Length; i++)
{
//找到温度传感器
if (hardware->Sensors[i]->SensorType == SensorType::Temperature)
{
String^ name = hardware->Sensors[i]->Name;
//保存每个CPU传感器的温度
m_all_cpu_temperature[ClrStringToStdWstring(name)] = Convert::ToDouble(hardware->Sensors[i]->Value);
}
}
//计算平均温度
if (!m_all_cpu_temperature.empty())
{
float sum{};
for (const auto& item : m_all_cpu_temperature)
sum += item.second;
temperature = sum / m_all_cpu_temperature.size();
}
return temperature > 0;
}
bool COpenHardwareMonitor::GetGpuUsage(IHardware^ hardware, float& gpu_usage)
{
for (int i = 0; i < hardware->Sensors->Length; i++)
{
//找到负载
if (hardware->Sensors[i]->SensorType == SensorType::Load)
{
if (hardware->Sensors[i]->Name == L"GPU Core")
{
gpu_usage = Convert::ToDouble(hardware->Sensors[i]->Value);
return true;
}
}
}
return false;
}
bool COpenHardwareMonitor::GetHddUsage(IHardware^ hardware, float& hdd_usage)
{
for (int i = 0; i < hardware->Sensors->Length; i++)
{
//找到负载
if (hardware->Sensors[i]->SensorType == SensorType::Load)
{
if (hardware->Sensors[i]->Name == L"Total Activity")
{
hdd_usage = Convert::ToDouble(hardware->Sensors[i]->Value);
return true;
}
}
}
return false;
}
COpenHardwareMonitor::COpenHardwareMonitor()
{
ResetAllValues();
}
COpenHardwareMonitor::~COpenHardwareMonitor()
{
MonitorGlobal::Instance()->UnInit();
}
void COpenHardwareMonitor::ResetAllValues()
{
m_cpu_temperature = -1;
m_gpu_nvidia_temperature = -1;
m_gpu_ati_temperature = -1;
m_hdd_temperature = -1;
m_main_board_temperature = -1;
m_gpu_nvidia_usage = -1;
m_gpu_ati_usage = -1;
m_all_hdd_temperature.clear();
m_all_hdd_usage.clear();
}
void COpenHardwareMonitor::InsertValueToMap(std::map& value_map, const std::wstring& key, float value)
{
auto iter = value_map.find(key);
if (iter == value_map.end())
{
value_map[key] = value;
}
else
{
std::wstring key_exist = iter->first;
size_t index = key_exist.rfind(L'#'); //查找字符串是否含有#号
if (index != std::wstring::npos)
{
//取到#号后面的数字,将其加1
int num = _wtoi(key_exist.substr(index + 1).c_str());
num++;
key_exist = key_exist.substr(0, index + 1);
key_exist += std::to_wstring(num);
}
else //没有#号则在末尾添加" #1"
{
key_exist += L" #1";
}
value_map[key_exist] = value;
}
}
void COpenHardwareMonitor::GetHardwareInfo()
{
ResetAllValues();
error_message.clear();
try
{
auto computer = MonitorGlobal::Instance()->computer;
computer->Accept(MonitorGlobal::Instance()->updateVisitor);
for (int i = 0; i < computer->Hardware->Count; i++)
{
//查找硬件类型
switch (computer->Hardware[i]->HardwareType)
{
case HardwareType::Cpu:
if (m_cpu_temperature < 0)
GetCpuTemperature(computer->Hardware[i], m_cpu_temperature);
GetCPUFreq(computer->Hardware[i], m_cpu_freq);
break;
case HardwareType::GpuNvidia:
if (m_gpu_nvidia_temperature < 0)
GetHardwareTemperature(computer->Hardware[i], m_gpu_nvidia_temperature);
if (m_gpu_nvidia_usage < 0)
GetGpuUsage(computer->Hardware[i], m_gpu_nvidia_usage);
break;
case HardwareType::GpuAmd:
if (m_gpu_ati_temperature < 0)
GetHardwareTemperature(computer->Hardware[i], m_gpu_ati_temperature);
if (m_gpu_ati_usage < 0)
GetGpuUsage(computer->Hardware[i], m_gpu_ati_usage);
break;
case HardwareType::Storage:
{
float cur_hdd_temperature = -1;
GetHardwareTemperature(computer->Hardware[i], cur_hdd_temperature);
//m_all_hdd_temperature[ClrStringToStdWstring(computer->Hardware[i]->Name)] = cur_hdd_temperature;
InsertValueToMap(m_all_hdd_temperature, ClrStringToStdWstring(computer->Hardware[i]->Name), cur_hdd_temperature);
float cur_hdd_usage = -1;
GetHddUsage(computer->Hardware[i], cur_hdd_usage);
//m_all_hdd_usage[ClrStringToStdWstring(computer->Hardware[i]->Name)] = cur_hdd_usage;
InsertValueToMap(m_all_hdd_usage, ClrStringToStdWstring(computer->Hardware[i]->Name), cur_hdd_usage);
if (m_hdd_temperature < 0)
m_hdd_temperature = cur_hdd_temperature;
}
break;
case HardwareType::Motherboard:
if (m_main_board_temperature < 0)
GetHardwareTemperature(computer->Hardware[i], m_main_board_temperature);
break;
default:
break;
}
}
}
catch (System::Exception^ e)
{
error_message = ClrStringToStdWstring(e->Message);
}
}
////////////////////////////////////////////////////////////////////////////////////
MonitorGlobal::MonitorGlobal()
{
}
MonitorGlobal::~MonitorGlobal()
{
}
void MonitorGlobal::Init()
{
updateVisitor = gcnew UpdateVisitor();
computer = gcnew Computer();
computer->Open();
}
void MonitorGlobal::UnInit()
{
computer->Close();
}
}
================================================
FILE: OpenHardwareMonitorApi/OpenHardwareMonitorImp.h
================================================
// OpenHardwareMonitorApi.h
#pragma once
#include
#include "OpenHardwareMonitor/OpenHardwareMonitorApi.h"
#include "UpdateVisitor.h"
#include
using namespace System;
using namespace LibreHardwareMonitor::Hardware;
namespace OpenHardwareMonitorApi {
public class COpenHardwareMonitor : public IOpenHardwareMonitor
{
public:
COpenHardwareMonitor();
virtual ~COpenHardwareMonitor();
virtual void GetHardwareInfo() override;
virtual float CpuTemperature() override;
virtual float GpuTemperature() override;
virtual float HDDTemperature() override;
virtual float MainboardTemperature() override;
virtual float GpuUsage() override;
virtual float CpuFreq() override;
virtual const std::map& AllHDDTemperature() override;
virtual const std::map& AllCpuTemperature() override;
virtual const std::map& AllHDDUsage() override;
virtual void SetCpuEnable(bool enable) override;
virtual void SetGpuEnable(bool enable) override;
virtual void SetHddEnable(bool enable) override;
virtual void SetMainboardEnable(bool enable) override;
private:
bool GetHardwareTemperature(IHardware^ hardware, float& temperature);
bool GetCpuTemperature(IHardware^ hardware, float& temperature);
bool GetGpuUsage(IHardware^ hardware, float& gpu_usage);
bool GetHddUsage(IHardware^ hardware, float& hdd_usage);
bool GetCPUFreq(IHardware^ hardware, float& freq);
void ResetAllValues();
//向map中插入一个数值,如果key已经存在,则自动对新插入的key重命名
static void InsertValueToMap(std::map& value_map, const std::wstring& key, float value);
private:
float m_cpu_temperature{};
float m_gpu_nvidia_temperature{};
float m_gpu_ati_temperature{};
float m_hdd_temperature{};
float m_main_board_temperature{};
float m_gpu_nvidia_usage{};
float m_gpu_ati_usage{};
float m_cpu_freq{};
std::map m_all_hdd_temperature;
std::map m_all_cpu_temperature;
std::map m_all_cpu_clock;
std::map m_all_hdd_usage;
};
//一个单实例类
//由于COpenHardwareMonitor是非托管类,不能将托管类的对象作为成员变量,此类用于保存托管类的对象
public ref class MonitorGlobal
{
public:
MonitorGlobal();
~MonitorGlobal();
static MonitorGlobal^ Instance()
{
if (m_instance == nullptr)
{
m_instance = gcnew MonitorGlobal();
}
return m_instance;
}
void Init();
void UnInit();
Computer^ computer;
UpdateVisitor^ updateVisitor{};
private:
static MonitorGlobal^ m_instance{};
};
}
================================================
FILE: OpenHardwareMonitorApi/ReadMe.txt
================================================
========================================================================
DYNAMIC LINK LIBRARY : OpenHardwareMonitorApi Project Overview
========================================================================
AppWizard has created this OpenHardwareMonitorApi DLL for you.
This file contains a summary of what you will find in each of the files that
make up your OpenHardwareMonitorApi application.
OpenHardwareMonitorApi.vcxproj
This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the
Application Wizard.
OpenHardwareMonitorApi.vcxproj.filters
This is the filters file for VC++ projects generated using an Application Wizard.
It contains information about the association between the files in your project
and the filters. This association is used in the IDE to show grouping of files with
similar extensions under a specific node (for e.g. ".cpp" files are associated with the
"Source Files" filter).
OpenHardwareMonitorApi.cpp
This is the main DLL source file.
OpenHardwareMonitorApi.h
This file contains a class declaration.
AssemblyInfo.cpp
Contains custom attributes for modifying assembly metadata.
/////////////////////////////////////////////////////////////////////////////
Other notes:
AppWizard uses "TODO:" to indicate parts of the source code you
should add to or customize.
/////////////////////////////////////////////////////////////////////////////
================================================
FILE: OpenHardwareMonitorApi/Stdafx.cpp
================================================
// stdafx.cpp : ֻļԴļ
// OpenHardwareMonitorApi.pch ΪԤͷ
// stdafx.obj ԤϢ
#include "stdafx.h"
================================================
FILE: OpenHardwareMonitorApi/Stdafx.h
================================================
// stdafx.h : ϵͳļİļ
// Ǿʹõĵ
// ضĿİļ
#pragma once
================================================
FILE: OpenHardwareMonitorApi/UpdateVisitor.cpp
================================================
#include "stdafx.h"
#include "UpdateVisitor.h"
namespace OpenHardwareMonitorApi
{
void UpdateVisitor::VisitComputer(IComputer ^ computer)
{
computer->Traverse(this);
}
void UpdateVisitor::VisitHardware(IHardware ^ hardware)
{
hardware->Update();
for each (IHardware^ subHardware in hardware->SubHardware)
{
subHardware->Accept(this);
}
}
void UpdateVisitor::VisitSensor(ISensor ^ sensor)
{
}
void UpdateVisitor::VisitParameter(IParameter ^ parameter)
{
}
}
================================================
FILE: OpenHardwareMonitorApi/UpdateVisitor.h
================================================
#pragma once
using namespace LibreHardwareMonitor::Hardware;
namespace OpenHardwareMonitorApi
{
public ref class UpdateVisitor : IVisitor
{
public:
virtual void VisitComputer(IComputer^ computer);
virtual void VisitHardware(IHardware^ hardware);
virtual void VisitSensor(ISensor^ sensor);
virtual void VisitParameter(IParameter^ parameter);
};
}
================================================
FILE: OpenHardwareMonitorApi/resource.h
================================================
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by app.rc
================================================
FILE: PluginDemo/CustomDrawItem.cpp
================================================
#include "pch.h"
#include "CustomDrawItem.h"
#include "DataManager.h"
const wchar_t* CCustomDrawItem::GetItemName() const
{
return CDataManager::Instance().StringRes(IDS_CUSTOM_DRAW_ITEM);
}
const wchar_t* CCustomDrawItem::GetItemId() const
{
return L"b4zc373y";
}
const wchar_t* CCustomDrawItem::GetItemLableText() const
{
return L"";
}
const wchar_t* CCustomDrawItem::GetItemValueText() const
{
return L"";
}
const wchar_t* CCustomDrawItem::GetItemValueSampleText() const
{
return L"";
}
bool CCustomDrawItem::IsCustomDraw() const
{
return true;
}
int CCustomDrawItem::GetItemWidth() const
{
return 50;
}
static void DrawLine(CDC* pDC, CPoint point1, CPoint point2, COLORREF color)
{
CPen aPen, * pOldPen;
aPen.CreatePen(PS_SOLID, 1, color);
pOldPen = pDC->SelectObject(&aPen);
pDC->MoveTo(point1);
pDC->LineTo(point2);
pDC->SelectObject(pOldPen);
}
void CCustomDrawItem::DrawItem(void* hDC, int x, int y, int w, int h, bool dark_mode)
{
//绘图句柄
CDC* pDC = CDC::FromHandle((HDC)hDC);
//矩形区域
CRect rect(CPoint(x, y), CSize(w, h));
//设置颜色
COLORREF color1{ dark_mode ? RGB(255, 143, 107) : RGB(227, 81, 16) };
COLORREF color2{ dark_mode ? RGB(183, 241, 96) : RGB(83, 131, 11) };
COLORREF color3{ dark_mode ? RGB(158, 218, 251) : RGB(6, 111, 168) };
//显示时、分、秒的矩形区域
CRect rect1{ rect }, rect2{ rect }, rect3{ rect };
rect1.bottom = rect.top + rect.Height() / 3;
rect2 = rect1;
rect2.MoveToY(rect1.bottom);
rect3.top = rect2.bottom;
rect1.DeflateRect(1, 1);
rect2.DeflateRect(1, 1);
rect3.DeflateRect(1, 1);
//根据当前时间计算矩形的宽度
SYSTEMTIME& time{ CDataManager::Instance().m_system_time };
int hour_width{ static_cast((time.wHour + time.wMinute / 60.0) * w / 24) };
int min_width{ static_cast((time.wMinute + time.wSecond / 60.0) * w / 60) };
int sec_width{ time.wSecond * w / 60 };
rect1.right = rect1.left + hour_width;
rect2.right = rect2.left + min_width;
rect3.right = rect3.left + sec_width;
//填充矩形
pDC->FillSolidRect(rect1, color1);
pDC->FillSolidRect(rect2, color2);
pDC->FillSolidRect(rect3, color3);
//绘制刻度
COLORREF color_scale{ dark_mode ? RGB(225, 225, 225) : RGB(45, 45, 45) };
for (int i{}; i < 24; i++)
{
int x_pos{ i * w / 24 + rect1.left };
DrawLine(pDC, CPoint(x_pos, rect1.top), CPoint(x_pos, i % 6 == 0 ? rect1.bottom : rect1.top + rect1.Height() / 2), color_scale);
}
}
================================================
FILE: PluginDemo/CustomDrawItem.h
================================================
#pragma once
#include "PluginInterface.h"
class CCustomDrawItem : public IPluginItem
{
// 通过 IPluginItem 继承
virtual const wchar_t* GetItemName() const override;
virtual const wchar_t* GetItemId() const override;
virtual const wchar_t* GetItemLableText() const override;
virtual const wchar_t* GetItemValueText() const override;
virtual const wchar_t* GetItemValueSampleText() const override;
virtual bool IsCustomDraw() const override;
virtual int GetItemWidth() const override;
virtual void DrawItem(void* hDC, int x, int y, int w, int h, bool dark_mode) override;
};
================================================
FILE: PluginDemo/DataManager.cpp
================================================
#include "pch.h"
#include "DataManager.h"
CDataManager CDataManager::m_instance;
CDataManager::CDataManager()
{
}
CDataManager::~CDataManager()
{
SaveConfig();
}
CDataManager& CDataManager::Instance()
{
return m_instance;
}
void CDataManager::LoadConfig(const std::wstring& config_dir)
{
//获取模块的路径
HMODULE hModule = reinterpret_cast(&__ImageBase);
wchar_t path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);
std::wstring module_path = path;
m_config_path = module_path;
if (!config_dir.empty())
{
size_t index = module_path.find_last_of(L"\\/");
//模块的文件名
std::wstring module_file_name = module_path.substr(index + 1);
m_config_path = config_dir + module_file_name;
}
m_config_path += L".ini";
m_setting_data.show_second = GetPrivateProfileInt(_T("config"), _T("show_second"), 0, m_config_path.c_str());
//m_setting_data.show_label_text = GetPrivateProfileInt(_T("config"), _T("show_label_text"), 1, config_path.c_str());
}
static void WritePrivateProfileInt(const wchar_t* app_name, const wchar_t* key_name, int value, const wchar_t* file_path)
{
wchar_t buff[16];
swprintf_s(buff, L"%d", value);
WritePrivateProfileString(app_name, key_name, buff, file_path);
}
void CDataManager::SaveConfig() const
{
WritePrivateProfileInt(_T("config"), _T("show_second"), m_setting_data.show_second, m_config_path.c_str());
//WritePrivateProfileInt(_T("config"), _T("show_label_text"), m_setting_data.show_label_text, config_path.c_str());
}
const CString& CDataManager::StringRes(UINT id)
{
auto iter = m_string_table.find(id);
if (iter != m_string_table.end())
{
return iter->second;
}
else
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_string_table[id].LoadString(id);
return m_string_table[id];
}
}
================================================
FILE: PluginDemo/DataManager.h
================================================
#pragma once
#include
#include
#define g_data CDataManager::Instance()
struct SettingData
{
bool show_second{};
//bool show_label_text{};
};
class CDataManager
{
private:
CDataManager();
~CDataManager();
public:
static CDataManager& Instance();
void LoadConfig(const std::wstring& config_dir);
void SaveConfig() const;
const CString& StringRes(UINT id); //根据资源id获取一个字符串资源
public:
std::wstring m_cur_time;
std::wstring m_cur_date;
SYSTEMTIME m_system_time;
SettingData m_setting_data;
private:
static CDataManager m_instance;
std::wstring m_config_path;
std::map m_string_table;
};
================================================
FILE: PluginDemo/OptionsDlg.cpp
================================================
// OptionsDlg.cpp: 实现文件
//
#include "pch.h"
#include "PluginDemo.h"
#include "OptionsDlg.h"
#include "afxdialogex.h"
// COptionsDlg 对话框
IMPLEMENT_DYNAMIC(COptionsDlg, CDialog)
COptionsDlg::COptionsDlg(CWnd* pParent /*=nullptr*/)
: CDialog(IDD_OPTIONS_DIALOG, pParent)
{
}
COptionsDlg::~COptionsDlg()
{
}
void COptionsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(COptionsDlg, CDialog)
ON_BN_CLICKED(IDC_SHOW_SECOND_CHECK, &COptionsDlg::OnBnClickedShowSecondCheck)
END_MESSAGE_MAP()
// COptionsDlg 消息处理程序
BOOL COptionsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
//初始化控件状态
CheckDlgButton(IDC_SHOW_SECOND_CHECK, m_data.show_second);
//CheckDlgButton(IDC_SHOW_LABEL_CHECK, m_data.show_label_text);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void COptionsDlg::OnBnClickedShowSecondCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_second = (IsDlgButtonChecked(IDC_SHOW_SECOND_CHECK) != 0);
}
================================================
FILE: PluginDemo/OptionsDlg.h
================================================
#pragma once
#include "DataManager.h"
// COptionsDlg 对话框
class COptionsDlg : public CDialog
{
DECLARE_DYNAMIC(COptionsDlg)
public:
COptionsDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~COptionsDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_OPTIONS_DIALOG };
#endif
SettingData m_data;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedShowSecondCheck();
};
================================================
FILE: PluginDemo/PluginDemo.cpp
================================================
#include "pch.h"
#include "PluginDemo.h"
#include "DataManager.h"
#include "OptionsDlg.h"
CPluginDemo CPluginDemo::m_instance;
CPluginDemo::CPluginDemo()
{
}
CPluginDemo& CPluginDemo::Instance()
{
return m_instance;
}
IPluginItem* CPluginDemo::GetItem(int index)
{
switch (index)
{
case 0:
return &m_system_date;
case 1:
return &m_system_time;
case 2:
return &m_custom_draw_item;
default:
break;
}
return nullptr;
}
void CPluginDemo::DataRequired()
{
//获取时间和日期
SYSTEMTIME& system_time{ CDataManager::Instance().m_system_time };
GetLocalTime(&system_time);
wchar_t buff[128];
swprintf_s(buff, L"%d/%.2d/%.2d", system_time.wYear, system_time.wMonth, system_time.wDay);
CDataManager::Instance().m_cur_date = buff;
if (CDataManager::Instance().m_setting_data.show_second)
swprintf_s(buff, L"%.2d:%.2d:%.2d", system_time.wHour, system_time.wMinute, system_time.wSecond);
else
swprintf_s(buff, L"%.2d:%.2d", system_time.wHour, system_time.wMinute);
CDataManager::Instance().m_cur_time = buff;
}
const wchar_t* CPluginDemo::GetInfo(PluginInfoIndex index)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
static CString str;
switch (index)
{
case TMI_NAME:
str.LoadString(IDS_PLUGIN_NAME);
return str.GetString();
case TMI_DESCRIPTION:
str.LoadString(IDS_PLUGIN_DESCRIPTION);
return str.GetString();
case TMI_AUTHOR:
return L"zhongyang219";
case TMI_COPYRIGHT:
return L"Copyright (C) by Zhong Yang 2021";
case TMI_VERSION:
return L"1.0";
case ITMPlugin::TMI_URL:
return L"https://github.com/zhongyang219/TrafficMonitor";
break;
default:
break;
}
return L"";
}
ITMPlugin::OptionReturn CPluginDemo::ShowOptionsDialog(void* hParent)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COptionsDlg dlg(CWnd::FromHandle((HWND)hParent));
dlg.m_data = CDataManager::Instance().m_setting_data;
if (dlg.DoModal() == IDOK)
{
CDataManager::Instance().m_setting_data = dlg.m_data;
return ITMPlugin::OR_OPTION_CHANGED;
}
return ITMPlugin::OR_OPTION_UNCHANGED;
}
void CPluginDemo::OnExtenedInfo(ExtendedInfoIndex index, const wchar_t* data)
{
switch (index)
{
case ITMPlugin::EI_CONFIG_DIR:
//从配置文件读取配置
g_data.LoadConfig(std::wstring(data));
break;
default:
break;
}
}
ITMPlugin* TMPluginGetInstance()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return &CPluginDemo::Instance();
}
================================================
FILE: PluginDemo/PluginDemo.h
================================================
#pragma once
#include "PluginInterface.h"
#include "PluginSystemDate.h"
#include "PluginSystemTime.h"
#include "CustomDrawItem.h"
class CPluginDemo : public ITMPlugin
{
private:
CPluginDemo();
public:
static CPluginDemo& Instance();
// 通过 ITMPlugin 继承
virtual IPluginItem* GetItem(int index) override;
virtual void DataRequired() override;
virtual const wchar_t* GetInfo(PluginInfoIndex index) override;
virtual OptionReturn ShowOptionsDialog(void* hParent) override;
virtual void OnExtenedInfo(ExtendedInfoIndex index, const wchar_t* data) override;
private:
CPluginSystemDate m_system_date;
CPluginSystemTime m_system_time;
CCustomDrawItem m_custom_draw_item;
static CPluginDemo m_instance;
};
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) ITMPlugin* TMPluginGetInstance();
#ifdef __cplusplus
}
#endif
================================================
FILE: PluginDemo/PluginDemo.rc
================================================
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// (壬й) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_OPTIONS_DIALOG DIALOGEX 0, 0, 165, 77
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "ʱں"
FONT 9, "ź", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "ȷ",IDOK,50,56,50,14
PUSHBUTTON "ȡ",IDCANCEL,108,56,50,14
GROUPBOX "ʽ",IDC_STATIC,7,7,151,45
CONTROL "ʾ",IDC_SHOW_SECOND_CHECK,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,21,63,10
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_OPTIONS_DIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 158
TOPMARGIN, 7
BOTTOMMARGIN, 70
END
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//
IDD_OPTIONS_DIALOG AFX_DIALOG_LAYOUT
BEGIN
0
END
/////////////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "080404b0"
BEGIN
VALUE "CompanyName", "By ZhongYang"
VALUE "FileDescription", "Plugin Demo for TrafficMonitor"
VALUE "FileVersion", "1.0.0.1"
VALUE "InternalName", "PluginDe.dll"
VALUE "LegalCopyright", "Copyright (C) 2021 By ZhongYang"
VALUE "OriginalFilename", "PluginDe.dll"
VALUE "ProductName", "Plugin Demo for TrafficMonitor"
VALUE "ProductVersion", "1.0.0.1"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x804, 1200
END
END
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
STRINGTABLE
BEGIN
IDS_PLUGIN_NAME "TrafficMonitorʾ"
IDS_PLUGIN_DESCRIPTION "TrafficMonitorʾΪ߿TrafficMonitorṩ"
IDS_TIME "ʱ"
IDS_DATE ""
IDS_CUSTOM_DRAW_ITEM "Իʾ"
END
#endif // (壬й) resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Ӣ() resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_OPTIONS_DIALOG DIALOGEX 0, 0, 165, 74
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Time and date settings"
FONT 9, "Segoe UI", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "OK",IDOK,50,53,50,14
PUSHBUTTON "Cancel",IDCANCEL,108,53,50,14
GROUPBOX "Format",IDC_STATIC,7,7,151,42
CONTROL "Show seconds",IDC_SHOW_SECOND_CHECK,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,21,63,10
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_OPTIONS_DIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 158
TOPMARGIN, 7
BOTTOMMARGIN, 67
END
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//
IDD_OPTIONS_DIALOG AFX_DIALOG_LAYOUT
BEGIN
0
END
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
STRINGTABLE
BEGIN
IDS_PLUGIN_NAME "A sample plug-in for TrafficMonitor."
IDS_PLUGIN_DESCRIPTION "A sample plug-in for TrafficMonitor, providing an example for developers to develop TrafficMonitor plug-ins."
IDS_TIME "Time"
IDS_DATE "Date"
IDS_CUSTOM_DRAW_ITEM "Custom draw example"
END
#endif // Ӣ() resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
================================================
FILE: PluginDemo/PluginDemo.vcxproj
================================================
Debug
Win32
Release
Win32
Debug
x64
Release
x64
16.0
Win32Proj
{d1ca3ecc-dc32-445a-b734-c4db08d4ba34}
PluginDemo
10.0
DynamicLibrary
true
v142
Unicode
Dynamic
DynamicLibrary
false
v142
true
Unicode
Dynamic
DynamicLibrary
true
v142
Unicode
Dynamic
DynamicLibrary
false
v142
true
Unicode
Dynamic
true
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Configuration)\plugins\
false
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Configuration)\plugins\
true
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Platform)\$(Configuration)\plugins\
false
$(ProjectDir)..\include;$(IncludePath)
$(SolutionDir)Bin\$(Platform)\$(Configuration)\plugins\
Level3
true
WIN32;_DEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
true
Use
pch.h
Windows
true
false
Level3
true
true
true
WIN32;NDEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
true
Use
pch.h
Windows
true
true
true
false
Level3
true
_DEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
true
Use
pch.h
Windows
true
false
Level3
true
true
true
NDEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
true
Use
pch.h
Windows
true
true
true
false
Create
Create
Create
Create
================================================
FILE: PluginDemo/PluginDemo.vcxproj.filters
================================================
{4FC737F1-C7A5-4376-A066-2A32D752A2FF}
cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
{93995380-89BD-4b04-88EB-625FBE52EBFB}
h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
头文件
头文件
头文件
头文件
头文件
头文件
头文件
头文件
头文件
头文件
源文件
源文件
源文件
源文件
源文件
源文件
源文件
资源文件
================================================
FILE: PluginDemo/PluginSystemDate.cpp
================================================
#include "pch.h"
#include
#include "PluginSystemDate.h"
#include "DataManager.h"
CPluginSystemDate::CPluginSystemDate()
{
}
const wchar_t* CPluginSystemDate::GetItemName() const
{
return CDataManager::Instance().StringRes(IDS_DATE);
}
const wchar_t* CPluginSystemDate::GetItemId() const
{
return L"o282ffc4";
}
const wchar_t* CPluginSystemDate::GetItemLableText() const
{
return CDataManager::Instance().StringRes(IDS_DATE);
}
const wchar_t* CPluginSystemDate::GetItemValueText() const
{
return CDataManager::Instance().m_cur_date.c_str();
}
const wchar_t* CPluginSystemDate::GetItemValueSampleText() const
{
return L"2022/08/08";
}
================================================
FILE: PluginDemo/PluginSystemDate.h
================================================
#pragma once
#include "PluginInterface.h"
class CPluginSystemDate : public IPluginItem
{
public:
CPluginSystemDate();
// 通过 IPluginItem 继承
virtual const wchar_t* GetItemName() const override;
virtual const wchar_t* GetItemId() const override;
virtual const wchar_t* GetItemLableText() const override;
virtual const wchar_t* GetItemValueText() const override;
virtual const wchar_t* GetItemValueSampleText() const override;
private:
};
================================================
FILE: PluginDemo/PluginSystemTime.cpp
================================================
#include "pch.h"
#include
#include "PluginSystemTime.h"
#include "DataManager.h"
CPluginSystemTime::CPluginSystemTime()
{
}
const wchar_t* CPluginSystemTime::GetItemName() const
{
return CDataManager::Instance().StringRes(IDS_TIME);
}
const wchar_t* CPluginSystemTime::GetItemId() const
{
return L"ra1YX2g1";
}
const wchar_t* CPluginSystemTime::GetItemLableText() const
{
return CDataManager::Instance().StringRes(IDS_TIME);
}
const wchar_t* CPluginSystemTime::GetItemValueText() const
{
return CDataManager::Instance().m_cur_time.c_str();
}
const wchar_t* CPluginSystemTime::GetItemValueSampleText() const
{
if (CDataManager::Instance().m_setting_data.show_second)
return L"12:00:00";
else
return L"12:00";
}
================================================
FILE: PluginDemo/PluginSystemTime.h
================================================
#pragma once
#include "PluginInterface.h"
class CPluginSystemTime : public IPluginItem
{
public:
CPluginSystemTime();
public:
// 通过 IPluginItem 继承
virtual const wchar_t* GetItemName() const override;
virtual const wchar_t* GetItemId() const override;
virtual const wchar_t* GetItemLableText() const override;
virtual const wchar_t* GetItemValueText() const override;
virtual const wchar_t* GetItemValueSampleText() const override;
private:
};
================================================
FILE: PluginDemo/framework.h
================================================
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
//// Windows 头文件
//#include
#include
#include // MFC 核心组件和标准组件
#include // MFC 扩展
#include // MFC 自动化类
#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif
================================================
FILE: PluginDemo/pch.cpp
================================================
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
// 当使用预编译的头时,需要使用此源文件,编译才能成功。
================================================
FILE: PluginDemo/pch.h
================================================
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#include "resource.h"
#endif //PCH_H
================================================
FILE: PluginDemo/resource.h
================================================
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 PluginDemo.rc 使用
//
#define IDD_DIALOG1 101
#define IDD_OPTIONS_DIALOG 101
#define IDS_PLUGIN_NAME 103
#define IDS_PLUGIN_DESCRIPTION 104
#define IDS_TIME 105
#define IDS_DATE 106
#define IDS_CUSTOM_DRAW_ITEM 107
#define IDC_SHOW_SECOND_CHECK 1001
#define IDC_SHOW_LABEL_CHECK 1002
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1003
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
================================================
FILE: README.md
================================================
**简体中文 | [English](./README_en-us.md)**
[](https://996.icu/#/en_US)
[](https://github.com/996icu/996.ICU/blob/master/LICENSE)
[](https://github.com/zhongyang219/TrafficMonitor/actions?query=workflow:"Release+CI")
[](https://github.com/zhongyang219/TrafficMonitor/releases/latest)
# TrafficMonitor 简介
Traffic Monitor是一款用于Windows平台的网速监控悬浮窗软件,可以显示当前网速、CPU及内存利用率,支持嵌入到任务栏显示,支持更换皮肤、历史流量统计等功能。
# 相关链接:
请[点击此处](https://github.com/zhongyang219/TrafficMonitor/releases/latest)下载TrafficMonitor的最新版本。
备用链接:[百度网盘下载](https://pan.baidu.com/s/15PMt7s-ASpyDwtS__4cUhg) 提取码:`ou0m`
国内用户如果遇到Github下载缓慢的问题,可以[点击此处](https://gitee.com/zhongyang219/TrafficMonitor)转到此项目在Gitee上的页面。
如果遇到问题,请[点击此处](./Help.md)。
你也可以[点击此处](https://github.com/zhongyang219/TrafficMonitor/actions?query=workflow:"Release+CI")下载TrafficMonitor的预发行构建版本。
从1.80版本开始,TrafficMonitor加入了温度监控功能,如果你不需要温度监控功能,并且在使用1.80以上版本中遇到了问题,建议下载不含温度监控的版本(Lite版本)。(在Release页面找到文件名包含`Lite`的版本。)
TrafficMonitor依赖于Microsoft Visual C++ 运行环境,如果程序启动时提示“找不到MSVC*.dll”,请点击以下链接下载并安装Microsoft Visual C++ 运行环境。
[最新支持的 Visual C++ 可再发行程序包下载 | Microsoft Docs](https://docs.microsoft.com/zh-CN/cpp/windows/latest-supported-vc-redist?view=msvc-170)
# 版本说明
TrafficMonitor提供了普通版和Lite版两种版本可用。普通版包含了所有的功能,Lite版本则不包含温度监控、显卡利用率、硬盘利用率等硬件监控功能。普通版运行需要管理员权限,而Lite版本则不需要。
如果没有监控温度等硬件信息的需要,建议使用Lite版。
以下是两个版本功能对比。
| 功能 | 普通版 | Lite版 |
| ----------------------------- | ------ | ------ |
| 网速监控 | ✔ | ✔ |
| CPU、内存利用率 | ✔ | ✔ |
| CPU、显卡、硬盘、主板温度监控、CPU频率监控 | ✔ | ❌ |
| 显卡利用率监控 | ✔ | ❌ |
| 硬盘利用率监控 | ✔ | ❌ |
| 网络详细信息 | ✔ | ✔ |
| 插件系统 | ✔ | ✔ |
| 主窗口更换皮肤 | ✔ | ✔ |
| 需要管理员权限 | 是 | 否 |
# 主要特性
* 显示当前实现网络传输速率、CPU和内存占用率
* 如果电脑有多个网卡,支持自动和手动选择网络连接
* 查看网络详细信息
* 支持嵌入到任务栏显示
* 支持更换皮肤和自定义皮肤
* 历史流量统计
* 硬件信息监控
* 插件系统
# 使用说明
**[点击这里](https://github.com/zhongyang219/TrafficMonitor/wiki)转到Wiki页面查看关于TrafficMonitor的详细说明文档。**
# 截图
主悬浮窗:

右键菜单:

任务栏窗口:

多彩皮肤:
# 如何使用
程序启动后在会在屏幕中显示一个显示网速的悬浮窗。在悬浮窗上点击鼠标右键可以弹出右键菜单。
TrafficMonitor支持将信息显示到任务栏。但是TrafficMonitor默认只显示主窗口(悬浮窗),如果需要让它嵌入到任务栏显示,请在右键菜单中选择“显示任务栏窗口”命令。
任务栏窗口支持自定义显示项目,默认情况下只显示网速,如果需要显示CPU和内存利用率,请在任务栏右键菜单中的“显示设置”子菜单下勾选需要显示的项目,如下图所示:
# 自定义皮肤
在主窗口或通知区图标右键菜单上选择“其他功能”——“更换皮肤”可以打开更换皮肤界面。[点击此处](https://github.com/zhongyang219/TrafficMonitorSkin/blob/master/皮肤下载.md)可以下载更多皮肤。用户还可以根据自己的需要编辑自己的皮肤。
皮肤文件放在程序所在目录的`skins`目录下,每个皮肤被放到单独的文件夹下,文件夹的名称就是皮肤的名称。
其中`background.bmp`和`background_l.bmp`是背景图片,`skin.ini`是皮肤的配置文件,可以通过`skin.ini`指定文本颜色、字体、皮肤作者、每个项目的大小和位置等信息。
从1.80版本开始增加了xml格式的皮肤配置文件`skin.xml`,只有xml格式的皮肤配置文件才支持温度和显卡使用率显示。
详细的皮肤制作教程请点击以下链接:
[皮肤制作教程 · zhongyang219/TrafficMonitor Wiki (github.com)](https://github.com/zhongyang219/TrafficMonitor/wiki/皮肤制作教程)
推荐使用[皮肤编辑器](https://github.com/zhongyang219/TrafficMonitorSkinEditor/releases)来创建或编辑皮肤。
# 选项设置
在右键菜单选择“选项...”可以进入选项设置。在选项设置对话框中,可以单独设置主窗口和任务栏窗口的文本颜色、字体、背景颜色、网速单位、显示的文本等。
在“常规设置”选项卡中,可以设置是否在程序时自动检查更新,以及是否需要在开机是自动运行。可以设置在什么时候需要发出消息通知。
从1.72版本开始,支持每个项目文本颜色单独设置。勾选“指定每个项目的颜色”后,点击“文本颜色”右边的颜色框,会弹出详细颜色设置的对话框,可以在这里单独指定每个项目的颜色。
# 插件系统
从1.82版本开始增加了插件系统,插件dll必须放在“TrafficMonitor.exe”同级目录的“plugins”目录下。程序启动后,插件会自动加载。你可以在右键菜单“更多功能”——“插件管理”中查看并管理已加载的插件。
关于如何开发TrafficMonitor的说明,请参见[插件开发指南 · zhongyang219/TrafficMonitor Wiki (github.com)](https://github.com/zhongyang219/TrafficMonitor/wiki/插件开发指南)。
要下载TrafficMonitor插件,请[点击这里](https://github.com/zhongyang219/TrafficMonitorPlugins/blob/main/download/plugin_download.md)。
# 关于硬件监控功能
从1.80版本开始,TrafficMonitor加入了硬件监控功能(包括温度监控和显卡使用率监控、CPU频率监控),它使用了第三方开源库[LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor)。如果你在使用温度监控功能时遇到了问题,请[点击这里](./Help.md#13-关于trafficmonitor温度监控的问题)。
需要注意的是,温度监控功能默认是关闭的,如果你要使用TrafficMonitor的温度监控功能,请到[“选项设置”-“常规设置”-“硬件监控”](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#硬件监控)中开启。
**注意:硬件监控功能(包括温度监控和显卡使用率监控)可能存在一些问题,它可能会占用更多的CPU和内存。据部分用户反馈,开启温度功能后会导致程序崩溃和系统死机等问题,请在知晓以上风险后再决定开启硬件监控功能。否则,请不要使用硬件监控功能。**
# 更新日志
**[点击此处查看更新日志](./UpdateLog/update_log.md)**
================================================
FILE: README_en-us.md
================================================
**[简体中文](./README.md) | English**
[](https://996.icu/#/en_US)
[](https://github.com/996icu/996.ICU/blob/master/LICENSE)
[](https://github.com/zhongyang219/TrafficMonitor/actions?query=workflow:"Release+CI")
[](https://github.com/zhongyang219/TrafficMonitor/releases/latest)
# TrafficMonitor Introduction
TrafficMonitor is a network monitoring suspension window software on Windows platform. It can display the current network speed, usage of CPU. It also has the function of displaying in the taskbar, skin changing and historical traffic statistics.
# Related Links
Please [click here](https://github.com/zhongyang219/TrafficMonitor/releases/latest) to download the latest version of TrafficMonitor.
Alternate link: Download from [Baidu Netdisk](https://pan.baidu.com/s/15PMt7s-ASpyDwtS__4cUhg). Access code: `ou0m`
If you encounter any problems, please [click here](./Help_en-us.md).
You can also [click here](https://github.com/zhongyang219/TrafficMonitor/actions?query=workflow:"Release+CI") to download the pre-release build version of TrafficMonitor.
Starting from version 1.80, the temperature monitoring function has been added. If you do not need the temperature monitoring function and encounter problems with version 1.80 or higher, it is recommended to download the version which is without temperature monitoring (Lite version). (Find the version that contains `Lite` in the file name on Release page.)
TrafficMonitor relies on the Microsoft Visual C++ runtime environment. If the program prompts "Cannot find MSVC*.dll" when the program starts, please click the link below to download and install the Microsoft Visual C++ runtime environment.
[Latest supported Visual C++ Redistributable downloads | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170)
# Version description
TrafficMonitor provides two versions, the standard version and the Lite version. The standard version includes all the functions, while the Lite version does not include hardware monitoring functions such as temperature monitoring, GPU usage, and hard disk usage. The standard version requires administrator to run, while the Lite version does not.
If there is no need to monitor the temperature and other hardware information, it is recommended to use the Lite version.
The functions comparison of the two versions is as the following.
| Functions | Standard Version | Lite Version |
| ------------------------------------------------------ | ---------------- | ------------ |
| Net speed monitoring | ✔ | ✔ |
| CPU and memory usage monitoring | ✔ | ✔ |
| CPU, GPU, hard disk, main board temperature, CPU frequency monitoring | ✔ | ❌ |
| GPU usage monitoring | ✔ | ❌ |
| Hard disk usage monitoring | ✔ | ❌ |
| Network connection details | ✔ | ✔ |
| Plug-in system | ✔ | ✔ |
| Change the skin of the main window | ✔ | ✔ |
| Administrator rights required | Yes | No |
# Main Features
* Displays the current network transfer speed, usage of CPU and memory.
* Support automatic or manual selection of network connections if there are multiple network adapters.
* Display the network connection details.
* Support the window displayed in the taskbar.
* Support for changing skin and customizing skin.
* Historical traffic statistics.
* Hardware information monitoring
* Plug-in system
# Instructions for use
**[Click here](https://github.com/zhongyang219/TrafficMonitor/wiki) to go to the Wiki page to view detailed documentation on TrafficMonitor (Only Chinese Available).**
# Screen Shots
Main Suspension Window:

Right-Click Menu:

Taskbar Window

Colorful Skins:

Change Skins:

Options:
# How to Use
After the program started, a floating window showing the network speed will be displayed on the screen. Click the right mouse button on the floating window to pop up the right-click menu.
TrafficMonitor supports displaying information on the taskbar. But TrafficMonitor only displays the main window (floating window) by default. If you want to embed it into the taskbar, please select the "Show taskbar window" command in the right-click menu.
Taskbar window supports customization of display items. By default, only the network speed is displayed. If you need to display the CPU and memory usage, please check the items that need to be displayed under the "Display Settings" submenu in the taskbar right-click menu, as shown in the figure below:
# Plug-in system
The plug-in system has been added since version 1.82. The plug-in dll must be placed in the "plugins" directory of the same level directory of "TrafficMonitor.exe". After the program is started, the plug-in will be loaded automatically. You can view and manage the loaded plugins in the context menu "More Functions"-"Plugin Management".
For instructions on how to develop the plugin-in of TrafficMonitor, please see [Plugin Development Guide · zhongyang219/TrafficMonitor Wiki (github.com)](https://github.com/zhongyang219/TrafficMonitor/wiki/Plugin-Development-Guide).
To download the TrafficMonitor plug-in, please [click here](https://github.com/zhongyang219/TrafficMonitorPlugins/blob/main/download/plugin_download.md).
# About the hardware monitoring function
Starting from version 1.80, the hardware monitoring functions (including temperature monitoring, CPU frequency and graphics card usage monitoring) has been added in TrafficMonitor. It relies on the open source library [LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor). If you encounter problems during using the temperature monitoring function, please [click here](./Help_en-us.md#13-about-the-temperature-monitoring-of-trafficmonitor).
It should be noted that the temperature monitoring function is turned off by default. If you want to use the temperature monitoring function of TrafficMonitor, please go to ["Option Settings"-"General Settings"-"Hardware Monitoring"](https://github.com/zhongyang219/TrafficMonitor/wiki/选项设置#硬件监控) to enable it.
**Note: The hardware monitoring function (including temperature monitoring and GPU usage monitoring) may have some problems, which may cause more CPU and memory usage. According to feedback from some users, turning on the temperature function will cause problems such as program crashes and system crashes. Please decide to turn on the hardware monitoring function after you are aware of the above risks. Otherwise, please do not use the hardware monitoring function.**
# Update log
**[Click here to view the update log.](./UpdateLog/update_log_en-us.md)**
================================================
FILE: TrafficMonitor/AboutDlg.cpp
================================================
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "AboutDlg.h"
#include "MessageDlg.h"
#include "DrawCommon.h"
BEGIN_MESSAGE_MAP(CAboutDlg, CBaseDialog)
//ON_STN_CLICKED(IDC_STATIC_DONATE, &CAboutDlg::OnStnClickedStaticDonate)
ON_MESSAGE(WM_LINK_CLICKED, &CAboutDlg::OnLinkClicked)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
CAboutDlg::CAboutDlg() : CBaseDialog(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC_MAIL, m_mail);
DDX_Control(pDX, IDC_STATIC_ACKNOWLEDGEMENT, m_acknowledgement);
DDX_Control(pDX, IDC_STATIC_GITHUB, m_github);
DDX_Control(pDX, IDC_STATIC_DONATE, m_donate);
DDX_Control(pDX, IDC_TRANSLATOR_STATIC, m_translaotr_static);
DDX_Control(pDX, IDC_STATIC_LICENSE, m_license);
DDX_Control(pDX, IDC_OPENHARDWAREMONITOR_LINK, m_openhardwaremonitor_link);
DDX_Control(pDX, IDC_TINYXML2_LINK, m_tinyxml2_link);
DDX_Control(pDX, IDC_MUSICPLAYER2_LINK, m_musicplayer2_link);
DDX_Control(pDX, IDC_SIMPLENOTEPAD_LINK, m_simplenotepad_link);
DDX_Control(pDX, IDC_STATIC_GITEE, m_gitee);
}
CString CAboutDlg::GetDonateList()
{
return CCommon::GetTextResource(IDR_ACKNOWLEDGEMENT_TEXT, 2);
}
CString CAboutDlg::GetDialogName() const
{
return _T("AboutDlg");
}
BOOL CAboutDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_ABOUT));
m_mail.SetURL(_T("mailto:zhongyang219@hotmail.com")); //设置超链接
//m_check_update.SetURL(_T("http://pan.baidu.com/s/1c1LkPQ4"));
m_github.SetURL(_T("https://github.com/zhongyang219/TrafficMonitor"));
m_gitee.SetURL(_T("https://gitee.com/zhongyang219/TrafficMonitor"));
m_donate.SetLinkIsURL(false);
m_acknowledgement.SetLinkIsURL(false);
m_license.SetLinkIsURL(false);
m_openhardwaremonitor_link.SetURL(_T("https://github.com/LibreHardwareMonitor/LibreHardwareMonitor"));
m_tinyxml2_link.SetURL(_T("https://github.com/leethomason/tinyxml2"));
m_musicplayer2_link.SetURL(_T("https://github.com/zhongyang219/MusicPlayer2"));
m_simplenotepad_link.SetURL(_T("https://github.com/zhongyang219/SimpleNotePad"));
m_openhardwaremonitor_link.SetBackgroundColor(GetSysColor(COLOR_WINDOW));
m_tinyxml2_link.SetBackgroundColor(GetSysColor(COLOR_WINDOW));
m_musicplayer2_link.SetBackgroundColor(GetSysColor(COLOR_WINDOW));
m_simplenotepad_link.SetBackgroundColor(GetSysColor(COLOR_WINDOW));
//设置版本信息
CString version_info;
GetDlgItemText(IDC_STATIC_VERSION, version_info);
CString str_lite;
#ifdef WITHOUT_TEMPERATURE
str_lite = CCommon::LoadText(_T(" ("), IDS_WITHOUT_TEMPERATURE, _T(")"));
#endif
version_info = CCommon::StringFormat(version_info, { str_lite, VERSION });
#ifdef COMPILE_FOR_WINXP
version_info += _T(" (For WinXP)");
#endif // COMPILE_FOR_WINXP
#ifdef _M_X64
version_info += _T(" (x64)");
#endif
#ifdef _DEBUG
version_info += _T(" (Debug)");
#endif
SetDlgItemText(IDC_STATIC_VERSION, version_info);
//设置最后编译日期
CString temp_str;
GetDlgItemText(IDC_STATIC_COPYRIGHT, temp_str);
CString str_compile_time = CCommon::GetLastCompileTime();
temp_str.Replace(_T(""), str_compile_time);
SetDlgItemText(IDC_STATIC_COPYRIGHT, temp_str);
m_tool_tip.Create(this, TTS_ALWAYSTIP | TTS_NOPREFIX);
m_tool_tip.AddTool(&m_mail, CCommon::LoadText(IDS_SEND_EMAIL_TO_ATHOUR, _T("\r\nmailto:zhongyang219@hotmail.com")));
//m_tool_tip.AddTool(&m_check_update, _T("到百度网盘链接查看是否有更新\r\nhttp://pan.baidu.com/s/1c1LkPQ4"));
m_tool_tip.AddTool(&m_github, CCommon::LoadText(IDS_GOTO_GITHUB, _T("\r\nhttps://github.com/zhongyang219/TrafficMonitor")));
m_tool_tip.AddTool(&m_gitee, CCommon::LoadText(IDS_GOTO_GITEE, _T("\r\nhttps://gitee.com/zhongyang219/TrafficMonitor")));
m_tool_tip.AddTool(&m_donate, CCommon::LoadText(IDS_DONATE_ATHOUR));
m_tool_tip.AddTool(&m_openhardwaremonitor_link, m_openhardwaremonitor_link.GetURL());
m_tool_tip.AddTool(&m_tinyxml2_link, m_tinyxml2_link.GetURL());
m_tool_tip.AddTool(&m_musicplayer2_link, CCommon::LoadText(IDS_MUSICPLAYER2_DESCRIPTION) + _T("\r\n") + m_musicplayer2_link.GetURL());
m_tool_tip.AddTool(&m_simplenotepad_link, CCommon::LoadText(IDS_SIMPLENOTEPAD_DESCRIPTION) + _T("\r\n") + m_simplenotepad_link.GetURL());
m_tool_tip.SetDelayTime(300); //设置延迟
m_tool_tip.SetMaxTipWidth(800);
//设置翻译者信息
int language_code;
language_code = _ttoi(CCommon::LoadText(IDS_LANGUAGE_CODE));
if (language_code == 1 || language_code == 2) //语言是简体中文和英文时不显示翻译者信息
m_translaotr_static.ShowWindow(SW_HIDE);
if (language_code == 3) //显示繁体中文翻译者的信息
{
m_translaotr_static.SetURL(_T("http://mkvq.blogspot.com/"));
m_tool_tip.AddTool(&m_translaotr_static, CCommon::LoadText(IDS_CONTACT_TRANSLATOR, _T("\r\nhttp://mkvq.blogspot.com/")));
}
m_translaotr_static.SetBackgroundColor(GetSysColor(COLOR_WINDOW));
//设置图片的位置
CRect rect;
GetClientRect(rect);
m_rc_pic = rect;
::GetWindowRect(GetDlgItem(IDC_STATIC_VERSION)->GetSafeHwnd(), rect);
ScreenToClient(rect);
m_rc_pic.bottom = rect.top - theApp.DPI(6);
if (m_rc_pic.Height() <= 0)
m_rc_pic.bottom = m_rc_pic.top + theApp.DPI(50);
//加载图片
m_about_pic.LoadBitmap(IDB_ABOUT_BACKGROUND_HD);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_MOUSEMOVE)
m_tool_tip.RelayEvent(pMsg);
return CBaseDialog::PreTranslateMessage(pMsg);
}
//void CAboutDlg::OnStnClickedStaticDonate()
//{
// CDonateDlg donateDlg;
// donateDlg.DoModal();
//}
afx_msg LRESULT CAboutDlg::OnLinkClicked(WPARAM wParam, LPARAM lParam)
{
CWnd* pCtrl = (CWnd*)wParam;
if (pCtrl == &m_donate)
{
CDonateDlg donateDlg;
donateDlg.DoModal();
}
else if (pCtrl == &m_acknowledgement)
{
CString strContent = GetDonateList();
//strContent += _T("\r\n");
//strContent += CCommon::LoadText(IDS_ACKNOWLEDGEMENT_EXPLAIN);
CMessageDlg dlg;
dlg.SetWindowTitle(CCommon::LoadText(IDS_TITLE_ACKNOWLEDGEMENT));
//dlg.SetInfoText(CCommon::LoadText(IDS_THANKS_DONORS));
dlg.SetMessageText(strContent);
dlg.DoModal();
}
else if (pCtrl == &m_license)
{
CMessageDlg dlg;
dlg.SetWindowTitle(CCommon::LoadText(IDS_LICENSE));
dlg.SetInfoText(CCommon::LoadText(IDS_LICENSE_EXPLAIN));
dlg.SetMessageText(CCommon::GetTextResource(IDR_LICENSE, 1));
dlg.DoModal();
}
return 0;
}
void CAboutDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CBaseDialog::OnPaint()
CDrawCommon draw;
draw.Create(&dc, this);
draw.GetDC()->FillSolidRect(m_rc_pic, RGB(161, 200, 255));
draw.DrawBitmap(m_about_pic, m_rc_pic.TopLeft(), m_rc_pic.Size(), CDrawCommon::StretchMode::FIT);
}
BOOL CAboutDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect draw_rect;
GetClientRect(draw_rect);
pDC->FillSolidRect(draw_rect, GetSysColor(COLOR_WINDOW));
//绘制白色背景
int white_height; //白色区域的高度
CRect rc_copyright{};
::GetWindowRect(GetDlgItem(IDOK)->GetSafeHwnd(), rc_copyright);
ScreenToClient(rc_copyright);
white_height = rc_copyright.top - theApp.DPI(6);
//绘制“确定”按钮上方的分割线
CRect rc_line{ draw_rect };
rc_line.top = white_height;
rc_line.bottom = white_height + theApp.DPI(1);
pDC->FillSolidRect(rc_line, RGB(210, 210, 210));
//绘制灰色背景
CRect rc_gray{ rc_line };
rc_gray.top = rc_line.bottom;
rc_gray.bottom = draw_rect.bottom;
pDC->FillSolidRect(rc_gray, GetSysColor(COLOR_BTNFACE));
return TRUE;
//return CBaseDialog::OnEraseBkgnd(pDC);
}
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CBaseDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
//去掉static控件的灰色灰色背景
UINT ctrl_id = pWnd->GetDlgCtrlID();
if (ctrl_id == IDC_STATIC_VERSION || ctrl_id == IDC_STATIC_COPYRIGHT || ctrl_id == IDC_STATIC)
{
static HBRUSH hBackBrush{};
if (hBackBrush == NULL)
hBackBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
return hBackBrush;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
================================================
FILE: TrafficMonitor/AboutDlg.h
================================================
#pragma once
#include "DonateDlg.h"
#include "LinkStatic.h"
#include "BaseDialog.h"
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CBaseDialog
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
CLinkStatic m_mail; //“联系作者”超链接
CLinkStatic m_acknowledgement; //“鸣谢”超链接
CLinkStatic m_github; //“GitHub”超链接
CLinkStatic m_gitee; //“Gitee”超链接
CLinkStatic m_donate; //“捐助”超链接
CLinkStatic m_license; //“开源协议”超链接
CToolTipCtrl m_tool_tip; //鼠标指向时的工具提示
CLinkStatic m_translaotr_static;
CLinkStatic m_openhardwaremonitor_link;
CLinkStatic m_tinyxml2_link;
CLinkStatic m_musicplayer2_link;
CLinkStatic m_simplenotepad_link;
CRect m_rc_pic; //图片的位置
CBitmap m_about_pic;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
CString GetDonateList(); //从资源文件加载捐赠人员名单
virtual CString GetDialogName() const override;
// 实现
protected:
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
// afx_msg void OnStnClickedStaticDonate();
protected:
afx_msg LRESULT OnLinkClicked(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
================================================
FILE: TrafficMonitor/AdapterCommon.cpp
================================================
#include "stdafx.h"
#include "AdapterCommon.h"
CAdapterCommon::CAdapterCommon()
{
}
CAdapterCommon::~CAdapterCommon()
{
}
void CAdapterCommon::GetAdapterInfo(vector& adapters)
{
adapters.clear();
PIP_ADAPTER_INFO pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[sizeof(IP_ADAPTER_INFO)]; //PIP_ADAPTER_INFOṹָ洢Ϣ
unsigned long stSize = sizeof(IP_ADAPTER_INFO); //õṹС,GetAdaptersInfo
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); //GetAdaptersInfo,pIpAdapterInfoָ;stSizeһҲһ
if (ERROR_BUFFER_OVERFLOW == nRel)
{
//صERROR_BUFFER_OVERFLOW
//˵GetAdaptersInfoݵڴռ䲻,ͬʱ䴫stSize,ʾҪĿռС
//Ҳ˵ΪʲôstSizeһҲһ
delete[] (BYTE*)pIpAdapterInfo; //ͷԭڴռ
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize]; //ڴռ洢Ϣ
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); //ٴεGetAdaptersInfo,pIpAdapterInfoָ
}
PIP_ADAPTER_INFO pIpAdapterInfoHead = pIpAdapterInfo; //pIpAdapterInfoеһԪصĵַ
if (ERROR_SUCCESS == nRel)
{
while (pIpAdapterInfo)
{
NetWorkConection connection;
connection.description = pIpAdapterInfo->Description;
connection.ip_address = CCommon::StrToUnicode(pIpAdapterInfo->IpAddressList.IpAddress.String);
connection.subnet_mask = CCommon::StrToUnicode(pIpAdapterInfo->IpAddressList.IpMask.String);
connection.default_gateway = CCommon::StrToUnicode(pIpAdapterInfo->GatewayList.IpAddress.String);
adapters.push_back(connection);
pIpAdapterInfo = pIpAdapterInfo->Next;
}
}
//ͷڴռ
if (pIpAdapterInfoHead)
{
delete[] (BYTE*)pIpAdapterInfoHead;
}
if (adapters.empty())
{
NetWorkConection connection{};
connection.description = CCommon::UnicodeToStr(CCommon::LoadText(L"<", IDS_NO_CONNECTION, L">"));
adapters.push_back(connection);
}
}
void CAdapterCommon::RefreshIpAddress(vector& adapters)
{
vector adapters_tmp;
GetAdapterInfo(adapters_tmp);
for (const auto& adapter_tmp : adapters_tmp)
{
for (auto& adapter : adapters)
{
if (adapter_tmp.description == adapter.description)
{
adapter.ip_address = adapter_tmp.ip_address;
adapter.subnet_mask = adapter_tmp.subnet_mask;
adapter.default_gateway = adapter_tmp.default_gateway;
}
}
}
}
void CAdapterCommon::GetIfTableInfo(vector& adapters, MIB_IFTABLE* pIfTable)
{
//IfTableÿ
for (size_t i{}; i < adapters.size(); i++)
{
if (adapters[i].description.empty())
continue;
int index;
index = FindConnectionInIfTable(adapters[i].description, pIfTable);
if (index == -1) //ʹþȷƥķʽûҵģƥķʽٲһ
index = FindConnectionInIfTableFuzzy(adapters[i].description, pIfTable);
//if (index != -1)
//{
adapters[i].index = index;
adapters[i].in_bytes = pIfTable->table[index].dwInOctets;
adapters[i].out_bytes = pIfTable->table[index].dwOutOctets;
adapters[i].description_2 = (const char*)pIfTable->table[index].bDescr;
//}
}
}
void CAdapterCommon::GetAllIfTableInfo(vector& adapters, MIB_IFTABLE * pIfTable)
{
vector adapters_tmp;
GetAdapterInfo(adapters_tmp); //ȡIPַ
adapters.clear();
for (size_t i{}; i < pIfTable->dwNumEntries; i++)
{
NetWorkConection connection;
connection.description = connection.description_2 = (const char*)pIfTable->table[i].bDescr;
connection.index = i;
connection.in_bytes = pIfTable->table[i].dwInOctets;
connection.out_bytes = pIfTable->table[i].dwOutOctets;
for (size_t j{}; j < adapters_tmp.size(); j++)
{
if (connection.description.find(adapters_tmp[j].description) != string::npos)
{
connection.ip_address = adapters_tmp[j].ip_address;
connection.subnet_mask = adapters_tmp[j].subnet_mask;
connection.default_gateway = adapters_tmp[j].default_gateway;
break;
}
}
adapters.push_back(connection);
}
}
int CAdapterCommon::FindConnectionInIfTable(string connection, MIB_IFTABLE* pIfTable)
{
for (size_t i{}; i < pIfTable->dwNumEntries; i++)
{
string descr = (const char*)pIfTable->table[i].bDescr;
if (descr == connection)
return i;
}
return -1;
}
int CAdapterCommon::FindConnectionInIfTableFuzzy(string connection, MIB_IFTABLE* pIfTable)
{
for (size_t i{}; i < pIfTable->dwNumEntries; i++)
{
string descr = (const char*)pIfTable->table[i].bDescr;
size_t index;
//ڽϳַҽ϶̵ַ
if (descr.size() >= connection.size())
index = descr.find(connection);
else
index = connection.find(descr);
if (index != wstring::npos)
return i;
}
//ûҵʹַƥ㷨
double max_degree{};
int best_index{};
for (size_t i{}; i < pIfTable->dwNumEntries; i++)
{
string descr = (const char*)pIfTable->table[i].bDescr;
double degree = CCommon::StringSimilarDegree_LD(descr, connection);
if (degree > max_degree)
{
max_degree = degree;
best_index = i;
}
}
return best_index;
}
================================================
FILE: TrafficMonitor/AdapterCommon.h
================================================
#pragma once
#include "Common.h"
//һϢ
struct NetWorkConection
{
int index{}; //MIB_IFTABLEе
string description; //ȡGetAdapterInfo
string description_2; //ȡGetIfTable
unsigned int in_bytes; //ʼʱѽֽ
unsigned int out_bytes; //ʼʱѷֽ
wstring ip_address{ L"-.-.-.-" }; //IPַ
wstring subnet_mask{ L"-.-.-.-" }; //
wstring default_gateway{ L"-.-.-.-" }; //Ĭ
};
class CAdapterCommon
{
public:
CAdapterCommon();
~CAdapterCommon();
//ȡбIPַ롢ĬϢ
static void GetAdapterInfo(vector& adapters);
//ˢбеIPַ롢ĬϢ
static void RefreshIpAddress(vector& adapters);
//ȡбÿӵMIB_IFTABLEеʼʱѽ/ֽϢ
static void GetIfTableInfo(vector& adapters, MIB_IFTABLE* pIfTable);
//ֱӽMIB_IFTABLEеӵadapters
static void GetAllIfTableInfo(vector& adapters, MIB_IFTABLE* pIfTable);
private:
//һжǷIfTableбҲ-1
static int FindConnectionInIfTable(string connection, MIB_IFTABLE* pIfTable);
//һжǷIfTableбҲ-1ֻҪƥ
static int FindConnectionInIfTableFuzzy(string connection, MIB_IFTABLE* pIfTable);
};
================================================
FILE: TrafficMonitor/AppAlreadyRuningDlg.cpp
================================================
// AppAlreadyRuningDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "AppAlreadyRuningDlg.h"
#include "afxdialogex.h"
// CAppAlreadyRuningDlg 对话框
IMPLEMENT_DYNAMIC(CAppAlreadyRuningDlg, CDialog)
CAppAlreadyRuningDlg::CAppAlreadyRuningDlg(HWND handel, CWnd* pParent /*=nullptr*/)
: CDialog(IDD_APP_ALREAD_RUNING_DIALOG, pParent), m_handle(handel)
{
}
CAppAlreadyRuningDlg::~CAppAlreadyRuningDlg()
{
}
void CAppAlreadyRuningDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAppAlreadyRuningDlg, CDialog)
ON_BN_CLICKED(IDC_EXIT_INST_BUTTON, &CAppAlreadyRuningDlg::OnBnClickedExitInstButton)
ON_BN_CLICKED(IDC_OPEN_SETTINGS_BUTTON, &CAppAlreadyRuningDlg::OnBnClickedOpenSettingsButton)
ON_BN_CLICKED(IDC_SHOW_HIDE_MAIN_WINDOW_BUTTON, &CAppAlreadyRuningDlg::OnBnClickedShowHideMainWindowButton)
ON_BN_CLICKED(IDC_SHOW_HIDE_TASKBAR_WINDOW_BUTTON, &CAppAlreadyRuningDlg::OnBnClickedShowHideTaskbarWindowButton)
END_MESSAGE_MAP()
// CAppAlreadyRuningDlg 消息处理程序
BOOL CAppAlreadyRuningDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME), FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CAppAlreadyRuningDlg::OnBnClickedExitInstButton()
{
::PostMessage(m_handle, WM_COMMAND, ID_APP_EXIT, 0);
}
void CAppAlreadyRuningDlg::OnBnClickedOpenSettingsButton()
{
::PostMessage(m_handle, WM_COMMAND, ID_OPTIONS, 0);
}
void CAppAlreadyRuningDlg::OnBnClickedShowHideMainWindowButton()
{
::PostMessage(m_handle, WM_COMMAND, ID_SHOW_MAIN_WND, 0);
}
void CAppAlreadyRuningDlg::OnBnClickedShowHideTaskbarWindowButton()
{
::PostMessage(m_handle, WM_COMMAND, ID_SHOW_TASK_BAR_WND, 0);
}
================================================
FILE: TrafficMonitor/AppAlreadyRuningDlg.h
================================================
#pragma once
// CAppAlreadyRuningDlg 对话框
class CAppAlreadyRuningDlg : public CDialog
{
DECLARE_DYNAMIC(CAppAlreadyRuningDlg)
public:
CAppAlreadyRuningDlg(HWND handel, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CAppAlreadyRuningDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_APP_ALREAD_RUNING_DIALOG };
#endif
private:
HWND m_handle{}; //正在运行的TrafficMonitor进程主窗口的句柄
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedExitInstButton();
afx_msg void OnBnClickedOpenSettingsButton();
afx_msg void OnBnClickedShowHideMainWindowButton();
afx_msg void OnBnClickedShowHideTaskbarWindowButton();
};
================================================
FILE: TrafficMonitor/BaseDialog.cpp
================================================
// BaseDialog.cpp : 实现文件
//
#include "stdafx.h"
#include "BaseDialog.h"
#include "afxdialogex.h"
#include "IniHelper.h"
#include "TrafficMonitor.h"
// CBaseDialog 对话框
std::map CBaseDialog::m_unique_hwnd;
IMPLEMENT_DYNAMIC(CBaseDialog, CDialog)
CBaseDialog::CBaseDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
}
CBaseDialog::~CBaseDialog()
{
}
void CBaseDialog::SetMinSize(int cx, int cy)
{
m_min_size.cx = cx;
m_min_size.cy = cy;
}
HWND CBaseDialog::GetUniqueHandel(LPCTSTR dlg_name)
{
return m_unique_hwnd[dlg_name];
}
const std::map& CBaseDialog::AllUniqueHandels()
{
return m_unique_hwnd;
}
void CBaseDialog::LoadConfig()
{
if (!GetDialogName().IsEmpty())
{
CIniHelper ini{ theApp.m_config_path };
//载入窗口大小设置
m_window_size.cx = ini.GetInt(_T("window_size"), GetDialogName() + _T("_width"), -1);
m_window_size.cy = ini.GetInt(_T("window_size"), GetDialogName() + _T("_height"), -1);
}
}
void CBaseDialog::SaveConfig() const
{
if (!GetDialogName().IsEmpty())
{
CIniHelper ini{ theApp.m_config_path };
//保存窗口大小设置
ini.WriteInt(_T("window_size"), GetDialogName() + _T("_width"), m_window_size.cx);
ini.WriteInt(_T("window_size"), GetDialogName() + _T("_height"), m_window_size.cy);
ini.Save();
}
}
void CBaseDialog::EnableDlgCtrl(UINT id, bool enable)
{
CWnd* pWnd = GetDlgItem(id);
if (pWnd != nullptr)
pWnd->EnableWindow(enable);
}
void CBaseDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CBaseDialog, CDialog)
ON_WM_DESTROY()
ON_WM_GETMINMAXINFO()
ON_WM_SIZE()
END_MESSAGE_MAP()
// CBaseDialog 消息处理程序
BOOL CBaseDialog::OnInitDialog()
{
m_unique_hwnd[GetDialogName()] = m_hWnd;
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
//获取初始时窗口的大小
if (m_min_size.cx <= 0 || m_min_size.cy <= 0)
{
CRect rect;
GetWindowRect(rect);
m_min_size.cx = rect.Width() * 96 / theApp.GetDpi();
m_min_size.cy = rect.Height() * 96 / theApp.GetDpi();
}
//载入设置
LoadConfig();
//初始化窗口大小
if (m_window_size.cx > 0 && m_window_size.cy > 0)
{
SetWindowPos(nullptr, 0, 0, m_window_size.cx, m_window_size.cy, SWP_NOZORDER | SWP_NOMOVE);
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CBaseDialog::OnDestroy()
{
CDialog::OnDestroy();
// TODO: 在此处添加消息处理程序代码
m_unique_hwnd[GetDialogName()] = NULL;
SaveConfig();
}
void CBaseDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//限制窗口最小大小
lpMMI->ptMinTrackSize.x = theApp.DPI(m_min_size.cx); //设置最小宽度
lpMMI->ptMinTrackSize.y = theApp.DPI(m_min_size.cy); //设置最小高度
CDialog::OnGetMinMaxInfo(lpMMI);
}
void CBaseDialog::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (nType != SIZE_MAXIMIZED && nType != SIZE_MINIMIZED)
{
//m_window_width = cx;
//m_window_hight = cy;
CRect rect;
GetWindowRect(&rect);
m_window_size.cx = rect.Width();
m_window_size.cy = rect.Height();
}
}
INT_PTR CBaseDialog::DoModal()
{
HWND unique_hwnd{ m_unique_hwnd[GetDialogName()] };
if (unique_hwnd != NULL && !GetDialogName().IsEmpty()) ///如果对话框已存在,则显示已存在的对话框
{
::ShowWindow(unique_hwnd, SW_RESTORE);
::SetForegroundWindow(unique_hwnd);
return 0;
}
return CDialog::DoModal();
}
================================================
FILE: TrafficMonitor/BaseDialog.h
================================================
#pragma once
// CBaseDialog 对话框
//用于实现记住对话框大小
//并将窗口初始大小设置为最小大小
class CBaseDialog : public CDialog
{
DECLARE_DYNAMIC(CBaseDialog)
public:
CBaseDialog(UINT nIDTemplate, CWnd* pParent = NULL); // 标准构造函数
virtual ~CBaseDialog();
// 对话框数据
//#ifdef AFX_DESIGN_TIME
// enum { IDD = IDD_BASEDIALOG };
//#endif
void SetMinSize(int cx, int cy); //设置窗口的最小大小,如果未设置,则使用窗口的初始大小作为最小大小
static HWND GetUniqueHandel(LPCTSTR dlg_name); //获指定窗口唯一的句柄
static const std::map& AllUniqueHandels(); //获取所有窗口的句柄
private:
void LoadConfig();
void SaveConfig() const;
private:
CSize m_min_size{}; //窗口的最小大小(以 96dpi 的大小保存)
CSize m_window_size{ -1, -1 };
static std::map m_unique_hwnd; //针对每一个基类的唯一的窗口句柄
protected:
virtual CString GetDialogName() const = 0;
void EnableDlgCtrl(UINT id, bool enable);
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnDestroy();
afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);
afx_msg void OnSize(UINT nType, int cx, int cy);
virtual INT_PTR DoModal();
};
================================================
FILE: TrafficMonitor/CAutoAdaptSettingsDlg.cpp
================================================
// CAutoAdaptSettingsDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "CAutoAdaptSettingsDlg.h"
#include "afxdialogex.h"
// CAutoAdaptSettingsDlg 对话框
IMPLEMENT_DYNAMIC(CAutoAdaptSettingsDlg, CDialog)
CAutoAdaptSettingsDlg::CAutoAdaptSettingsDlg(TaskBarSettingData& data, CWnd* pParent /*=nullptr*/)
: CDialog(IDD_ATUO_ADAPT_SETTING_DIALOG, pParent), m_data(data)
{
}
CAutoAdaptSettingsDlg::~CAutoAdaptSettingsDlg()
{
}
void CAutoAdaptSettingsDlg::InitComboBox(CComboBox& combo_box, int style_sel)
{
for (int i = 0; i < TASKBAR_DEFAULT_STYLE_NUM; i++)
combo_box.AddString(CCommon::LoadText(IDS_PRESET, std::to_wstring(i + 1).c_str()));
if (style_sel >= 0 && style_sel < TASKBAR_DEFAULT_STYLE_NUM)
combo_box.SetCurSel(style_sel);
else
combo_box.SetCurSel(0);
}
int CAutoAdaptSettingsDlg::GetComboBoxSel(const CComboBox& combo_box)
{
int sel = combo_box.GetCurSel();
if (sel >= 0 && sel < TASKBAR_DEFAULT_STYLE_NUM)
return sel;
else
return 0;
}
void CAutoAdaptSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_DARK_MODE_DEFAULT_STYLE_COMBO, m_dark_mode_default_style_combo);
DDX_Control(pDX, IDC_LIGHT_MODE_DEFAULT_STYLE_COMBO, m_light_mode_default_style_combo);
}
BEGIN_MESSAGE_MAP(CAutoAdaptSettingsDlg, CDialog)
END_MESSAGE_MAP()
// CAutoAdaptSettingsDlg 消息处理程序
BOOL CAutoAdaptSettingsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
InitComboBox(m_dark_mode_default_style_combo, m_data.dark_default_style);
InitComboBox(m_light_mode_default_style_combo, m_data.light_default_style);
CheckDlgButton(IDC_AUTO_SAVE_TO_PRESET_CHECK, m_data.auto_save_taskbar_color_settings_to_preset);
m_toolTip.Create(this);
m_toolTip.SetMaxTipWidth(theApp.DPI(300));
m_toolTip.AddTool(GetDlgItem(IDC_AUTO_SAVE_TO_PRESET_CHECK), CCommon::LoadText(IDS_AUTO_SAVE_TO_PRESET_TIP));
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CAutoAdaptSettingsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//获取控件中的设置
m_data.dark_default_style = GetComboBoxSel(m_dark_mode_default_style_combo);
m_data.light_default_style = GetComboBoxSel(m_light_mode_default_style_combo);
m_data.auto_save_taskbar_color_settings_to_preset = (IsDlgButtonChecked(IDC_AUTO_SAVE_TO_PRESET_CHECK) != 0);
CDialog::OnOK();
}
BOOL CAutoAdaptSettingsDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_MOUSEMOVE)
m_toolTip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
================================================
FILE: TrafficMonitor/CAutoAdaptSettingsDlg.h
================================================
#pragma once
// CAutoAdaptSettingsDlg 对话框
class CAutoAdaptSettingsDlg : public CDialog
{
DECLARE_DYNAMIC(CAutoAdaptSettingsDlg)
public:
CAutoAdaptSettingsDlg(TaskBarSettingData& data, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CAutoAdaptSettingsDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ATUO_ADAPT_SETTING_DIALOG };
#endif
private:
CComboBox m_dark_mode_default_style_combo;
CComboBox m_light_mode_default_style_combo;
TaskBarSettingData& m_data;
CToolTipCtrl m_toolTip;
private:
void InitComboBox(CComboBox& combo_box, int style_sel);
int GetComboBoxSel(const CComboBox& combo_box);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
================================================
FILE: TrafficMonitor/CMFCColorDialogEx.cpp
================================================
// CMFCColorDialogEx.cpp: 实现文件
//
#include "stdafx.h"
#include "CMFCColorDialogEx.h"
#include "Common.h"
// CMFCColorDialogEx
//IMPLEMENT_DYNAMIC(CMFCColorDialogEx, CMFCColorDialog)
CMFCColorDialogEx::CMFCColorDialogEx(COLORREF clrInit, DWORD dwFlags, CWnd* pParentWnd, HPALETTE hPal)
:CMFCColorDialog(clrInit, dwFlags, pParentWnd, hPal)
{
}
CMFCColorDialogEx::~CMFCColorDialogEx()
{
}
BEGIN_MESSAGE_MAP(CMFCColorDialogEx, CMFCColorDialog)
END_MESSAGE_MAP()
// CMFCColorDialogEx 消息处理程序
BOOL CMFCColorDialogEx::OnInitDialog()
{
CMFCColorDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
//设置控件字体
CWnd* pParent = GetParent();
if (pParent != nullptr)
{
CCommon::SetDialogFont(this, pParent->GetFont());
CWnd* pPropSheet = m_pColourSheetOne->GetParent();
if (pPropSheet != nullptr)
CCommon::SetDialogFont(pPropSheet, pParent->GetFont());
//CCommon::SetDialogFont(m_pColourSheetOne, pParent->GetFont());
//CCommon::SetDialogFont(m_pColourSheetTwo, pParent->GetFont());
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
================================================
FILE: TrafficMonitor/CMFCColorDialogEx.h
================================================
#pragma once
// CMFCColorDialogEx
class CMFCColorDialogEx : public CMFCColorDialog
{
//DECLARE_DYNAMIC(CMFCColorDialogEx)
public:
CMFCColorDialogEx(COLORREF clrInit = 0, DWORD dwFlags = 0 /* reserved */, CWnd* pParentWnd = NULL, HPALETTE hPal = NULL);
virtual ~CMFCColorDialogEx();
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL OnInitDialog();
};
================================================
FILE: TrafficMonitor/CPUUsage.cpp
================================================
#include "stdafx.h"
#include "CPUUsage.h"
#include "Common.h"
#include "TrafficMonitor.h"
void CCPUUsage::SetUseCPUTimes(bool use_get_system_times)
{
if (m_use_get_system_times != use_get_system_times)
{
m_use_get_system_times = use_get_system_times;
m_first_get_CPU_utility = true;
}
}
int CCPUUsage::GetCPUUsage()
{
if (m_use_get_system_times)
return GetCPUUsageByGetSystemTimes();
else
return GetCPUUsageByPdh();
}
int CCPUUsage::GetCPUUsageByGetSystemTimes()
{
int cpu_usage{};
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
GetSystemTimes(&idleTime, &kernelTime, &userTime);
__int64 idle = CCommon::CompareFileTime2(m_preidleTime, idleTime);
__int64 kernel = CCommon::CompareFileTime2(m_prekernelTime, kernelTime);
__int64 user = CCommon::CompareFileTime2(m_preuserTime, userTime);
if (kernel + user == 0)
{
cpu_usage = 0;
}
else
{
//ܵʱ-ʱ䣩/ܵʱ=ռcpuʱʹ
cpu_usage = static_cast(abs((kernel + user - idle) * 100 / (kernel + user)));
}
m_preidleTime = idleTime;
m_prekernelTime = kernelTime;
m_preuserTime = userTime;
return cpu_usage;
}
int CCPUUsage::GetCPUUsageByPdh()
{
int cpu_usage{};
HQUERY hQuery;
HCOUNTER hCounter;
DWORD counterType;
PDH_RAW_COUNTER rawData;
PdhOpenQuery(NULL, 0, &hQuery);//ʼѯ
const wchar_t* query_str{};
if (theApp.m_win_version.GetMajorVersion() >= 10)
query_str = L"\\Processor Information(_Total)\\% Processor Utility";
else
query_str = L"\\Processor Information(_Total)\\% Processor Time";
PdhAddCounter(hQuery, query_str, NULL, &hCounter);
PdhCollectQueryData(hQuery);
PdhGetRawCounterValue(hCounter, &counterType, &rawData);
if (m_first_get_CPU_utility) {//ҪݲܼCPUʹ
cpu_usage = 0;
m_first_get_CPU_utility = false;
}
else {
PDH_FMT_COUNTERVALUE fmtValue;
PdhCalculateCounterFromRawValue(hCounter, PDH_FMT_DOUBLE, &rawData, &m_last_rawData, &fmtValue);//ʹ
cpu_usage = fmtValue.doubleValue;//
if (cpu_usage > 100)
cpu_usage = 100;
}
m_last_rawData = rawData;//һ
PdhCloseQuery(hQuery);//رղѯ
return cpu_usage;
}
================================================
FILE: TrafficMonitor/CPUUsage.h
================================================
#pragma once
#include
#include
#pragma comment(lib,"pdh.lib")
class CCPUUsage
{
public:
CCPUUsage()
{}
~CCPUUsage()
{}
void SetUseCPUTimes(bool use_get_system_times); //ûȡCPUʵķʽͨGetSystemTimesPdh
int GetCPUUsage();
private:
int GetCPUUsageByGetSystemTimes();
int GetCPUUsageByPdh();
private:
bool m_use_get_system_times{ true }; //ǷʹGetSysTimeAPIȡCPU
PDH_RAW_COUNTER m_last_rawData;//CPUʹʵһ
bool m_first_get_CPU_utility{ true };
FILETIME m_preidleTime{};
FILETIME m_prekernelTime{};
FILETIME m_preuserTime{};
};
================================================
FILE: TrafficMonitor/CSkinPreviewView.cpp
================================================
// CSkinPreviewView.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "CSkinPreviewView.h"
// CSkinPreviewView
IMPLEMENT_DYNCREATE(CSkinPreviewView, CScrollView)
CSkinPreviewView::CSkinPreviewView()
{
}
CSkinPreviewView::~CSkinPreviewView()
{
}
BEGIN_MESSAGE_MAP(CSkinPreviewView, CScrollView)
END_MESSAGE_MAP()
// CSkinPreviewView 绘图
void CSkinPreviewView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: 计算此视图的合计大小
m_size.cx = 0;
m_size.cy = 0;
SetScrollSizes(MM_TEXT, m_size);
}
void CSkinPreviewView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: 在此添加绘制代码
CRect draw_rect(CPoint(0, 0), m_size);
m_skin_data->DrawPreview(pDC, draw_rect);
}
// CSkinPreviewView 诊断
//#ifdef _DEBUG
//void CSkinPreviewView::AssertValid() const
//{
// CScrollView::AssertValid();
//}
//#ifndef _WIN32_WCE
//void CSkinPreviewView::Dump(CDumpContext& dc) const
//{
// CScrollView::Dump(dc);
//}
//#endif
//#endif //_DEBUG
void CSkinPreviewView::InitialUpdate()
{
OnInitialUpdate();
}
void CSkinPreviewView::SetSize(int width, int hight)
{
m_size = CSize(width, hight);
SetScrollSizes(MM_TEXT, m_size);
}
// CSkinPreviewView 消息处理程序
================================================
FILE: TrafficMonitor/CSkinPreviewView.h
================================================
#pragma once
#include "DrawCommon.h"
#include "SkinFile.h"
// CSkinPreviewView 视图
class CSkinPreviewView : public CScrollView
{
DECLARE_DYNCREATE(CSkinPreviewView)
protected:
CSkinPreviewView(); // 动态创建所使用的受保护的构造函数
virtual ~CSkinPreviewView();
public:
//#ifdef _DEBUG
// virtual void AssertValid() const;
//#ifndef _WIN32_WCE
// virtual void Dump(CDumpContext& dc) const;
//#endif
//#endif
//成员函数
public:
void InitialUpdate();
void SetSize(int width, int hight);
void SetSkinData(CSkinFile* skin_data) { m_skin_data = skin_data; }
//成员变量
protected:
CSize m_size;
CPoint m_start_point; //绘图的起始位置
CSkinFile* m_skin_data;
protected:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
virtual void OnInitialUpdate(); // 构造后的第一次
DECLARE_MESSAGE_MAP()
};
================================================
FILE: TrafficMonitor/CTabCtrlEx.cpp
================================================
// CTabCtrlEx.cpp: 实现文件
//
#include "stdafx.h"
#include "CTabCtrlEx.h"
// CTabCtrlEx
IMPLEMENT_DYNAMIC(CTabCtrlEx, CTabCtrl)
CTabCtrlEx::CTabCtrlEx()
{
}
CTabCtrlEx::~CTabCtrlEx()
{
}
void CTabCtrlEx::AddWindow(CWnd* pWnd, LPCTSTR lable_text)
{
if (pWnd == nullptr || pWnd->GetSafeHwnd() == NULL)
return;
InsertItem(m_tab_list.size(), lable_text, m_tab_list.size());
pWnd->SetParent(this);
pWnd->MoveWindow(m_tab_rect);
m_tab_list.push_back(pWnd);
}
void CTabCtrlEx::SetCurTab(int index)
{
if (index < 0 || index >= static_cast(m_tab_list.size()))
index = 0;
SetCurSel(index);
int tab_size = m_tab_list.size();
for (int i = 0; i < tab_size; i++)
{
if (i == index)
{
m_tab_list[i]->ShowWindow(SW_SHOW);
m_tab_list[i]->SetFocus();
}
else
{
m_tab_list[i]->ShowWindow(SW_HIDE);
}
}
}
CWnd* CTabCtrlEx::GetCurrentTab()
{
int cur_tab_index = GetCurSel();
if (cur_tab_index >= 0 && cur_tab_index < m_tab_list.size())
{
return m_tab_list[cur_tab_index];
}
return nullptr;
}
void CTabCtrlEx::AdjustTabWindowSize()
{
CalSubWindowSize();
for (size_t i{}; i < m_tab_list.size(); i++)
{
m_tab_list[i]->MoveWindow(m_tab_rect);
}
}
void CTabCtrlEx::CalSubWindowSize()
{
GetClientRect(m_tab_rect);
CRect rc_temp = m_tab_rect;
AdjustRect(FALSE, rc_temp);
int margin = rc_temp.left - m_tab_rect.left;
CRect rcTabItem;
GetItemRect(0, rcTabItem);
m_tab_rect.top += rcTabItem.Height() + margin;
m_tab_rect.left += margin;
m_tab_rect.bottom -= margin;
m_tab_rect.right -= margin;
}
BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
ON_NOTIFY_REFLECT(TCN_SELCHANGE, &CTabCtrlEx::OnTcnSelchange)
ON_WM_SIZE()
END_MESSAGE_MAP()
// CTabCtrlEx 消息处理程序
void CTabCtrlEx::OnTcnSelchange(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
int tab_selected = GetCurSel();
SetCurTab(tab_selected);
*pResult = 0;
}
void CTabCtrlEx::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
//计算子窗口的位置
CalSubWindowSize();
CTabCtrl::PreSubclassWindow();
}
void CTabCtrlEx::OnSize(UINT nType, int cx, int cy)
{
CTabCtrl::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
AdjustTabWindowSize();
}
================================================
FILE: TrafficMonitor/CTabCtrlEx.h
================================================
#pragma once
// CTabCtrlEx
class CTabCtrlEx : public CTabCtrl
{
DECLARE_DYNAMIC(CTabCtrlEx)
public:
CTabCtrlEx();
virtual ~CTabCtrlEx();
void AddWindow(CWnd* pWnd, LPCTSTR lable_text); //ǰtabؼһӴ
void SetCurTab(int index);
CWnd* GetCurrentTab();
void AdjustTabWindowSize();
protected:
void CalSubWindowSize();
DECLARE_MESSAGE_MAP()
protected:
vector m_tab_list; //tabؼÿӴڵָ
public:
afx_msg void OnTcnSelchange(NMHDR *pNMHDR, LRESULT *pResult);
virtual void PreSubclassWindow();
CRect m_tab_rect;
afx_msg void OnSize(UINT nType, int cx, int cy);
};
================================================
FILE: TrafficMonitor/CVariant.cpp
================================================
#include "stdafx.h"
#include "CVariant.h"
CVariant::CVariant(int value)
{
m_value_int = value;
m_type = eType::INT;
}
CVariant::CVariant(size_t value)
{
m_value_int = static_cast(value);
m_type = eType::UINT;
}
CVariant::CVariant(double value)
{
m_value_double = value;
m_type = eType::DOUBLE;
}
CVariant::CVariant(LPCTSTR value)
{
m_value_string = value;
m_type = eType::STRING;
}
CVariant::CVariant(const CString& value)
{
m_value_string = value;
m_type = eType::STRING;
}
CVariant::CVariant(const wstring & value)
{
m_value_string = value.c_str();
m_type = eType::STRING;
}
CVariant::~CVariant()
{
}
CString CVariant::ToString() const
{
CString str;
switch (m_type)
{
case CVariant::eType::INT:
str.Format(_T("%d"), m_value_int);
break;
case eType::UINT:
str.Format(_T("%u"), static_cast(m_value_int));
break;
case CVariant::eType::DOUBLE:
str.Format(_T("%g"), m_value_double);
break;
case CVariant::eType::STRING:
str = m_value_string;
break;
default:
break;
}
return str;
}
================================================
FILE: TrafficMonitor/CVariant.h
================================================
#pragma once
class CVariant
{
public:
CVariant(int value);
CVariant(size_t value);
CVariant(double value);
CVariant(LPCTSTR value);
CVariant(const CString& value);
CVariant(const wstring& value);
~CVariant();
CString ToString() const;
private:
enum class eType { INT, UINT, DOUBLE, STRING };
int m_value_int;
double m_value_double;
CString m_value_string;
eType m_type;
};
================================================
FILE: TrafficMonitor/CalendarHelper.cpp
================================================
#include "stdafx.h"
#include "CalendarHelper.h"
CCalendarHelper::CCalendarHelper()
{
}
CCalendarHelper::~CCalendarHelper()
{
}
bool CCalendarHelper::IsLeapYear(int year)
{
return ((0 == year % 4 && 0 != year % 100) || 0 == year % 400);
}
int CCalendarHelper::CaculateWeekDay(int y, int m, int d)
{
if (m <= 2)
{
m += 12;
y--;
}
return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
}
int CCalendarHelper::DaysInMonth(int year, int month)
{
bool leap{ IsLeapYear(year) };
switch (month)
{
case 2:
if (leap) return 29;
else return 28;
case 4: case 6: case 9: case 11:
return 30;
default:
return 31;
}
}
void CCalendarHelper::GetCalendar(int year, int month, DayTraffic calendar[CALENDAR_HEIGHT][CALENDAR_WIDTH], bool sunday_first)
{
memset(calendar, 0, sizeof(DayTraffic)*CALENDAR_HEIGHT*CALENDAR_WIDTH);
int days{ DaysInMonth(year, month) };
int first_weak_day{ CaculateWeekDay(year, month, 1) };
if(!sunday_first)
{
first_weak_day--;
if (first_weak_day < 0)
first_weak_day = 6;
}
int i{}, j{};
for (int n{}; n < 37; n++)
{
if (n < first_weak_day)
{
calendar[i][j].day = 0;
}
else
{
int day = n - first_weak_day + 1;
if (day <= days)
calendar[i][j].day = day;
}
j++;
if (j >= 7)
{
j = 0;
i++;
}
}
}
================================================
FILE: TrafficMonitor/CalendarHelper.h
================================================
#pragma once
#define CALENDAR_WIDTH 7
#define CALENDAR_HEIGHT 6
struct DayTraffic
{
int day;
__int64 up_traffic;
__int64 down_traffic;
bool mixed;
CRect rect;
__int64 traffic() const
{
return up_traffic + down_traffic;
}
};
class CCalendarHelper
{
public:
CCalendarHelper();
~CCalendarHelper();
//ǷΪ
static bool IsLeapYear(int year);
//ڼ(0~6~)
static int CaculateWeekDay(int y, int m, int d);
//һж
static int DaysInMonth(int year, int month);
//ȡָ·ݵݣcalendar
//sunday_firstΪtrueΪÿܵĵһ죬һΪÿܵĵһ
static void GetCalendar(int year, int month, DayTraffic calendar[CALENDAR_HEIGHT][CALENDAR_WIDTH], bool sunday_first = true);
};
================================================
FILE: TrafficMonitor/ColorSettingListCtrl.cpp
================================================
#include "stdafx.h"
#include "ColorSettingListCtrl.h"
IMPLEMENT_DYNAMIC(CColorSettingListCtrl, CListCtrl)
CColorSettingListCtrl::CColorSettingListCtrl()
{
}
CColorSettingListCtrl::~CColorSettingListCtrl()
{
}
void CColorSettingListCtrl::SetItemColor(int row, int col, COLORREF color)
{
m_colors[row][col] = color;
}
COLORREF CColorSettingListCtrl::GetItemColor(int row, int col)
{
return m_colors[row][col];
}
BEGIN_MESSAGE_MAP(CColorSettingListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CColorSettingListCtrl::OnNMCustomdraw)
END_MESSAGE_MAP()
void CColorSettingListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
*pResult = CDRF_DODEFAULT;
LPNMLVCUSTOMDRAW lplvdr = reinterpret_cast(pNMHDR);
NMCUSTOMDRAW& nmcd = lplvdr->nmcd;
switch (lplvdr->nmcd.dwDrawStage) //判断状态
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT: //如果为画ITEM之前就要进行颜色的改变
if (nmcd.dwItemSpec >= 0 && nmcd.dwItemSpec < GetItemCount())
{
//double range = m_item_rage_data[nmcd.dwItemSpec].data_value;
//CDC* pDC = CDC::FromHandle(nmcd.hdc); //获取绘图DC
//CRect item_rect, draw_rect;
//GetSubItemRect(nmcd.dwItemSpec,m_draw_item_range_row, LVIR_BOUNDS, item_rect); //获取绘图单元格的矩形区域
//CDrawCommon::SetDrawRect(pDC, item_rect); //设置绘图区域为当前列
//draw_rect = item_rect;
//if (draw_rect.Height() > 2 * m_margin)
//{
// draw_rect.top += m_margin;
// draw_rect.bottom -= m_margin;
//}
//int width;
//if (m_use_log_scale) //使用对数比例(y=ln(x+1))
//{
// range = std::log(range + 1);
// width = static_cast(range*draw_rect.Width() / std::log(1000 + 1));
//}
//else //使用线性比例(y=x)
//{
// width = static_cast(range*draw_rect.Width() / 1000);
//}
//draw_rect.right = draw_rect.left + width;
//pDC->FillSolidRect(draw_rect, m_item_rage_data[nmcd.dwItemSpec].color);
////当前列绘制完成后将绘图区域设置为左边的区域,防止当前列的区域被覆盖
//CRect rect1{ item_rect };
//rect1.left = 0;
//rect1.right = item_rect.left;
//CDrawCommon::SetDrawRect(pDC, rect1);
CDC* pDC = CDC::FromHandle(nmcd.hdc); //获取绘图DC
CRect item_rect;
auto& col_color_map = m_colors[nmcd.dwItemSpec];
for (auto iter = col_color_map.begin(); iter != col_color_map.end(); ++iter)
{
GetSubItemRect(nmcd.dwItemSpec, iter->first, LVIR_BOUNDS, item_rect); //获取绘图单元格的矩形区域
//设置绘图剪辑区域
CDrawCommon::SetDrawRect(pDC, item_rect);
//使用双缓冲绘图
CDrawDoubleBuffer draw_double_buffer(pDC, item_rect);
//填充背景
item_rect.MoveToXY(0, 0);
draw_double_buffer.GetMemDC()->FillSolidRect(item_rect, GetSysColor(COLOR_WINDOW));
//绘制颜色矩形
item_rect.DeflateRect(m_margin, m_margin);
draw_double_buffer.GetMemDC()->FillSolidRect(item_rect, iter->second);
//绘制矩形边框
CDrawCommon drawer;
drawer.Create(draw_double_buffer.GetMemDC(), this);
drawer.DrawRectOutLine(item_rect, RGB(192, 192, 192));
}
//当前列绘制完成后将绘图区域设置为第一列的区域,防止颜色列的区域被覆盖
CRect rect1{};
GetSubItemRect(nmcd.dwItemSpec, 1, LVIR_BOUNDS, rect1); //获取第1列单元格的矩形区域
rect1.right = rect1.left;
rect1.left = 0;
CDrawCommon::SetDrawRect(pDC, rect1);
}
*pResult = CDRF_DODEFAULT;
break;
}
}
================================================
FILE: TrafficMonitor/ColorSettingListCtrl.h
================================================
#pragma once
#include "afxcmn.h"
#include "DrawCommon.h"
class CColorSettingListCtrl :
public CListCtrl
{
DECLARE_DYNAMIC(CColorSettingListCtrl)
public:
CColorSettingListCtrl();
~CColorSettingListCtrl();
void SetItemColor(int row, int col, COLORREF color);
COLORREF GetItemColor(int row, int col);
void SetDrawItemRangMargin(int margin) { m_margin = margin; } //ûƻƵľεıԵԪ߿ľΣֵԽƵľԽϸDzܳбоһ
protected:
int m_margin{};
std::map> m_colors; //ڱÿһÿһеɫ
DECLARE_MESSAGE_MAP()
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
};
================================================
FILE: TrafficMonitor/ColorStatic.cpp
================================================
// ColorStatic.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "ColorStatic.h"
#include "DrawCommon.h"
// CColorStatic
IMPLEMENT_DYNAMIC(CColorStatic, CStatic)
CColorStatic::CColorStatic()
{
}
CColorStatic::~CColorStatic()
{
}
void CColorStatic::SetFillColor(COLORREF fill_color)
{
m_colors.resize(1);
m_colors[0] = fill_color;
//m_fill_color = fill_color;
Invalidate();
}
void CColorStatic::SetColorNum(int color_num)
{
if (color_num <= 0 || color_num > 32)
color_num = 1;
m_colors.resize(color_num);
}
void CColorStatic::SetFillColor(int index, COLORREF fill_color)
{
if (index >= 0 && index < static_cast(m_colors.size()))
m_colors[index] = fill_color;
}
void CColorStatic::SetLinkCursor(bool link_cursor)
{
m_link_cursor = link_cursor;
}
void CColorStatic::EnableWindow(bool enable)
{
CStatic::EnableWindow(enable);
Invalidate(FALSE);
}
BEGIN_MESSAGE_MAP(CColorStatic, CStatic)
ON_WM_PAINT()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
// CColorStatic 消息处理程序
void CColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CStatic::OnPaint()
CRect rect;
GetClientRect(rect);
rect.MoveToXY(0, 0);
CRect rc_tmp{ rect };
CDrawCommon draw;
draw.Create(&dc, this);
int color_num = static_cast(m_colors.size());
if (IsWindowEnabled() && color_num > 0)
{
switch (color_num)
{
case 1:
dc.FillSolidRect(rect, m_colors[0]);
break;
case 4:
dc.FillSolidRect(rect, RGB(255,255,255));
rc_tmp.right /= 2;
rc_tmp.bottom /= 2;
dc.FillSolidRect(rc_tmp, m_colors[0]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[1]);
rc_tmp.MoveToXY(0, rc_tmp.bottom);
dc.FillSolidRect(rc_tmp, m_colors[2]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[3]);
break;
case 8:
dc.FillSolidRect(rect, RGB(255, 255, 255));
rc_tmp.right /= (color_num / 2);
rc_tmp.bottom /= 2;
dc.FillSolidRect(rc_tmp, m_colors[0]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[1]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[4]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[5]);
rc_tmp.MoveToXY(0, rc_tmp.bottom);
dc.FillSolidRect(rc_tmp, m_colors[2]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[3]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[6]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[7]);
break;
case 16:
dc.FillSolidRect(rect, RGB(255, 255, 255));
rc_tmp.right /= color_num / 2;
rc_tmp.bottom /= 2;
dc.FillSolidRect(rc_tmp, m_colors[0]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[1]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[4]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[5]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[8]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[9]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[12]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[13]);
rc_tmp.MoveToXY(0, rc_tmp.bottom);
dc.FillSolidRect(rc_tmp, m_colors[2]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[3]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[6]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[7]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[10]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[11]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[14]);
rc_tmp.MoveToX(rc_tmp.right);
dc.FillSolidRect(rc_tmp, m_colors[15]);
break;
default:
dc.FillSolidRect(rect, RGB(255, 255, 255));
rc_tmp.right = rect.Width() / color_num;
for (int i{}; i < color_num; i++)
{
rc_tmp.MoveToX(i*(rect.Width() / color_num));
dc.FillSolidRect(rc_tmp, m_colors[i]);
}
}
//画边框
draw.DrawRectOutLine(rect, RGB(160, 160, 160));
}
else
{
CBrush brush(HS_BDIAGONAL, RGB(160, 160, 160));
dc.FillRect(rect, &brush);
draw.DrawRectOutLine(rect, RGB(192, 192, 192));
}
}
void CColorStatic::OnMouseHover(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_hover = true;
CStatic::OnMouseHover(nFlags, point);
}
void CColorStatic::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_hover = false;
CStatic::OnMouseLeave();
}
BOOL CColorStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_link_cursor && m_hover)
{
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(32649)));
return TRUE;
}
return CStatic::OnSetCursor(pWnd, nHitTest, message);
}
void CColorStatic::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
ModifyStyleEx(WS_EX_STATICEDGE, NULL);
CStatic::PreSubclassWindow();
}
void CColorStatic::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_link_cursor)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 1;
_TrackMouseEvent(&tme);
}
CStatic::OnMouseMove(nFlags, point);
}
void CColorStatic::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//如果单击了鼠标左键,则向父窗口发送一个WM_STATIC_CLICKED消息
CWnd* pParent{ GetParent() };
if (pParent != nullptr)
pParent->SendMessage(WM_STATIC_CLICKED, (WPARAM)this);
CStatic::OnLButtonUp(nFlags, point);
}
================================================
FILE: TrafficMonitor/ColorStatic.h
================================================
// CColorStatic
//派生自CStatic,用于显示颜色的Static控件
#pragma once
#define WM_STATIC_CLICKED (WM_USER + 1001)
class CColorStatic : public CStatic
{
DECLARE_DYNAMIC(CColorStatic)
public:
CColorStatic();
virtual ~CColorStatic();
void SetFillColor(COLORREF fill_color); //设置要填充单一的背景色
void SetColorNum(int color_num); //设置颜色的数量
void SetFillColor(int index, COLORREF fill_color);
void SetLinkCursor(bool link_cursor = true); //设置指向控件时光标变成超链接形状
void EnableWindow(bool enable);
protected:
//COLORREF m_fill_color{ RGB(255, 255,255) };
vector m_colors;
bool m_hover{ false }; //当鼠标指向控件时为true
bool m_link_cursor{ false }; //是否启用超链接形状的光标
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
virtual void PreSubclassWindow();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
================================================
FILE: TrafficMonitor/ComboBox2.cpp
================================================
// ComboBox2.cpp: 实现文件
//
#include "stdafx.h"
#include "ComboBox2.h"
// CComboBox2
IMPLEMENT_DYNAMIC(CComboBox2, CComboBox)
CComboBox2::CComboBox2()
{
}
CComboBox2::~CComboBox2()
{
}
void CComboBox2::SetMouseWheelEnable(bool enable)
{
m_mouse_wheel_enable = enable;
}
BEGIN_MESSAGE_MAP(CComboBox2, CComboBox)
END_MESSAGE_MAP()
// CComboBox2 消息处理程序
BOOL CComboBox2::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
//如果m_mouse_wheel_enable为false,则不响应鼠标滚轮消息
if (pMsg->message == WM_MOUSEWHEEL && !m_mouse_wheel_enable)
{
//将鼠标滚轮消息发送给父窗口
CWnd* pParent = GetParent();
pParent->SendMessage(WM_MOUSEWHEEL, pMsg->wParam, pMsg->lParam);
return true;
}
return CComboBox::PreTranslateMessage(pMsg);
}
================================================
FILE: TrafficMonitor/ComboBox2.h
================================================
#pragma once
// CComboBox2
class CComboBox2 : public CComboBox
{
DECLARE_DYNAMIC(CComboBox2)
public:
CComboBox2();
virtual ~CComboBox2();
void SetMouseWheelEnable(bool enable); //设置是否允许响应鼠标滚轮
private:
bool m_mouse_wheel_enable{ true };
protected:
DECLARE_MESSAGE_MAP()
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
================================================
FILE: TrafficMonitor/Common.cpp
================================================
#include "stdafx.h"
#include "Common.h"
#include "TrafficMonitor.h"
CCommon::CCommon()
{
}
CCommon::~CCommon()
{
}
wstring CCommon::StrToUnicode(const char* str, bool utf8)
{
wstring result;
int size;
size = MultiByteToWideChar((utf8 ? CP_UTF8 : CP_ACP), 0, str, -1, NULL, 0);
if (size <= 0) return wstring();
wchar_t* str_unicode = new wchar_t[size + 1];
MultiByteToWideChar((utf8 ? CP_UTF8 : CP_ACP), 0, str, -1, str_unicode, size);
result.assign(str_unicode);
delete[] str_unicode;
return result;
}
string CCommon::UnicodeToStr(const wchar_t* wstr, bool utf8)
{
string result;
int size{ 0 };
size = WideCharToMultiByte((utf8 ? CP_UTF8 : CP_ACP), 0, wstr, -1, NULL, 0, NULL, NULL);
if (size <= 0) return string();
char* str = new char[size + 1];
WideCharToMultiByte((utf8 ? CP_UTF8 : CP_ACP), 0, wstr, -1, str, size, NULL, NULL);
result.assign(str);
delete[] str;
return result;
}
bool CCommon::GetFileContent(const wchar_t* file_path, string& contents_buff, bool binary /*= true*/)
{
std::ifstream file{ file_path, (binary ? std::ios::binary : std::ios::in) };
if (file.fail())
return false;
//获取文件长度
file.seekg(0, file.end);
size_t length = file.tellg();
file.seekg(0, file.beg);
char* buff = new char[length];
file.read(buff, length);
file.close();
contents_buff.assign(buff, length);
delete[] buff;
return true;
}
const char* CCommon::GetFileContent(const wchar_t* file_path, size_t& length, bool binary /*= true*/)
{
std::ifstream file{ file_path, (binary ? std::ios::binary : std::ios::in) };
length = 0;
if (file.fail())
return nullptr;
//获取文件长度
file.seekg(0, file.end);
length = file.tellg();
file.seekg(0, file.beg);
char* buff = new char[length];
file.read(buff, length);
file.close();
return buff;
}
CString CCommon::DataSizeToString(unsigned long long size, const PublicSettingData& cfg)
{
//CString str;
CString value_str, unit_str;
if (!cfg.unit_byte) //如果使用比特(bit)为单位,则数值乘以8
{
size *= 8;
}
switch (cfg.speed_unit)
{
case SpeedUnit::AUTO:
if (cfg.speed_short_mode)
{
if (size < 1024 * 10) //10KB以下以KB为单位,保留1位小数
{
value_str.Format(_T("%.1f"), size / 1024.0f);
unit_str = _T("K");
}
else if (size < 1024 * 1000) //1000KB以下以KB为单位,保留整数
{
value_str.Format(_T("%.0f"), size / 1024.0f);
unit_str = _T("K");
}
else if (size < 1024 * 1024 * 1000) //1000MB以下以MB为单位,保留1位小数
{
value_str.Format(_T("%.1f"), size / 1024.0f / 1024.0f);
unit_str = _T("M");
}
else
{
value_str.Format(_T("%.2f"), size / 1024.0f / 1024.0f / 1024.0f);
unit_str = _T("G");
}
}
else
{
if (size < 1024 * 10) //10KB以下以KB为单位,保留2位小数
{
value_str.Format(_T("%.2f"), size / 1024.0f);
unit_str = _T("KB");
}
else if (size < 1024 * 1000) //1000KB以下以KB为单位,保留1位小数
{
value_str.Format(_T("%.1f"), size / 1024.0f);
unit_str = _T("KB");
}
else if (size < 1024 * 1024 * 1000) //1000MB以下以MB为单位,保留2位小数
{
value_str.Format(_T("%.2f"), size / 1024.0f / 1024.0f);
unit_str = _T("MB");
}
else
{
value_str.Format(_T("%.2f"), size / 1024.0f / 1024.0f / 1024.0f);
unit_str = _T("GB");
}
}
break;
case SpeedUnit::KBPS:
if (cfg.speed_short_mode)
{
if (size < 1024 * 10) //10KB以下保留1位小数
value_str.Format(_T("%.1f"), size / 1024.0f);
else //10KB以上保留整数
value_str.Format(_T("%.0f"), size / 1024.0f);
if (!cfg.hide_unit)
unit_str = _T("K");
}
else
{
if (size < 1024 * 10) //10KB以下保留2位小数
value_str.Format(_T("%.2f"), size / 1024.0f);
else //10KB以上保留1位小数
value_str.Format(_T("%.1f"), size / 1024.0f);
if (!cfg.hide_unit)
unit_str = _T("KB");
}
break;
case SpeedUnit::MBPS:
if (cfg.speed_short_mode)
{
value_str.Format(_T("%.1f"), size / 1024.0f / 1024.0f);
if (!cfg.hide_unit)
unit_str = _T("M");
}
else
{
value_str.Format(_T("%.2f"), size / 1024.0f / 1024.0f);
if (!cfg.hide_unit)
unit_str = _T("MB");
}
break;
}
CString str;
if (cfg.separate_value_unit_with_space && !cfg.hide_unit)
str = value_str + _T(' ') + unit_str;
else
str = value_str + unit_str;
if (!cfg.unit_byte)
{
if (cfg.speed_short_mode && !cfg.hide_unit)
str += _T('b'); //如果使用比特(bit)为单位,即使设置了网速简洁模式,也将“b”显示出来
else
str.Replace(_T('B'), _T('b')); //如果使用比特(bit)为单位,将B替换成b
}
return str;
}
CString CCommon::DataSizeToString(unsigned long long size, bool with_space)
{
CString str;
if (size < 1024 * 10) //10KB以下以KB为单位,保留2位小数
str.Format(_T("%.2f KB"), size / 1024.0);
else if (size < 1024 * 1024) //1MB以下以KB为单位,保留1位小数
str.Format(_T("%.1f KB"), size / 1024.0);
else if (size < 1024 * 1024 * 1024) //1GB以下以MB为单位,保留2位小数
str.Format(_T("%.2f MB"), size / 1024.0 / 1024.0);
else if (size < 1024ll * 1024 * 1024 * 1024)
str.Format(_T("%.2f GB"), size / 1024.0 / 1024.0 / 1024.0);
else
str.Format(_T("%.2f TB"), size / 1024.0 / 1024.0 / 1024.0 / 1024.0);
if (!with_space)
str.Remove(_T(' '));
return str;
}
CString CCommon::TemperatureToString(float temperature, const PublicSettingData& cfg)
{
CString str_val;
if (temperature <= 0)
str_val = _T("--");
else
str_val.Format(_T("%d"), static_cast(temperature));
if (cfg.separate_value_unit_with_space)
str_val += _T(' ');
str_val += _T("°C");
return str_val;
}
CString CCommon::UsageToString(int usage, const PublicSettingData& cfg)
{
CString str_val;
if (usage < 0)
str_val = _T("--");
else
str_val.Format(_T("%d"), usage);
if (!cfg.hide_percent)
{
if (cfg.separate_value_unit_with_space)
str_val += _T(' ');
str_val += _T('%');
}
return str_val;
}
CString CCommon::FreqToString(float freq, const PublicSettingData& cfg)
{
CString str_val;
if (freq < 0)
str_val = _T("--");
else
str_val.Format(_T("%.2f GHz"), freq);
return str_val;
}
//CString CCommon::KBytesToString(unsigned int kb_size)
//{
// CString k_bytes_str;
// if (kb_size < 1024)
// k_bytes_str.Format(_T("%d KB"), kb_size);
// else if (kb_size < 1024 * 1024)
// k_bytes_str.Format(_T("%.2f MB"), kb_size / 1024.0);
// else if (kb_size < 1024 * 1024 * 1024)
// k_bytes_str.Format(_T("%.2f GB"), kb_size / 1024.0 / 1024.0);
// else
// k_bytes_str.Format(_T("%.2f TB"), kb_size / 1024.0 / 1024.0 / 1024.0);
// return k_bytes_str;
//}
CString CCommon::KBytesToString(unsigned __int64 kb_size)
{
CString k_bytes_str;
if (kb_size < 1024)
k_bytes_str.Format(_T("%d KB"), kb_size);
else if (kb_size < 1024 * 1024)
k_bytes_str.Format(_T("%.2f MB"), kb_size / 1024.0);
else if (kb_size < 1024 * 1024 * 1024)
k_bytes_str.Format(_T("%.2f GB"), kb_size / 1024.0 / 1024.0);
else
k_bytes_str.Format(_T("%.2f TB"), kb_size / 1024.0 / 1024.0 / 1024.0);
return k_bytes_str;
}
__int64 CCommon::CompareFileTime2(FILETIME time1, FILETIME time2)
{
__int64 a = static_cast<__int64>(time1.dwHighDateTime) << 32 | time1.dwLowDateTime;
__int64 b = static_cast<__int64>(time2.dwHighDateTime) << 32 | time2.dwLowDateTime;
return b - a;
}
void CCommon::WriteLog(const char* str_text, LPCTSTR file_path)
{
SYSTEMTIME cur_time;
GetLocalTime(&cur_time);
char buff[32];
sprintf_s(buff, "%d/%.2d/%.2d %.2d:%.2d:%.2d.%.3d: ", cur_time.wYear, cur_time.wMonth, cur_time.wDay,
cur_time.wHour, cur_time.wMinute, cur_time.wSecond, cur_time.wMilliseconds);
ofstream file{ file_path, std::ios::app }; //以追加的方式打开日志文件
file << buff;
file << str_text << std::endl;
}
void CCommon::WriteLog(const wchar_t* str_text, LPCTSTR file_path)
{
SYSTEMTIME cur_time;
GetLocalTime(&cur_time);
char buff[32];
sprintf_s(buff, "%d/%.2d/%.2d %.2d:%.2d:%.2d.%.3d: ", cur_time.wYear, cur_time.wMonth, cur_time.wDay,
cur_time.wHour, cur_time.wMinute, cur_time.wSecond, cur_time.wMilliseconds);
ofstream file{ file_path, std::ios::app }; //以追加的方式打开日志文件
file << buff;
file << UnicodeToStr(str_text).c_str() << std::endl;
}
BOOL CCommon::CreateFileShortcut(LPCTSTR lpszLnkFileDir, LPCTSTR lpszFileName, LPCTSTR lpszLnkFileName, LPCTSTR lpszWorkDir, WORD wHotkey, LPCTSTR lpszDescription, int iShowCmd)
{
if (lpszLnkFileDir == NULL)
return FALSE;
HRESULT hr;
IShellLink* pLink; //IShellLink对象指针
IPersistFile* ppf; //IPersisFil对象指针
//创建IShellLink对象
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink);
if (FAILED(hr))
return FALSE;
//从IShellLink对象中获取IPersistFile接口
hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (FAILED(hr))
{
pLink->Release();
return FALSE;
}
TCHAR file_path[MAX_PATH];
GetModuleFileName(NULL, file_path, MAX_PATH);
//目标
if (lpszFileName == NULL)
pLink->SetPath(file_path);
else
pLink->SetPath(lpszFileName);
//工作目录
if (lpszWorkDir != NULL)
{
pLink->SetWorkingDirectory(lpszWorkDir);
}
else
{
//设置工作目录为快捷方式目标所在位置
TCHAR workDirBuf[MAX_PATH]{};
if (lpszFileName == NULL)
//wcscpy_s(workDirBuf, file_path);
WStringCopy(workDirBuf, 260, file_path, 260);
else
//wcscpy_s(workDirBuf, lpszFileName);
WStringCopy(workDirBuf, 260, lpszFileName);
LPTSTR pstr = wcsrchr(workDirBuf, _T('\\'));
*pstr = _T('\0');
pLink->SetWorkingDirectory(workDirBuf);
}
//快捷键
if (wHotkey != 0)
pLink->SetHotkey(wHotkey);
//备注
if (lpszDescription != NULL)
pLink->SetDescription(lpszDescription);
//显示方式
pLink->SetShowCmd(iShowCmd);
//快捷方式的路径 + 名称
wchar_t szBuffer[MAX_PATH];
if (lpszLnkFileName != NULL) //指定了快捷方式的名称
swprintf_s(szBuffer, L"%s\\%s", lpszLnkFileDir, lpszLnkFileName);
else
{
//没有指定名称,就从取指定文件的文件名作为快捷方式名称。
const wchar_t* pstr;
if (lpszFileName != NULL)
pstr = wcsrchr(lpszFileName, L'\\');
else
pstr = wcsrchr(file_path, L'\\');
if (pstr == NULL)
{
ppf->Release();
pLink->Release();
return FALSE;
}
//注意后缀名要从.exe改为.lnk
swprintf_s(szBuffer, L"%s\\%s", lpszLnkFileDir, pstr);
int nLen = wcslen(szBuffer);
szBuffer[nLen - 3] = L'l';
szBuffer[nLen - 2] = L'n';
szBuffer[nLen - 1] = L'k';
}
//保存快捷方式到指定目录下
//WCHAR wsz[MAX_PATH]; //定义Unicode字符串
//MultiByteToWideChar(CP_ACP, 0, szBuffer, -1, wsz, MAX_PATH);
hr = ppf->Save(szBuffer, TRUE);
ppf->Release();
pLink->Release();
return SUCCEEDED(hr);
}
wstring CCommon::GetStartUpPath()
{
LPITEMIDLIST ppidl;
TCHAR pszStartUpPath[MAX_PATH]{};
if (SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &ppidl) == S_OK)
{
SHGetPathFromIDList(ppidl, pszStartUpPath);
CoTaskMemFree(ppidl);
}
return wstring(pszStartUpPath);
}
void CCommon::GetFiles(const wchar_t* path, vector& files)
{
//文件句柄
intptr_t hFile = 0;
//文件信息(用Unicode保存使用_wfinddata_t,多字节字符集使用_finddata_t)
_wfinddata_t fileinfo;
wstring file_name;
if ((hFile = _wfindfirst(path, &fileinfo)) != -1)
{
do
{
file_name.assign(fileinfo.name);
if (file_name != L"." && file_name != L"..")
//files.push_back(wstring(path) + L"\\" + file_name); //将文件名保存(忽略"."和"..")
files.push_back(L"\\" + file_name); //将文件名保存(忽略"."和"..")
} while (_wfindnext(hFile, &fileinfo) == 0);
}
_findclose(hFile);
}
void CCommon::GetFiles(const wchar_t* path, std::function func)
{
//文件句柄
intptr_t hFile = 0;
_wfinddata_t fileinfo;
wstring file_name;
if ((hFile = _wfindfirst(path, &fileinfo)) != -1)
{
do
{
file_name.assign(fileinfo.name);
if (file_name != L"." && file_name != L"..")
func(file_name);
} while (_wfindnext(hFile, &fileinfo) == 0);
}
_findclose(hFile);
}
bool CCommon::FileExist(LPCTSTR file_name)
{
return (PathFileExists(file_name) != 0);
}
bool CCommon::IsFolder(const wstring& path)
{
DWORD dwAttrib = GetFileAttributes(path.c_str());
return (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
bool CCommon::MoveAFile(LPCTSTR exist_file, LPCTSTR new_file)
{
if (!FileExist(exist_file))
return false;
//if (FileExist(new_file)) //如果目标文件已经存在,则先删除它
// DeleteFile(new_file);
return (MoveFile(exist_file, new_file) != 0);
}
SYSTEMTIME CCommon::CompareSystemTime(SYSTEMTIME a, SYSTEMTIME b)
{
SYSTEMTIME result{};
short hour = a.wHour - b.wHour;
short minute = a.wMinute - b.wMinute;
short second = a.wSecond - b.wSecond;
if (second < 0)
{
second += 60;
minute--;
}
if (minute < 0)
{
minute += 60;
hour--;
}
if (hour < 0)
{
hour += 24;
}
result.wHour = hour;
result.wMinute = minute;
result.wSecond = second;
return result;
}
wstring CCommon::GetModuleDir()
{
wchar_t path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
size_t index;
wstring current_path{ path };
index = current_path.find_last_of(L'\\');
current_path = current_path.substr(0, index + 1);
return current_path;
}
wstring CCommon::GetSystemDir()
{
wchar_t buff[MAX_PATH];
GetSystemDirectory(buff, MAX_PATH);
return wstring(buff);
}
wstring CCommon::GetTemplateDir()
{
wstring result;
wchar_t buff[MAX_PATH];
GetTempPath(MAX_PATH, buff); //获取临时文件夹的路径
result = buff;
if (result.back() != L'\\' && result.back() != L'/') //确保路径后面有斜杠
result.push_back(L'\\');
return result;
}
wstring CCommon::GetAppDataConfigDir()
{
LPITEMIDLIST ppidl;
TCHAR pszAppDataPath[MAX_PATH];
if (SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &ppidl) == S_OK)
{
SHGetPathFromIDList(ppidl, pszAppDataPath);
CoTaskMemFree(ppidl);
}
wstring app_data_path{ pszAppDataPath }; //获取到C:/User/用户名/AppData/Roaming路径
CreateDirectory(app_data_path.c_str(), NULL); //如果Roaming不存在,则创建它
app_data_path += L"\\TrafficMonitor\\";
CreateDirectory(app_data_path.c_str(), NULL); //如果C:/User/用户名/AppData/Roaming/TrafficMonitor不存在,则创建它
return app_data_path;
}
void CCommon::DrawWindowText(CDC* pDC, CRect rect, LPCTSTR lpszString, COLORREF color, COLORREF back_color)
{
pDC->SetTextColor(color);
//m_pDC->SetBkMode(TRANSPARENT);
//用背景色填充矩形区域
pDC->FillSolidRect(rect, back_color);
pDC->DrawText(lpszString, rect, DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
}
//void CCommon::SetDrawArea(CDC * pDC, CRect rect)
//{
// CRgn rgn;
// rgn.CreateRectRgnIndirect(rect);
// pDC->SelectClipRgn(&rgn);
//}
bool CCommon::IsForegroundFullscreen()
{
bool bFullscreen{ false }; //用于指示前台窗口是否是全屏
HWND hWnd;
RECT rcApp;
RECT rcDesk;
hWnd = GetForegroundWindow(); //获取当前正在与用户交互的前台窗口句柄
TCHAR buff[256];
GetClassName(hWnd, buff, 256); //获取前台窗口的类名
CString class_name{ buff };
if (hWnd != GetDesktopWindow() && class_name != _T("WorkerW") && hWnd != GetShellWindow())//如果前台窗口不是桌面窗口,也不是控制台窗口
{
GetWindowRect(hWnd, &rcApp); //获取前台窗口的坐标
GetWindowRect(GetDesktopWindow(), &rcDesk); //根据桌面窗口句柄,获取整个屏幕的坐标
if (rcApp.left <= rcDesk.left && //如果前台窗口的坐标完全覆盖住桌面窗口,就表示前台窗口是全屏的
rcApp.top <= rcDesk.top &&
rcApp.right >= rcDesk.right &&
rcApp.bottom >= rcDesk.bottom)
{
bFullscreen = true;
}
}//如果前台窗口是桌面窗口,或者是控制台窗口,就直接返回不是全屏
return bFullscreen;
}
bool CCommon::CopyStringToClipboard(const wstring& str)
{
if (OpenClipboard(NULL))
{
HGLOBAL clipbuffer;
EmptyClipboard();
size_t size = (str.size() + 1) * 2;
clipbuffer = GlobalAlloc(GMEM_DDESHARE, size);
memcpy_s(GlobalLock(clipbuffer), size, str.c_str(), size);
GlobalUnlock(clipbuffer);
if (SetClipboardData(CF_UNICODETEXT, clipbuffer) == NULL)
return false;
CloseClipboard();
return true;
}
else return false;
}
wstring CCommon::GetJsonValueSimple(const wstring& json_str, const wstring& name)
{
wstring str_name{ L"\"" };
str_name += name;
str_name += L'\"';
size_t index = json_str.find(str_name);
if (index == wstring::npos)
return wstring();
index = json_str.find(L':', index + 1);
if (index == wstring::npos)
return wstring();
index = json_str.find_first_not_of(L"\" ", index + 1);
size_t index_end = json_str.find_first_of(L"\",]}\r\n", index);
wstring result = json_str.substr(index, index_end - index);
return result;
}
bool CCommon::GetURL(const wstring& url, wstring& result, bool utf8, const wstring& user_agent)
{
bool succeed{ false };
CInternetSession* pSession{};
CHttpFile* pfile{};
try
{
pSession = new CInternetSession(user_agent.c_str());
pfile = (CHttpFile*)pSession->OpenURL(url.c_str());
DWORD dwStatusCode;
pfile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode == HTTP_STATUS_OK)
{
CString content;
CString data;
while (pfile->ReadString(data))
{
content += data;
}
result = StrToUnicode((const char*)content.GetString(), utf8);
succeed = true;
}
pfile->Close();
delete pfile;
pSession->Close();
}
catch (CInternetException* e)
{
//写入错误日志
if (theApp.m_debug_log)
{
CString info = CCommon::LoadTextFormat(IDS_GET_URL_ERROR_LOG_INFO, { url, static_cast(e->m_dwError) });
CCommon::WriteLog(info, theApp.m_log_path.c_str());
}
if (pfile != nullptr)
{
pfile->Close();
delete pfile;
}
if (pSession != nullptr)
pSession->Close();
succeed = false;
e->Delete(); //没有这句会造成内存泄露
SAFE_DELETE(pSession);
}
SAFE_DELETE(pSession);
return succeed;
}
void CCommon::GetInternetIp(wstring& ip_address, wstring& ip_location, bool global)
{
wstring web_page;
if (GetURL(L"https://ip.cn/", web_page, true))
{
#ifdef _DEBUG
ofstream file{ L".\\IP_web_page.log" };
file << UnicodeToStr(web_page.c_str()) << std::endl;
#endif // _DEBUG
size_t index, index1;
index = web_page.find(L"");
index1 = web_page.find(L"", index + 6);
if (index == wstring::npos || index1 == wstring::npos)
ip_address.clear();
else
ip_address = web_page.substr(index + 6, index1 - index - 6); //获取IP地址
if (ip_address.size() > 15 || ip_address.size() < 7) //IP地址最长15个字符,最短7个字符
ip_address.clear();
//获取IP地址归属地
if (!global)
{
index = web_page.find(L"", index1 + 7);
index1 = web_page.find(L"", index + 6);
if (index == wstring::npos || index1 == wstring::npos)
ip_location.clear();
else
ip_location = web_page.substr(index + 6, index1 - index - 6);
}
else
{
index = web_page.find(L"GeoIP", index1 + 7);
index1 = web_page.find(L"
", index + 6);
if (index == wstring::npos || index1 == wstring::npos)
ip_location.clear();
else
ip_location = web_page.substr(index + 7, index1 - index - 7);
}
}
else
{
ip_address.clear();
}
}
void CCommon::GetInternetIp2(wstring& ip_address, wstring& ip_location, bool ipv6)
{
wstring raw_string;
wstring user_agent{ L"TrafficMonitor/" };
user_agent += VERSION;
if (GetURL((ipv6 ? L"https://v6.yinghualuo.cn/bejson" : L"https://v4.yinghualuo.cn/bejson"), raw_string, true, user_agent))
{
//解析获取的json字符串
ip_address = GetJsonValueSimple(raw_string, L"ip");
ip_location = GetJsonValueSimple(raw_string, L"location");
}
else
{
ip_address.clear();
ip_location.clear();
}
}
void CCommon::SetRect(CRect& rect, int x, int y, int width, int height)
{
rect.left = x;
rect.top = y;
rect.right = x + width;
rect.bottom = y + height;
}
CString CCommon::LoadText(UINT id, LPCTSTR back_str)
{
CString str;
str.LoadString(id);
if (back_str != nullptr)
str += back_str;
return str;
}
CString CCommon::LoadText(LPCTSTR front_str, UINT id, LPCTSTR back_str)
{
CString str;
str.LoadString(id);
if (back_str != nullptr)
str += back_str;
if (front_str != nullptr)
str = front_str + str;
return str;
}
CString CCommon::StringFormat(LPCTSTR format_str, const std::initializer_list& paras)
{
CString str_rtn = format_str;
int index = 1;
for (const auto& para : paras)
{
CString para_str = para.ToString();
CString format_para;
format_para.Format(_T("<%%%d%%>"), index);
str_rtn.Replace(format_para, para_str);
index++;
}
return str_rtn;
}
CString CCommon::LoadTextFormat(UINT id, const std::initializer_list& paras)
{
CString str;
str.LoadString(id);
return StringFormat(str.GetString(), paras);
}
CString CCommon::IntToString(__int64 n, bool thousand_separation, bool is_unsigned)
{
wstring str = std::to_wstring(is_unsigned ? static_cast(n) : n);
int count{};
if (thousand_separation)
{
int length{ static_cast(str.size()) };
for (int i{ length - 1 }; i > 0; i--)
{
count++;
if (count % 3 == 0)
str.insert(i, L",");
}
}
return str.c_str();
}
void CCommon::NormalizeFont(LOGFONT& font)
{
wstring name;
wstring style;
name = font.lfFaceName;
if (name.empty())
return;
if (name.back() == L' ')
name.pop_back();
size_t index = name.rfind(L' ');
if (index == wstring::npos)
return;
style = name.substr(index + 1);
bool style_acquired = false;
if (style == L"Light")
{
font.lfWeight = FW_LIGHT;
style_acquired = true;
}
else if (style == L"Semilight")
{
font.lfWeight = 350;
style_acquired = true;
}
else if (style == L"Semibold")
{
font.lfWeight = FW_SEMIBOLD;
style_acquired = true;
}
else if (style == L"Bold")
{
font.lfWeight = FW_BOLD;
style_acquired = true;
}
else if (style == L"Black")
{
font.lfWeight = FW_BLACK;
style_acquired = true;
}
if (style_acquired)
{
name = name.substr(0, index);
}
//wcsncpy_s(font.lfFaceName, name.c_str(), 32);
WStringCopy(font.lfFaceName, 32, name.c_str(), name.size());
}
void CCommon::WStringCopy(wchar_t* str_dest, int dest_size, const wchar_t* str_source, int source_size)
{
if (dest_size <= 0)
return;
if (source_size <= 0 || str_source == nullptr)
{
str_dest[0] = L'\0';
return;
}
int i;
for (i = 0; i < dest_size && i < source_size && str_source[i] != L'\0'; i++)
str_dest[i] = str_source[i];
//确保目标字符串末尾有一个\0
int copy_cnt = i;
if (copy_cnt < dest_size)
str_dest[copy_cnt] = L'\0';
else
str_dest[dest_size - 1] = L'\0';
}
void CCommon::SetThreadLanguage(Language language)
{
switch (language)
{
case Language::ENGLISH: SetThreadUILanguage(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); break;
case Language::SIMPLIFIED_CHINESE: SetThreadUILanguage(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)); break;
case Language::TRADITIONAL_CHINESE: SetThreadUILanguage(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)); break;
default: break;
}
}
void CCommon::SetColorMode(ColorMode mode)
{
switch (mode)
{
case ColorMode::Default:
CTrafficMonitorApp::self->m_taskbar_data.dft_back_color = 0;
CTrafficMonitorApp::self->m_taskbar_data.dft_transparent_color = 0;
CTrafficMonitorApp::self->m_taskbar_data.dft_status_bar_color = 0x005A5A5A;
CTrafficMonitorApp::self->m_taskbar_data.dft_text_colors = 0x00ffffffU;
CTrafficMonitorApp::self->m_cfg_data.m_dft_notify_icon = 0;
break;
case ColorMode::Light:
CTrafficMonitorApp::self->m_taskbar_data.dft_back_color = 0x00D3D2D2;
CTrafficMonitorApp::self->m_taskbar_data.dft_transparent_color = 0x00D3D2D2;
CTrafficMonitorApp::self->m_taskbar_data.dft_status_bar_color = 0x00A5A5A5;
CTrafficMonitorApp::self->m_taskbar_data.dft_text_colors = 0x00000000U;
CTrafficMonitorApp::self->m_cfg_data.m_dft_notify_icon = 4;
break;
default:
break;
}
}
void CCommon::TransparentColorConvert(COLORREF& transparent_color)
{
if (transparent_color == 0)
return;
BYTE r = GetRValue(transparent_color);
BYTE g = GetGValue(transparent_color);
BYTE b = GetBValue(transparent_color);
if (r == b)
{
if (b >= 255)
b--;
else
b++;
transparent_color = RGB(r, g, b);
}
}
void CCommon::SetDialogFont(CWnd* pDlg, CFont* pFont)
{
if (pDlg->GetSafeHwnd() != NULL)
{
CWnd* pWndChild;
pWndChild = pDlg->GetWindow(GW_CHILD);
while (pWndChild)
{
pWndChild->SetFont(pFont);
pWndChild = pWndChild->GetWindow(GW_HWNDNEXT);
}
}
}
CString CCommon::GetTextResource(UINT id, int code_type)
{
CString res_str;
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(id), _T("TEXT"));
if (hRes != NULL)
{
HGLOBAL hglobal = LoadResource(NULL, hRes);
if (hglobal != NULL)
{
if (code_type == 2)
{
res_str = (const wchar_t*)hglobal;
}
else
{
res_str = CCommon::StrToUnicode((const char*)hglobal, (code_type != 0)).c_str();
}
}
}
return res_str;
}
CString CCommon::GetLastCompileTime()
{
CString str_compile_time = GetTextResource(IDR_COMPILE_TIME, 0);
str_compile_time.Replace(_T("\r\n"), _T(""));
str_compile_time.Delete(str_compile_time.GetLength() - 1, 1);
return str_compile_time;
}
HICON CCommon::LoadIconResource(UINT id, int size)
{
return (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(id), IMAGE_ICON, size, size, 0);
}
int CCommon::GetMenuItemPosition(CMenu* pMenu, UINT id)
{
int pos = -1;
int item_count = pMenu->GetMenuItemCount();
for (int i = 0; i < item_count; i++)
{
if (pMenu->GetMenuItemID(i) == id)
{
pos = i;
break;
}
}
return pos;
}
bool CCommon::IsColorSimilar(COLORREF color1, COLORREF color2)
{
const int DIFF{ 24 };
return (std::abs(GetRValue(color1) - GetRValue(color2)) < DIFF
&& std::abs(GetGValue(color1) - GetGValue(color2)) < DIFF
&& std::abs(GetBValue(color1) - GetBValue(color2)) < DIFF);
}
int CCommon::CountOneBits(unsigned int value)
{
int count = 0;
while (value != 0)
{
if (value % 2 == 1)
{
count++;
}
value = value >> 1;
}
return count;
}
void CCommon::SetNumberBit(unsigned int& num, int bit, bool value)
{
if (value)
{
num |= (1 << bit);
}
else
{
num &= ~(1 << bit);
}
}
bool CCommon::GetNumberBit(unsigned int num, int bit)
{
return (num & (1 << bit)) != 0;
}
================================================
FILE: TrafficMonitor/Common.h
================================================
#pragma once
#include "CommonData.h"
#include "CVariant.h"
#include
#include
class CCommon
{
public:
CCommon();
~CCommon();
//将const char*字符串转换成宽字符字符串
static wstring StrToUnicode(const char* str, bool utf8 = false);
static string UnicodeToStr(const wchar_t* wstr, bool utf8 = false);
template
static void StringNormalize(T& str)
{
if (str.empty()) return;
int size = str.size(); //字符串的长度
if (size < 0) return;
int index1 = 0; //字符串中第1个不是空格或控制字符的位置
int index2 = size - 1; //字符串中最后一个不是空格或控制字符的位置
while (index1 < size && str[index1] >= 0 && str[index1] <= 32)
index1++;
while (index2 >= 0 && str[index2] >= 0 && str[index2] <= 32)
index2--;
if (index1 > index2) //如果index1 > index2,说明字符串全是空格或控制字符
str.clear();
else if (index1 == 0 && index2 == size - 1) //如果index1和index2的值分别为0和size - 1,说明字符串前后没有空格或控制字符,直接返回
return;
else
str = str.substr(index1, index2 - index1 + 1);
}
//将一个字符串分割成若干个字符(模板类型只能为string或wstring)
//str: 原始字符串
//div_ch: 用于分割的字符
//result: 接收分割后的结果
template
static void StringSplit(const T& str, wchar_t div_ch, vector& results, bool skip_empty = true, bool trim = true)
{
results.clear();
size_t split_index = -1;
size_t last_split_index = -1;
while (true)
{
split_index = str.find(div_ch, split_index + 1);
T split_str = str.substr(last_split_index + 1, split_index - last_split_index - 1);
if (trim)
StringNormalize(split_str);
if (!split_str.empty() || !skip_empty)
results.push_back(split_str);
if (split_index == wstring::npos)
break;
last_split_index = split_index;
}
}
template
static void StringSplit(const T& str, const T& div_str, vector& results, bool skip_empty = true, bool trim = true)
{
results.clear();
size_t split_index = 0 - div_str.size();
size_t last_split_index = 0 - div_str.size();
while (true)
{
split_index = str.find(div_str, split_index + div_str.size());
T split_str = str.substr(last_split_index + div_str.size(), split_index - last_split_index - div_str.size());
if (trim)
StringNormalize(split_str);
if (!split_str.empty() || !skip_empty)
results.push_back(split_str);
if (split_index == wstring::npos)
break;
last_split_index = split_index;
}
}
template
static bool StringTransform(T& str, bool upper)
{
if (str.empty()) return false;
if (upper)
{
for (auto& ch : str)
{
{
if (ch >= 'a' && ch <= 'z')
ch -= 32;
}
}
}
else
{
for (auto& ch : str)
{
if (ch >= 'A' && ch <= 'Z')
ch += 32;
}
}
return true;
}
//读取文件内容
static bool GetFileContent(const wchar_t* file_path, string& contents_buff, bool binary = true);
//读取文件内容
//file_path: 文件的路径
//length: 文件的长度
//binary: 是否以进制方式读取
//返回值: 读取成功返回读取到的文件内容的const char类型的指针,在使用完毕后这个指针需要自行使用delete释放。读取失败返回nullptr
static const char* GetFileContent(const wchar_t* file_path, size_t& length, bool binary = true);
/*根据数据的大小转换成以KB、MB、GB为单位的字符串
size:数据的字节数
返回值:转换后的字符串
*/
static CString DataSizeToString(unsigned long long size, const PublicSettingData& cfg);
/*根据数据的大小转换成以KB、MB、GB为单位的字符串
size:数据的字节数
with_space:数值和单位是否使用空格分隔
返回值:转换后的字符串
*/
static CString DataSizeToString(unsigned long long size, bool with_space = true);
//将温度信息转换成字符串
static CString TemperatureToString(float temperature, const PublicSettingData& cfg);
//将使用率转换成字符串
static CString UsageToString(int usage, const PublicSettingData& cfg);
static CString FreqToString(float usage, const PublicSettingData& cfg);
//static CString KBytesToString(unsigned int kb_size);
static CString KBytesToString(unsigned __int64 kb_size);
//返回两个FILETIME结构的时间差
static __int64 CompareFileTime2(FILETIME time1, FILETIME time2);
//将一个日志信息str_text写入到file_path文件中
static void WriteLog(const char* str_text, LPCTSTR file_path);
static void WriteLog(const wchar_t* str_text, LPCTSTR file_path);
/*
函数功能:对指定文件在指定的目录下创建其快捷方式
函数参数:
lpszLnkFileDir 指定目录,不能为NULL。
lpszFileName 指定文件,为NULL表示当前进程的EXE文件。
lpszLnkFileName 快捷方式名称,为NULL表示EXE文件名。
wHotkey 为0表示不设置快捷键
pszDescription 备注
iShowCmd 运行方式,默认为常规窗口
*/
static BOOL CreateFileShortcut(LPCTSTR lpszLnkFileDir, LPCTSTR lpszFileName = NULL, LPCTSTR lpszLnkFileName = NULL, LPCTSTR lpszWorkDir = NULL, WORD wHotkey = 0, LPCTSTR lpszDescription = NULL, int iShowCmd = SW_SHOWNORMAL);
//获取开始菜单“所有程序”中的“启动”目录的路径
static wstring GetStartUpPath();
//获取path路径下的文件或文件夹,并将文件或文件夹名称保存在files容器中。
static void GetFiles(const wchar_t* path, vector& files);
//获取path路径下的文件或文件夹,每次遍历时调用函数对数func
//path: 查找的路径
//func: 可以是一个函数对象或lambda表达式,参数是遍历到的文件或文件夹名
static void GetFiles(const wchar_t* path, std::function func);
//判断一个文件是否存在
static bool FileExist(LPCTSTR file_name);
//判断是否是文件夹
static bool IsFolder(const wstring& path);
static bool MoveAFile(LPCTSTR exist_file, LPCTSTR new_file);
//计算两个SYSTEMTIME结构时间的差(a-b,只保留时、分、秒)
static SYSTEMTIME CompareSystemTime(SYSTEMTIME a, SYSTEMTIME b);
//获取当前程序的目录
static wstring GetModuleDir();
//获取system32文件夹的路径
static wstring GetSystemDir();
//获取临时文件夹的路径
static wstring GetTemplateDir();
//获取Appdata/Local/TrafficMonitor的目录,如果不存在,则会自动创建
static wstring GetAppDataConfigDir();
//在指定位置绘制文本
static void DrawWindowText(CDC* pDC, CRect rect, LPCTSTR lpszString, COLORREF color, COLORREF back_color);
////设置绘图的剪辑区域
//static void SetDrawArea(CDC* pDC, CRect rect);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 函数名称:IsForegroundFullscreen
* 功能说明:判断当前正在与用户交互的前台窗口是否是全屏的。
* 参数说明:无
* 返回说明:true:是。
false:否。
* 线程安全:是
* 调用样例:IsForegroundFullscreen (),表示判断当前正在与用户交互的前台窗口是否是全屏的。
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static bool IsForegroundFullscreen();
//将一个字符串保存到剪贴板
static bool CopyStringToClipboard(const wstring& str);
static wstring GetJsonValueSimple(const wstring& json_str, const wstring& name);
//获取URL的内容
static bool GetURL(const wstring& url, wstring& result, bool utf8 = false, const wstring& user_agent = wstring());
//获取外网IP地址和IP归属地
static void GetInternetIp(wstring& ip_address, wstring& ip_location, bool global);
static void GetInternetIp2(wstring& ip_address, wstring& ip_location, bool ipv6);
static void SetRect(CRect& rect, int x, int y, int width, int height);
//从资源文件载入字符串。其中,front_str、back_str为载入字符串时需要在前面或后面添加的字符串
static CString LoadText(UINT id, LPCTSTR back_str = nullptr);
static CString LoadText(LPCTSTR front_str, UINT id, LPCTSTR back_str = nullptr);
//安全的格式化字符串,将format_str中形如<%序号%>的字符串替换成初始化列表paras中的元素,元素支持int/double/LPCTSTR/CString格式,序号从1开始
static CString StringFormat(LPCTSTR format_str, const std::initializer_list& paras);
//从资源文件中载入字符串,并将资源字符串中形如<%序号%>的字符串替换成可变参数列表中的参数
static CString LoadTextFormat(UINT id, const std::initializer_list& paras);
//将int类型转换成字符串
//n:要转换的数值
//thousand_separation:是否要每隔3位数使用逗号分隔
//is_unsigned:数值是否是无符号的
static CString IntToString(__int64 n, bool thousand_separation = false, bool is_unsigned = false);
//删除字体名称后面的Bold、Light等字符串,并根据这些字符串设置字体粗细
static void NormalizeFont(LOGFONT& font);
//安全的字符串复制函数
static void WStringCopy(wchar_t* str_dest, int dest_size, const wchar_t* str_source, int source_size = INT_MAX);
///
/// 字符串相似度算法-编辑距离法
///
/// 返回的值为0~1,越大相似度越高
template
static double StringSimilarDegree_LD(const T& srcString, const T& matchString)
{
int n = srcString.size();
int m = matchString.size();
//int[, ] d = new int[n + 1, m + 1]; // matrix
vector> d(n + 1, vector(m + 1));
int cost; // cost
// Step 1(如果其中一个字符串长度为0,则相似度为1)?
//if (n == 0) return (double)m / max(srcString.size(), matchString.size());
//if (m == 0) return (double)n / max(srcString.size(), matchString.size());
if (n == 0 || m == 0) return 0.0; //如果其中一个字符串长度为0,则相似度为0
// Step 2
for (int i = 0; i <= n; d[i][0] = i++);
for (int j = 0; j <= m; d[0][j] = j++);
// Step 3
for (int i = 1; i <= n; i++)
{
//Step 4
for (int j = 1; j <= m; j++)
{
// Step 5
cost = (matchString.substr(j - 1, 1) == srcString.substr(i - 1, 1) ? 0 : 1);
// Step 6
d[i][j] = min(min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + cost);
}
}
// Step 7
double ds = 1 - (double)d[n][m] / max(srcString.size(), matchString.size());
return ds;
}
//设置线程语言
static void SetThreadLanguage(Language language);
//设置颜色模式
static void SetColorMode(ColorMode mode);
//经过测试发现,似乎当透明色的R和B值相等时,会出现右击任务栏窗口时无法弹出右键菜单,而是弹出系统任务栏右键菜单的问题
//为了解决这个问题,需要将颜色值进行转换
//此函数的作用是判断一个颜色的R和B值是否相等,如果是则将颜色的B值加1(如果B==255,则减1)
static void TransparentColorConvert(COLORREF& transparent_color);
static void SetDialogFont(CWnd* pDlg, CFont* pFont);
//从资源加载自定义文本资源。id:资源的ID,code_type:文本的编码格式:0:ANSI, 1:UTF8, 2:UTF16
static CString GetTextResource(UINT id, int code_type);
//从资源文件读取上次编译时间
static CString GetLastCompileTime();
//从资源加载一个图标
static HICON LoadIconResource(UINT id, int size);
//获取一个菜单项的序号
static int GetMenuItemPosition(CMenu* pMenu, UINT id);
static bool IsColorSimilar(COLORREF color1, COLORREF color2);
//计算二进制中1的个数
static int CountOneBits(unsigned int value);
//设置一个数字的某个bit位
static void SetNumberBit(unsigned int& num, int bit, bool value);
//获取一个数字的某个bit位
static bool GetNumberBit(unsigned int num, int bit);
//在不排序的情况下删除vector中的重复元素
template
static void RemoveVectorDuplicateItem(vector& vec)
{
std::set si;
for (auto it = vec.begin(); it != vec.end();)
{
if (si.count(*it) == 0)//这里判断当前元素是否已经出现过,若没有出现过则将该元素保留,并做标记
{
si.insert(*it);
++it;
}
else//如果前面已经出现过,则删除当前元素
{
it = vec.erase(it);//这里删除it指向的元素
}
}
}
};
================================================
FILE: TrafficMonitor/CommonData.cpp
================================================
#include "stdafx.h"
#include "CommonData.h"
#include "Common.h"
#include "CalendarHelper.h"
#include "TrafficMonitor.h"
///////////////////////////////////////////////////////////////////////////////////
int Date::week() const
{
//计算当前是一年的第几天
int days{};
for (int i{ 1 }; i < month; i++)
{
days += CCalendarHelper::DaysInMonth(year, i);
}
days += day;
//计算这一年的1月1日是星期几
int week_day = CCalendarHelper::CaculateWeekDay(year, 1, 1);
if (theApp.m_cfg_data.m_sunday_first)
{
days += (week_day - 1);
}
else
{
days += (week_day - 2);
}
return days / 7 + 1;
}
bool Date::DateGreater(const Date& a, const Date& b)
{
if (a.year != b.year)
return a.year > b.year;
else if (a.month != b.month)
return a.month > b.month;
else if (a.day != b.day)
return a.day > b.day;
else
return false;
}
bool Date::DateEqual(const Date& a, const Date& b)
{
return a.year == b.year && a.month == b.month && a.day == b.day;
}
///////////////////////////////////////////////////////////////////////////////////
//HistoryTraffic
unsigned __int64 HistoryTraffic::kBytes() const
{
return up_kBytes + down_kBytes;
}
///////////////////////////////////////////////////////////////////////////////////
wstring& DispStrings::Get(CommonDisplayItem item)
{
return map_str[item];
}
const std::map& DispStrings::GetAllItems() const
{
return map_str;
}
void DispStrings::operator=(const DispStrings& disp_str)
{
map_str = disp_str.map_str;
//如果赋值的字符串是定义的无效字符串,则不赋值
for (auto& iter = map_str.begin(); iter != map_str.end(); ++iter)
{
if (iter->second == NONE_STR)
iter->second.clear();
}
}
bool DispStrings::IsInvalid() const
{
for (auto& iter = map_str.begin(); iter != map_str.end(); ++iter)
{
if (iter->second == NONE_STR)
return true;
}
return false;
}
void DispStrings::Load(const std::wstring& plugin_id, const std::wstring& disp_str)
{
auto plugin = theApp.m_plugins.GetItemById(plugin_id);
if (plugin != nullptr)
{
map_str[plugin] = disp_str;
}
}
///////////////////////////////////////////////////////////////////////////////////
bool StringSet::Contains(const std::wstring& str) const
{
return string_set.count(str) != 0;
}
void StringSet::SetStrContained(const std::wstring& str, bool contained)
{
if (contained)
string_set.insert(str);
else
string_set.erase(str);
}
void StringSet::FromString(const std::wstring& str)
{
std::vector item_vect;
CCommon::StringSplit(str, L',', item_vect);
string_set.clear();
for (const auto& i : item_vect)
string_set.insert(i);
}
std::wstring StringSet::ToString() const
{
std::vector item_vect;
for (const auto& i : string_set)
item_vect.push_back(i);
std::wstring item_str;
for (const auto& i : item_vect)
{
item_str += i;
item_str += L',';
}
if (!item_str.empty())
item_str.pop_back();
return item_str;
}
void StringSet::FromVector(const std::vector& vec)
{
string_set.clear();
for (const auto& str : vec)
string_set.insert(str);
}
std::vector StringSet::ToVector() const
{
std::vector vec;
for (const auto& str : string_set)
vec.push_back(str);
return vec;
}
std::set& StringSet::data()
{
return string_set;
}
void TaskBarSettingData::ValidItemSpace()
{
if (item_space < 0)
item_space = 0;
if (item_space > 32)
item_space = 32;
}
unsigned __int64 TaskBarSettingData::GetNetspeedFigureMaxValueInBytes() const
{
if (netspeed_figure_max_value_unit == 0) //单位为KB
return static_cast(netspeed_figure_max_value) * 1024;
else
return static_cast(netspeed_figure_max_value) * 1024 * 1024;
}
================================================
FILE: TrafficMonitor/CommonData.h
================================================
//此文件包含全局结构体、枚举类型的定义
#pragma once
#include "stdafx.h"
#include "TaskbarItemOrderHelper.h"
struct Date
{
int year{};
int month{};
int day{};
int week() const; //该日期是一年的第几周
//比较两个HistoryTraffic对象的日期,如果a的时间大于b,则返回true
static bool DateGreater(const Date& a, const Date& b);
//判断两个HistoryTraffic对象的日期是否相等
static bool DateEqual(const Date& a, const Date& b);
};
//储存某一天的历史流量
struct HistoryTraffic : public Date
{
//当天使用的流量(以KB为单位)
unsigned __int64 up_kBytes{};
unsigned __int64 down_kBytes{};
bool mixed{ true }; //如果不区分上传和下载流量,则为true
unsigned __int64 kBytes() const;
};
//历史流量统计中用于指示不同范围内的流量的颜色
#define TRAFFIC_COLOR_BLUE RGB(0, 183, 238)
#define TRAFFIC_COLOR_GREEN RGB(128, 194, 105)
#define TRAFFIC_COLOR_YELLOE RGB(255, 216, 58)
#define TRAFFIC_COLOR_RED RGB(255, 95, 74)
#define TRAFFIC_COLOR_DARK_RED RGB(166, 19, 0)
//网速单位
enum class SpeedUnit
{
AUTO, //自动
KBPS, //KB/s
MBPS //MB/s
};
//硬件监控的项目
enum HardwareItem
{
HI_CPU = 1 << 0, //CPU
HI_GPU = 1 << 1, //显卡
HI_HDD = 1 << 2, //硬盘
HI_MBD = 1 << 3 //主板
};
#define DEF_CH L'\"' //写入和读取ini文件字符串时,在字符串前后添加的字符
#define NONE_STR L"@@@" //用于指定一个无效字符串
struct DispStrings //显示的文本
{
private:
std::map map_str;
public:
//获取一个显示的文本
wstring& Get(CommonDisplayItem item);
const std::map& GetAllItems() const;
void operator=(const DispStrings& disp_str); //重载赋值运算符
//载入一个插件项目的显示文本
void Load(const std::wstring& plugin_id, const std::wstring& disp_str);
//是否无效
bool IsInvalid() const;
};
//鼠标双击窗口的动作
enum class DoubleClickAction
{
CONNECTION_INFO, //连接详情
HISTORY_TRAFFIC, //历史流量统计
SHOW_MORE_INFO, //显示更多信息
OPTIONS, //选项设置
TASK_MANAGER, //任务管理器
SEPCIFIC_APP, //指定应用程序
CHANGE_SKIN, //更换皮肤
NONE //不执行任何动作
};
//语言
enum class Language
{
FOLLOWING_SYSTEM, //跟随系统
ENGLISH, //英语
SIMPLIFIED_CHINESE, //简体中文
TRADITIONAL_CHINESE //繁体中文
};
//颜色模式
enum class ColorMode
{
Default, //默认颜色
Light //浅色
};
//将字号转成LOGFONT结构中的lfHeight
inline int FontSizeToLfHeight(int font_size, int dpi = 0)
{
if (dpi == 0)
{
HDC hDC = ::GetDC(HWND_DESKTOP);
dpi = GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(HWND_DESKTOP, hDC);
}
int lfHeight = -MulDiv(font_size, dpi, 72);
return lfHeight;
}
//字体
struct FontInfo
{
CString name; //字体名称
int size; //字体大小
bool bold; //粗体
bool italic; //斜体
bool underline; //下划线
bool strike_out; //删除线
//创建一个CFont对象
void Create(CFont& font, int dpi = 0)
{
font.CreateFont(
FontSizeToLfHeight(size, dpi), // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
(bold ? FW_BOLD : FW_NORMAL), // nWeight
italic, // bItalic
underline, // bUnderline
strike_out, // cStrikeOut
DEFAULT_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
name);
}
};
//历史流量统计列表视图中显示模式
enum class HistoryTrafficViewType
{
HV_DAY, //日视图
HV_WEEK, //周视图
HV_MONTH, //月视图
HV_QUARTER, //季视图
HV_YEAR //年视图
};
struct StringSet
{
public:
bool Contains(const std::wstring& str) const;
void SetStrContained(const std::wstring& str, bool contained);
void FromString(const std::wstring& str);
std::wstring ToString() const;
void FromVector(const std::vector& vec);
std::vector ToVector() const;
std::set& data();
private:
std::set string_set;
};
//选项设置数据
struct MainConfigData
{
int m_transparency{ 100 }; //窗口透明度
bool m_show_more_info{ false }; //显示更多信息
bool m_show_task_bar_wnd{ false }; //显示任务栏窗口
bool m_hide_main_window; //隐藏主窗口
//bool m_tbar_show_cpu_memory; //任务栏窗口显示CPU和内存利用率
int m_position_x; //窗口位置的x坐标
int m_position_y; //窗口位置的y坐标
bool m_auto_select{ false }; //自动选择连接
bool m_select_all{ false }; //统计所有连接的网速
string m_connection_name; //当前选择网络的名称
wstring m_skin_name; //选择的皮肤的名称
int m_dft_notify_icon = 0; //默认的通知图标(用于区分win10的深色和浅色模式)
int m_notify_icon_selected{}; //要显示的通知区图标
bool m_notify_icon_auto_adapt{ false }; //通知区图标是否自动适应Win10深浅色模式
//bool m_show_internet_ip{ false }; //是否在“连接详情”对话框中显示外网IP地址
bool m_use_log_scale{ false }; //“历史流量统计”对话框中绘制表示历史流量数值的矩形时是否使用对数比例
HistoryTrafficViewType m_view_type{};
bool m_sunday_first{ true }; //是否将周日作为一周的第一天
StringSet plugin_disabled; //已禁用的插件
int taskbar_left_space_win11{}; //Windows11下,任务栏窗口显示在左侧时的边距
};
//内存显示方式
enum class MemoryDisplay
{
USAGE_PERCENTAGE, //已使用百分比
MEMORY_USED, //内存已使用
MEMORY_AVAILABLE //内存可用
};
//选项设置中“主窗口设置”和“任务栏窗口设置”中公共的数据(不使用此结构体创建对象)
struct PublicSettingData
{
bool specify_each_item_color{ false }; //是否指定每个项目的颜色
FontInfo font; //字体
DispStrings disp_str; //显示的文本
bool speed_short_mode{ false }; //网速显示简洁模式(减少小数点的位数,单位不显示“B”)
bool separate_value_unit_with_space{ true }; //网速数值和单位用空格分隔
bool show_tool_tip{ true }; //显示鼠标提示
MemoryDisplay memory_display{ MemoryDisplay::USAGE_PERCENTAGE }; //内存显示方式
bool unit_byte{ true }; //使用字节(B)而不是比特(b)为单位
SpeedUnit speed_unit; //网速的单位
bool hide_unit; //隐藏单位
bool hide_percent; //隐藏百分号
DoubleClickAction double_click_action; //鼠标双击动作
wstring double_click_exe; //鼠标双击动作为打开指定应用程序时,打开的程序路径
};
//#define MAIN_WND_COLOR_NUM 9 //主窗口颜色数量
//选项设置中“主窗口设置”的数据
struct MainWndSettingData : public PublicSettingData
{
std::map text_colors{}; //方字的颜色
bool swap_up_down{ false }; //交换上传和下载显示的位置
bool hide_main_wnd_when_fullscreen; //有程序全屏运行时隐藏悬浮窗
bool m_always_on_top{ false }; //窗口置顶
bool m_lock_window_pos{ false }; //锁定窗口位置
bool m_mouse_penetrate{ false }; //鼠标穿透
bool m_alow_out_of_border{ false }; //是否允许悬浮窗超出屏幕边界
};
//#define TASKBAR_COLOR_NUM 18 //任务栏窗口颜色数量
struct TaskbarItemColor //任务栏窗口每一项的颜色
{
COLORREF label{}; //标签颜色
COLORREF value{}; //数值颜色
bool operator==(const TaskbarItemColor& item) const
{
return label == item.label && value == item.value;
}
};
//选项设置中“任务栏窗口设置”的数据
struct TaskBarSettingData : public PublicSettingData
{
COLORREF back_color{ RGB(0, 0, 0) }; //背景颜色
COLORREF transparent_color{ RGB(0, 0, 0) }; //透明色
COLORREF status_bar_color{ RGB(0, 0, 0) }; // CPU/内存 状态条颜色
std::map text_colors{}; //文字的颜色
int dft_back_color = 0; //默认背景颜色
int dft_transparent_color = 0; //默认透明色
int dft_status_bar_color = 0x005A5A5A; //默认CPU/内存 状态条颜色
int dft_text_colors = 0x00ffffffU; //默认文字颜色
bool auto_adapt_light_theme{ true }; //是否自动适应浅色主题
int dark_default_style{ 0 }; //深色主题时使用的预设方案
int light_default_style{ -1 }; //浅色主题时使用的预设方案
bool auto_set_background_color{ false }; //根据任务栏颜色自动设置背景色
bool auto_save_taskbar_color_settings_to_preset{}; //当启用“自动适应Windows10深色/浅色主题”时,是否在颜色设置有更改时自动将当前颜色设置保存到对应的预设
CTaskbarItemOrderHelper item_order;
unsigned int m_tbar_display_item{ TDI_UP | TDI_DOWN }; //任务栏窗口显示的项目
StringSet plugin_display_item; //任务窗口显示的插件项目
bool value_right_align{ false }; //数值是否右对齐
int digits_number{ 4 }; //数据位数
bool horizontal_arrange{ true }; //水平排列
bool show_status_bar{ true }; //显示 CPU/内存的状态条
bool tbar_wnd_on_left{ false }; //如果为true,则任务栏窗口显示在任务栏的左侧(或上方)
bool tbar_wnd_snap{ false }; //如果为true,则在Win11中任务栏窗口贴靠中间任务栏,否则靠近边缘
bool cm_graph_type{ false }; //如果为false,默认原样式,柱状图显示占用率,如为true,滚动显示占用率
bool show_graph_dashed_box{ true }; //显示占用图虚线框
int item_space{}; //任务栏项目间距
void ValidItemSpace();
bool show_netspeed_figure{ false }; //是否显示网速占用图
int netspeed_figure_max_value; //网速占用图的最大值
int netspeed_figure_max_value_unit{}; //网速占用图最大值的单位(0: KB, 1: MB)
unsigned __int64 GetNetspeedFigureMaxValueInBytes() const; //获取网速占用图的最大值(以字节为单位)
};
//选项设置中“常规设置”的数据
struct GeneralSettingData
{
bool check_update_when_start{ true };
int update_source{}; //更新源。0: GitHub; 1: Gitee
bool auto_run{ false };
bool allow_skin_cover_font{ true };
bool allow_skin_cover_text{ true };
bool show_notify_icon{ true }; //显示通知区域图标
//通知消息
bool traffic_tip_enable{ false }; //是否启用流量超出时提示
int traffic_tip_value; //要提示的流量临界值
int traffic_tip_unit{}; //要提示的流量值的单位(0: MB, 1: GB)
struct NotifyTipSettings //超过某个值时弹出提示的设置
{
bool enable; //是否启用提示
int tip_value; //要弹出提示的临界值
};
NotifyTipSettings memory_usage_tip; //用内存使用率超出提示
NotifyTipSettings cpu_temp_tip; //CPU温度超出提示
NotifyTipSettings gpu_temp_tip; //显卡温度超出提示
NotifyTipSettings hdd_temp_tip; //硬盘温度超出提示
NotifyTipSettings mainboard_temp_tip; //主板温度超出提示
//语言
Language language;
bool show_all_interface{ true };
bool m_get_cpu_usage_by_cpu_times{ true }; //获取CPU利用率的方式,如果为true则是使用GetSystemTimes,否则使用Pdh(性能计数器)
bool portable_mode{ false }; //便携模式,如果为true,则程序所有数据都保存到exe所在目录下,否则保存到Appdata\Romaing目录下
int monitor_time_span{ 1000 }; //监控的时间间隔
std::wstring hard_disk_name; //要监控的硬盘名称
std::wstring cpu_core_name; //要监控的CPU核心的名称
unsigned int hardware_monitor_item{}; //要监控哪些硬件
bool IsHardwareEnable(HardwareItem item_type) const
{
return hardware_monitor_item & item_type;
}
void SetHardwareEnable(HardwareItem item_type, bool enable)
{
if (enable)
hardware_monitor_item |= item_type;
else
hardware_monitor_item &= ~item_type;
}
StringSet connections_hide; //用于保存哪些网络要从“选择网络连接”子菜单项中隐藏
};
//定义监控时间间隔有效的最大值和最小值
#define MONITOR_TIME_SPAN_MIN 200
#define MONITOR_TIME_SPAN_MAX 2000
enum class Alignment
{
LEFT, //左对齐
RIGHT, //右对齐
CENTER, //居中
SIDE //两端对齐
};
//通过构造函数传递一个bool变量的引用,在构造时将其置为true,析构时置为false
class CFlagLocker
{
public:
CFlagLocker(bool& flag)
: m_flag(flag)
{
m_flag = true;
}
~CFlagLocker()
{
m_flag = false;
}
private:
bool& m_flag;
};
================================================
FILE: TrafficMonitor/DisplayTextSettingDlg.cpp
================================================
// DisplayTextSettingDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "DisplayTextSettingDlg.h"
// CDisplayTextSettingDlg 对话框
IMPLEMENT_DYNAMIC(CDisplayTextSettingDlg, CBaseDialog)
CDisplayTextSettingDlg::CDisplayTextSettingDlg(DispStrings& display_texts, bool main_window_text, CWnd* pParent /*=nullptr*/)
: CBaseDialog(IDD_DISPLAY_TEXT_SETTINGS_DIALOG, pParent), m_display_texts(display_texts), m_main_window_text(main_window_text)
{
}
CDisplayTextSettingDlg::~CDisplayTextSettingDlg()
{
}
void CDisplayTextSettingDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
}
CString CDisplayTextSettingDlg::GetDialogName() const
{
return _T("DisplayTextSettingDlg");
}
BEGIN_MESSAGE_MAP(CDisplayTextSettingDlg, CBaseDialog)
ON_BN_CLICKED(IDC_RESTORE_DEFAULT_BUTTON, &CDisplayTextSettingDlg::OnBnClickedRestoreDefaultButton)
END_MESSAGE_MAP()
// CDisplayTextSettingDlg 消息处理程序
BOOL CDisplayTextSettingDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_ITEM), FALSE); // 设置小图标
//初始化列表控件
CRect rect;
m_list_ctrl.GetClientRect(rect);
m_list_ctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0, width1;
width0 = rect.Width() / 2;
width1 = rect.Width() - width0 - theApp.DPI(20) - 1;
m_list_ctrl.InsertColumn(0, CCommon::LoadText(IDS_ITEM), LVCFMT_LEFT, width0); //插入第0列
m_list_ctrl.InsertColumn(1, CCommon::LoadText(IDS_VALUE), LVCFMT_LEFT, width1); //插入第1列
//向列表中插入行
for (auto iter = m_display_texts.GetAllItems().begin(); iter != m_display_texts.GetAllItems().end(); ++iter)
{
CString item_name = iter->first.GetItemName();
if (!item_name.IsEmpty())
{
int index = m_list_ctrl.GetItemCount();
m_list_ctrl.InsertItem(index, item_name);
m_list_ctrl.SetItemText(index, 1, iter->second.c_str());
m_list_ctrl.SetItemData(index, (DWORD_PTR)&(iter->first));
}
}
m_list_ctrl.SetEditColMethod(CListCtrlEx::EC_SPECIFIED); //设置列表可编辑
m_list_ctrl.SetEditableCol({ 1 }); //设置可编辑的列
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
CommonDisplayItem CDisplayTextSettingDlg::GetDisplayItem(int row)
{
CommonDisplayItem* item = (CommonDisplayItem*)m_list_ctrl.GetItemData(row);
return *item;
}
void CDisplayTextSettingDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
int item_count = m_list_ctrl.GetItemCount();
for (int i{}; i < item_count; i++)
{
CommonDisplayItem display_item = GetDisplayItem(i);
m_display_texts.Get(display_item) = m_list_ctrl.GetItemText(i, 1).GetString();
}
CBaseDialog::OnOK();
}
void CDisplayTextSettingDlg::OnBnClickedRestoreDefaultButton()
{
// TODO: 在此添加控件通知处理程序代码
int item_count = m_list_ctrl.GetItemCount();
for (int i{}; i < item_count; i++)
{
CommonDisplayItem display_item = GetDisplayItem(i);
CString default_text;
if (display_item.is_plugin)
{
default_text = display_item.plugin_item->GetItemLableText();
}
else
{
switch (display_item.item_type)
{
case TDI_UP:
if (m_main_window_text)
default_text = CCommon::LoadText(IDS_UPLOAD_DISP, _T(": "));
else
default_text = _T("↑: ");
break;
case TDI_DOWN:
if (m_main_window_text)
default_text = CCommon::LoadText(IDS_DOWNLOAD_DISP, _T(": "));
else
default_text = _T("↓: ");
break;
case TDI_TOTAL_SPEED:
default_text = _T("↑↓: ");
break;
case TDI_CPU:
default_text = _T("CPU: ");
break;
case TDI_CPU_FREQ:
default_text = CCommon::LoadText(IDS_CPU_FREQ, _T(": "));
break;
case TDI_MEMORY:
default_text = CCommon::LoadText(IDS_MEMORY_DISP, _T(": "));
break;
case TDI_GPU_USAGE:
default_text = CCommon::LoadText(IDS_GPU_DISP, _T(": "));
break;
case TDI_CPU_TEMP:
default_text = _T("CPU: ");
break;
case TDI_GPU_TEMP:
default_text = CCommon::LoadText(IDS_GPU_DISP, _T(": "));
break;
case TDI_HDD_TEMP:
default_text = CCommon::LoadText(IDS_HDD_DISP, _T(": "));
break;
case TDI_MAIN_BOARD_TEMP:
default_text = CCommon::LoadText(IDS_MAINBOARD_DISP, _T(": "));
break;
case TDI_HDD_USAGE:
default_text = CCommon::LoadText(IDS_HDD_DISP, _T(": "));
break;
default:
break;
}
}
m_list_ctrl.SetItemText(i, 1, default_text);
}
}
================================================
FILE: TrafficMonitor/DisplayTextSettingDlg.h
================================================
#pragma once
#include "BaseDialog.h"
#include "CommonData.h"
#include "ListCtrlEx.h"
// CDisplayTextSettingDlg 对话框
class CDisplayTextSettingDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CDisplayTextSettingDlg)
public:
CDisplayTextSettingDlg(DispStrings& display_texts, bool main_window_text = false, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CDisplayTextSettingDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DISPLAY_TEXT_SETTINGS_DIALOG };
#endif
private:
DispStrings& m_display_texts;
CListCtrlEx m_list_ctrl;
bool m_main_window_text{ false }; //如果为true,则为主窗口文本设置,否则为任务栏窗口设置
protected:
virtual CString GetDialogName() const override;
CommonDisplayItem GetDisplayItem(int row);
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
afx_msg void OnBnClickedRestoreDefaultButton();
};
================================================
FILE: TrafficMonitor/DonateDlg.cpp
================================================
// DonateDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "DonateDlg.h"
#include "afxdialogex.h"
#include "DrawCommon.h"
// CDonateDlg 对话框
IMPLEMENT_DYNAMIC(CDonateDlg, CBaseDialog)
CDonateDlg::CDonateDlg(CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_DONATE_DIALOG, pParent)
{
}
CDonateDlg::~CDonateDlg()
{
}
void CDonateDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
//DDX_Control(pDX, IDC_DONATE_PIC, m_donate_pic);
}
CString CDonateDlg::GetDialogName() const
{
return _T("DonateDlg");
}
BEGIN_MESSAGE_MAP(CDonateDlg, CBaseDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CDonateDlg 消息处理程序
BOOL CDonateDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_DONATE));
//计算两个二维码图片的位置
CRect rect{};
CWnd* pWnd = nullptr;
pWnd = GetDlgItem(IDC_TEXT_STATIC);
if (pWnd != nullptr)
{
pWnd->GetWindowRect(rect);
ScreenToClient(rect);
}
CRect rc_pic_area;
GetClientRect(rc_pic_area);
rc_pic_area.top = rect.bottom + theApp.DPI(8);
rc_pic_area.left += theApp.DPI(10);
rc_pic_area.right -= theApp.DPI(10);
pWnd = GetDlgItem(IDOK);
if (pWnd != nullptr)
{
pWnd->GetWindowRect(rect);
ScreenToClient(rect);
}
rc_pic_area.bottom = rect.top - theApp.DPI(8);
m_pic1_rect = rc_pic_area;
m_pic1_rect.right = m_pic1_rect.left + (rc_pic_area.Width() / 2) - theApp.DPI(4);
m_pic2_rect = rc_pic_area;
m_pic2_rect.left = m_pic2_rect.right - (rc_pic_area.Width() / 2) + theApp.DPI(4);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDonateDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CBaseDialog::OnPaint()
CDrawCommon draw;
draw.Create(&dc, this);
draw.DrawBitmap(IDB_DONATE_BITMAP, m_pic1_rect.TopLeft(), m_pic1_rect.Size(), CDrawCommon::StretchMode::FIT);
draw.DrawBitmap(IDB_DONATE_WECHAT, m_pic2_rect.TopLeft(), m_pic2_rect.Size(), CDrawCommon::StretchMode::FIT);
}
================================================
FILE: TrafficMonitor/DonateDlg.h
================================================
#pragma once
#include "afxwin.h"
#include "PictureStatic.h"
#include "BaseDialog.h"
// CDonateDlg 对话框
class CDonateDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CDonateDlg)
public:
CDonateDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CDonateDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DONATE_DIALOG };
#endif
protected:
//CPictureStatic m_donate_pic;
CRect m_pic1_rect;
CRect m_pic2_rect;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
virtual CString GetDialogName() const override;
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
};
================================================
FILE: TrafficMonitor/DrawCommon.cpp
================================================
#include "stdafx.h"
#include "DrawCommon.h"
CDrawCommon::CDrawCommon()
{
}
CDrawCommon::~CDrawCommon()
{
}
void CDrawCommon::Create(CDC* pDC, CWnd* pMainWnd)
{
m_pDC = pDC;
m_pMainWnd = pMainWnd;
if (pMainWnd != nullptr)
m_pfont = m_pMainWnd->GetFont();
}
void CDrawCommon::SetFont(CFont* pfont)
{
m_pfont = pfont;
m_pDC->SelectObject(m_pfont);
}
void CDrawCommon::SetDC(CDC* pDC)
{
m_pDC = pDC;
}
void CDrawCommon::DrawWindowText(CRect rect, LPCTSTR lpszString, COLORREF color, Alignment align, bool draw_back_ground, bool multi_line)
{
m_pDC->SetTextColor(color);
if (!draw_back_ground)
m_pDC->SetBkMode(TRANSPARENT);
m_pDC->SelectObject(m_pfont);
CSize text_size = m_pDC->GetTextExtent(lpszString);
UINT format; //CDC::DrawText()函数的文本格式
if (multi_line)
format = DT_EDITCONTROL | DT_WORDBREAK | DT_NOPREFIX;
else
format = DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX;
if (text_size.cx > rect.Width()) //如果文本宽度超过了矩形区域的宽度,设置了居中时左对齐
{
if (align == Alignment::RIGHT)
format |= DT_RIGHT;
}
else
{
switch (align)
{
case Alignment::RIGHT: format |= DT_RIGHT; break;
case Alignment::CENTER: format |= DT_CENTER; break;
}
}
if (draw_back_ground)
m_pDC->FillSolidRect(rect, m_back_color);
m_pDC->DrawText(lpszString, rect, format);
}
void CDrawCommon::SetDrawRect(CRect rect)
{
CRgn rgn;
rgn.CreateRectRgnIndirect(rect);
m_pDC->SelectClipRgn(&rgn);
}
void CDrawCommon::SetDrawRect(CDC* pDC, CRect rect)
{
CRgn rgn;
rgn.CreateRectRgnIndirect(rect);
pDC->SelectClipRgn(&rgn);
}
void CDrawCommon::DrawBitmap(CBitmap& bitmap, CPoint start_point, CSize size, StretchMode stretch_mode)
{
CDC memDC;
//获取图像实际大小
BITMAP bm;
GetObject(bitmap, sizeof(BITMAP), &bm);
memDC.CreateCompatibleDC(m_pDC);
memDC.SelectObject(&bitmap);
// 以下两行避免图片失真
m_pDC->SetStretchBltMode(HALFTONE);
m_pDC->SetBrushOrg(0, 0);
CSize draw_size;
if (size.cx == 0 || size.cy == 0) //如果指定的size为0,则使用位图的实际大小绘制
{
draw_size = CSize(bm.bmWidth, bm.bmHeight);
}
else
{
draw_size = size;
if (stretch_mode == StretchMode::FILL)
{
SetDrawRect(CRect(start_point, draw_size));
float w_h_radio, w_h_radio_draw; //图像的宽高比、绘制大小的宽高比
w_h_radio = static_cast(bm.bmWidth) / bm.bmHeight;
w_h_radio_draw = static_cast(size.cx) / size.cy;
if (w_h_radio > w_h_radio_draw) //如果图像的宽高比大于绘制区域的宽高比,则需要裁剪两边的图像
{
int image_width; //按比例缩放后的宽度
image_width = bm.bmWidth * draw_size.cy / bm.bmHeight;
start_point.x -= ((image_width - draw_size.cx) / 2);
draw_size.cx = image_width;
}
else
{
int image_height; //按比例缩放后的高度
image_height = bm.bmHeight * draw_size.cx / bm.bmWidth;
start_point.y -= ((image_height - draw_size.cy) / 2);
draw_size.cy = image_height;
}
}
else if (stretch_mode == StretchMode::FIT)
{
draw_size = CSize(bm.bmWidth, bm.bmHeight);
float w_h_radio, w_h_radio_draw; //图像的宽高比、绘制大小的宽高比
w_h_radio = static_cast(bm.bmWidth) / bm.bmHeight;
w_h_radio_draw = static_cast(size.cx) / size.cy;
if (w_h_radio > w_h_radio_draw) //如果图像的宽高比大于绘制区域的宽高比
{
draw_size.cy = draw_size.cy * size.cx / draw_size.cx;
draw_size.cx = size.cx;
start_point.y += ((size.cy - draw_size.cy) / 2);
}
else
{
draw_size.cx = draw_size.cx * size.cy / draw_size.cy;
draw_size.cy = size.cy;
start_point.x += ((size.cx - draw_size.cx) / 2);
}
}
}
m_pDC->StretchBlt(start_point.x, start_point.y, draw_size.cx, draw_size.cy, &memDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
memDC.DeleteDC();
}
void CDrawCommon::DrawBitmap(UINT bitmap_id, CPoint start_point, CSize size, StretchMode stretch_mode)
{
CBitmap bitmap;
bitmap.LoadBitmap(bitmap_id);
DrawBitmap(bitmap, start_point, size, stretch_mode);
}
void CDrawCommon::DrawBitmap(HBITMAP hbitmap, CPoint start_point, CSize size, StretchMode stretch_mode)
{
CBitmap bitmap;
if (!bitmap.Attach(hbitmap))
return;
DrawBitmap(bitmap, start_point, size, stretch_mode);
bitmap.Detach();
}
void CDrawCommon::DrawIcon(HICON hIcon, CPoint start_point, CSize size)
{
if (m_pDC->GetSafeHdc() == NULL)
return;
if (size.cx == 0 || size.cy == 0)
::DrawIconEx(m_pDC->GetSafeHdc(), start_point.x, start_point.y, hIcon, 0, 0, 0, NULL, DI_NORMAL | DI_DEFAULTSIZE);
else
::DrawIconEx(m_pDC->GetSafeHdc(), start_point.x, start_point.y, hIcon, size.cx, size.cy, 0, NULL, DI_NORMAL);
}
void CDrawCommon::BitmapStretch(CImage* pImage, CImage* ResultImage, CSize size)
{
if (pImage->IsDIBSection())
{
// 取得 pImage 的 DC
CDC* pImageDC1 = CDC::FromHandle(pImage->GetDC()); // Image 因為有自己的 DC, 所以必須使用 FromHandle 取得對應的 DC
CBitmap* bitmap1 = pImageDC1->GetCurrentBitmap();
BITMAP bmpInfo;
bitmap1->GetBitmap(&bmpInfo);
// 建立新的 CImage
ResultImage->Create(size.cx, size.cy, bmpInfo.bmBitsPixel);
CDC* ResultImageDC = CDC::FromHandle(ResultImage->GetDC());
// 當 Destination 比較小的時候, 會根據 Destination DC 上的 Stretch Blt mode 決定是否要保留被刪除點的資訊
ResultImageDC->SetStretchBltMode(HALFTONE); // 使用最高品質的方式
::SetBrushOrgEx(ResultImageDC->m_hDC, 0, 0, NULL); // 調整 Brush 的起點
// 把 pImage 畫到 ResultImage 上面
StretchBlt(*ResultImageDC, 0, 0, size.cx, size.cy, *pImageDC1, 0, 0, pImage->GetWidth(), pImage->GetHeight(), SRCCOPY);
// pImage->Draw(*ResultImageDC,0,0,StretchWidth,StretchHeight,0,0,pImage->GetWidth(),pImage->GetHeight());
pImage->ReleaseDC();
ResultImage->ReleaseDC();
}
}
void CDrawCommon::FillRect(CRect rect, COLORREF color)
{
m_pDC->FillSolidRect(rect, color);
}
void CDrawCommon::FillRectWithBackColor(CRect rect)
{
m_pDC->FillSolidRect(rect, m_back_color);
}
void CDrawCommon::DrawRectOutLine(CRect rect, COLORREF color, int width, bool dot_line)
{
CPen aPen, * pOldPen;
aPen.CreatePen((dot_line ? PS_DOT : PS_SOLID), width, color);
pOldPen = m_pDC->SelectObject(&aPen);
CBrush* pOldBrush{ dynamic_cast(m_pDC->SelectStockObject(NULL_BRUSH)) };
rect.DeflateRect(width / 2, width / 2);
m_pDC->Rectangle(rect);
m_pDC->SelectObject(pOldPen);
m_pDC->SelectObject(pOldBrush); // Restore the old brush
aPen.DeleteObject();
}
void CDrawCommon::GetRegionFromImage(CRgn& rgn, CBitmap& cBitmap, int threshold)
{
CDC memDC;
memDC.CreateCompatibleDC(NULL);
CBitmap* pOldMemBmp = NULL;
pOldMemBmp = memDC.SelectObject(&cBitmap);
//创建总的窗体区域,初始region为0
rgn.CreateRectRgn(0, 0, 0, 0);
BITMAP bit;
cBitmap.GetBitmap(&bit);//取得位图参数,这里要用到位图的长和宽
int y;
for (y = 0; y < bit.bmHeight; y++)
{
CRgn rgnTemp; //保存临时region
int iX = 0;
do
{
//跳过透明色找到下一个非透明色的点.
while (iX < bit.bmWidth && GetColorBritness(memDC.GetPixel(iX, y)) <= threshold)
iX++;
int iLeftX = iX; //记住这个起始点
//寻找下个透明色的点
while (iX < bit.bmWidth && GetColorBritness(memDC.GetPixel(iX, y)) > threshold)
++iX;
//创建一个包含起点与重点间高为1像素的临时“region”
rgnTemp.CreateRectRgn(iLeftX, y, iX, y + 1);
rgn.CombineRgn(&rgn, &rgnTemp, RGN_OR);
//删除临时"region",否则下次创建时和出错
rgnTemp.DeleteObject();
} while (iX < bit.bmWidth);
}
memDC.DeleteDC();
}
int CDrawCommon::GetColorBritness(COLORREF color)
{
return (GetRValue(color) + GetGValue(color) + GetBValue(color)) / 3;
}
void CDrawCommon::DrawLine(CPoint start_point, int height, COLORREF color)
{
CPen aPen, * pOldPen;
aPen.CreatePen(PS_SOLID, 1, color);
pOldPen = m_pDC->SelectObject(&aPen);
CBrush* pOldBrush{ dynamic_cast(m_pDC->SelectStockObject(NULL_BRUSH)) };
m_pDC->MoveTo(start_point); //移动到起始点,默认是从下向上画
m_pDC->LineTo(CPoint(start_point.x, start_point.y - height));
m_pDC->SelectObject(pOldPen);
m_pDC->SelectObject(pOldBrush); // Restore the old brush
aPen.DeleteObject();
}
================================================
FILE: TrafficMonitor/DrawCommon.h
================================================
//封装的绘图类
#pragma once
#include "CommonData.h"
class CDrawCommon
{
public:
//拉伸模式
enum class StretchMode
{
STRETCH, //拉伸,会改变比例
FILL, //填充,不改变比例,会裁剪长边
FIT //适应,不会改变比例,不裁剪
};
CDrawCommon();
~CDrawCommon();
void Create(CDC* pDC, CWnd* pMainWnd);
void SetFont(CFont* pfont); //设置绘制文本的字体
void SetDC(CDC* pDC); //设置绘图的DC
CDC* GetDC() { return m_pDC; }
void SetBackColor(COLORREF back_color) { m_back_color = back_color; }
void DrawWindowText(CRect rect, LPCTSTR lpszString, COLORREF color, Alignment align = Alignment::LEFT, bool draw_back_ground = false, bool multi_line = false); //在指定的矩形区域内绘制文本
void SetDrawRect(CRect rect); //设置绘图剪辑区域
static void SetDrawRect(CDC* pDC, CRect rect);
//绘制一个位图
//(注意:当stretch_mode设置为StretchMode::FILL(填充)时,会设置绘图剪辑区域,如果之后需要绘制其他图形,
//需要重新设置绘图剪辑区域,否则图片外的区域会无法绘制)
void DrawBitmap(CBitmap& bitmap, CPoint start_point, CSize size, StretchMode stretch_mode = StretchMode::STRETCH);
void DrawBitmap(UINT bitmap_id, CPoint start_point, CSize size, StretchMode stretch_mode = StretchMode::STRETCH);
void DrawBitmap(HBITMAP hbitmap, CPoint start_point, CSize size, StretchMode stretch_mode = StretchMode::STRETCH);
void DrawIcon(HICON hIcon, CPoint start_point, CSize size);
//将图片拉伸到指定尺寸(https://blog.csdn.net/sichuanpb/article/details/22986877)
static void BitmapStretch(CImage* pImage, CImage* ResultImage, CSize size);
void FillRect(CRect rect, COLORREF color); //用纯色填充矩形
void FillRectWithBackColor(CRect rect); //使用背景色填充矩形
void DrawRectOutLine(CRect rect, COLORREF color, int width = 1, bool dot_line = false); //绘制矩形边框。如果dot_line为true,则为虚线
//从图像创建区域,如果像素点的亮度小于threshold(取值为0~255,0为黑色,255为白色),则该像素点在区域外
//https://blog.csdn.net/tajon1226/article/details/6589180
static void GetRegionFromImage(CRgn& rgn, CBitmap& cBitmap, int threshold);
void DrawLine(CPoint start_point, int height, COLORREF color); //使用当前画笔画线
private:
CDC* m_pDC{}; //用于绘图的CDC类的指针
CWnd* m_pMainWnd{}; //绘图窗口的句柄
CFont* m_pfont{};
COLORREF m_back_color{};
static int GetColorBritness(COLORREF color);
};
//用于双缓冲绘图的类
class CDrawDoubleBuffer
{
public:
CDrawDoubleBuffer(CDC* pDC, CRect rect)
: m_pDC(pDC), m_rect(rect)
{
if (m_pDC != nullptr)
{
m_memDC.CreateCompatibleDC(NULL);
m_memBitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
m_pOldBit = m_memDC.SelectObject(&m_memBitmap);
}
}
~CDrawDoubleBuffer()
{
if (m_pDC != nullptr)
{
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), &m_memDC, 0, 0, SRCCOPY);
m_memDC.SelectObject(m_pOldBit);
m_memBitmap.DeleteObject();
m_memDC.DeleteDC();
}
}
CDC* GetMemDC()
{
return &m_memDC;
}
private:
CDC* m_pDC;
CDC m_memDC;
CBitmap m_memBitmap;
CBitmap* m_pOldBit;
CRect m_rect;
};
================================================
FILE: TrafficMonitor/DrawCommonEx.cpp
================================================
#include "stdafx.h"
#include "DrawCommonEx.h"
CDrawCommonEx::CDrawCommonEx(CDC* pDC)
{
m_pDC = pDC;
hThm = OpenThemeData(::GetDesktopWindow(), L"Window");
}
CDrawCommonEx::~CDrawCommonEx()
{
CloseThemeData(hThm);
}
void CDrawCommonEx::DrawWindowText(CRect rect, LPCTSTR str, COLORREF color)
{
m_pDC->SelectObject(m_pFont);
m_pDC->SetTextColor(color);
DTTOPTS dttopts{};
dttopts.dwSize = sizeof(DTTOPTS);
dttopts.dwFlags = DTT_GLOWSIZE | DTT_COMPOSITED; //ѡ
dttopts.iGlowSize = 0; //ķΧС
HRESULT hr = DrawThemeTextEx(hThm, m_pDC->GetSafeHdc(), TEXT_LABEL, 0, str, -1, DT_LEFT | DT_SINGLELINE, rect, &dttopts);
}
void CDrawCommonEx::SetFont(CFont * pFont)
{
m_pFont = pFont;
}
================================================
FILE: TrafficMonitor/DrawCommonEx.h
================================================
#pragma once
class CDrawCommonEx
{
public:
CDrawCommonEx(CDC* pDC);
~CDrawCommonEx();
void DrawWindowText(CRect rect, LPCTSTR str, COLORREF color);
void SetFont(CFont* pFont);
private:
HTHEME hThm;
CFont* m_pFont{};
CDC* m_pDC{};
};
================================================
FILE: TrafficMonitor/FilePathHelper.cpp
================================================
#include "stdafx.h"
#include "FilePathHelper.h"
#include "Common.h"
CFilePathHelper::CFilePathHelper(const wstring & file_path)
: m_file_path{ file_path }
{
}
CFilePathHelper::~CFilePathHelper()
{
}
wstring CFilePathHelper::GetFileExtension(bool upper, bool width_dot) const
{
size_t index;
index = m_file_path.rfind('.');
if (index == wstring::npos || index == m_file_path.size() - 1)
return wstring();
wstring file_extension{ m_file_path.substr(width_dot ? index : index + 1) };
CCommon::StringTransform(file_extension, upper);
return file_extension;
}
wstring CFilePathHelper::GetFileName() const
{
size_t index;
index = m_file_path.rfind('\\');
if (index == wstring::npos)
index = m_file_path.rfind('/');
return m_file_path.substr(index + 1);
}
wstring CFilePathHelper::GetFileNameWithoutExtension() const
{
size_t index, index1;
index = m_file_path.rfind('.');
index1 = m_file_path.rfind('\\');
if (index1 == wstring::npos)
index1 = m_file_path.rfind('/');
return m_file_path.substr(index1 + 1, (index - index1 - 1));
}
wstring CFilePathHelper::GetFolderName() const
{
int index, index1;
index = m_file_path.find_last_of(L"\\/");
if (index == wstring::npos || index == 0)
return wstring();
index1 = m_file_path.find_last_of(L"\\/", index - 1);
if (index1 == wstring::npos || index1 == 0)
return wstring();
return m_file_path.substr(index1 + 1, (index - index1 - 1));
}
wstring CFilePathHelper::GetDir() const
{
if (!m_file_path.empty() && (m_file_path.back() == L'\\' || m_file_path.back() == L'/'))
return m_file_path;
size_t index;
index = m_file_path.rfind('\\');
if (index == wstring::npos)
index = m_file_path.rfind('/');
return m_file_path.substr(0, index + 1);
}
wstring CFilePathHelper::GetParentDir() const
{
wstring dir{ GetDir() };
size_t index;
if (!dir.empty() && (dir.back() == L'\\' || dir.back() == L'/'))
dir.pop_back();
index = dir.rfind('\\');
if (index == wstring::npos)
index = dir.rfind('/');
return m_file_path.substr(0, index + 1);
}
const wstring& CFilePathHelper::ReplaceFileExtension(const wchar_t * new_extension)
{
size_t index, index1;
index = m_file_path.rfind('.');
index1 = m_file_path.rfind('\\');
if (index == wstring::npos || (index1 != wstring::npos && index < index1)) //如果没有找到“.”,或者“.”在反斜杠的左边,则在末尾添加一个“.”
{
m_file_path.push_back(L'.');
}
else if (index != m_file_path.size() - 1) //如果“.”不在最后的位置,则删除“.”后面的字符串
{
m_file_path.erase(index + 1);
}
if (new_extension == nullptr || *new_extension == L'\0')
{
if (!m_file_path.empty() && m_file_path.back() == L'.')
m_file_path.pop_back();
}
else
{
m_file_path.append(new_extension); //在末尾添加扩展名
}
return m_file_path;
}
wstring CFilePathHelper::GetFilePathWithoutExtension() const
{
size_t index;
index = m_file_path.rfind('.');
return m_file_path.substr(0, index);
}
================================================
FILE: TrafficMonitor/FilePathHelper.h
================================================
#pragma once
class CFilePathHelper
{
public:
CFilePathHelper(const wstring& file_path);
CFilePathHelper(){}
~CFilePathHelper();
void SetFilePath(const wstring& file_path) { m_file_path = file_path; }
wstring GetFileExtension(bool upper = false, bool width_dot = false) const; //获取文件的扩展名(upper:是否大写; width_dot:是否包含“.”)
wstring GetFileName() const; //获取文件名
wstring GetFileNameWithoutExtension() const; //获取文件名(不含扩展名)
wstring GetFolderName() const; //获取文件夹名
wstring GetDir() const; //获取目录
wstring GetParentDir() const; //获取上级目录
wstring GetFilePath() const { return m_file_path; } //获取完整路径
const wstring& ReplaceFileExtension(const wchar_t* new_extension); //替换文件的扩展名,返回文件完整路径
wstring GetFilePathWithoutExtension() const; //获取文件路径(不含扩展名)
protected:
wstring m_file_path;
};
================================================
FILE: TrafficMonitor/GeneralSettingsDlg.cpp
================================================
// GeneralSettingsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "GeneralSettingsDlg.h"
#include "PluginManagerDlg.h"
#include "SelectConnectionsDlg.h"
// CGeneralSettingsDlg dialog
static const int MONITOR_SPAN_STEP = 100;
IMPLEMENT_DYNAMIC(CGeneralSettingsDlg, CTabDlg)
CGeneralSettingsDlg::CGeneralSettingsDlg(CWnd* pParent /*=NULL*/)
: CTabDlg(IDD_GENERAL_SETTINGS_DIALOG, pParent)
{
}
CGeneralSettingsDlg::~CGeneralSettingsDlg()
{
}
void CGeneralSettingsDlg::CheckTaskbarDisplayItem()
{
//如果选项设置中关闭了某个硬件监控,则不显示对应的温度监控相关项目
int taskbar_displat_item_ori = theApp.m_taskbar_data.m_tbar_display_item;
if (!theApp.m_general_data.IsHardwareEnable(HI_CPU))
{
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_TEMP;
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_FREQ;
}
if (!theApp.m_general_data.IsHardwareEnable(HI_GPU))
{
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_USAGE;
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_TEMP;
}
if (!theApp.m_general_data.IsHardwareEnable(HI_HDD))
{
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_TEMP;
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_USAGE;
}
if (!theApp.m_general_data.IsHardwareEnable(HI_MBD))
theApp.m_taskbar_data.m_tbar_display_item &= ~TDI_MAIN_BOARD_TEMP;
}
void CGeneralSettingsDlg::SetControlMouseWheelEnable(bool enable)
{
m_traffic_tip_combo.SetMouseWheelEnable(enable);
m_language_combo.SetMouseWheelEnable(enable);
m_traffic_tip_edit.SetMouseWheelEnable(enable);
m_memory_tip_edit.SetMouseWheelEnable(enable);
m_monitor_span_edit.SetMouseWheelEnable(enable);
m_cpu_temp_tip_edit.SetMouseWheelEnable(enable);
m_gpu_temp_tip_edit.SetMouseWheelEnable(enable);
m_hdd_temp_tip_edit.SetMouseWheelEnable(enable);
m_mbd_temp_tip_edit.SetMouseWheelEnable(enable);
m_hard_disk_combo.SetMouseWheelEnable(enable);
m_select_cpu_combo.SetMouseWheelEnable(enable);
}
bool CGeneralSettingsDlg::ShowHardwareMonitorWarning()
{
if (SHMessageBoxCheck(m_hWnd, CCommon::LoadText(IDS_HARDWARE_MONITOR_WARNING), APP_NAME, MB_OKCANCEL | MB_ICONWARNING, IDOK, _T("{B8A281A7-76DF-4F0F-BF6A-1A394EF8BAD5}")) == IDOK)
{
//if (SHMessageBoxCheck(m_hWnd, CCommon::LoadText(IDS_HARDWARE_MONITOR_WARNING2), APP_NAME, MB_OKCANCEL | MB_ICONWARNING, IDOK, _T("{2777F260-6175-41E4-AF59-4085B3F58E32}")) == IDOK)
//{
return true;
//}
}
return false;
}
void CGeneralSettingsDlg::AddOrUpdateAutoRunTooltip(bool add)
{
CString str_tool_tip;
#ifdef WITHOUT_TEMPERATURE
str_tool_tip = CCommon::LoadText(IDS_AUTO_RUN_METHOD_REGESTRY);
#else
str_tool_tip = CCommon::LoadText(IDS_AUTO_RUN_METHOD_TASK_SCHEDULE);
#endif
if (!m_auto_run_path.empty())
{
str_tool_tip += _T("\r\n");
str_tool_tip += CCommon::LoadText(IDS_PATH, _T(": "));
str_tool_tip += m_auto_run_path.c_str();
}
if (add)
m_toolTip.AddTool(GetDlgItem(IDC_AUTO_RUN_CHECK), str_tool_tip);
else
m_toolTip.UpdateTipText(str_tool_tip, GetDlgItem(IDC_AUTO_RUN_CHECK));
}
bool CGeneralSettingsDlg::IsMonitorTimeSpanModified() const
{
return m_data.monitor_time_span != m_monitor_time_span_ori;
}
void CGeneralSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
CTabDlg::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TODAY_TRAFFIC_TIP_EDIT, m_traffic_tip_edit);
DDX_Control(pDX, IDC_TODAY_TRAFFIC_TIP_COMBO, m_traffic_tip_combo);
DDX_Control(pDX, IDC_MEMORY_USAGE_TIP_EDIT, m_memory_tip_edit);
DDX_Control(pDX, IDC_LANGUAGE_COMBO, m_language_combo);
DDX_Control(pDX, IDC_MONITOR_SPAN_EDIT, m_monitor_span_edit);
DDX_Control(pDX, IDC_CPU_TEMP_TIP_EDIT, m_cpu_temp_tip_edit);
DDX_Control(pDX, IDC_GPU_TEMP_TIP_EDIT, m_gpu_temp_tip_edit);
DDX_Control(pDX, IDC_HDD_TIP_EDIT, m_hdd_temp_tip_edit);
DDX_Control(pDX, IDC_MBD_TEMP_TIP_EDIT, m_mbd_temp_tip_edit);
DDX_Control(pDX, IDC_SELECT_HARD_DISK_COMBO, m_hard_disk_combo);
DDX_Control(pDX, IDC_SELECT_CPU_COMBO, m_select_cpu_combo);
DDX_Control(pDX, IDC_PLUGIN_MANAGE_BUTTON, m_plugin_manager_btn);
DDX_Control(pDX, IDC_SELECT_CONNECTIONS_BUTTON, m_select_connection_btn);
}
void CGeneralSettingsDlg::SetControlEnable()
{
m_traffic_tip_edit.EnableWindow(m_data.traffic_tip_enable);
m_traffic_tip_combo.EnableWindow(m_data.traffic_tip_enable);
m_memory_tip_edit.EnableWindow(m_data.memory_usage_tip.enable);
m_cpu_temp_tip_edit.EnableWindow(m_data.cpu_temp_tip.enable);
m_gpu_temp_tip_edit.EnableWindow(m_data.gpu_temp_tip.enable);
m_hdd_temp_tip_edit.EnableWindow(m_data.hdd_temp_tip.enable);
m_mbd_temp_tip_edit.EnableWindow(m_data.mainboard_temp_tip.enable);
m_hard_disk_combo.EnableWindow(m_data.IsHardwareEnable(HI_HDD));
m_select_cpu_combo.EnableWindow(m_data.IsHardwareEnable(HI_CPU));
}
BEGIN_MESSAGE_MAP(CGeneralSettingsDlg, CTabDlg)
ON_BN_CLICKED(IDC_CHECK_NOW_BUTTON, &CGeneralSettingsDlg::OnBnClickedCheckNowButton)
ON_BN_CLICKED(IDC_CHECK_UPDATE_CHECK, &CGeneralSettingsDlg::OnBnClickedCheckUpdateCheck)
ON_BN_CLICKED(IDC_AUTO_RUN_CHECK, &CGeneralSettingsDlg::OnBnClickedAutoRunCheck)
ON_BN_CLICKED(IDC_ALLOW_SKIN_FONT_CHECK, &CGeneralSettingsDlg::OnBnClickedAllowSkinFontCheck)
ON_BN_CLICKED(IDC_ALLOW_SKIN_DISP_STR_CHECK, &CGeneralSettingsDlg::OnBnClickedAllowSkinDispStrCheck)
ON_BN_CLICKED(IDC_TODAY_TRAFFIC_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedTodayTrafficTipCheck)
ON_BN_CLICKED(IDC_MEMORY_USAGE_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedMemoryUsageTipCheck)
ON_BN_CLICKED(IDC_OPEN_CONFIG_PATH_BUTTON, &CGeneralSettingsDlg::OnBnClickedOpenConfigPathButton)
ON_BN_CLICKED(IDC_SHOW_ALL_CONNECTION_CHECK, &CGeneralSettingsDlg::OnBnClickedShowAllConnectionCheck)
ON_BN_CLICKED(IDC_USE_CPU_TIME_RADIO, &CGeneralSettingsDlg::OnBnClickedUseCpuTimeRadio)
ON_BN_CLICKED(IDC_USE_PDH_RADIO, &CGeneralSettingsDlg::OnBnClickedUsePdhRadio)
ON_NOTIFY(UDN_DELTAPOS, SPIN_ID, &CGeneralSettingsDlg::OnDeltaposSpin)
ON_EN_KILLFOCUS(IDC_MONITOR_SPAN_EDIT, &CGeneralSettingsDlg::OnEnKillfocusMonitorSpanEdit)
ON_BN_CLICKED(IDC_CPU_TEMP_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedCpuTempTipCheck)
ON_BN_CLICKED(IDC_GPU_TEMP_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedGpuTempTipCheck)
ON_BN_CLICKED(IDC_HDD_TEMP_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedHddTempTipCheck)
ON_BN_CLICKED(IDC_MBD_TEMP_TIP_CHECK, &CGeneralSettingsDlg::OnBnClickedMbdTempTipCheck)
ON_BN_CLICKED(IDC_GITHUB_RADIO, &CGeneralSettingsDlg::OnBnClickedGithubRadio)
ON_BN_CLICKED(IDC_GITEE_RADIO, &CGeneralSettingsDlg::OnBnClickedGiteeRadio)
ON_BN_CLICKED(IDC_RESTORE_DEFAULT_TIME_SPAN_BUTTON, &CGeneralSettingsDlg::OnBnClickedRestoreDefaultTimeSpanButton)
ON_CBN_SELCHANGE(IDC_SELECT_HARD_DISK_COMBO, &CGeneralSettingsDlg::OnCbnSelchangeSelectHardDiskCombo)
ON_BN_CLICKED(IDC_CPU_CHECK, &CGeneralSettingsDlg::OnBnClickedCpuCheck)
ON_BN_CLICKED(IDC_GPU_CHECK, &CGeneralSettingsDlg::OnBnClickedGpuCheck)
ON_BN_CLICKED(IDC_HDD_CHECK, &CGeneralSettingsDlg::OnBnClickedHddCheck)
ON_BN_CLICKED(IDC_MBD_CHECK, &CGeneralSettingsDlg::OnBnClickedMbdCheck)
ON_CBN_SELCHANGE(IDC_SELECT_CPU_COMBO, &CGeneralSettingsDlg::OnCbnSelchangeSelectCpuCombo)
ON_BN_CLICKED(IDC_PLUGIN_MANAGE_BUTTON, &CGeneralSettingsDlg::OnBnClickedPluginManageButton)
ON_BN_CLICKED(IDC_SHOW_NOTIFY_ICON_CHECK, &CGeneralSettingsDlg::OnBnClickedShowNotifyIconCheck)
ON_BN_CLICKED(IDC_SELECT_CONNECTIONS_BUTTON, &CGeneralSettingsDlg::OnBnClickedSelectConnectionsButton)
ON_BN_CLICKED(IDC_RESET_AUTO_RUN_BUTTON, &CGeneralSettingsDlg::OnBnClickedResetAutoRunButton)
END_MESSAGE_MAP()
// CGeneralSettingsDlg 消息处理程序
BOOL CGeneralSettingsDlg::OnInitDialog()
{
CTabDlg::OnInitDialog();
// TODO: 在此添加额外的初始化
((CButton*)GetDlgItem(IDC_CHECK_UPDATE_CHECK))->SetCheck(m_data.check_update_when_start);
((CButton*)GetDlgItem(IDC_ALLOW_SKIN_FONT_CHECK))->SetCheck(m_data.allow_skin_cover_font);
((CButton*)GetDlgItem(IDC_ALLOW_SKIN_DISP_STR_CHECK))->SetCheck(m_data.allow_skin_cover_text);
if (theApp.IsForceShowNotifyIcon())
{
m_data.show_notify_icon = true;
EnableDlgCtrl(IDC_SHOW_NOTIFY_ICON_CHECK, FALSE);
}
CheckDlgButton(IDC_SHOW_NOTIFY_ICON_CHECK, m_data.show_notify_icon);
if (m_data.update_source == 0)
CheckDlgButton(IDC_GITHUB_RADIO, TRUE);
else
CheckDlgButton(IDC_GITEE_RADIO, TRUE);
//检查开始菜单的“启动”目录下有没有程序的快捷方式,如果有则设置开机自启动,然后删除快捷方式
wstring start_up_path = CCommon::GetStartUpPath();
bool shortcut_exist = CCommon::FileExist((start_up_path + L"\\TrafficMonitor.lnk").c_str());
if (shortcut_exist)
{
theApp.SetAutoRun(true);
m_data.auto_run = true;
DeleteFile((start_up_path + L"\\TrafficMonitor.lnk").c_str());
}
else
{
m_data.auto_run = theApp.GetAutoRun(&m_auto_run_path);
}
((CButton*)GetDlgItem(IDC_SAVE_TO_APPDATA_RADIO))->SetCheck(!m_data.portable_mode);
((CButton*)GetDlgItem(IDC_SAVE_TO_PROGRAM_DIR_RADIO))->SetCheck(m_data.portable_mode);
GetDlgItem(IDC_SAVE_TO_PROGRAM_DIR_RADIO)->EnableWindow(theApp.m_module_dir_writable);
((CButton*)GetDlgItem(IDC_AUTO_RUN_CHECK))->SetCheck(m_data.auto_run);
((CButton*)GetDlgItem(IDC_TODAY_TRAFFIC_TIP_CHECK))->SetCheck(m_data.traffic_tip_enable);
m_traffic_tip_edit.SetRange(1, 32767);
m_traffic_tip_edit.SetValue(m_data.traffic_tip_value);
m_traffic_tip_combo.AddString(_T("MB"));
m_traffic_tip_combo.AddString(_T("GB"));
m_traffic_tip_combo.SetCurSel(m_data.traffic_tip_unit);
CheckDlgButton(IDC_MEMORY_USAGE_TIP_CHECK, m_data.memory_usage_tip.enable);
m_memory_tip_edit.SetRange(1, 100);
m_memory_tip_edit.SetValue(m_data.memory_usage_tip.tip_value);
CheckDlgButton(IDC_CPU_TEMP_TIP_CHECK, m_data.cpu_temp_tip.enable);
m_cpu_temp_tip_edit.SetRange(1, 100);
m_cpu_temp_tip_edit.SetValue(m_data.cpu_temp_tip.tip_value);
CheckDlgButton(IDC_GPU_TEMP_TIP_CHECK, m_data.gpu_temp_tip.enable);
m_gpu_temp_tip_edit.SetRange(1, 100);
m_gpu_temp_tip_edit.SetValue(m_data.gpu_temp_tip.tip_value);
CheckDlgButton(IDC_HDD_TEMP_TIP_CHECK, m_data.hdd_temp_tip.enable);
m_hdd_temp_tip_edit.SetRange(1, 100);
m_hdd_temp_tip_edit.SetValue(m_data.hdd_temp_tip.tip_value);
CheckDlgButton(IDC_MBD_TEMP_TIP_CHECK, m_data.mainboard_temp_tip.enable);
m_mbd_temp_tip_edit.SetRange(1, 100);
m_mbd_temp_tip_edit.SetValue(m_data.mainboard_temp_tip.tip_value);
SetControlEnable();
m_language_combo.AddString(CCommon::LoadText(IDS_FOLLOWING_SYSTEM));
m_language_combo.AddString(_T("English"));
m_language_combo.AddString(_T("简体中文"));
m_language_combo.AddString(_T("繁體中文"));
m_language_combo.SetCurSel(static_cast(m_data.language));
((CButton*)GetDlgItem(IDC_SHOW_ALL_CONNECTION_CHECK))->SetCheck(m_data.show_all_interface);
m_toolTip.Create(this);
m_toolTip.SetMaxTipWidth(theApp.DPI(300));
m_toolTip.AddTool(GetDlgItem(IDC_SHOW_ALL_CONNECTION_CHECK), CCommon::LoadText(IDS_SHOW_ALL_INFO_TIP));
m_toolTip.AddTool(GetDlgItem(IDC_SAVE_TO_APPDATA_RADIO), theApp.m_appdata_dir.c_str());
m_toolTip.AddTool(GetDlgItem(IDC_SAVE_TO_PROGRAM_DIR_RADIO), theApp.m_module_dir.c_str());
AddOrUpdateAutoRunTooltip(true);
((CButton*)GetDlgItem(IDC_USE_CPU_TIME_RADIO))->SetCheck(m_data.m_get_cpu_usage_by_cpu_times);
((CButton*)GetDlgItem(IDC_USE_PDH_RADIO))->SetCheck(!m_data.m_get_cpu_usage_by_cpu_times);
m_monitor_span_edit.SetRange(MONITOR_TIME_SPAN_MIN, MONITOR_TIME_SPAN_MAX);
m_monitor_span_edit.SetValue(m_data.monitor_time_span);
m_monitor_time_span_ori = m_data.monitor_time_span;
m_update_source_ori = m_data.update_source;
#ifndef WITHOUT_TEMPERATURE
//初始化硬件监控Check box
CheckDlgButton(IDC_CPU_CHECK, m_data.IsHardwareEnable(HI_CPU));
CheckDlgButton(IDC_GPU_CHECK, m_data.IsHardwareEnable(HI_GPU));
CheckDlgButton(IDC_HDD_CHECK, m_data.IsHardwareEnable(HI_HDD));
CheckDlgButton(IDC_MBD_CHECK, m_data.IsHardwareEnable(HI_MBD));
if (theApp.m_pMonitor != nullptr)
{
CSingleLock sync(&theApp.m_minitor_lib_critical, TRUE);
//初始化选择硬盘下拉列表
for (const auto& hdd_item : theApp.m_pMonitor->AllHDDTemperature())
m_hard_disk_combo.AddString(hdd_item.first.c_str());
int cur_index = m_hard_disk_combo.FindString(-1, m_data.hard_disk_name.c_str());
m_hard_disk_combo.SetCurSel(cur_index);
//初始化选择CPU下拉列表
m_select_cpu_combo.AddString(CCommon::LoadText(IDS_AVREAGE_TEMPERATURE));
for (const auto& cpu_item : theApp.m_pMonitor->AllCpuTemperature())
m_select_cpu_combo.AddString(cpu_item.first.c_str());
cur_index = m_select_cpu_combo.FindString(-1, m_data.cpu_core_name.c_str());
if (cur_index < 0)
cur_index = 0;
m_select_cpu_combo.SetCurSel(cur_index);
}
#endif
//不含温度监控的版本,禁用温度相关的控件
#ifdef WITHOUT_TEMPERATURE
EnableDlgCtrl(IDC_CPU_TEMP_TIP_CHECK, false);
EnableDlgCtrl(IDC_CPU_TEMP_TIP_EDIT, false);
EnableDlgCtrl(IDC_GPU_TEMP_TIP_CHECK, false);
EnableDlgCtrl(IDC_GPU_TEMP_TIP_EDIT, false);
EnableDlgCtrl(IDC_HDD_TEMP_TIP_CHECK, false);
EnableDlgCtrl(IDC_HDD_TIP_EDIT, false);
EnableDlgCtrl(IDC_MBD_TEMP_TIP_CHECK, false);
EnableDlgCtrl(IDC_MBD_TEMP_TIP_EDIT, false);
EnableDlgCtrl(IDC_CPU_CHECK, false);
EnableDlgCtrl(IDC_GPU_CHECK, false);
EnableDlgCtrl(IDC_HDD_CHECK, false);
EnableDlgCtrl(IDC_MBD_CHECK, false);
EnableDlgCtrl(IDC_SELECT_HARD_DISK_COMBO, false);
EnableDlgCtrl(IDC_SELECT_CPU_COMBO, false);
EnableDlgCtrl(IDC_CPU_TEMP_STATIC, false);
EnableDlgCtrl(IDC_GPU_TEMP_STATIC, false);
EnableDlgCtrl(IDC_HDD_STATIC, false);
EnableDlgCtrl(IDC_MBD_TEMP_STATIC, false);
EnableDlgCtrl(IDC_SELECT_HDD_STATIC, false);
EnableDlgCtrl(IDC_SELECT_CPU_STATIC, false);
EnableDlgCtrl(IDC_HARDWARE_MONITOR_STATIC, false);
#endif
m_plugin_manager_btn.SetIcon(theApp.GetMenuIcon(IDI_PLUGINS));
m_select_connection_btn.SetIcon(theApp.GetMenuIcon(IDI_CONNECTION));
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CGeneralSettingsDlg::OnBnClickedCheckNowButton()
{
// TODO: 在此添加控件通知处理程序代码
theApp.CheckUpdateInThread(true);
}
void CGeneralSettingsDlg::OnBnClickedCheckUpdateCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.check_update_when_start = (((CButton*)GetDlgItem(IDC_CHECK_UPDATE_CHECK))->GetCheck() != 0);
}
void CGeneralSettingsDlg::OnBnClickedAutoRunCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.auto_run = (((CButton*)GetDlgItem(IDC_AUTO_RUN_CHECK))->GetCheck() != 0);
m_auto_run_modified = true;
}
void CGeneralSettingsDlg::OnBnClickedAllowSkinFontCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.allow_skin_cover_font = (((CButton*)GetDlgItem(IDC_ALLOW_SKIN_FONT_CHECK))->GetCheck() != 0);
}
void CGeneralSettingsDlg::OnBnClickedAllowSkinDispStrCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.allow_skin_cover_text = (((CButton*)GetDlgItem(IDC_ALLOW_SKIN_DISP_STR_CHECK))->GetCheck() != 0);
}
void CGeneralSettingsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//获取消息提示的设置
m_data.traffic_tip_value = m_traffic_tip_edit.GetValue();
if (m_data.traffic_tip_value < 1) m_data.traffic_tip_value = 1;
if (m_data.traffic_tip_value > 32767) m_data.traffic_tip_value = 32767;
m_data.traffic_tip_unit = m_traffic_tip_combo.GetCurSel();
auto checkTipValue = [](int& value)
{
if (value < 1) value = 1;
if (value > 100) value = 100;
};
m_data.memory_usage_tip.tip_value = m_memory_tip_edit.GetValue();
checkTipValue(m_data.memory_usage_tip.tip_value);
m_data.cpu_temp_tip.tip_value = m_cpu_temp_tip_edit.GetValue();
checkTipValue(m_data.cpu_temp_tip.tip_value);
m_data.gpu_temp_tip.tip_value = m_gpu_temp_tip_edit.GetValue();
checkTipValue(m_data.gpu_temp_tip.tip_value);
m_data.hdd_temp_tip.tip_value = m_hdd_temp_tip_edit.GetValue();
checkTipValue(m_data.hdd_temp_tip.tip_value);
m_data.mainboard_temp_tip.tip_value = m_mbd_temp_tip_edit.GetValue();
checkTipValue(m_data.mainboard_temp_tip.tip_value);
//获取语言的设置
m_data.language = static_cast(m_language_combo.GetCurSel());
if (m_data.language != theApp.m_general_data.language)
{
MessageBox(CCommon::LoadText(IDS_LANGUAGE_CHANGE_INFO), NULL, MB_ICONINFORMATION | MB_OK);
}
m_show_all_interface_modified = (m_data.show_all_interface != theApp.m_general_data.show_all_interface);
//获取数据文件保存位置的设置
m_data.portable_mode = (((CButton*)GetDlgItem(IDC_SAVE_TO_PROGRAM_DIR_RADIO))->GetCheck() != 0);
if (m_data.portable_mode != theApp.m_general_data.portable_mode)
{
MessageBox(CCommon::LoadText(IDS_CFG_DIR_CHANGED_INFO), NULL, MB_ICONINFORMATION | MB_OK);
}
m_data.monitor_time_span = m_monitor_span_edit.GetValue();
//m_taskbar_item_modified = (theApp.m_taskbar_data.m_tbar_display_item != taskbar_displat_item_ori);
CTabDlg::OnOK();
}
void CGeneralSettingsDlg::OnBnClickedTodayTrafficTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.traffic_tip_enable = (((CButton*)GetDlgItem(IDC_TODAY_TRAFFIC_TIP_CHECK))->GetCheck() != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedMemoryUsageTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.memory_usage_tip.enable = (((CButton*)GetDlgItem(IDC_MEMORY_USAGE_TIP_CHECK))->GetCheck() != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedOpenConfigPathButton()
{
// TODO: 在此添加控件通知处理程序代码
ShellExecute(NULL, _T("explore"), theApp.m_config_dir.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
void CGeneralSettingsDlg::OnBnClickedShowAllConnectionCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_all_interface = (((CButton*)GetDlgItem(IDC_SHOW_ALL_CONNECTION_CHECK))->GetCheck() != 0);
}
BOOL CGeneralSettingsDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_MOUSEMOVE)
m_toolTip.RelayEvent(pMsg);
return CTabDlg::PreTranslateMessage(pMsg);
}
void CGeneralSettingsDlg::OnBnClickedUseCpuTimeRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_get_cpu_usage_by_cpu_times = true;
}
void CGeneralSettingsDlg::OnBnClickedUsePdhRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_get_cpu_usage_by_cpu_times = false;
}
void CGeneralSettingsDlg::OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult)
{
//这里响应微调按钮(spin button)点击上下按钮时的事件,
//CSpinButtonCtrl的对象是作为CSpinEdit的成员变量的,而此消息会向CSpinButtonCtrl的父窗口发送,但是CSpinEdit不是它的父窗口,
//因此此消息无法在CSpinEdit中响应,只能在这里响应。
//所有CSpinEdit类中的Spin按钮点击时的响应都在这里,因为这些Spin按钮的ID都是“SPIN_ID”。
//通过GetBuddy的返回值判断微调按钮是属于哪个EditBox的。
CSpinButtonCtrl* pSpin = (CSpinButtonCtrl*)CWnd::FromHandle(pNMHDR->hwndFrom);
if (pSpin == nullptr)
return;
CWnd* pEdit = pSpin->GetBuddy();
if (pEdit == &m_monitor_span_edit) //当用户点击了“监控时间间隔”的微调按钮时
{
LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR);
if (pNMUpDown->iDelta == -1)
{
// 用户按下了spin控件的向下箭头
int value = m_monitor_span_edit.GetValue();
value -= MONITOR_SPAN_STEP;
value /= MONITOR_SPAN_STEP;
value *= MONITOR_SPAN_STEP;
m_monitor_span_edit.SetValue(value);
}
else if (pNMUpDown->iDelta == 1)
{
// 用户按下了spin控件的向上箭头
int value = m_monitor_span_edit.GetValue();
value += MONITOR_SPAN_STEP;
value /= MONITOR_SPAN_STEP;
value *= MONITOR_SPAN_STEP;
m_monitor_span_edit.SetValue(value);
}
pNMUpDown->iDelta = 0;
}
*pResult = 0;
}
void CGeneralSettingsDlg::OnEnKillfocusMonitorSpanEdit()
{
// TODO: 在此添加控件通知处理程序代码
//这里限制监控时间间隔只能输入100的倍数
CString str;
GetDlgItemText(IDC_MONITOR_SPAN_EDIT, str);
str.Replace(_T(","), _T(""));
int value = _ttoi(str.GetString());
if (value < MONITOR_TIME_SPAN_MIN || value > MONITOR_TIME_SPAN_MAX)
{
value = 1000;
}
else
{
value /= MONITOR_SPAN_STEP;
value *= MONITOR_SPAN_STEP;
}
m_monitor_span_edit.SetValue(value);
}
void CGeneralSettingsDlg::OnBnClickedCpuTempTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.cpu_temp_tip.enable = (IsDlgButtonChecked(IDC_CPU_TEMP_TIP_CHECK) != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedGpuTempTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.gpu_temp_tip.enable = (IsDlgButtonChecked(IDC_GPU_TEMP_TIP_CHECK) != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedHddTempTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hdd_temp_tip.enable = (IsDlgButtonChecked(IDC_HDD_TEMP_TIP_CHECK) != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedMbdTempTipCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.mainboard_temp_tip.enable = (IsDlgButtonChecked(IDC_MBD_TEMP_TIP_CHECK) != 0);
SetControlEnable();
}
void CGeneralSettingsDlg::OnBnClickedGithubRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.update_source = 0;
theApp.m_general_data.update_source = 0; //点击“更新源”的单选按钮时,同时更改theApp中的设置,以确保点击“立即检查”按钮时使用选择的更新源更新
}
void CGeneralSettingsDlg::OnBnClickedGiteeRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.update_source = 1;
theApp.m_general_data.update_source = 1;
}
void CGeneralSettingsDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
theApp.m_general_data.update_source = m_update_source_ori; //点击“取消”时恢复开始的“更新源”选项
CTabDlg::OnCancel();
}
void CGeneralSettingsDlg::OnBnClickedRestoreDefaultTimeSpanButton()
{
// TODO: 在此添加控件通知处理程序代码
m_monitor_span_edit.SetValue(1000);
}
void CGeneralSettingsDlg::OnCbnSelchangeSelectHardDiskCombo()
{
// TODO: 在此添加控件通知处理程序代码
CString hard_disk_name;
m_hard_disk_combo.GetWindowText(hard_disk_name);
m_data.hard_disk_name = hard_disk_name.GetString();
}
void CGeneralSettingsDlg::OnBnClickedCpuCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = IsDlgButtonChecked(IDC_CPU_CHECK) != 0;
if (checked && !ShowHardwareMonitorWarning())
{
checked = false;
CheckDlgButton(IDC_CPU_CHECK, FALSE);
}
m_data.SetHardwareEnable(HI_CPU, checked);
}
void CGeneralSettingsDlg::OnBnClickedGpuCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = IsDlgButtonChecked(IDC_GPU_CHECK) != 0;
if (checked && !ShowHardwareMonitorWarning())
{
checked = false;
CheckDlgButton(IDC_GPU_CHECK, FALSE);
}
m_data.SetHardwareEnable(HI_GPU, checked);
}
void CGeneralSettingsDlg::OnBnClickedHddCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = IsDlgButtonChecked(IDC_HDD_CHECK) != 0;
if (checked && !ShowHardwareMonitorWarning())
{
checked = false;
CheckDlgButton(IDC_HDD_CHECK, FALSE);
}
m_data.SetHardwareEnable(HI_HDD, checked);
}
void CGeneralSettingsDlg::OnBnClickedMbdCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = IsDlgButtonChecked(IDC_MBD_CHECK) != 0;
if (checked && !ShowHardwareMonitorWarning())
{
checked = false;
CheckDlgButton(IDC_MBD_CHECK, FALSE);
}
m_data.SetHardwareEnable(HI_MBD, checked);
}
void CGeneralSettingsDlg::OnCbnSelchangeSelectCpuCombo()
{
// TODO: 在此添加控件通知处理程序代码
CString cpu_core_name;
m_select_cpu_combo.GetWindowText(cpu_core_name);
m_data.cpu_core_name = cpu_core_name.GetString();
}
void CGeneralSettingsDlg::OnBnClickedPluginManageButton()
{
// TODO: 在此添加控件通知处理程序代码
CPluginManagerDlg dlg;
dlg.DoModal();
}
void CGeneralSettingsDlg::OnBnClickedShowNotifyIconCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_notify_icon = (IsDlgButtonChecked(IDC_SHOW_NOTIFY_ICON_CHECK) != 0);
}
void CGeneralSettingsDlg::OnBnClickedSelectConnectionsButton()
{
// TODO: 在此添加控件通知处理程序代码
CSelectConnectionsDlg dlg(m_data.connections_hide);
if (dlg.DoModal() == IDOK)
{
m_data.connections_hide = dlg.GetData();
}
}
void CGeneralSettingsDlg::OnBnClickedResetAutoRunButton()
{
//先删除开机自动运行
theApp.SetAutoRunByRegistry(false);
theApp.SetAutoRunByTaskScheduler(false);
if (!theApp.SetAutoRun(true)) //重新设置开机自动运行
{
MessageBox(CCommon::LoadText(IDS_SET_AUTO_RUN_FAILED_WARNING), NULL, MB_ICONWARNING | MB_OK);
return;
}
//获取开机自动运行的路径
bool auto_run = theApp.GetAutoRun(&m_auto_run_path);
//重新勾选“开机自动运行”复选框
CheckDlgButton(IDC_AUTO_RUN_CHECK, auto_run);
//更新鼠标提示
AddOrUpdateAutoRunTooltip(false);
}
================================================
FILE: TrafficMonitor/GeneralSettingsDlg.h
================================================
#pragma once
#include "TabDlg.h"
#include "SpinEdit.h"
#include "ComboBox2.h"
// CGeneralSettingsDlg dialog
class CGeneralSettingsDlg : public CTabDlg
{
DECLARE_DYNAMIC(CGeneralSettingsDlg)
public:
CGeneralSettingsDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CGeneralSettingsDlg();
static void CheckTaskbarDisplayItem();
//选项设置数据
GeneralSettingData m_data;
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_GENERAL_SETTINGS_DIALOG };
#endif
public:
bool IsAutoRunModified() const { return m_auto_run_modified; }
bool IsShowAllInterfaceModified() const { return m_show_all_interface_modified; }
bool IsMonitorTimeSpanModified() const;
//bool IsTaskbarItemModified() const { return m_taskbar_item_modified; }
protected:
bool m_auto_run_modified{ false }; //如果更改了开机自动运行的设置,则会置为true
bool m_show_all_interface_modified{ false };
int m_monitor_time_span_ori{};
int m_update_source_ori{};
//bool m_taskbar_item_modified{ false };
wstring m_auto_run_path;
//控件变量
CSpinEdit m_traffic_tip_edit;
CComboBox2 m_traffic_tip_combo;
CSpinEdit m_memory_tip_edit;
CComboBox2 m_language_combo;
CToolTipCtrl m_toolTip;
CSpinEdit m_monitor_span_edit;
CSpinEdit m_cpu_temp_tip_edit;
CSpinEdit m_gpu_temp_tip_edit;
CSpinEdit m_hdd_temp_tip_edit;
CSpinEdit m_mbd_temp_tip_edit;
CComboBox2 m_hard_disk_combo;
CComboBox2 m_select_cpu_combo;
CButton m_plugin_manager_btn;
CButton m_select_connection_btn;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//设置控件的启用和禁用
void SetControlEnable();
virtual void SetControlMouseWheelEnable(bool enable) override;
//显示开启硬件监控时的提示,如果用户选择了“是”则返回true,否则返回false
//“以后不再显示该对话框”的标记保存在注册表“\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DontShowMeThisDialogAgain”
bool ShowHardwareMonitorWarning();
//添加或更新开机自动运行的鼠标提示
void AddOrUpdateAutoRunTooltip(bool add);
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedCheckNowButton();
afx_msg void OnBnClickedCheckUpdateCheck();
afx_msg void OnBnClickedAutoRunCheck();
afx_msg void OnBnClickedAllowSkinFontCheck();
afx_msg void OnBnClickedAllowSkinDispStrCheck();
virtual void OnOK();
afx_msg void OnBnClickedTodayTrafficTipCheck();
afx_msg void OnBnClickedMemoryUsageTipCheck();
afx_msg void OnBnClickedOpenConfigPathButton();
afx_msg void OnBnClickedShowAllConnectionCheck();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnBnClickedUseCpuTimeRadio();
afx_msg void OnBnClickedUsePdhRadio();
afx_msg void OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnEnKillfocusMonitorSpanEdit();
afx_msg void OnBnClickedCpuTempTipCheck();
afx_msg void OnBnClickedGpuTempTipCheck();
afx_msg void OnBnClickedHddTempTipCheck();
afx_msg void OnBnClickedMbdTempTipCheck();
afx_msg void OnBnClickedGithubRadio();
afx_msg void OnBnClickedGiteeRadio();
virtual void OnCancel();
afx_msg void OnBnClickedRestoreDefaultTimeSpanButton();
afx_msg void OnCbnSelchangeSelectHardDiskCombo();
afx_msg void OnBnClickedCpuCheck();
afx_msg void OnBnClickedGpuCheck();
afx_msg void OnBnClickedHddCheck();
afx_msg void OnBnClickedMbdCheck();
afx_msg void OnCbnSelchangeSelectCpuCombo();
afx_msg void OnBnClickedPluginManageButton();
afx_msg void OnBnClickedShowNotifyIconCheck();
afx_msg void OnBnClickedSelectConnectionsButton();
afx_msg void OnBnClickedResetAutoRunButton();
};
================================================
FILE: TrafficMonitor/HighResolutionTimer.h
================================================
#include
#pragma comment(lib, "WINMM.LIB")
class CHighResolutionTimer
{
typedef void(*TIMERCALLBACK)(DWORD_PTR);
private:
DWORD_PTR m_dwUser;
MMRESULT m_nIDTimer;
UINT m_uDelay;
TIMERCALLBACK m_pfnCallback;
public:
void KillTimer()
{
if (m_nIDTimer != NULL)
{
timeKillEvent(m_nIDTimer);
m_nIDTimer = NULL;
}
}
CHighResolutionTimer()
{
m_nIDTimer = NULL;
m_uDelay = 0;
m_pfnCallback = 0;
}
virtual ~CHighResolutionTimer()
{
KillTimer();
}
static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
// dwUser contains ptr to Timer object
CHighResolutionTimer * ptimer = (CHighResolutionTimer *)dwUser;
// Call user-specified callback and pass back user specified data
(ptimer->m_pfnCallback) (ptimer->m_dwUser);
}
BOOL CreateTimer(DWORD_PTR dwUser, UINT uDelay, TIMERCALLBACK lpTimeProc)
{
ASSERT(dwUser);
ASSERT(lpTimeProc);
m_dwUser = dwUser;
m_pfnCallback = lpTimeProc;
BOOL ret = FALSE;
KillTimer();
m_nIDTimer = timeSetEvent(uDelay, 0, (LPTIMECALLBACK)TimeProc, (DWORD_PTR)this, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
if (m_nIDTimer != NULL)
ret = TRUE;
return(ret);
}
};
================================================
FILE: TrafficMonitor/HistoryTrafficCalendarDlg.cpp
================================================
// HistoryTrafficCalendarDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "HistoryTrafficCalendarDlg.h"
#include "afxdialogex.h"
// CHistoryTrafficCalendarDlg 对话框
IMPLEMENT_DYNAMIC(CHistoryTrafficCalendarDlg, CTabDlg)
CHistoryTrafficCalendarDlg::CHistoryTrafficCalendarDlg(deque& history_traffics, CWnd* pParent /*=nullptr*/)
: CTabDlg(IDD_HISTORY_TRAFFIC_CALENDAR_DIALOG, pParent), m_history_traffics(history_traffics)
{
}
CHistoryTrafficCalendarDlg::~CHistoryTrafficCalendarDlg()
{
}
void CHistoryTrafficCalendarDlg::SetDayTraffic()
{
for (int i{}; i < CALENDAR_HEIGHT; i++)
{
for (int j{}; j < CALENDAR_WIDTH; j++)
{
HistoryTraffic history_traffic;
history_traffic.year = m_year;
history_traffic.month = m_month;
history_traffic.day = m_calendar[i][j].day;
//使用二分法查找日历中每一个日期的流量
if (history_traffic.day > 0)
{
if (std::binary_search(m_history_traffics.begin(), m_history_traffics.end(), history_traffic, HistoryTraffic::DateGreater))
{
auto iter = std::lower_bound(m_history_traffics.begin(), m_history_traffics.end(), history_traffic, HistoryTraffic::DateGreater);
if (iter != m_history_traffics.end())
{
m_calendar[i][j].up_traffic = iter->up_kBytes;
m_calendar[i][j].down_traffic = iter->down_kBytes;
m_calendar[i][j].mixed = iter->mixed;
}
}
}
}
}
}
void CHistoryTrafficCalendarDlg::MonthSelectChanged()
{
CCalendarHelper::GetCalendar(m_year, m_month, m_calendar, theApp.m_cfg_data.m_sunday_first);
SetDayTraffic();
CalculateMonthTotalTraffic();
InvalidateRect(m_draw_rect);
}
void CHistoryTrafficCalendarDlg::CalculateMonthTotalTraffic()
{
m_month_total_upload = 0;
m_month_total_download = 0;
for (int i{}; i < CALENDAR_HEIGHT; i++)
{
for (int j{}; j < CALENDAR_WIDTH; j++)
{
m_month_total_upload += m_calendar[i][j].up_traffic;
m_month_total_download += m_calendar[i][j].down_traffic;
}
}
}
void CHistoryTrafficCalendarDlg::SetComboSel()
{
int cnt{};
int year_selected;
for (int i{ m_year_max }; i >= m_year_min; i--)
{
if (i == m_year)
year_selected = cnt;
cnt++;
}
m_year_combo.SetCurSel(year_selected);
m_month_combo.SetCurSel(m_month - 1);
}
void CHistoryTrafficCalendarDlg::DoDataExchange(CDataExchange* pDX)
{
CTabDlg::DoDataExchange(pDX);
DDX_Control(pDX, IDC_YEAR_COMBO, m_year_combo);
DDX_Control(pDX, IDC_MONTH_COMBO, m_month_combo);
}
bool CHistoryTrafficCalendarDlg::IsWeekend(int index)
{
if (theApp.m_cfg_data.m_sunday_first)
return (index == 0 || index == 6);
else
return (index == 5 || index == 6);
}
CString CHistoryTrafficCalendarDlg::GetWeekdayString(int index)
{
CString str;
if (!theApp.m_cfg_data.m_sunday_first)
{
index++;
if (index > 6)
index = 0;
}
switch (index)
{
case 0:
str.LoadString(IDS_SUNDAY);
break;
case 1:
str.LoadString(IDS_MONDAY);
break;
case 2:
str.LoadString(IDS_TUESDAY);
break;
case 3:
str.LoadString(IDS_WEDNESDAY);
break;
case 4:
str.LoadString(IDS_THURSDAY);
break;
case 5:
str.LoadString(IDS_FRIDAY);
break;
case 6:
str.LoadString(IDS_SATURDAY);
break;
}
return str;
}
BEGIN_MESSAGE_MAP(CHistoryTrafficCalendarDlg, CTabDlg)
ON_WM_PAINT()
ON_CBN_SELCHANGE(IDC_YEAR_COMBO, &CHistoryTrafficCalendarDlg::OnCbnSelchangeYearCombo)
ON_CBN_SELCHANGE(IDC_MONTH_COMBO, &CHistoryTrafficCalendarDlg::OnCbnSelchangeMonthCombo)
ON_WM_MOUSEMOVE()
ON_BN_CLICKED(IDC_PREVIOUS_BUTTON, &CHistoryTrafficCalendarDlg::OnBnClickedPreviousButton)
ON_BN_CLICKED(IDC_NEXT_BUTTON, &CHistoryTrafficCalendarDlg::OnBnClickedNextButton)
ON_WM_MOUSEWHEEL()
ON_BN_CLICKED(IDC_MENU_BUTTON, &CHistoryTrafficCalendarDlg::OnBnClickedMenuButton)
ON_WM_INITMENU()
ON_COMMAND(ID_FIRST_DAY_OF_WEEK_SUNDAY, &CHistoryTrafficCalendarDlg::OnFirstDayOfWeekSunday)
ON_COMMAND(ID_FIRST_DAY_OF_WEEK_MONDAY, &CHistoryTrafficCalendarDlg::OnFirstDayOfWeekMonday)
ON_COMMAND(ID_CALENDAR_JUMP_TO_TODAY, &CHistoryTrafficCalendarDlg::OnCalendarJumpToToday)
END_MESSAGE_MAP()
// CHistoryTrafficCalendarDlg 消息处理程序
BOOL CHistoryTrafficCalendarDlg::OnInitDialog()
{
CTabDlg::OnInitDialog();
// TODO: 在此添加额外的初始化
m_year = m_history_traffics[0].year;
m_month = m_history_traffics[0].month;
CCalendarHelper::GetCalendar(m_year, m_month, m_calendar, theApp.m_cfg_data.m_sunday_first);
SetDayTraffic();
CalculateMonthTotalTraffic();
//初始化Combo Box
m_year_max = m_history_traffics[0].year;
m_year_min = m_history_traffics.back().year;
for (int i{ m_year_max }; i >= m_year_min; i--)
{
m_year_combo.AddString(CCommon::IntToString(i));
}
m_year_combo.SetCurSel(0);
for (int i{ 1 }; i <= 12; i++)
{
m_month_combo.AddString(CCommon::IntToString(i));
}
m_month_combo.SetCurSel(m_month - 1);
//初始化鼠标提示
m_tool_tips.Create(this, TTS_ALWAYSTIP | TTS_NOPREFIX);
m_tool_tips.SetMaxTipWidth(800); //为鼠标提示设置一个最大宽度,以允许其换行
m_tool_tips.AddTool(this, _T(""));
m_menu.LoadMenu(IDR_HISTORY_TRAFFIC_MENU);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CHistoryTrafficCalendarDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CTabDlg::OnPaint()
CRect wndRect;
GetWindowRect(wndRect);
const int width = theApp.DPI(40); //日历中每一个格子的宽度
const int height = theApp.DPI(30); //日历中第一个格子的高度
m_draw_rect.left = m_start_x;
m_draw_rect.top = m_start_y;
//m_draw_rect.right = m_draw_rect.left + (CALENDAR_WIDTH*width);
m_draw_rect.right = wndRect.Width() - m_start_x;
m_draw_rect.bottom = m_draw_rect.top + ((CALENDAR_HEIGHT + 2) * height) + theApp.DPI(20);
//使用双缓冲绘图
CDrawDoubleBuffer draw_double_buffer(&dc, m_draw_rect);
//绘图
CDrawCommon draw;
draw.Create(draw_double_buffer.GetMemDC(), this);
draw.FillRect(CRect(0, 0, m_draw_rect.Width(), m_draw_rect.Height()), RGB(255, 255, 255)); //填充白色背景色
CRect rect{};
rect.left = 0;
rect.top = 0;
rect.right = rect.left + width;
rect.bottom = rect.top + height;
//画星期的行
for (int i{}; i < CALENDAR_WIDTH; i++)
{
rect.MoveToX(i * width);
if (IsWeekend(i))
draw.SetBackColor(RGB(217, 86, 86));
else if (i % 2 == 0)
draw.SetBackColor(RGB(1, 84, 151));
else
draw.SetBackColor(RGB(1, 107, 191));
CString str = GetWeekdayString(i);
draw.DrawWindowText(rect, str, RGB(255, 255, 255), Alignment::CENTER, true);
}
//绘制日历
for (int i{}; i < CALENDAR_HEIGHT; i++)
{
for (int j{}; j < CALENDAR_WIDTH; j++)
{
//设置日历中每一天的矩形的位置
rect.MoveToXY(j * width, height + theApp.DPI(2) + i * height);
//保存矩形的位置
m_calendar[i][j].rect = rect;
//绘制日期的数据
//绘制格子的背景颜色
if (IsWeekend(j)) //是周末时
{
if ((i + j) % 2 == 0)
draw.SetBackColor(RGB(250, 234, 234));
else
draw.SetBackColor(RGB(252, 242, 242));
}
else
{
if ((i + j) % 2 == 0)
draw.SetBackColor(RGB(226, 241, 254));
else
draw.SetBackColor(RGB(236, 246, 254));
}
draw.FillRectWithBackColor(rect);
//绘制格子上的日期的数字
CRect day_rect{ rect };
day_rect.bottom -= (rect.Height() / 2);
COLORREF text_color;
if (IsWeekend(j))
text_color = RGB(131, 29, 28);
else
text_color = RGB(0, 57, 107);
if (m_calendar[i][j].day != 0)
draw.DrawWindowText(day_rect, CCommon::IntToString(m_calendar[i][j].day), text_color, Alignment::CENTER, true);
//在今天的日期上画一个矩形框
COLORREF frame_color;
if (IsWeekend(j))
frame_color = RGB(218, 91, 91);
else
frame_color = RGB(1, 133, 238);
if (m_year == m_history_traffics[0].year && m_month == m_history_traffics[0].month && m_calendar[i][j].day == m_history_traffics[0].day)
draw.DrawRectOutLine(rect, frame_color, theApp.DPI(2));
//绘制指示流量大小的矩形
COLORREF color;
if (m_calendar[i][j].traffic() < 1024 * 1024) //流量小于1GB时绘制蓝色
color = TRAFFIC_COLOR_BLUE;
else if (m_calendar[i][j].traffic() < 10 * 1024 * 1024) //流量小于10GB时绘制绿色
color = TRAFFIC_COLOR_GREEN;
else if (m_calendar[i][j].traffic() < 100 * 1024 * 1024) //流量小于100GB时绘制黄色
color = TRAFFIC_COLOR_YELLOE;
else if (m_calendar[i][j].traffic() < 1024 * 1024 * 1024) //流量小于1TB时绘制红色
color = TRAFFIC_COLOR_RED;
else //流量超过1TB时显示深红色
color = TRAFFIC_COLOR_DARK_RED;
if (m_calendar[i][j].traffic() > 0)
{
CRect traffic_rect;
traffic_rect.left = rect.left + theApp.DPI(14);
traffic_rect.right = traffic_rect.left + theApp.DPI(12);
traffic_rect.top = rect.top + theApp.DPI(16);
traffic_rect.bottom = traffic_rect.top + theApp.DPI(12);
draw.FillRect(traffic_rect, color);
}
}
}
const COLORREF text_color{ RGB(0, 57, 107) };
//画当前月总流量
CString info;
info.Format(_T("%s %s (%s: %s, %s: %s)"), CCommon::LoadText(IDS_CURRENT_MONTH_TOTAL_TRAFFIC),
CCommon::KBytesToString(m_month_total_upload + m_month_total_download),
CCommon::LoadText(IDS_UPLOAD), CCommon::KBytesToString(m_month_total_upload),
CCommon::LoadText(IDS_DOWNLOAD), CCommon::KBytesToString(m_month_total_download)
);
CRect info_rect;
info_rect.left = 0;
info_rect.top = height * (CALENDAR_HEIGHT + 1) + theApp.DPI(5);
info_rect.right = info_rect.left + m_draw_rect.Width();
info_rect.bottom = info_rect.top + theApp.DPI(36);
draw.SetBackColor(RGB(255, 255, 255));
draw.DrawWindowText(info_rect, info, text_color, Alignment::LEFT, true, true);
//画图例
CRect rc_legend{ info_rect };
rc_legend.MoveToXY(CALENDAR_WIDTH * width + theApp.DPI(32), theApp.DPI(16));
rc_legend.right = m_draw_rect.Width();
rc_legend.bottom = rc_legend.top + theApp.DPI(16);
draw.DrawWindowText(rc_legend, CCommon::LoadText(IDS_LEGEND, _T(":")), text_color, Alignment::LEFT, true, true);
rc_legend.MoveToY(rc_legend.bottom + theApp.DPI(6));
CRect rc_legend_box{ rc_legend };
const int box_side{ theApp.DPI(12) };
const int line_gap{ theApp.DPI(6) };
rc_legend_box.top = rc_legend.top + (rc_legend.Height() - box_side) / 2;
rc_legend_box.bottom = rc_legend_box.top + box_side;
rc_legend_box.right = rc_legend_box.left + box_side;
rc_legend.left = rc_legend_box.right + theApp.DPI(4);
//蓝色图例
draw.FillRect(rc_legend_box, TRAFFIC_COLOR_BLUE);
draw.DrawWindowText(rc_legend, _T("0~1GB"), text_color);
//绿色图例
rc_legend.MoveToY(rc_legend.bottom + line_gap);
rc_legend_box.MoveToY(rc_legend.top + (rc_legend.Height() - box_side) / 2);
draw.FillRect(rc_legend_box, TRAFFIC_COLOR_GREEN);
draw.DrawWindowText(rc_legend, _T("1GB~10GB"), text_color);
//黄色图例
rc_legend.MoveToY(rc_legend.bottom + line_gap);
rc_legend_box.MoveToY(rc_legend.top + (rc_legend.Height() - box_side) / 2);
draw.FillRect(rc_legend_box, TRAFFIC_COLOR_YELLOE);
draw.DrawWindowText(rc_legend, _T("10GB~100GB"), text_color);
//红色图例
rc_legend.MoveToY(rc_legend.bottom + line_gap);
rc_legend_box.MoveToY(rc_legend.top + (rc_legend.Height() - box_side) / 2);
draw.FillRect(rc_legend_box, TRAFFIC_COLOR_RED);
draw.DrawWindowText(rc_legend, _T("100GB~1TB"), text_color);
//深红色图例
rc_legend.MoveToY(rc_legend.bottom + line_gap);
rc_legend_box.MoveToY(rc_legend.top + (rc_legend.Height() - box_side) / 2);
draw.FillRect(rc_legend_box, TRAFFIC_COLOR_DARK_RED);
draw.DrawWindowText(rc_legend, _T("1TB~"), text_color);
}
void CHistoryTrafficCalendarDlg::OnCbnSelchangeYearCombo()
{
// TODO: 在此添加控件通知处理程序代码
int index = m_year_combo.GetCurSel();
CString str;
m_year_combo.GetLBText(index, str);
m_year = _ttoi(str);
MonthSelectChanged();
}
void CHistoryTrafficCalendarDlg::OnCbnSelchangeMonthCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_month = m_month_combo.GetCurSel() + 1;
MonthSelectChanged();
}
void CHistoryTrafficCalendarDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int tip_day{}; //要显示鼠标提示的日期
DayTraffic tip_traffic{};
static int last_tip_day{};
//查找鼠标指针在哪个日期的矩形内
for (int i{}; i < CALENDAR_HEIGHT; i++)
{
for (int j{}; j < CALENDAR_WIDTH; j++)
{
CRect rect{ m_calendar[i][j].rect };
rect.OffsetRect(m_start_x, m_start_y);
if (rect.PtInRect(point))
{
tip_day = m_calendar[i][j].day;
tip_traffic = m_calendar[i][j];
}
}
}
bool show_tip = (tip_day > 0);
if (show_tip && last_tip_day != tip_day)
{
CString tip_info;
tip_info.Format(_T("%d/%d/%d\r\n"), m_year, m_month, tip_day);
tip_info += CCommon::LoadText(IDS_TRAFFIC_USED1);
tip_info += CCommon::KBytesToString(tip_traffic.traffic());
if (!tip_traffic.mixed && tip_traffic.traffic() > 0)
{
tip_info += _T("\r\n");
tip_info += CCommon::LoadText(IDS_UPLOAD, _T(": "));
tip_info += CCommon::KBytesToString(tip_traffic.up_traffic);
tip_info += _T("\r\n");
tip_info += CCommon::LoadText(IDS_DOWNLOAD, _T(": "));
tip_info += CCommon::KBytesToString(tip_traffic.down_traffic);
}
m_tool_tips.AddTool(this, tip_info);
m_tool_tips.Pop();
last_tip_day = tip_day;
}
if (!show_tip)
{
m_tool_tips.AddTool(this, _T(""));
m_tool_tips.Pop();
last_tip_day = 0;
}
CTabDlg::OnMouseMove(nFlags, point);
}
BOOL CHistoryTrafficCalendarDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (m_tool_tips.GetSafeHwnd() != 0)
{
m_tool_tips.RelayEvent(pMsg);
}
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_LEFT)
{
OnBnClickedPreviousButton();
return TRUE;
}
if (pMsg->wParam == VK_RIGHT)
{
OnBnClickedNextButton();
return TRUE;
}
}
return CTabDlg::PreTranslateMessage(pMsg);
}
void CHistoryTrafficCalendarDlg::OnBnClickedPreviousButton()
{
// TODO: 在此添加控件通知处理程序代码
if (m_year == m_year_min && m_month == 1)
return;
m_month--;
if (m_month <= 0)
{
m_month = 12;
m_year--;
}
SetComboSel();
MonthSelectChanged();
}
void CHistoryTrafficCalendarDlg::OnBnClickedNextButton()
{
// TODO: 在此添加控件通知处理程序代码
if (m_year == m_year_max && m_month == 12)
return;
m_month++;
if (m_month > 12)
{
m_month = 1;
m_year++;
}
SetComboSel();
MonthSelectChanged();
}
BOOL CHistoryTrafficCalendarDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//通过鼠标滚轮翻页
if (zDelta > 0)
{
OnBnClickedPreviousButton();
}
if (zDelta < 0)
{
OnBnClickedNextButton();
}
return CTabDlg::OnMouseWheel(nFlags, zDelta, pt);
}
void CHistoryTrafficCalendarDlg::OnBnClickedMenuButton()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pBtn = GetDlgItem(IDC_MENU_BUTTON);
if (pBtn != nullptr)
{
CRect rect;
pBtn->GetWindowRect(rect);
CPoint point;
point.x = rect.left;
point.y = rect.bottom;
CMenu* pMenu = m_menu.GetSubMenu(1);
if (pMenu != NULL)
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
void CHistoryTrafficCalendarDlg::OnInitMenu(CMenu* pMenu)
{
CTabDlg::OnInitMenu(pMenu);
// TODO: 在此处添加消息处理程序代码
if (theApp.m_cfg_data.m_sunday_first)
pMenu->CheckMenuRadioItem(ID_FIRST_DAY_OF_WEEK_SUNDAY, ID_FIRST_DAY_OF_WEEK_MONDAY, ID_FIRST_DAY_OF_WEEK_SUNDAY, MF_BYCOMMAND | MF_CHECKED);
else
pMenu->CheckMenuRadioItem(ID_FIRST_DAY_OF_WEEK_SUNDAY, ID_FIRST_DAY_OF_WEEK_MONDAY, ID_FIRST_DAY_OF_WEEK_MONDAY, MF_BYCOMMAND | MF_CHECKED);
}
void CHistoryTrafficCalendarDlg::OnFirstDayOfWeekSunday()
{
// TODO: 在此添加命令处理程序代码
theApp.m_cfg_data.m_sunday_first = true;
MonthSelectChanged();
}
void CHistoryTrafficCalendarDlg::OnFirstDayOfWeekMonday()
{
// TODO: 在此添加命令处理程序代码
theApp.m_cfg_data.m_sunday_first = false;
MonthSelectChanged();
}
void CHistoryTrafficCalendarDlg::OnCalendarJumpToToday()
{
// TODO: 在此添加命令处理程序代码
m_year = m_history_traffics[0].year;
m_month = m_history_traffics[0].month;
SetComboSel();
MonthSelectChanged();
}
================================================
FILE: TrafficMonitor/HistoryTrafficDlg.cpp
================================================
// HistoryTrafficDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "HistoryTrafficDlg.h"
#include "afxdialogex.h"
// CHistoryTrafficDlg 对话框
IMPLEMENT_DYNAMIC(CHistoryTrafficDlg, CBaseDialog)
CHistoryTrafficDlg::CHistoryTrafficDlg(deque& history_traffics, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_HISTORY_TRAFFIC_DIALOG, pParent), m_history_traffics(history_traffics), m_tab1_dlg(history_traffics, this), m_tab2_dlg(history_traffics, this)
{
}
CHistoryTrafficDlg::~CHistoryTrafficDlg()
{
}
CString CHistoryTrafficDlg::GetDialogName() const
{
return _T("HistoryTrafficDlg");
}
void CHistoryTrafficDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TAB1, m_tab);
}
void CHistoryTrafficDlg::SetTabWndSize()
{
CRect rect;
m_tab.GetClientRect(rect);
CRect rcTabItem;
m_tab.GetItemRect(0, rcTabItem);
rect.top += rcTabItem.Height() + 4;
rect.left += 4;
rect.bottom -= 4;
rect.right -= 4;
m_tab1_dlg.MoveWindow(&rect);
m_tab2_dlg.MoveWindow(&rect);
}
BEGIN_MESSAGE_MAP(CHistoryTrafficDlg, CBaseDialog)
ON_WM_GETMINMAXINFO()
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CHistoryTrafficDlg::OnTcnSelchangeTab1)
ON_WM_SIZE()
END_MESSAGE_MAP()
// CHistoryTrafficDlg 消息处理程序
BOOL CHistoryTrafficDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_HISTORY_TRAFFIC));
SetIcon(theApp.GetMenuIcon(IDI_STATISTICS), FALSE); // 设置小图标
//插入标签
m_tab.InsertItem(0, CCommon::LoadText(IDS_LIST_VIEW));
m_tab.InsertItem(1, CCommon::LoadText(IDS_CALENDAR_VIEW));
//创建子对话框
m_tab1_dlg.Create(IDD_HISTORY_TRAFFIC_LIST_DIALOG, &m_tab);
m_tab2_dlg.Create(IDD_HISTORY_TRAFFIC_CALENDAR_DIALOG, &m_tab);
//调整子对话框的大小和位置
SetTabWndSize();
//设置默认选中的标签
switch (m_tab_selected)
{
case 0:
m_tab1_dlg.ShowWindow(SW_SHOW);
break;
case 1:
m_tab2_dlg.ShowWindow(SW_SHOW);
break;
}
m_tab.SetCurFocus(m_tab_selected);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL CHistoryTrafficDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
//if (GetKeyState(VK_CONTROL) & 0x80)
//{
// if (pMsg->wParam == 'D')
// {
// HistoryTraffic h{};
// h.year = 2018;
// h.month = 4;
// h.day = 29;
// auto iter = std::lower_bound(m_history_traffics.begin(), m_history_traffics.end(), h, HistoryTraffic::DateGreater);
// int index = iter - m_history_traffics.begin();
// }
//}
return CBaseDialog::PreTranslateMessage(pMsg);
}
void CHistoryTrafficDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: 在此添加控件通知处理程序代码
m_tab_selected = m_tab.GetCurSel();
switch (m_tab_selected)
{
case 0:
m_tab1_dlg.ShowWindow(SW_SHOW);
m_tab2_dlg.ShowWindow(SW_HIDE);
m_tab1_dlg.SetFocus();
break;
case 1:
m_tab2_dlg.ShowWindow(SW_SHOW);
m_tab1_dlg.ShowWindow(SW_HIDE);
m_tab2_dlg.SetFocus();
break;
}
*pResult = 0;
}
void CHistoryTrafficDlg::OnSize(UINT nType, int cx, int cy)
{
CBaseDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (nType != SIZE_MINIMIZED && m_tab1_dlg.GetSafeHwnd() != NULL && m_tab2_dlg.GetSafeHwnd() != NULL)
{
SetTabWndSize();
}
}
================================================
FILE: TrafficMonitor/HistoryTrafficDlg.h
================================================
#pragma once
#include "afxcmn.h"
#include "Common.h"
#include "HistoryTrafficListDlg.h"
#include "HistoryTrafficCalendarDlg.h"
#include "BaseDialog.h"
// CHistoryTrafficDlg 对话框
class CHistoryTrafficDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CHistoryTrafficDlg)
public:
CHistoryTrafficDlg(deque& history_traffics, CWnd* pParent = NULL); // 标准构造函数
virtual ~CHistoryTrafficDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_HISTORY_TRAFFIC_DIALOG };
#endif
public:
CHistoryTrafficListDlg m_tab1_dlg;
CHistoryTrafficCalendarDlg m_tab2_dlg;
protected:
deque& m_history_traffics;
CTabCtrl m_tab;
int m_tab_selected;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void SetTabWndSize();
virtual CString GetDialogName() const override;
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
================================================
FILE: TrafficMonitor/HistoryTrafficFile.cpp
================================================
#include "stdafx.h"
#include "HistoryTrafficFile.h"
#include "Common.h"
CHistoryTrafficFile::CHistoryTrafficFile(const wstring& file_path)
: m_file_path(file_path)
{
}
CHistoryTrafficFile::~CHistoryTrafficFile()
{
}
void CHistoryTrafficFile::Save() const
{
ofstream file{ m_file_path };
char buff[64];
sprintf_s(buff, "lines: \"%u\"", static_cast(m_history_traffics.size())); //ڵһд
file << buff << std::endl;
for (const auto& history_traffic : m_history_traffics)
{
if (history_traffic.mixed)
sprintf_s(buff, "%.4d/%.2d/%.2d %llu", history_traffic.year, history_traffic.month, history_traffic.day, history_traffic.down_kBytes);
else
sprintf_s(buff, "%.4d/%.2d/%.2d %llu/%llu", history_traffic.year, history_traffic.month, history_traffic.day, history_traffic.up_kBytes, history_traffic.down_kBytes);
file << buff << std::endl;
}
file.close();
}
void CHistoryTrafficFile::Load()
{
ifstream file{ m_file_path };
string current_line, temp;
HistoryTraffic traffic;
//bool first_line{ true };
if (CCommon::FileExist(m_file_path.c_str()))
{
while (!file.eof())
{
if (m_history_traffics.size() > 9999) break; //ȡ10000ʷ¼
std::getline(file, current_line);
//if (first_line)
//{
// first_line = false;
// size_t index = current_line.find("lines:");
// if(index != wstring::npos)
// {
// index = current_line.find("\"", index + 6);
// size_t index1 = current_line.find("\"", index + 1);
// temp = current_line.substr(index + 1, index1 - index - 1);
// m_size = atoll(temp.c_str());
// continue;
// }
//}
if (current_line.size() < 12) continue;
temp = current_line.substr(0, 4);
traffic.year = atoi(temp.c_str());
if (traffic.year < 1900 || traffic.year > 3000)
continue;
temp = current_line.substr(5, 2);
traffic.month = atoi(temp.c_str());
if (traffic.month < 1 || traffic.month > 12)
continue;
temp = current_line.substr(8, 2);
traffic.day = atoi(temp.c_str());
if (traffic.day < 1 || traffic.day > 31)
continue;
int index = current_line.find(L'/', 11);
traffic.mixed = (index == wstring::npos);
if (traffic.mixed)
{
temp = current_line.substr(11);
traffic.down_kBytes = atoll(temp.c_str());
traffic.up_kBytes = 0;
}
else
{
temp = current_line.substr(11, index - 11);
traffic.up_kBytes = atoll(temp.c_str());
temp = current_line.substr(index + 1);
traffic.down_kBytes = atoll(temp.c_str());
}
if (traffic.year > 0 && traffic.month > 0 && traffic.day > 0 && traffic.kBytes() > 0)
m_history_traffics.push_back(traffic);
}
}
MormalizeData();
}
void CHistoryTrafficFile::LoadSize()
{
ifstream file{ m_file_path };
string current_line, temp;
if (CCommon::FileExist(m_file_path.c_str()))
{
std::getline(file, current_line); //ȡһ
size_t index = current_line.find("lines:");
if (index != wstring::npos)
{
index = current_line.find("\"", index + 6);
size_t index1 = current_line.find("\"", index + 1);
temp = current_line.substr(index + 1, index1 - index - 1);
m_size = atoll(temp.c_str());
}
}
}
void CHistoryTrafficFile::Merge(const CHistoryTrafficFile& history_traffic, bool ignore_same_data)
{
for (const HistoryTraffic& traffic : history_traffic.m_history_traffics)
{
if(ignore_same_data)
{
//Ҫͬڵʹöַͬҵˣ
if (std::binary_search(m_history_traffics.begin(), m_history_traffics.end(), traffic, HistoryTraffic::DateGreater))
{
auto iter = std::lower_bound(m_history_traffics.begin(), m_history_traffics.end(), traffic, HistoryTraffic::DateGreater);
if (iter != m_history_traffics.end())
{
continue;
}
}
}
m_history_traffics.push_back(traffic);
}
MormalizeData();
}
void CHistoryTrafficFile::MormalizeData()
{
SYSTEMTIME current_time;
GetLocalTime(¤t_time);
HistoryTraffic traffic;
traffic.year = current_time.wYear;
traffic.month = current_time.wMonth;
traffic.day = current_time.wDay;
traffic.up_kBytes = 0;
traffic.down_kBytes = 0;
traffic.mixed = false;
if (m_history_traffics.empty())
{
m_history_traffics.push_front(traffic);
}
if (m_history_traffics.size() >= 2)
{
//ȡʷбڴӴС
std::sort(m_history_traffics.begin(), m_history_traffics.end(), HistoryTraffic::DateGreater);
//бͬڵĿϲ
for (int i{}; i < static_cast(m_history_traffics.size() - 1); i++)
{
if (HistoryTraffic::DateEqual(m_history_traffics[i], m_history_traffics[i + 1]))
{
m_history_traffics[i].up_kBytes += m_history_traffics[i + 1].up_kBytes;
m_history_traffics[i].down_kBytes += m_history_traffics[i + 1].down_kBytes;
m_history_traffics.erase(m_history_traffics.begin() + i + 1);
}
}
}
//бһĿǽ죬һĿͳƵΪʹõбǰһΪĿ
if (HistoryTraffic::DateEqual(m_history_traffics[0], traffic))
{
m_today_up_traffic = static_cast<__int64>(m_history_traffics[0].up_kBytes) * 1024;
m_today_down_traffic = static_cast<__int64>(m_history_traffics[0].down_kBytes) * 1024;
m_history_traffics[0].mixed = false;
}
else
{
m_history_traffics.push_front(traffic);
}
m_size = m_history_traffics.size();
}
================================================
FILE: TrafficMonitor/HistoryTrafficFile.h
================================================
#pragma once
#include "CommonData.h"
class CHistoryTrafficFile
{
public:
CHistoryTrafficFile(const wstring& file_path);
~CHistoryTrafficFile();
void Save() const;
void Load();
void LoadSize(); //ȡļĴС
void Merge(const CHistoryTrafficFile& history_traffic, bool ignore_same_data = false); //ϲһCHistoryTrafficFileignore_same_dataΪtrueͬڵͬڵ
const wstring& GetFilePath() const { return m_file_path; }
const void SetFilePath(const wstring& file_path) { m_file_path = file_path; }
deque& GetTraffics() { return m_history_traffics; }
__int64 GetTodayUpTraffic() const { return m_today_up_traffic; }
__int64 GetTodayDownTraffic() const { return m_today_down_traffic; }
size_t Size() { return m_size; }
private:
void MormalizeData(); //ʷϲͬ
private:
wstring m_file_path;
deque m_history_traffics; //ʷ
__int64 m_today_up_traffic{}; //ʹõϴ
__int64 m_today_down_traffic{}; //ʹõ
size_t m_size{}; //ݵ
};
================================================
FILE: TrafficMonitor/HistoryTrafficListCtrl.cpp
================================================
#include "stdafx.h"
#include "HistoryTrafficListCtrl.h"
IMPLEMENT_DYNAMIC(CHistoryTrafficListCtrl, CListCtrl)
CHistoryTrafficListCtrl::CHistoryTrafficListCtrl()
{
}
CHistoryTrafficListCtrl::~CHistoryTrafficListCtrl()
{
}
void CHistoryTrafficListCtrl::SetDrawItemRangeData(int item, double range, COLORREF color)
{
if (item < 0) return;
if (item >= static_cast(m_item_rage_data.size()))
m_item_rage_data.resize(item + 1);
m_item_rage_data[item].data_value = range;
m_item_rage_data[item].color = color;
}
void CHistoryTrafficListCtrl::SetDrawItemRangInLogScale(bool log_scale)
{
m_use_log_scale = log_scale;
Invalidate();
}
BEGIN_MESSAGE_MAP(CHistoryTrafficListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CHistoryTrafficListCtrl::OnNMCustomdraw)
END_MESSAGE_MAP()
void CHistoryTrafficListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
if (m_draw_item_range)
{
*pResult = CDRF_DODEFAULT;
LPNMLVCUSTOMDRAW lplvdr = reinterpret_cast(pNMHDR);
NMCUSTOMDRAW& nmcd = lplvdr->nmcd;
switch (lplvdr->nmcd.dwDrawStage) //判断状态
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT: //如果为画ITEM之前就要进行颜色的改变
if (nmcd.dwItemSpec >= 0 && nmcd.dwItemSpec < m_item_rage_data.size())
{
double range = m_item_rage_data[nmcd.dwItemSpec].data_value;
CDC* pDC = CDC::FromHandle(nmcd.hdc); //获取绘图DC
CRect item_rect, draw_rect;
GetSubItemRect(nmcd.dwItemSpec,m_draw_item_range_row, LVIR_BOUNDS, item_rect); //获取绘图单元格的矩形区域
CDrawCommon::SetDrawRect(pDC, item_rect); //设置绘图区域为当前列
//使用双缓冲绘图
{
CDrawDoubleBuffer draw_double_buffer(pDC, item_rect);
//填充背景
draw_rect = item_rect;
draw_rect.MoveToXY(0, 0);
draw_double_buffer.GetMemDC()->FillSolidRect(draw_rect, GetSysColor(COLOR_WINDOW));
if (draw_rect.Height() > 2 * m_margin)
{
draw_rect.top += m_margin;
draw_rect.bottom -= m_margin;
}
int width;
if (m_use_log_scale) //使用对数比例(y=ln(x+1))
{
range = std::log(range + 1);
width = static_cast(range*draw_rect.Width() / std::log(1000 + 1));
}
else //使用线性比例(y=x)
{
width = static_cast(range*draw_rect.Width() / 1000);
}
draw_rect.right = draw_rect.left + width;
draw_double_buffer.GetMemDC()->FillSolidRect(draw_rect, m_item_rage_data[nmcd.dwItemSpec].color);
}
//当前列绘制完成后将绘图区域设置为左边的区域,防止当前列的区域被覆盖
CRect rect1{ item_rect };
rect1.left = 0;
rect1.right = item_rect.left;
CDrawCommon::SetDrawRect(pDC, rect1);
}
*pResult = CDRF_DODEFAULT;
break;
}
}
}
================================================
FILE: TrafficMonitor/HistoryTrafficListCtrl.h
================================================
#pragma once
#include "afxcmn.h"
#include "DrawCommon.h"
class CHistoryTrafficListCtrl :
public CListCtrl
{
DECLARE_DYNAMIC(CHistoryTrafficListCtrl)
public:
CHistoryTrafficListCtrl();
~CHistoryTrafficListCtrl();
void EnableDrawItemRange(bool draw = true) { m_draw_item_range = draw; } //ǷҪijһеԪлƱʾֵСľ
void SetDrawItemRangeRow(int row) { m_draw_item_range_row = row; } //ҪƱʾֵСľεУעбΪұߵУұߵнʾ
void SetDrawItemRangeData(int item, double range, COLORREF color); //ijһеĿݴСȡֵΪ1~1000ɫ
void SetDrawItemRangMargin(int margin) { m_margin = margin; } //ûƻƵľεıԵԪ߿ľΣֵԽƵľԽϸDzܳбоһ
void SetDrawItemRangInLogScale(bool log_scale); //ҪƱʾֵСľʱǷʹöʹԱ
protected:
struct ItemData
{
double data_value; //ҪƵľαʾֵСΧΪ0~1000
COLORREF color; //ҪƵľεɫ
};
bool m_draw_item_range{ false }; //ǷҪijһеԪлƱʾֵСľ
int m_draw_item_range_row{}; //ҪƱʾֵСľε
int m_margin{};
vector m_item_rage_data; //ڱʾÿһҪƵݴСͻͼɫ
bool m_use_log_scale{ false }; //ΪtrueʹöƾΣʹԱ
DECLARE_MESSAGE_MAP()
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
};
================================================
FILE: TrafficMonitor/HistoryTrafficListDlg.cpp
================================================
// HistoryTrafficList.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "HistoryTrafficListDlg.h"
#include "afxdialogex.h"
#include "CalendarHelper.h"
// CHistoryTrafficListDlgDlg 对话框
IMPLEMENT_DYNAMIC(CHistoryTrafficListDlg, CTabDlg)
CHistoryTrafficListDlg::CHistoryTrafficListDlg(deque& history_traffics, CWnd* pParent /*=nullptr*/)
: CTabDlg(IDD_HISTORY_TRAFFIC_LIST_DIALOG, pParent), m_history_traffics(history_traffics)
{
}
CHistoryTrafficListDlg::~CHistoryTrafficListDlg()
{
}
bool CHistoryTrafficListDlg::CalculateColumeWidth(std::vector& widths)
{
if (!IsWindow(m_history_list.GetSafeHwnd()))
return false;
CRect rect;
m_history_list.GetWindowRect(rect);
if (rect.Width() <= 0)
return false;
const int MAX_COLUME{ 5 };
int width_date; //“日期”列的宽度
int width0; //“上传”、“下载”、“总流量”列的宽度
int width1; //“图表”列的宽度
width_date = rect.Width() * 3 / 11;
width0 = rect.Width() * 2 / 11;
if (width_date > theApp.DPI(150))
width_date = theApp.DPI(150);
if (width0 > theApp.DPI(120))
width0 = theApp.DPI(120);
width1 = rect.Width() - (MAX_COLUME - 2) * width0 - width_date - theApp.DPI(20) - 1;
widths.resize(MAX_COLUME);
widths[0] = width_date;
widths[1] = widths[2] = widths[3] = width0;
widths[4] = width1;
return true;
}
void CHistoryTrafficListDlg::AddListRow(const ListRowData& data, unsigned __int64 max_traffic)
{
unsigned __int64 total_kbytes = data.up_kBytes + data.down_kBytes;
int index = m_history_list.GetItemCount();
m_history_list.InsertItem(index, data.str);
if (data.mixed)
{
m_history_list.SetItemText(index, 1, _T("-"));
m_history_list.SetItemText(index, 2, _T("-"));
}
else
{
m_history_list.SetItemText(index, 1, CCommon::KBytesToString(data.up_kBytes));
m_history_list.SetItemText(index, 2, CCommon::KBytesToString(data.down_kBytes));
}
m_history_list.SetItemText(index, 3, CCommon::KBytesToString(total_kbytes));
double range = static_cast(total_kbytes) * 1000 / max_traffic;
COLORREF color;
if (total_kbytes < 1024 * 1024) //流量小于1GB时绘制蓝色
color = TRAFFIC_COLOR_BLUE;
else if (total_kbytes < 10 * 1024 * 1024) //流量小于10GB时绘制绿色
color = TRAFFIC_COLOR_GREEN;
else if (total_kbytes < 100 * 1024 * 1024) //流量小于100GB时绘制黄色
color = TRAFFIC_COLOR_YELLOE;
else if (total_kbytes < 1024 * 1024 * 1024) //流量小于1TB时绘制红色
color = TRAFFIC_COLOR_RED;
else //流量超过1TB时显示深红色
color = TRAFFIC_COLOR_DARK_RED;
m_history_list.SetDrawItemRangeData(index, range, color);
}
void CHistoryTrafficListDlg::ShowListData()
{
m_history_list.DeleteAllItems();
//显示日视图
if(theApp.m_cfg_data.m_view_type == HistoryTrafficViewType::HV_DAY)
{
//获取历史流量列表中流量的最大值
unsigned __int64 max_traffic{};
for (const auto& traffic : m_history_traffics)
{
if (traffic.kBytes() > max_traffic)
max_traffic = traffic.kBytes();
}
for (size_t i{}; i < m_history_traffics.size(); i++)
{
CString date_str;
//CString k_bytes_str;
date_str.Format(_T("%.4d/%.2d/%.2d ("), m_history_traffics[i].year, m_history_traffics[i].month, m_history_traffics[i].day);
int week_day = CCalendarHelper::CaculateWeekDay(m_history_traffics[i].year, m_history_traffics[i].month, m_history_traffics[i].day);
switch (week_day)
{
case 0:
date_str += CCommon::LoadText(IDS_SUNDAY);
break;
case 1:
date_str += CCommon::LoadText(IDS_MONDAY);
break;
case 2:
date_str += CCommon::LoadText(IDS_TUESDAY);
break;
case 3:
date_str += CCommon::LoadText(IDS_WEDNESDAY);
break;
case 4:
date_str += CCommon::LoadText(IDS_THURSDAY);
break;
case 5:
date_str += CCommon::LoadText(IDS_FRIDAY);
break;
case 6:
date_str += CCommon::LoadText(IDS_SATURDAY);
break;
default:
break;
}
date_str += _T(')');
ListRowData data;
data.str = date_str;
data.up_kBytes = m_history_traffics[i].up_kBytes;
data.down_kBytes = m_history_traffics[i].down_kBytes;
data.mixed = m_history_traffics[i].mixed;
AddListRow(data, max_traffic);
}
}
//显示月/季/年视图
else
{
std::vector list_data;
unsigned __int64 max_traffic{};
for (const auto& traffic : m_history_traffics)
{
CString date_str;
if (theApp.m_cfg_data.m_view_type == HistoryTrafficViewType::HV_WEEK)
{
date_str.Format(_T("%.4d/"), traffic.year);
date_str += CCommon::LoadTextFormat(IDS_WEEK_NUM, { traffic.week() });
}
else if (theApp.m_cfg_data.m_view_type == HistoryTrafficViewType::HV_MONTH) //月视图
{
date_str.Format(_T("%.4d/%.2d"), traffic.year, traffic.month);
}
else if (theApp.m_cfg_data.m_view_type == HistoryTrafficViewType::HV_QUARTER) //季视图
{
date_str.Format(_T("%.4d/"), traffic.year);
if (traffic.month <= 3)
date_str += _T("Q1");
else if (traffic.month <= 6)
date_str += _T("Q2");
else if (traffic.month <= 9)
date_str += _T("Q3");
else
date_str += _T("Q4");
}
else //年视图
{
date_str.Format(_T("%.4d"), traffic.year);
}
if (list_data.empty() || list_data.back().str != date_str)
{
if(!list_data.empty())
{
unsigned __int64 cur_traffic{ list_data.back().up_kBytes + list_data.back().down_kBytes };
if (cur_traffic > max_traffic)
max_traffic = cur_traffic;
}
ListRowData data;
data.str = date_str;
data.up_kBytes = traffic.up_kBytes;
data.down_kBytes = traffic.down_kBytes;
list_data.push_back(data);
}
else
{
list_data.back().up_kBytes += traffic.up_kBytes;
list_data.back().down_kBytes += traffic.down_kBytes;
}
}
for (const auto& data : list_data)
{
AddListRow(data, max_traffic);
}
}
}
void CHistoryTrafficListDlg::DoDataExchange(CDataExchange* pDX)
{
CTabDlg::DoDataExchange(pDX);
DDX_Control(pDX, IDC_HISTORY_INFO_LIST, m_history_list);
DDX_Control(pDX, IDC_VIEW_TYPE_COMBO, m_view_type_combo);
DDX_Control(pDX, IDC_VIEW_SCALE_COMBO, m_view_scale_combo);
}
BEGIN_MESSAGE_MAP(CHistoryTrafficListDlg, CTabDlg)
ON_WM_INITMENU()
ON_COMMAND(ID_USE_LINEAR_SCALE, &CHistoryTrafficListDlg::OnUseLinearScale)
ON_COMMAND(ID_USE_LOG_SCALE, &CHistoryTrafficListDlg::OnUseLogScale)
ON_WM_SIZE()
ON_CBN_SELCHANGE(IDC_VIEW_TYPE_COMBO, &CHistoryTrafficListDlg::OnCbnSelchangeViewTypeCombo)
ON_CBN_SELCHANGE(IDC_VIEW_SCALE_COMBO, &CHistoryTrafficListDlg::OnCbnSelchangeViewScaleCombo)
END_MESSAGE_MAP()
// CHistoryTrafficListDlgDlg 消息处理程序
BOOL CHistoryTrafficListDlg::OnInitDialog()
{
CTabDlg::OnInitDialog();
// TODO: 在此添加额外的初始化
//初始化列表控件
m_history_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
std::vector widths;
CalculateColumeWidth(widths);
m_history_list.InsertColumn(0, CCommon::LoadText(IDS_DATE), LVCFMT_LEFT, widths[0]);
m_history_list.InsertColumn(1, CCommon::LoadText(IDS_UPLOAD), LVCFMT_RIGHT, widths[1]);
m_history_list.InsertColumn(2, CCommon::LoadText(IDS_DOWNLOAD), LVCFMT_RIGHT, widths[2]);
m_history_list.InsertColumn(3, CCommon::LoadText(IDS_TRAFFIC_USED), LVCFMT_RIGHT, widths[3]);
m_history_list.InsertColumn(4, CCommon::LoadText(IDS_FIGURE), LVCFMT_LEFT, widths[4]);
m_history_list.EnableDrawItemRange();
m_history_list.SetDrawItemRangeRow(4);
m_history_list.SetDrawItemRangMargin(theApp.DPI(4));
m_history_list.SetDrawItemRangInLogScale(theApp.m_cfg_data.m_use_log_scale);
//初始化控件
m_view_type_combo.AddString(CCommon::LoadText(IDS_DAY_VIEW));
m_view_type_combo.AddString(CCommon::LoadText(IDS_WEEK_VIEW));
m_view_type_combo.AddString(CCommon::LoadText(IDS_MONTH_VIEW));
m_view_type_combo.AddString(CCommon::LoadText(IDS_QUARTER_VIEW));
m_view_type_combo.AddString(CCommon::LoadText(IDS_YEAR_VIEW));
m_view_type_combo.SetCurSel(static_cast(theApp.m_cfg_data.m_view_type));
m_view_scale_combo.AddString(CCommon::LoadText(IDS_LINEAR_SCALE));
m_view_scale_combo.AddString(CCommon::LoadText(IDS_LOG_SCALE));
m_view_scale_combo.SetCurSel(theApp.m_cfg_data.m_use_log_scale ? 1 : 0);
//显示列表数据
ShowListData();
m_menu.LoadMenu(IDR_HISTORY_TRAFFIC_MENU);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL CHistoryTrafficListDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// TODO: 在此添加专用代码和/或调用基类
LPNMHDR lpnmhdr = (LPNMHDR)lParam;
if (lpnmhdr->code == NM_RCLICK)
{
CPoint point;
GetCursorPos(&point);//获得光标的位置
m_history_list.ScreenToClient(&point);//获得list控件在窗口上的坐标
CWnd* pWnd = m_history_list.ChildWindowFromPoint(point);
CHeaderCtrl* pHeader = m_history_list.GetHeaderCtrl();//获取列表视图控件的标题控件
CRect item_rect;
pHeader->GetItemRect(4, item_rect); //获取列表标题控件第2列的矩形区域,只有光标在第4列点击时才弹出右键菜单
if (pWnd && (pWnd->GetSafeHwnd() == pHeader->GetSafeHwnd()) && item_rect.PtInRect(point))
{
HDHITTESTINFO info{};
info.pt = point;
pHeader->SendMessage(HDM_HITTEST, 0, (LPARAM)&info);
CMenu * pMenu = m_menu.GetSubMenu(0);
GetCursorPos(&point);
pMenu->TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
return CTabDlg::OnNotify(wParam, lParam, pResult);
}
void CHistoryTrafficListDlg::OnInitMenu(CMenu* pMenu)
{
CTabDlg::OnInitMenu(pMenu);
// TODO: 在此处添加消息处理程序代码
if (theApp.m_cfg_data.m_use_log_scale)
pMenu->CheckMenuRadioItem(ID_USE_LINEAR_SCALE, ID_USE_LOG_SCALE, ID_USE_LOG_SCALE, MF_BYCOMMAND | MF_CHECKED);
else
pMenu->CheckMenuRadioItem(ID_USE_LINEAR_SCALE, ID_USE_LOG_SCALE, ID_USE_LINEAR_SCALE, MF_BYCOMMAND | MF_CHECKED);
}
void CHistoryTrafficListDlg::OnUseLinearScale()
{
// TODO: 在此添加命令处理程序代码
theApp.m_cfg_data.m_use_log_scale = false;
m_history_list.SetDrawItemRangInLogScale(theApp.m_cfg_data.m_use_log_scale);
}
void CHistoryTrafficListDlg::OnUseLogScale()
{
// TODO: 在此添加命令处理程序代码
theApp.m_cfg_data.m_use_log_scale = true;
m_history_list.SetDrawItemRangInLogScale(theApp.m_cfg_data.m_use_log_scale);
}
void CHistoryTrafficListDlg::OnSize(UINT nType, int cx, int cy)
{
CTabDlg::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (nType != SIZE_MINIMIZED)
{
std::vector widths;
if(CalculateColumeWidth(widths))
{
for (size_t i{}; i < widths.size(); i++)
{
m_history_list.SetColumnWidth(i, widths[i]);
}
}
}
}
void CHistoryTrafficListDlg::OnCbnSelchangeViewTypeCombo()
{
// TODO: 在此添加控件通知处理程序代码
theApp.m_cfg_data.m_view_type = static_cast(m_view_type_combo.GetCurSel());
ShowListData();
}
void CHistoryTrafficListDlg::OnCbnSelchangeViewScaleCombo()
{
// TODO: 在此添加控件通知处理程序代码
theApp.m_cfg_data.m_use_log_scale = (m_view_scale_combo.GetCurSel() != 0);
m_history_list.SetDrawItemRangInLogScale(theApp.m_cfg_data.m_use_log_scale);
}
================================================
FILE: TrafficMonitor/IconSelectDlg.cpp
================================================
// IconSelectDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "IconSelectDlg.h"
#include "afxdialogex.h"
// CIconSelectDlg 对话框
IMPLEMENT_DYNAMIC(CIconSelectDlg, CBaseDialog)
CIconSelectDlg::CIconSelectDlg(int icon_selected, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_ICON_SELECT_DIALOG, pParent), m_icon_selected{ icon_selected }
{
}
CIconSelectDlg::~CIconSelectDlg()
{
}
int CIconSelectDlg::GetIconSelected() const
{
if (m_icon_selected < 0 || m_icon_selected >= MAX_NOTIFY_ICON)
return 0;
return m_icon_selected;
}
void CIconSelectDlg::SetAutoAdaptNotifyIcon(bool val)
{
m_atuo_adapt_notify_icon = val;
}
bool CIconSelectDlg::AutoAdaptNotifyIcon() const
{
return m_atuo_adapt_notify_icon;
}
void CIconSelectDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_ICON_PREVIEW, m_preview_pic);
DDX_Control(pDX, IDC_COMBO1, m_icon_select_combo);
DDX_Control(pDX, IDC_AUTO_ADAPT_CHECK, m_auto_adapt_chk);
}
void CIconSelectDlg::DrawPreviewIcon(CDC* pDC)
{
//pDC->FillSolidRect(CRect(CPoint(ICON_X, ICON_Y), CSize(theApp.DPI(16), theApp.DPI(16))), RGB(0, 0, 0));
//pDC->DrawIcon(ICON_X, ICON_Y, m_icons[m_icon_selected]);
::DrawIconEx(pDC->m_hDC, ICON_X, ICON_Y, theApp.m_notify_icons[GetIconSelected()], theApp.DPI(16), theApp.DPI(16), 0, NULL, DI_NORMAL);
}
BEGIN_MESSAGE_MAP(CIconSelectDlg, CBaseDialog)
//ON_WM_TIMER()
ON_CBN_SELCHANGE(IDC_COMBO1, &CIconSelectDlg::OnCbnSelchangeCombo1)
ON_MESSAGE(WM_CONTROL_REPAINT, &CIconSelectDlg::OnControlRepaint)
ON_BN_CLICKED(IDC_AUTO_ADAPT_CHECK, &CIconSelectDlg::OnBnClickedAutoAdaptCheck)
END_MESSAGE_MAP()
// CIconSelectDlg 消息处理程序
BOOL CIconSelectDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_CHANGE_ICON));
//设置预览图大小
m_preview_pic.SetWindowPos(nullptr, 0, 0, PREVIEW_WIDTH, PREVIEW_HEIGHT, SWP_NOZORDER | SWP_NOMOVE);
if (m_icon_selected == 4 || m_icon_selected == 5)
m_preview_pic.SetPicture((HBITMAP)LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_NOTIFY_ICON_PREVIEW_LIGHT), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION));
else
m_preview_pic.SetPicture((HBITMAP)LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_NOTIFY_ICON_PREVIEW), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION));
//初始化下拉列表
m_icon_select_combo.AddString(CCommon::LoadText(IDS_DEFAULT_ICON));
m_icon_select_combo.AddString(CCommon::LoadText(IDS_ICON, _T(" 1")));
m_icon_select_combo.AddString(CCommon::LoadText(IDS_ICON, _T(" 2")));
m_icon_select_combo.AddString(CCommon::LoadText(IDS_ICON, _T(" 3")));
m_icon_select_combo.AddString(CCommon::LoadText(IDS_ICON, _T(" 4")));
m_icon_select_combo.AddString(CCommon::LoadText(IDS_ICON, _T(" 5")));
m_icon_select_combo.SetCurSel(m_icon_selected);
m_auto_adapt_chk.SetCheck(m_atuo_adapt_notify_icon);
m_auto_adapt_chk.EnableWindow(theApp.m_win_version.GetMajorVersion() >= 10);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CIconSelectDlg::OnCbnSelchangeCombo1()
{
// TODO: 在此添加控件通知处理程序代码
m_icon_selected = m_icon_select_combo.GetCurSel();
if (m_icon_selected == 4 || m_icon_selected == 5)
m_preview_pic.SetPicture((HBITMAP)LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_NOTIFY_ICON_PREVIEW_LIGHT), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION));
else
m_preview_pic.SetPicture((HBITMAP)LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_NOTIFY_ICON_PREVIEW), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION));
DrawPreviewIcon(m_preview_pic.GetDC());
}
afx_msg LRESULT CIconSelectDlg::OnControlRepaint(WPARAM wParam, LPARAM lParam)
{
CWnd* pControl = (CWnd*)wParam;
CDC* pDC = (CDC*)lParam;
if (pControl == &m_preview_pic)
{
//当收到m_preview_pic控件的重绘消息时,同时重绘图标
DrawPreviewIcon(pDC);
}
return 0;
}
void CIconSelectDlg::OnBnClickedAutoAdaptCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_atuo_adapt_notify_icon = (m_auto_adapt_chk.GetCheck() != 0);
}
CString CIconSelectDlg::GetDialogName() const
{
return _T("");
}
================================================
FILE: TrafficMonitor/IconSelectDlg.h
================================================
#pragma once
#include "afxwin.h"
#include "PictureStatic.h"
#include "BaseDialog.h"
// CIconSelectDlg 对话框
class CIconSelectDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CIconSelectDlg)
public:
CIconSelectDlg(int icon_selected, CWnd* pParent = NULL); // 标准构造函数
virtual ~CIconSelectDlg();
int GetIconSelected() const;
void SetAutoAdaptNotifyIcon(bool val);
bool AutoAdaptNotifyIcon() const;
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ICON_SELECT_DIALOG };
#endif
#define PREVIEW_WIDTH theApp.DPI(200) //预览图的宽高
#define PREVIEW_HEIGHT theApp.DPI(40)
#define ICON_X theApp.DPI(46) //预览图中图标的位置
#define ICON_Y theApp.DPI(12)
protected:
CPictureStatic m_preview_pic;
CComboBox m_icon_select_combo;
CButton m_auto_adapt_chk;
int m_icon_selected{};
bool m_atuo_adapt_notify_icon;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
virtual CString GetDialogName() const override;
void DrawPreviewIcon(CDC* pDC);
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
//afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnCbnSelchangeCombo1();
protected:
afx_msg LRESULT OnControlRepaint(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnBnClickedAutoAdaptCheck();
};
================================================
FILE: TrafficMonitor/IniHelper.cpp
================================================
#include "stdafx.h"
#include "IniHelper.h"
#include "TrafficMonitor.h"
CIniHelper::CIniHelper(const wstring& file_path)
{
m_file_path = file_path;
ifstream file_stream{ file_path };
if (file_stream.fail())
{
return;
}
//读取文件内容
string ini_str;
while (!file_stream.eof())
{
ini_str.push_back(file_stream.get());
}
ini_str.pop_back();
if (!ini_str.empty() && ini_str.back() != L'\n') //确保文件末尾有回车符
ini_str.push_back(L'\n');
//判断文件是否是utf8编码
bool is_utf8;
if (ini_str.size() >= 3 && ini_str[0] == -17 && ini_str[1] == -69 && ini_str[2] == -65)
{
//如果有UTF8的BOM,则删除BOM
is_utf8 = true;
ini_str = ini_str.substr(3);
}
else
{
is_utf8 = false;
}
//转换成Unicode
m_ini_str = CCommon::StrToUnicode(ini_str.c_str(), is_utf8);
}
CIniHelper::~CIniHelper()
{
}
void CIniHelper::SetSaveAsUTF8(bool utf8)
{
m_save_as_utf8 = utf8;
}
void CIniHelper::WriteString(const wchar_t * AppName, const wchar_t * KeyName, const wstring& str)
{
wstring write_str{ str };
if (!write_str.empty() && (write_str[0] == L' ' || write_str.back() == L' ')) //如果字符串前后含有空格,则在字符串前后添加引号
{
write_str = DEF_CH + write_str;
write_str.push_back(DEF_CH);
}
_WriteString(AppName, KeyName, write_str);
}
wstring CIniHelper::GetString(const wchar_t * AppName, const wchar_t * KeyName, const wchar_t* default_str) const
{
wstring rtn{_GetString(AppName, KeyName, default_str)};
//如果读取的字符串前后有指定的字符,则删除它
if (!rtn.empty() && (rtn.front() == L'$' || rtn.front() == DEF_CH))
rtn = rtn.substr(1);
if (!rtn.empty() && (rtn.back() == L'$' || rtn.back() == DEF_CH))
rtn.pop_back();
return rtn;
}
void CIniHelper::WriteInt(const wchar_t * AppName, const wchar_t * KeyName, int value)
{
wchar_t buff[16]{};
_itow_s(value, buff, 10);
_WriteString(AppName, KeyName, wstring(buff));
}
int CIniHelper::GetInt(const wchar_t * AppName, const wchar_t * KeyName, int default_value) const
{
wchar_t default_str_buff[16]{};
_itow_s(default_value, default_str_buff, 10);
wstring rtn{ _GetString(AppName, KeyName, default_str_buff) };
return _ttoi(rtn.c_str());
}
void CIniHelper::WriteBool(const wchar_t * AppName, const wchar_t * KeyName, bool value)
{
if(value)
_WriteString(AppName, KeyName, wstring(L"true"));
else
_WriteString(AppName, KeyName, wstring(L"false"));
}
bool CIniHelper::GetBool(const wchar_t * AppName, const wchar_t * KeyName, bool default_value) const
{
wstring rtn{ _GetString(AppName, KeyName, (default_value ? L"true" : L"false")) };
if (rtn == L"true")
return true;
else if (rtn == L"false")
return false;
else
return (_ttoi(rtn.c_str()) != 0);
}
void CIniHelper::WriteIntArray(const wchar_t * AppName, const wchar_t * KeyName, const int * values, int size)
{
CString str, tmp;
for (int i{}; i < size; i++)
{
tmp.Format(_T("%d,"), values[i]);
str += tmp;
}
_WriteString(AppName, KeyName, wstring(str));
}
void CIniHelper::GetIntArray(const wchar_t * AppName, const wchar_t * KeyName, int * values, int size, int default_value) const
{
CString default_str;
default_str.Format(_T("%d"), default_value);
wstring str;
str = _GetString(AppName, KeyName, default_str);
std::vector split_result;
CCommon::StringSplit(str, L',', split_result);
for (int i = 0; i < size; i++)
{
if (i < split_result.size())
values[i] = _wtoi(split_result[i].c_str());
else if (i > 0)
values[i] = values[i - 1];
else
values[i] = default_value;
}
}
void CIniHelper::WriteBoolArray(const wchar_t * AppName, const wchar_t * KeyName, const bool * values, int size)
{
int value{};
for (int i{}; i < size; i++)
{
if (values[i])
value |= (1 << i);
}
return WriteInt(AppName, KeyName, value);
}
void CIniHelper::GetBoolArray(const wchar_t * AppName, const wchar_t * KeyName, bool * values, int size, bool default_value) const
{
int value = GetInt(AppName, KeyName, 0);
for (int i{}; i < size; i++)
{
values[i] = ((value >> i) % 2 != 0);
}
}
void CIniHelper::WriteStringList(const wchar_t* AppName, const wchar_t* KeyName, const vector& values)
{
wstring str_write = MergeStringList(values);
_WriteString(AppName, KeyName, str_write);
}
void CIniHelper::GetStringList(const wchar_t* AppName, const wchar_t* KeyName, vector& values, const vector& default_value) const
{
wstring default_str = MergeStringList(default_value);
wstring str_value = _GetString(AppName, KeyName, default_str.c_str());
SplitStringList(values, str_value);
}
void CIniHelper::SaveFontData(const wchar_t * AppName, const FontInfo & font)
{
WriteString(AppName, L"font_name", wstring(font.name));
WriteInt(AppName, L"font_size", font.size);
bool style[4];
style[0] = font.bold;
style[1] = font.italic;
style[2] = font.underline;
style[3] = font.strike_out;
WriteBoolArray(AppName, L"font_style", style, 4);
}
bool CIniHelper::Save()
{
ofstream file_stream{ m_file_path };
if(file_stream.fail())
return false;
string ini_str{ CCommon::UnicodeToStr(m_ini_str.c_str(), m_save_as_utf8) };
if (m_save_as_utf8) //如果以UTF8编码保存,先插入BOM
{
string utf8_bom;
utf8_bom.push_back(-17);
utf8_bom.push_back(-69);
utf8_bom.push_back(-65);
file_stream << utf8_bom;
}
file_stream << ini_str;
return true;
}
void CIniHelper::LoadFontData(const wchar_t * AppName, FontInfo & font, const FontInfo& default_font) const
{
font.name = GetString(AppName, L"font_name", default_font.name).c_str();
font.size = GetInt(AppName, L"font_size", default_font.size);
bool style[4];
GetBoolArray(AppName, L"font_style", style, 4);
font.bold = style[0];
font.italic = style[1];
font.underline = style[2];
font.strike_out = style[3];
}
void CIniHelper::LoadMainWndColors(const wchar_t * AppName, const wchar_t * KeyName, std::map& text_colors, COLORREF default_color)
{
CString default_str;
default_str.Format(_T("%d"), default_color);
wstring str;
str = _GetString(AppName, KeyName, default_str);
std::vector split_result;
CCommon::StringSplit(str, L',', split_result);
size_t index = 0;
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
if (index < split_result.size())
text_colors[*iter] = _wtoi(split_result[index].c_str());
else if (!split_result.empty())
text_colors[*iter] = _wtoi(split_result[0].c_str());
else
text_colors[*iter] = default_color;
index++;
}
}
void CIniHelper::SaveMainWndColors(const wchar_t * AppName, const wchar_t * KeyName, const std::map& text_colors)
{
CString str;
for (auto iter = text_colors.begin(); iter != text_colors.end(); ++iter)
{
CString tmp;
tmp.Format(_T("%d,"), iter->second);
str += tmp;
}
_WriteString(AppName, KeyName, wstring(str));
}
void CIniHelper::LoadTaskbarWndColors(const wchar_t * AppName, const wchar_t * KeyName, std::map& text_colors, COLORREF default_color)
{
CString default_str;
default_str.Format(_T("%d"), default_color);
wstring str;
str = _GetString(AppName, KeyName, default_str);
std::vector split_result;
CCommon::StringSplit(str, L',', split_result);
size_t index = 0;
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
if (index < split_result.size())
text_colors[*iter].label = _wtoi(split_result[index].c_str());
else if (!split_result.empty())
text_colors[*iter].label = _wtoi(split_result[0].c_str());
else
text_colors[*iter].label = default_color;
if (index + 1 < split_result.size())
text_colors[*iter].value = _wtoi(split_result[index + 1].c_str());
else if (split_result.size() > 1)
text_colors[*iter].value = _wtoi(split_result[1].c_str());
else
text_colors[*iter].value = default_color;
index += 2;
}
}
void CIniHelper::SaveTaskbarWndColors(const wchar_t * AppName, const wchar_t * KeyName, const std::map& text_colors)
{
CString str;
for (auto iter = text_colors.begin(); iter != text_colors.end(); ++iter)
{
CString tmp;
tmp.Format(_T("%d,%d,"), iter->second.label, iter->second.value);
str += tmp;
}
_WriteString(AppName, KeyName, wstring(str));
}
void CIniHelper::LoadPluginDisplayStr(bool is_main_window)
{
DispStrings& disp_str{ is_main_window ? theApp.m_main_wnd_data.disp_str : theApp.m_taskbar_data.disp_str };
std::wstring app_name{ is_main_window ? L"plugin_display_str_main_window" : L"plugin_display_str_taskbar_window" };
for (const auto& plugin : theApp.m_plugins.GetPluginItems())
{
disp_str.Load(plugin->GetItemId(), GetString(app_name.c_str(), plugin->GetItemId(), plugin->GetItemLableText()));
}
}
void CIniHelper::SavePluginDisplayStr(bool is_main_window)
{
DispStrings& disp_str{ is_main_window ? theApp.m_main_wnd_data.disp_str : theApp.m_taskbar_data.disp_str };
std::wstring app_name{ is_main_window ? L"plugin_display_str_main_window" : L"plugin_display_str_taskbar_window" };
for (const auto& plugin : theApp.m_plugins.GetPluginItems())
{
WriteString(app_name.c_str(), plugin->GetItemId(), disp_str.Get(plugin));
}
}
void CIniHelper::_WriteString(const wchar_t * AppName, const wchar_t * KeyName, const wstring & str)
{
wstring app_str{ L"[" };
app_str.append(AppName).append(L"]");
size_t app_pos{}, app_end_pos, key_pos;
app_pos = m_ini_str.find(app_str);
if (app_pos == wstring::npos) //找不到AppName,则在最后面添加
{
if (!m_ini_str.empty() && m_ini_str.back() != L'\n')
m_ini_str += L"\n";
app_pos = m_ini_str.size();
m_ini_str += app_str;
m_ini_str += L"\n";
}
app_end_pos = m_ini_str.find(L"\n[", app_pos + 2);
if (app_end_pos != wstring::npos)
app_end_pos++;
key_pos = m_ini_str.find(wstring(L"\n") + KeyName + L' ', app_pos); //查找“\nkey_name ”
if (key_pos >= app_end_pos) //如果找不到“\nkey_name ”,则查找“\nkey_name=”
key_pos = m_ini_str.find(wstring(L"\n") + KeyName + L'=', app_pos);
if (key_pos >= app_end_pos) //找不到KeyName,则插入一个
{
//wchar_t buff[256];
//swprintf_s(buff, L"%s = %s\n", KeyName, str.c_str());
std::wstring str_temp = KeyName;
str_temp += L" = ";
str_temp += str;
str_temp += L"\n";
if (app_end_pos == wstring::npos)
m_ini_str += str_temp;
else
m_ini_str.insert(app_end_pos, str_temp);
}
else //找到了KeyName,将等号到换行符之间的文本替换
{
size_t str_pos;
str_pos = m_ini_str.find(L'=', key_pos + 2);
size_t line_end_pos = m_ini_str.find(L'\n', key_pos + 2);
if (str_pos > line_end_pos) //所在行没有等号,则插入一个等号
{
m_ini_str.insert(key_pos + wcslen(KeyName) + 1, L" =");
str_pos = key_pos + wcslen(KeyName) + 2;
}
else
{
str_pos++;
}
size_t str_end_pos;
str_end_pos = m_ini_str.find(L"\n", str_pos);
m_ini_str.replace(str_pos, str_end_pos - str_pos, L" " + str);
}
}
wstring CIniHelper::_GetString(const wchar_t * AppName, const wchar_t * KeyName, const wchar_t* default_str) const
{
wstring app_str{ L"[" };
app_str.append(AppName).append(L"]");
size_t app_pos{}, app_end_pos, key_pos;
app_pos = m_ini_str.find(app_str);
if (app_pos == wstring::npos) //找不到AppName,返回默认字符串
return default_str;
app_end_pos = m_ini_str.find(L"\n[", app_pos + 2);
if (app_end_pos != wstring::npos)
app_end_pos++;
key_pos = m_ini_str.find(wstring(L"\n") + KeyName + L' ', app_pos); //查找“\nkey_name ”
if (key_pos >= app_end_pos) //如果找不到“\nkey_name ”,则查找“\nkey_name=”
key_pos = m_ini_str.find(wstring(L"\n") + KeyName + L'=', app_pos);
if (key_pos >= app_end_pos) //找不到KeyName,返回默认字符串
{
return default_str;
}
else //找到了KeyName,获取等号到换行符之间的文本
{
size_t str_pos;
str_pos = m_ini_str.find(L'=', key_pos + 2);
size_t line_end_pos = m_ini_str.find(L'\n', key_pos + 2);
if (str_pos > line_end_pos) //所在行没有等号,返回默认字符串
{
return default_str;
}
else
{
str_pos++;
}
size_t str_end_pos;
str_end_pos = m_ini_str.find(L"\n", str_pos);
//获取文本
wstring return_str{ m_ini_str.substr(str_pos, str_end_pos - str_pos) };
//如果前后有空格,则将其删除
CCommon::StringNormalize(return_str);
return return_str;
}
}
wstring CIniHelper::MergeStringList(const vector& values)
{
wstring str_merge;
int index = 0;
//在每个字符串前后加上引号,再将它们用逗号连接起来
for (const wstring& str : values)
{
if (index > 0)
str_merge.push_back(L',');
str_merge.push_back(L'\"');
str_merge += str;
str_merge.push_back(L'\"');
index++;
}
return str_merge;
}
void CIniHelper::SplitStringList(vector& values, wstring str_value)
{
CCommon::StringSplit(str_value, wstring(L"\",\""), values);
if (!values.empty())
{
//结果中第一项前面和最后一项的后面各还有一个引号,将它们删除
values.front() = values.front().substr(1);
values.back().pop_back();
}
}
================================================
FILE: TrafficMonitor/IniHelper.h
================================================
//ini读写类
//使用时将ini文件路径通过构造函数参数传递
//在向ini文件写入数据时,需要在最后调用Save()函数以将更改保存到文件
//默认以UTF8_BOM编码保存,如果要以ANSI保存,请调用SetSaveAsUTF8(false);
#pragma once
#include "CommonData.h"
#include "Common.h"
class CIniHelper
{
public:
CIniHelper(const wstring& file_path);
~CIniHelper();
void SetSaveAsUTF8(bool utf8);
void WriteString(const wchar_t* AppName, const wchar_t* KeyName, const wstring& str);
wstring GetString(const wchar_t* AppName, const wchar_t* KeyName, const wchar_t* default_str) const;
void WriteInt(const wchar_t * AppName, const wchar_t * KeyName, int value);
int GetInt(const wchar_t * AppName, const wchar_t * KeyName, int default_value) const;
void WriteBool(const wchar_t * AppName, const wchar_t * KeyName, bool value);
bool GetBool(const wchar_t * AppName, const wchar_t * KeyName, bool default_value) const;
void WriteIntArray(const wchar_t * AppName, const wchar_t * KeyName, const int* values, int size); //写入一个int数组,元素个数为size
void GetIntArray(const wchar_t * AppName, const wchar_t * KeyName, int* values, int size, int default_value = 0) const; //读取一个int数组,储存到values,元素个数为size
void WriteBoolArray(const wchar_t * AppName, const wchar_t * KeyName, const bool* values, int size);
void GetBoolArray(const wchar_t * AppName, const wchar_t * KeyName, bool* values, int size, bool default_value = false) const;
void WriteStringList(const wchar_t* AppName, const wchar_t* KeyName, const vector& values); //写入一个字符串列表,由于保存到ini文件中时字符串前后会加上引号,所以字符串中不能包含引号
void GetStringList(const wchar_t* AppName, const wchar_t* KeyName, vector& values, const vector& default_value) const;
void SaveFontData(const wchar_t * AppName, const FontInfo& font);
void LoadFontData(const wchar_t * AppName, FontInfo& font, const FontInfo& default_font) const;
void LoadMainWndColors(const wchar_t * AppName, const wchar_t * KeyName, std::map& text_colors, COLORREF default_color);
void SaveMainWndColors(const wchar_t * AppName, const wchar_t * KeyName, const std::map& text_colors);
void LoadTaskbarWndColors(const wchar_t * AppName, const wchar_t * KeyName, std::map& text_colors, COLORREF default_color);
void SaveTaskbarWndColors(const wchar_t * AppName, const wchar_t * KeyName, const std::map& text_colors);
void LoadPluginDisplayStr(bool is_main_window);
void SavePluginDisplayStr(bool is_main_window);
bool Save(); //将ini文件保存到文件,成功返回true
protected:
wstring m_file_path;
wstring m_ini_str;
bool m_save_as_utf8{ true }; //是否以及UTF8编码保存
void _WriteString(const wchar_t* AppName, const wchar_t* KeyName, const wstring& str);
wstring _GetString(const wchar_t* AppName, const wchar_t* KeyName, const wchar_t* default_str) const;
static wstring MergeStringList(const vector& values);
static void SplitStringList(vector& values, wstring str_value);
};
================================================
FILE: TrafficMonitor/ListCtrlEx.cpp
================================================
#include "stdafx.h"
#include "ListCtrlEx.h"
IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
CListCtrlEx::CListCtrlEx()
{
}
CListCtrlEx::~CListCtrlEx()
{
}
void CListCtrlEx::Edit(int row, int col)
{
EnsureVisible(row, FALSE); //༭һʱȷпɼ
m_editing = true;
m_edit_row = row;
m_edit_col = col;
CRect item_rect;
GetSubItemRect(row, col, LVIR_LABEL, item_rect); //ȡľ
CString text = GetItemText(row, col); //ȡ
m_item_edit.SetWindowText(text); //ʾ༭
m_item_edit.ShowWindow(SW_SHOW); //ʾ༭
m_item_edit.MoveWindow(item_rect); //༭ƶ棬
m_item_edit.SetFocus(); //ʹ༭ȡý
m_item_edit.SetSel(0, -1);
}
void CListCtrlEx::SetEditColMethod(eEditColMethod method)
{
m_edit_col_method = method;
}
void CListCtrlEx::SetEditableCol(const std::initializer_list& paras)
{
m_edit_cols = paras;
}
void CListCtrlEx::EndEdit()
{
if (m_editing)
{
if (m_edit_row >= 0 && m_edit_row < GetItemCount())
{
CString str;
m_item_edit.GetWindowText(str); //ȡñ༭
SetItemText(m_edit_row, m_edit_col, str); //ݸµCListCtrl
}
m_item_edit.ShowWindow(SW_HIDE);//ر༭
m_editing = false;
}
}
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
ON_EN_KILLFOCUS(IDC_ITEM_EDITBOX, &CListCtrlEx::OnEnKillfocusEdit1)
ON_NOTIFY_REFLECT(NM_DBLCLK, &CListCtrlEx::OnNMDblclk)
ON_NOTIFY_REFLECT(LVN_BEGINSCROLL, &CListCtrlEx::OnLvnBeginScroll)
END_MESSAGE_MAP()
void CListCtrlEx::OnEnKillfocusEdit1()
{
//ı༭ؼؼʧȥʱӦ
EndEdit();
}
void CListCtrlEx::PreSubclassWindow()
{
// TODO: ڴרô/û
m_item_edit.Create(WS_BORDER | ES_AUTOHSCROLL, CRect(), this, IDC_ITEM_EDITBOX);
m_item_edit.SetFont(GetFont());
CListCtrl::PreSubclassWindow();
}
void CListCtrlEx::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: ڴӿؼ֪ͨ
//˫Ҫ༭
if (m_edit_col_method == EC_ALL
|| (m_edit_col_method == EC_SPECIFIED && m_edit_cols.find(pNMItemActivate->iSubItem) != m_edit_cols.end()))
{
Edit(pNMItemActivate->iItem, pNMItemActivate->iSubItem);
}
*pResult = 0;
}
void CListCtrlEx::OnLvnBeginScroll(NMHDR *pNMHDR, LRESULT *pResult)
{
// ˹Ҫ Internet Explorer 5.5 ߰汾
// _WIN32_IE >= 0x0560
LPNMLVSCROLL pStateChanged = reinterpret_cast(pNMHDR);
// TODO: ڴӿؼ֪ͨ
EndEdit();
*pResult = 0;
}
================================================
FILE: TrafficMonitor/ListCtrlEx.h
================================================
#pragma once
#include "afxcmn.h"
#include "DrawCommon.h"
#include
#define IDC_ITEM_EDITBOX 1991
class CListCtrlEx :
public CListCtrl
{
DECLARE_DYNAMIC(CListCtrlEx)
public:
CListCtrlEx();
~CListCtrlEx();
void Edit(int row, int col); //༭ָԪ
enum eEditColMethod //Ҫ༭еķʽ
{
EC_NONE, //
EC_ALL, //ȫ
EC_SPECIFIED //ָ
};
void SetEditColMethod(eEditColMethod method);
void SetEditableCol(const std::initializer_list& paras); //༭
private:
CEdit m_item_edit;
int m_edit_row{};
int m_edit_col{};
bool m_editing{};
eEditColMethod m_edit_col_method{ EC_NONE };
std::set m_edit_cols;
protected:
void EndEdit();
DECLARE_MESSAGE_MAP()
afx_msg void OnEnKillfocusEdit1();
virtual void PreSubclassWindow();
afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnLvnBeginScroll(NMHDR *pNMHDR, LRESULT *pResult);
};
================================================
FILE: TrafficMonitor/MainWndColorDlg.cpp
================================================
// MainWndColorDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "MainWndColorDlg.h"
#include "afxdialogex.h"
#include "CMFCColorDialogEx.h"
// CMainWndColorDlg 对话框
IMPLEMENT_DYNAMIC(CMainWndColorDlg, CBaseDialog)
CMainWndColorDlg::CMainWndColorDlg(const std::map& colors, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_MAIN_COLOR_DIALOG, pParent), m_colors(colors)
{
}
CMainWndColorDlg::~CMainWndColorDlg()
{
}
CString CMainWndColorDlg::GetDialogName() const
{
return _T("MainWndColorDlg");
}
void CMainWndColorDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
}
BEGIN_MESSAGE_MAP(CMainWndColorDlg, CBaseDialog)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CMainWndColorDlg::OnNMDblclkList1)
END_MESSAGE_MAP()
// CMainWndColorDlg 消息处理程序
BOOL CMainWndColorDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_MAIN_WINDOW), FALSE); // 设置小图标
//初始化列表控件
CRect rect;
m_list_ctrl.GetClientRect(rect);
m_list_ctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0, width1;
width0 = rect.Width() * 2 / 3;
width1 = rect.Width() - width0 - theApp.DPI(20) - 1;
m_list_ctrl.InsertColumn(0, CCommon::LoadText(IDS_ITEM), LVCFMT_LEFT, width0); //插入第0列
m_list_ctrl.InsertColumn(1, CCommon::LoadText(IDS_COLOR), LVCFMT_LEFT, width1); //插入第1列
m_list_ctrl.SetDrawItemRangMargin(theApp.DPI(2));
//向列表中插入行
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
CString item_name = iter->GetItemName();
if (!item_name.IsEmpty())
{
int index = m_list_ctrl.GetItemCount();
m_list_ctrl.InsertItem(index, item_name);
m_list_ctrl.SetItemColor(index, 1, m_colors[*iter]);
m_list_ctrl.SetItemData(index, (DWORD_PTR)&(*iter));
}
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CMainWndColorDlg::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
int index = pNMItemActivate->iItem;
COLORREF color = m_list_ctrl.GetItemColor(index, 1);
CMFCColorDialogEx colorDlg(color, 0, this);
if (colorDlg.DoModal() == IDOK)
{
color = colorDlg.GetColor();
m_list_ctrl.SetItemColor(index, 1, color);
CommonDisplayItem* item = (CommonDisplayItem*)(m_list_ctrl.GetItemData(index));
m_colors[*item] = color;
}
*pResult = 0;
}
================================================
FILE: TrafficMonitor/MainWndColorDlg.h
================================================
#pragma once
#include "ColorStatic.h"
#include "afxwin.h"
#include "ColorSettingListCtrl.h"
#include "BaseDialog.h"
// CMainWndColorDlg 对话框
class CMainWndColorDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CMainWndColorDlg)
public:
CMainWndColorDlg(const std::map& colors, CWnd* pParent = NULL); // 标准构造函数
virtual ~CMainWndColorDlg();
const std::map& GetColors() const { return m_colors; }
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MAIN_COLOR_DIALOG };
#endif
protected:
std::map m_colors;
CColorSettingListCtrl m_list_ctrl;
virtual CString GetDialogName() const override;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult);
};
================================================
FILE: TrafficMonitor/MainWndSettingsDlg.cpp
================================================
// MainWndSettingsDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "MainWndSettingsDlg.h"
#include "afxdialogex.h"
#include "CMFCColorDialogEx.h"
#include "DisplayTextSettingDlg.h"
// CMainWndSettingsDlg 对话框
IMPLEMENT_DYNAMIC(CMainWndSettingsDlg, CTabDlg)
CMainWndSettingsDlg::CMainWndSettingsDlg(CWnd* pParent /*=NULL*/)
: CTabDlg(IDD_MAIN_WND_SETTINGS_DIALOG, pParent)
{
}
CMainWndSettingsDlg::~CMainWndSettingsDlg()
{
}
void CMainWndSettingsDlg::SetControlMouseWheelEnable(bool enable)
{
m_unit_combo.SetMouseWheelEnable(enable);
m_double_click_combo.SetMouseWheelEnable(enable);
m_font_size_edit.SetMouseWheelEnable(enable);
m_memory_display_combo.SetMouseWheelEnable(enable);
}
void CMainWndSettingsDlg::DrawStaticColor()
{
//CCommon::FillStaticColor(m_color_static, m_data.text_color);
if (m_data.text_colors.empty())
return;
if (m_data.specify_each_item_color)
{
int color_num{};
#ifdef WITHOUT_TEMPERATURE
color_num = 4;
#else
color_num = 8;
#endif
m_color_static.SetColorNum(color_num);
int index{};
for (const auto& item : m_data.text_colors)
{
m_color_static.SetFillColor(index, item.second);
index++;
}
m_color_static.Invalidate();
}
else
{
m_color_static.SetFillColor(m_data.text_colors.begin()->second);
}
}
void CMainWndSettingsDlg::IniUnitCombo()
{
m_unit_combo.ResetContent();
m_unit_combo.AddString(CCommon::LoadText(IDS_AUTO));
if (m_data.unit_byte)
{
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" KB/s")));
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" MB/s")));
}
else
{
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" Kb/s")));
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" Mb/s")));
}
m_unit_combo.SetCurSel(static_cast(m_data.speed_unit));
}
void CMainWndSettingsDlg::EnableControl()
{
bool exe_path_enable = (m_data.double_click_action == DoubleClickAction::SEPCIFIC_APP);
CWnd* pWnd{};
pWnd = GetDlgItem(IDC_EXE_PATH_STATIC);
if (pWnd != nullptr)
pWnd->ShowWindow(exe_path_enable ? SW_SHOW : SW_HIDE);
pWnd = GetDlgItem(IDC_EXE_PATH_EDIT);
if (pWnd != nullptr)
pWnd->ShowWindow(exe_path_enable ? SW_SHOW : SW_HIDE);
pWnd = GetDlgItem(IDC_BROWSE_BUTTON);
if (pWnd != nullptr)
pWnd->ShowWindow(exe_path_enable ? SW_SHOW : SW_HIDE);
}
void CMainWndSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_TEXT_COLOR_STATIC, m_color_static);
CTabDlg::DoDataExchange(pDX);
DDX_Control(pDX, IDC_HIDE_UNIT_CHECK, m_hide_unit_chk);
DDX_Control(pDX, IDC_UNIT_COMBO, m_unit_combo);
DDX_Control(pDX, IDC_FONT_SIZE_EDIT, m_font_size_edit);
DDX_Control(pDX, IDC_DOUBLE_CLICK_COMBO, m_double_click_combo);
DDX_Control(pDX, IDC_MEMORY_DISPLAY_COMBO, m_memory_display_combo);
}
BEGIN_MESSAGE_MAP(CMainWndSettingsDlg, CTabDlg)
//ON_EN_CHANGE(IDC_UPLOAD_EDIT, &CMainWndSettingsDlg::OnEnChangeUploadEdit)
//ON_EN_CHANGE(IDC_DOWNLOAD_EDIT, &CMainWndSettingsDlg::OnEnChangeDownloadEdit)
//ON_EN_CHANGE(IDC_CPU_EDIT, &CMainWndSettingsDlg::OnEnChangeCpuEdit)
//ON_EN_CHANGE(IDC_MEMORY_EDIT, &CMainWndSettingsDlg::OnEnChangeMemoryEdit)
//ON_BN_CLICKED(IDC_SET_COLOR_BUTTON1, &CMainWndSettingsDlg::OnBnClickedSetColorButton1)
//ON_BN_CLICKED(IDC_SET_DEFAULT_BUTTON, &CMainWndSettingsDlg::OnBnClickedSetDefaultButton)
ON_BN_CLICKED(IDC_SET_FONT_BUTTON, &CMainWndSettingsDlg::OnBnClickedSetFontButton)
ON_BN_CLICKED(IDC_SWITCH_UP_DOWN_CHECK, &CMainWndSettingsDlg::OnBnClickedSwitchUpDownCheck)
ON_BN_CLICKED(IDC_FULLSCREEN_HIDE_CHECK, &CMainWndSettingsDlg::OnBnClickedFullscreenHideCheck)
ON_BN_CLICKED(IDC_SPEED_SHORT_MODE_CHECK2, &CMainWndSettingsDlg::OnBnClickedSpeedShortModeCheck2)
ON_CBN_SELCHANGE(IDC_UNIT_COMBO, &CMainWndSettingsDlg::OnCbnSelchangeUnitCombo)
ON_BN_CLICKED(IDC_HIDE_UNIT_CHECK, &CMainWndSettingsDlg::OnBnClickedHideUnitCheck)
ON_BN_CLICKED(IDC_HIDE_PERCENTAGE_CHECK, &CMainWndSettingsDlg::OnBnClickedHidePercentageCheck)
ON_MESSAGE(WM_STATIC_CLICKED, &CMainWndSettingsDlg::OnStaticClicked)
ON_BN_CLICKED(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK, &CMainWndSettingsDlg::OnBnClickedSpecifyEachItemColorCheck)
ON_CBN_SELCHANGE(IDC_DOUBLE_CLICK_COMBO, &CMainWndSettingsDlg::OnCbnSelchangeDoubleClickCombo)
ON_BN_CLICKED(IDC_SEPARATE_VALUE_UNIT_CHECK, &CMainWndSettingsDlg::OnBnClickedSeparateValueUnitCheck)
ON_BN_CLICKED(IDC_UNIT_BYTE_RADIO, &CMainWndSettingsDlg::OnBnClickedUnitByteRadio)
ON_BN_CLICKED(IDC_UNIT_BIT_RADIO, &CMainWndSettingsDlg::OnBnClickedUnitBitRadio)
ON_BN_CLICKED(IDC_SHOW_TOOL_TIP_CHK, &CMainWndSettingsDlg::OnBnClickedShowToolTipChk)
ON_BN_CLICKED(IDC_BROWSE_BUTTON, &CMainWndSettingsDlg::OnBnClickedBrowseButton)
ON_BN_CLICKED(IDC_DISPLAY_TEXT_SETTING_BUTTON, &CMainWndSettingsDlg::OnBnClickedDisplayTextSettingButton)
ON_CBN_SELCHANGE(IDC_MEMORY_DISPLAY_COMBO, &CMainWndSettingsDlg::OnCbnSelchangeMemoryDisplayCombo)
ON_BN_CLICKED(IDC_ALWAYS_ON_TOP_CHECK, &CMainWndSettingsDlg::OnBnClickedAlwaysOnTopCheck)
ON_BN_CLICKED(IDC_MOUSE_PENETRATE_CHECK, &CMainWndSettingsDlg::OnBnClickedMousePenetrateCheck)
ON_BN_CLICKED(IDC_LOCK_WINDOW_POS_CHECK, &CMainWndSettingsDlg::OnBnClickedLockWindowPosCheck)
ON_BN_CLICKED(IDC_ALOW_OUT_OF_BORDER_CHECK, &CMainWndSettingsDlg::OnBnClickedAlowOutOfBorderCheck)
END_MESSAGE_MAP()
// CMainWndSettingsDlg 消息处理程序
BOOL CMainWndSettingsDlg::OnInitDialog()
{
CTabDlg::OnInitDialog();
// TODO: 在此添加额外的初始化
//初始化各控件状态
SetDlgItemText(IDC_FONT_NAME_EDIT, m_data.font.name);
//wchar_t buff[16];
//swprintf_s(buff, L"%d", m_data.font_size);
//SetDlgItemText(IDC_FONT_SIZE_EDIT, buff);
m_font_size_edit.SetRange(5, 72);
m_font_size_edit.SetValue(m_data.font.size);
//SetDlgItemText(IDC_UPLOAD_EDIT, m_data.disp_str.Get(TDI_UP).c_str());
//SetDlgItemText(IDC_DOWNLOAD_EDIT, m_data.disp_str.Get(TDI_DOWN).c_str());
//SetDlgItemText(IDC_CPU_EDIT, m_data.disp_str.Get(TDI_CPU).c_str());
//SetDlgItemText(IDC_MEMORY_EDIT, m_data.disp_str.Get(TDI_MEMORY).c_str());
((CButton*)GetDlgItem(IDC_SWITCH_UP_DOWN_CHECK))->SetCheck(m_data.swap_up_down);
((CButton*)GetDlgItem(IDC_FULLSCREEN_HIDE_CHECK))->SetCheck(m_data.hide_main_wnd_when_fullscreen);
((CButton*)GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK2))->SetCheck(m_data.speed_short_mode);
((CButton*)GetDlgItem(IDC_SEPARATE_VALUE_UNIT_CHECK))->SetCheck(m_data.separate_value_unit_with_space);
((CButton*)GetDlgItem(IDC_SHOW_TOOL_TIP_CHK))->SetCheck(m_data.show_tool_tip);
m_color_static.SetLinkCursor();
DrawStaticColor();
m_toolTip.Create(this);
m_toolTip.SetMaxTipWidth(theApp.DPI(300));
m_toolTip.AddTool(GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK2), CCommon::LoadText(IDS_SPEED_SHORT_MODE_TIP));
if (m_data.unit_byte)
((CButton*)GetDlgItem(IDC_UNIT_BYTE_RADIO))->SetCheck(TRUE);
else
((CButton*)GetDlgItem(IDC_UNIT_BIT_RADIO))->SetCheck(TRUE);
IniUnitCombo();
m_hide_unit_chk.SetCheck(m_data.hide_unit);
if (m_data.speed_unit == SpeedUnit::AUTO)
{
m_hide_unit_chk.SetCheck(FALSE);
m_data.hide_unit = false;
m_hide_unit_chk.EnableWindow(FALSE);
}
((CButton*)GetDlgItem(IDC_HIDE_PERCENTAGE_CHECK))->SetCheck(m_data.hide_percent);
if (m_text_disable)
{
//GetDlgItem(IDC_UPLOAD_EDIT)->EnableWindow(FALSE);
//GetDlgItem(IDC_DOWNLOAD_EDIT)->EnableWindow(FALSE);
//GetDlgItem(IDC_CPU_EDIT)->EnableWindow(FALSE);
//GetDlgItem(IDC_MEMORY_EDIT)->EnableWindow(FALSE);
EnableDlgCtrl(IDC_DISPLAY_TEXT_SETTING_BUTTON, false);
m_data.swap_up_down = false;
((CButton*)GetDlgItem(IDC_SWITCH_UP_DOWN_CHECK))->SetCheck(FALSE);
GetDlgItem(IDC_SWITCH_UP_DOWN_CHECK)->EnableWindow(FALSE);
//GetDlgItem(IDC_SET_DEFAULT_BUTTON)->EnableWindow(FALSE);
}
CheckDlgButton(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK, m_data.specify_each_item_color);
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_CONNECTION_DETIAL));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_HISTORICAL_TRAFFIC));
m_double_click_combo.AddString(CCommon::LoadText(IDS_SHOW_HIDE_MORE_INFO));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_OPTION_SETTINGS));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_TASK_MANAGER));
m_double_click_combo.AddString(CCommon::LoadText(IDS_SPECIFIC_APP));
m_double_click_combo.AddString(CCommon::LoadText(IDS_CHANGE_SKIN));
m_double_click_combo.AddString(CCommon::LoadText(IDS_NONE));
m_double_click_combo.SetCurSel(static_cast(m_data.double_click_action));
SetDlgItemText(IDC_EXE_PATH_EDIT, m_data.double_click_exe.c_str());
EnableControl();
//初始化内存显示方式下拉列表
m_memory_display_combo.AddString(CCommon::LoadText(IDS_USAGE_PERCENTAGE));
m_memory_display_combo.AddString(CCommon::LoadText(IDS_MEMORY_USED));
m_memory_display_combo.AddString(CCommon::LoadText(IDS_MEMORY_AVAILABLE));
m_memory_display_combo.SetCurSel(static_cast(m_data.memory_display));
CheckDlgButton(IDC_ALWAYS_ON_TOP_CHECK, m_data.m_always_on_top);
CheckDlgButton(IDC_MOUSE_PENETRATE_CHECK, m_data.m_mouse_penetrate);
CheckDlgButton(IDC_LOCK_WINDOW_POS_CHECK, m_data.m_lock_window_pos);
CheckDlgButton(IDC_ALOW_OUT_OF_BORDER_CHECK, m_data.m_alow_out_of_border);
////设置控件不响应鼠标滚轮消息
//m_unit_combo.SetMouseWheelEnable(false);
//m_double_click_combo.SetMouseWheelEnable(false);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
//void CMainWndSettingsDlg::OnEnChangeUploadEdit()
//{
// // TODO: 如果该控件是 RICHEDIT 控件,它将不
// // 发送此通知,除非重写 CTabDlg::OnInitDialog()
// // 函数并调用 CRichEditCtrl().SetEventMask(),
// // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
//
// // TODO: 在此添加控件通知处理程序代码
// CString tmp;
// GetDlgItemText(IDC_UPLOAD_EDIT, tmp);
// m_data.disp_str.Get(TDI_UP) = tmp;
//}
//
//
//void CMainWndSettingsDlg::OnEnChangeDownloadEdit()
//{
// // TODO: 如果该控件是 RICHEDIT 控件,它将不
// // 发送此通知,除非重写 CTabDlg::OnInitDialog()
// // 函数并调用 CRichEditCtrl().SetEventMask(),
// // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
//
// // TODO: 在此添加控件通知处理程序代码
// CString tmp;
// GetDlgItemText(IDC_DOWNLOAD_EDIT, tmp);
// m_data.disp_str.Get(TDI_DOWN) = tmp;
//}
//
//
//void CMainWndSettingsDlg::OnEnChangeCpuEdit()
//{
// // TODO: 如果该控件是 RICHEDIT 控件,它将不
// // 发送此通知,除非重写 CTabDlg::OnInitDialog()
// // 函数并调用 CRichEditCtrl().SetEventMask(),
// // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
//
// // TODO: 在此添加控件通知处理程序代码
// CString tmp;
// GetDlgItemText(IDC_CPU_EDIT, tmp);
// m_data.disp_str.Get(TDI_CPU) = tmp;
//}
//
//
//void CMainWndSettingsDlg::OnEnChangeMemoryEdit()
//{
// // TODO: 如果该控件是 RICHEDIT 控件,它将不
// // 发送此通知,除非重写 CTabDlg::OnInitDialog()
// // 函数并调用 CRichEditCtrl().SetEventMask(),
// // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
//
// // TODO: 在此添加控件通知处理程序代码
// CString tmp;
// GetDlgItemText(IDC_MEMORY_EDIT, tmp);
// m_data.disp_str.Get(TDI_MEMORY) = tmp;
//}
//void CMainWndSettingsDlg::OnBnClickedSetDefaultButton()
//{
// // TODO: 在此添加控件通知处理程序代码
// m_data.disp_str.Get(TDI_UP) = CCommon::LoadText(IDS_UPLOAD_DISP, _T(": "));
// m_data.disp_str.Get(TDI_DOWN) = CCommon::LoadText(IDS_DOWNLOAD_DISP, _T(": "));
// m_data.disp_str.Get(TDI_CPU) = L"CPU: ";
// m_data.disp_str.Get(TDI_MEMORY) = CCommon::LoadText(IDS_MEMORY_DISP, _T(": "));
// SetDlgItemText(IDC_UPLOAD_EDIT, m_data.disp_str.Get(TDI_UP).c_str());
// SetDlgItemText(IDC_DOWNLOAD_EDIT, m_data.disp_str.Get(TDI_DOWN).c_str());
// SetDlgItemText(IDC_CPU_EDIT, m_data.disp_str.Get(TDI_CPU).c_str());
// SetDlgItemText(IDC_MEMORY_EDIT, m_data.disp_str.Get(TDI_MEMORY).c_str());
//}
void CMainWndSettingsDlg::OnBnClickedSetFontButton()
{
// TODO: 在此添加控件通知处理程序代码
LOGFONT lf{};
lf.lfHeight = FontSizeToLfHeight(m_data.font.size);
lf.lfWeight = (m_data.font.bold ? FW_BOLD : FW_NORMAL);
lf.lfItalic = m_data.font.italic;
lf.lfUnderline = m_data.font.underline;
lf.lfStrikeOut = m_data.font.strike_out;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
//wcsncpy_s(lf.lfFaceName, m_data.font.name.GetString(), 32);
CCommon::WStringCopy(lf.lfFaceName, 32, m_data.font.name.GetString());
CCommon::NormalizeFont(lf);
CFontDialog fontDlg(&lf); //构造字体对话框,初始选择字体为之前字体
if (IDOK == fontDlg.DoModal()) // 显示字体对话框
{
//获取字体信息
m_data.font.name = fontDlg.GetFaceName();
m_data.font.size = fontDlg.GetSize() / 10;
m_data.font.bold = (fontDlg.IsBold() != FALSE);
m_data.font.italic = (fontDlg.IsItalic() != FALSE);
m_data.font.underline = (fontDlg.IsUnderline() != FALSE);
m_data.font.strike_out = (fontDlg.IsStrikeOut() != FALSE);
//将字体信息显示出来
SetDlgItemText(IDC_FONT_NAME_EDIT, m_data.font.name);
wchar_t buff[16];
swprintf_s(buff, L"%d", m_data.font.size);
SetDlgItemText(IDC_FONT_SIZE_EDIT, buff);
}
}
void CMainWndSettingsDlg::OnBnClickedSwitchUpDownCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.swap_up_down = (((CButton*)GetDlgItem(IDC_SWITCH_UP_DOWN_CHECK))->GetCheck() != 0);
}
void CMainWndSettingsDlg::OnBnClickedFullscreenHideCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hide_main_wnd_when_fullscreen = (((CButton*)GetDlgItem(IDC_FULLSCREEN_HIDE_CHECK))->GetCheck() != 0);
}
void CMainWndSettingsDlg::OnBnClickedSpeedShortModeCheck2()
{
// TODO: 在此添加控件通知处理程序代码
m_data.speed_short_mode = (((CButton*)GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK2))->GetCheck() != 0);
}
void CMainWndSettingsDlg::OnCbnSelchangeUnitCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.speed_unit = static_cast(m_unit_combo.GetCurSel());
if (m_data.speed_unit == SpeedUnit::AUTO)
{
m_hide_unit_chk.SetCheck(FALSE);
m_data.hide_unit = false;
m_hide_unit_chk.EnableWindow(FALSE);
}
else
{
m_hide_unit_chk.EnableWindow(TRUE);
}
}
void CMainWndSettingsDlg::OnBnClickedHideUnitCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hide_unit = (m_hide_unit_chk.GetCheck() != 0);
}
BOOL CMainWndSettingsDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_MOUSEMOVE)
m_toolTip.RelayEvent(pMsg);
return CTabDlg::PreTranslateMessage(pMsg);
}
void CMainWndSettingsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//获取字体设置
int font_size;
font_size = m_font_size_edit.GetValue();
if (font_size > MAX_FONT_SIZE || font_size < MIN_FONT_SIZE)
{
CString info;
info.Format(CCommon::LoadText(IDS_FONT_SIZE_WARNING), MIN_FONT_SIZE, MAX_FONT_SIZE);
MessageBox(info, NULL, MB_OK | MB_ICONWARNING);
}
else
{
m_data.font.size = font_size;
}
GetDlgItemText(IDC_FONT_NAME_EDIT, m_data.font.name);
CTabDlg::OnOK();
}
void CMainWndSettingsDlg::OnBnClickedHidePercentageCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hide_percent = (((CButton*)GetDlgItem(IDC_HIDE_PERCENTAGE_CHECK))->GetCheck() != 0);
}
afx_msg LRESULT CMainWndSettingsDlg::OnStaticClicked(WPARAM wParam, LPARAM lParam)
{
switch (::GetDlgCtrlID(((CWnd*)wParam)->m_hWnd))
{
case IDC_TEXT_COLOR_STATIC:
{
//设置文本颜色
if (m_data.specify_each_item_color)
{
CMainWndColorDlg colorDlg(m_data.text_colors);
if (colorDlg.DoModal() == IDOK)
{
m_data.text_colors = colorDlg.GetColors();
DrawStaticColor();
}
}
else if (!m_data.text_colors.empty())
{
CMFCColorDialogEx colorDlg(m_data.text_colors.begin()->second, 0, this);
if (colorDlg.DoModal() == IDOK)
{
m_data.text_colors.begin()->second = colorDlg.GetColor();
DrawStaticColor();
}
}
break;
}
default:
break;
}
return 0;
}
void CMainWndSettingsDlg::OnBnClickedSpecifyEachItemColorCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.specify_each_item_color = (((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->GetCheck() != 0);
DrawStaticColor();
}
void CMainWndSettingsDlg::OnCbnSelchangeDoubleClickCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.double_click_action = static_cast(m_double_click_combo.GetCurSel());
EnableControl();
}
void CMainWndSettingsDlg::OnBnClickedSeparateValueUnitCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.separate_value_unit_with_space = (((CButton*)GetDlgItem(IDC_SEPARATE_VALUE_UNIT_CHECK))->GetCheck() != 0);
}
void CMainWndSettingsDlg::OnBnClickedUnitByteRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.unit_byte = true;
IniUnitCombo();
}
void CMainWndSettingsDlg::OnBnClickedUnitBitRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.unit_byte = false;
IniUnitCombo();
}
void CMainWndSettingsDlg::OnBnClickedShowToolTipChk()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_tool_tip = (((CButton*)GetDlgItem(IDC_SHOW_TOOL_TIP_CHK))->GetCheck() != 0);
}
void CMainWndSettingsDlg::OnBnClickedBrowseButton()
{
// TODO: 在此添加控件通知处理程序代码
CString szFilter = CCommon::LoadText(IDS_EXE_FILTER);
CFileDialog fileDlg(TRUE, NULL, NULL, 0, szFilter, this);
if (IDOK == fileDlg.DoModal())
{
m_data.double_click_exe = fileDlg.GetPathName();
SetDlgItemText(IDC_EXE_PATH_EDIT, m_data.double_click_exe.c_str());
}
}
void CMainWndSettingsDlg::OnBnClickedDisplayTextSettingButton()
{
// TODO: 在此添加控件通知处理程序代码
CDisplayTextSettingDlg dlg(m_data.disp_str, true);
dlg.DoModal();
}
void CMainWndSettingsDlg::OnCbnSelchangeMemoryDisplayCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.memory_display = static_cast(m_memory_display_combo.GetCurSel());
}
void CMainWndSettingsDlg::OnBnClickedAlwaysOnTopCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_always_on_top = IsDlgButtonChecked(IDC_ALWAYS_ON_TOP_CHECK) != 0;
}
void CMainWndSettingsDlg::OnBnClickedMousePenetrateCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_mouse_penetrate = IsDlgButtonChecked(IDC_MOUSE_PENETRATE_CHECK) != 0;
}
void CMainWndSettingsDlg::OnBnClickedLockWindowPosCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_lock_window_pos = IsDlgButtonChecked(IDC_LOCK_WINDOW_POS_CHECK) != 0;
}
void CMainWndSettingsDlg::OnBnClickedAlowOutOfBorderCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.m_alow_out_of_border = IsDlgButtonChecked(IDC_ALOW_OUT_OF_BORDER_CHECK) != 0;
}
================================================
FILE: TrafficMonitor/MainWndSettingsDlg.h
================================================
#pragma once
#include "ColorStatic.h"
#include "afxwin.h"
#include "SpinEdit.h"
#include "TabDlg.h"
#include "MainWndColorDlg.h"
#include "ComboBox2.h"
// CMainWndSettingsDlg 对话框
class CMainWndSettingsDlg : public CTabDlg
{
DECLARE_DYNAMIC(CMainWndSettingsDlg)
public:
CMainWndSettingsDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CMainWndSettingsDlg();
//选项设置数据
MainWndSettingData m_data;
bool m_text_disable{ false }; //如果为true,则不允许设置“显示文本”,并不允许交换上传和下载的位置
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MAIN_WND_SETTINGS_DIALOG };
#endif
protected:
//控件变量
CColorStatic m_color_static;
CToolTipCtrl m_toolTip;
CComboBox2 m_unit_combo;
CButton m_hide_unit_chk;
CSpinEdit m_font_size_edit;
CComboBox2 m_double_click_combo;
CComboBox2 m_memory_display_combo;
protected:
void DrawStaticColor();
void IniUnitCombo();
void EnableControl();
virtual void SetControlMouseWheelEnable(bool enable) override;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
//afx_msg void OnEnChangeUploadEdit();
//afx_msg void OnEnChangeDownloadEdit();
//afx_msg void OnEnChangeCpuEdit();
//afx_msg void OnEnChangeMemoryEdit();
//afx_msg void OnBnClickedSetDefaultButton();
afx_msg void OnBnClickedSetFontButton();
afx_msg void OnBnClickedSwitchUpDownCheck();
afx_msg void OnBnClickedFullscreenHideCheck();
afx_msg void OnBnClickedSpeedShortModeCheck2();
afx_msg void OnCbnSelchangeUnitCombo();
afx_msg void OnBnClickedHideUnitCheck();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual void OnOK();
afx_msg void OnBnClickedHidePercentageCheck();
protected:
afx_msg LRESULT OnStaticClicked(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnBnClickedSpecifyEachItemColorCheck();
afx_msg void OnCbnSelchangeDoubleClickCombo();
afx_msg void OnBnClickedSeparateValueUnitCheck();
afx_msg void OnBnClickedUnitByteRadio();
afx_msg void OnBnClickedUnitBitRadio();
afx_msg void OnBnClickedShowToolTipChk();
afx_msg void OnBnClickedBrowseButton();
afx_msg void OnBnClickedDisplayTextSettingButton();
afx_msg void OnCbnSelchangeMemoryDisplayCombo();
afx_msg void OnBnClickedAlwaysOnTopCheck();
afx_msg void OnBnClickedMousePenetrateCheck();
afx_msg void OnBnClickedLockWindowPosCheck();
afx_msg void OnBnClickedAlowOutOfBorderCheck();
};
================================================
FILE: TrafficMonitor/MessageDlg.cpp
================================================
// HelpDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "MessageDlg.h"
#include "DrawCommon.h"
// CMessageDlg 对话框
#define MESSAGE_DLG_ICON_SIZE (theApp.DPI(32))
IMPLEMENT_DYNAMIC(CMessageDlg, CDialog)
CMessageDlg::CMessageDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_MESSAGE_DIALOG, pParent)
{
}
CMessageDlg::~CMessageDlg()
{
}
void CMessageDlg::SetWindowTitle(LPCTSTR str)
{
m_title = str;
}
void CMessageDlg::SetInfoText(LPCTSTR str)
{
m_info = str;
}
void CMessageDlg::SetMessageText(LPCTSTR str)
{
m_message = str;
}
//void CMessageDlg::SetLinkInfo(LPCTSTR text, LPCTSTR url)
//{
// m_link_text = text;
// m_link_url = url;
//}
void CMessageDlg::SetMessageIcon(HICON hIcon)
{
m_icon = hIcon;
}
void CMessageDlg::SetInfoStaticSize(int cx)
{
if (m_icon != NULL && m_info_static.GetSafeHwnd() != NULL)
{
CRect rc_info{ m_rc_info };
rc_info.left = m_rc_info.left + MESSAGE_DLG_ICON_SIZE + theApp.DPI(8);
if (cx > 0)
rc_info.right = cx;
m_info_static.MoveWindow(rc_info);
}
}
void CMessageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_HELP_EDIT, m_message_edit);
DDX_Control(pDX, IDC_INFO_STATIC, m_info_static);
}
BEGIN_MESSAGE_MAP(CMessageDlg, CDialog)
ON_WM_GETMINMAXINFO()
//ON_NOTIFY(NM_CLICK, IDC_SYSLINK1, &CMessageDlg::OnNMClickSyslink1)
ON_WM_PAINT()
ON_WM_SIZE()
END_MESSAGE_MAP()
// CMessageDlg 消息处理程序
BOOL CMessageDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME), FALSE); // 设置小图标
//获取初始时窗口的大小
CRect rect;
GetWindowRect(rect);
m_min_size.cx = rect.Width();
m_min_size.cy = rect.Height();
SetWindowText(m_title);
m_info_static.SetWindowText(m_info);
m_message_edit.SetWindowText(m_message);
//CWnd* pLinkCtrl = GetDlgItem(IDC_SYSLINK1);
//if (pLinkCtrl != nullptr)
//{
// pLinkCtrl->ShowWindow(m_show_link_ctrl);
// pLinkCtrl->SetWindowText(_T("") + m_link_text + _T(" "));
//}
//设置图标的位置
if (m_icon != NULL)
{
CRect rc_edit;
m_message_edit.GetWindowRect(rc_edit);
ScreenToClient(rc_edit);
m_icon_pos.x = rc_edit.left;
m_icon_pos.y = (rc_edit.top - MESSAGE_DLG_ICON_SIZE) / 2;
m_info_static.GetWindowRect(m_rc_info);
ScreenToClient(m_rc_info);
SetInfoStaticSize(0);
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CMessageDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//限制窗口最小大小
lpMMI->ptMinTrackSize.x = m_min_size.cx; //设置最小宽度
lpMMI->ptMinTrackSize.y = m_min_size.cy; //设置最小高度
CDialog::OnGetMinMaxInfo(lpMMI);
}
//void CMessageDlg::OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult)
//{
// // TODO: 在此添加控件通知处理程序代码
// if(!m_link_url.IsEmpty())
// ShellExecute(NULL, _T("open"), m_link_url, NULL, NULL, SW_SHOW); //打开超链接
//
// *pResult = 0;
//}
void CMessageDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialog::OnPaint()
CDrawCommon draw;
draw.Create(&dc, this);
draw.DrawIcon(m_icon, m_icon_pos, CSize(MESSAGE_DLG_ICON_SIZE, MESSAGE_DLG_ICON_SIZE));
}
void CMessageDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
SetInfoStaticSize(cx);
}
================================================
FILE: TrafficMonitor/MessageDlg.h
================================================
#pragma once
#include "afxwin.h"
// CMessageDlg 对话框
class CMessageDlg : public CDialog
{
DECLARE_DYNAMIC(CMessageDlg)
public:
CMessageDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CMessageDlg();
void SetWindowTitle(LPCTSTR str);
void SetInfoText(LPCTSTR str);
void SetMessageText(LPCTSTR str);
//void ShowLinkStatic(bool show = true) { m_show_link_ctrl = show; }
//void SetLinkInfo(LPCTSTR text, LPCTSTR url);
void SetMessageIcon(HICON hIcon);
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MESSAGE_DIALOG };
#endif
protected:
CEdit m_message_edit;
CSize m_min_size; //窗口的最小大小
CStatic m_info_static;
CString m_title;
CString m_info;
CString m_message;
//CString m_link_text;
//CString m_link_url;
HICON m_icon{};
CPoint m_icon_pos{}; //图标的位置
CRect m_rc_info{}; //错误信息Static控件的初始区域
//bool m_show_link_ctrl{ false };
protected:
void SetInfoStaticSize(int cx); //如果设置了图标,则需要将错误信息Static控件向右移动一些
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);
//afx_msg void OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
};
================================================
FILE: TrafficMonitor/NetworkInfoDlg.cpp
================================================
// CNetworkInfoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "NetworkInfoDlg.h"
#include "afxdialogex.h"
// CNetworkInfoDlg 对话框
IMPLEMENT_DYNAMIC(CNetworkInfoDlg, CBaseDialog)
CNetworkInfoDlg::CNetworkInfoDlg(vector& adapters, MIB_IFROW* pIfRow, int connection_selected, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_NETWORK_INFO_DIALOG, pParent), m_connections(adapters), m_pIfRow(pIfRow), m_connection_selected(connection_selected)
{
m_current_connection = connection_selected;
}
CNetworkInfoDlg::~CNetworkInfoDlg()
{
}
void CNetworkInfoDlg::ShowInfo()
{
CString temp;
MIB_IFROW& network_info = GetConnectIfTable(m_connection_selected);
//接口名
m_info_list.SetItemText(0, 1, network_info.wszName);
//接口描述
m_info_list.SetItemText(1, 1, CCommon::StrToUnicode((const char*)network_info.bDescr).c_str());
//连接类型
switch (network_info.dwType)
{
case IF_TYPE_OTHER: temp = CCommon::LoadText(IDS_IF_TYPE_OTHER); break;
case IF_TYPE_ETHERNET_CSMACD: temp = CCommon::LoadText(IDS_IF_TYPE_ETHERNET_CSMACD); break;
case IF_TYPE_ISO88025_TOKENRING: temp = CCommon::LoadText(IDS_IF_TYPE_ISO88025_TOKENRING); break;
case IF_TYPE_FDDI: temp = CCommon::LoadText(IDS_IF_TYPE_FDDI); break;
case IF_TYPE_PPP: temp = CCommon::LoadText(IDS_IF_TYPE_PPP); break;
case IF_TYPE_SOFTWARE_LOOPBACK: temp = CCommon::LoadText(IDS_IF_TYPE_SOFTWARE_LOOPBACK); break;
case IF_TYPE_ATM: temp = CCommon::LoadText(IDS_IF_TYPE_ATM); break;
case IF_TYPE_IEEE80211: temp = CCommon::LoadText(IDS_IF_TYPE_IEEE80211); break;
case IF_TYPE_TUNNEL: temp = CCommon::LoadText(IDS_IF_TYPE_TUNNEL); break;
case IF_TYPE_IEEE1394: temp = CCommon::LoadText(IDS_IF_TYPE_IEEE1394); break;
case IF_TYPE_IEEE80216_WMAN: temp = CCommon::LoadText(IDS_IF_TYPE_IEEE80216_WMAN); break;
case IF_TYPE_WWANPP: temp = CCommon::LoadText(IDS_IF_TYPE_WWANPP); break;
case IF_TYPE_WWANPP2: temp = CCommon::LoadText(IDS_IF_TYPE_WWANPP2); break;
default: temp = CCommon::LoadText(IDS_UNKNOW_CONNECTION); break;
}
m_info_list.SetItemText(2, 1, temp);
//速度
temp.Format(_T("%dMbps"), network_info.dwSpeed / 1000000);
m_info_list.SetItemText(3, 1, temp);
//适配器物理地址
temp = _T("");
char buff[3];
for (size_t i{}; i < network_info.dwPhysAddrLen; i++)
{
sprintf_s(buff, "%.2x", network_info.bPhysAddr[i]);
temp += buff;
if (i != network_info.dwPhysAddrLen - 1)
temp += _T('-');
}
m_info_list.SetItemText(4, 1, temp);
//IP地址
m_info_list.SetItemText(5, 1, GetConnection(m_connection_selected).ip_address.c_str());
//子网掩码
m_info_list.SetItemText(6, 1, GetConnection(m_connection_selected).subnet_mask.c_str());
//默认网关
m_info_list.SetItemText(7, 1, GetConnection(m_connection_selected).default_gateway.c_str());
//连接状态
switch (network_info.dwOperStatus)
{
case IF_OPER_STATUS_NON_OPERATIONAL: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_NON_OPERATIONAL); break;
case IF_OPER_STATUS_UNREACHABLE: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_UNREACHABLE); break;
case IF_OPER_STATUS_DISCONNECTED: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_DISCONNECTED); break;
case IF_OPER_STATUS_CONNECTING: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_CONNECTING); break;
case IF_OPER_STATUS_CONNECTED: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_CONNECTED); break;
case IF_OPER_STATUS_OPERATIONAL: temp = CCommon::LoadText(IDS_IF_OPER_STATUS_OPERATIONAL); break;
//case IfOperStatusUp:
// temp = CCommon::LoadText(IDS_IF_OPER_STATUS_UP);
// break;
//case IfOperStatusDown:
//case IfOperStatusNotPresent:
//case IfOperStatusLowerLayerDown:
// temp = CCommon::LoadText(IDS_IF_OPER_STATUS_DOWN);
// break;
//case IfOperStatusTesting:
//case IfOperStatusUnknown:
// temp = CCommon::LoadText(IDS_UNKNOW_STATUS);
// break;
//case IfOperStatusDormant:
// temp = CCommon::LoadText(IDS_IF_OPER_STATUS_DORMANT);
//break;
default: temp = CCommon::LoadText(IDS_UNKNOW_STATUS); break;
}
m_info_list.SetItemText(8, 1, temp);
//已接收字节数
temp.Format(_T("%s (%s)"), CCommon::IntToString(network_info.dwInOctets, true, true), CCommon::DataSizeToString(network_info.dwInOctets));
m_info_list.SetItemText(9, 1, temp);
//已发送字节数
temp.Format(_T("%s (%s)"), CCommon::IntToString(network_info.dwOutOctets, true, true), CCommon::DataSizeToString(network_info.dwOutOctets));
m_info_list.SetItemText(10, 1, temp);
//自程序启动以来已接收字节数
unsigned __int64 in_bytes_since_start;
in_bytes_since_start = network_info.dwInOctets - GetConnection(m_connection_selected).in_bytes;
temp.Format(_T("%s (%s)"), CCommon::IntToString(in_bytes_since_start, true, true), CCommon::DataSizeToString(in_bytes_since_start));
m_info_list.SetItemText(11, 1, temp);
//自程序启动以来已发送字节数
unsigned __int64 out_bytes_since_start;
out_bytes_since_start = network_info.dwOutOctets - GetConnection(m_connection_selected).out_bytes;
temp.Format(_T("%s (%s)"), CCommon::IntToString(out_bytes_since_start, true, true), CCommon::DataSizeToString(out_bytes_since_start));
m_info_list.SetItemText(12, 1, temp);
//显示当前选择指示
CString str;
str.Format(_T("%d/%d"), m_connection_selected + 1, m_connections.size());
SetDlgItemText(IDC_INDEX_STATIC, str);
CFont* font = GetFont();
CWnd* index_static = GetDlgItem(IDC_INDEX_STATIC);
if (m_current_connection == m_connection_selected && !theApp.m_cfg_data.m_select_all)
index_static->SetFont(&m_font_bold);
else
index_static->SetFont(font);
}
void CNetworkInfoDlg::GetProgramElapsedTime()
{
//程序已运行时间
SYSTEMTIME current_time, time;
GetLocalTime(¤t_time);
time = CCommon::CompareSystemTime(current_time, m_start_time);
CString temp;
temp.Format(CCommon::LoadText(IDS_HOUR_MINUTE_SECOND), time.wHour, time.wMinute, time.wSecond);
m_info_list.SetItemText(13, 1, temp);
}
MIB_IFROW& CNetworkInfoDlg::GetConnectIfTable(int connection_index)
{
static MIB_IFROW nouse{};
if (connection_index >= 0 && connection_index < static_cast(m_connections.size()))
{
int index = m_connections[connection_index].index;
if (m_pIfRow != nullptr)
return m_pIfRow[index];
}
return nouse;
}
NetWorkConection CNetworkInfoDlg::GetConnection(int connection_index)
{
if (connection_index >= 0 && connection_index < static_cast(m_connections.size()))
return m_connections[connection_index];
else
return NetWorkConection();
}
UINT CNetworkInfoDlg::GetInternetIPThreadFunc(LPVOID lpParam)
{
CCommon::SetThreadLanguage(theApp.m_general_data.language); //设置线程语言
CNetworkInfoDlg* p_instance = (CNetworkInfoDlg*)lpParam;
wstring ip_address, ip_location;
//IPV4
CCommon::GetInternetIp2(ip_address, ip_location, false); //获取外网IP地址,
if (!IsWindow(p_instance->GetSafeHwnd())) //如果当前对话框已经销毁,则退出线程
return 0;
if (!ip_address.empty())
{
CString info;
if (ip_location.empty())
info = ip_address.c_str();
else
info.Format(_T("%s (%s)"), ip_address.c_str(), ip_location.c_str());
p_instance->m_info_list.SetItemText(14, 1, info);
}
else
{
p_instance->m_info_list.SetItemText(14, 1, CCommon::LoadText(IDS_GET_FAILED));
}
//IPV6
wstring ipv6_address, ipv6_location;
CCommon::GetInternetIp2(ip_address, ip_location, true); //获取外网IP地址,
if (!IsWindow(p_instance->GetSafeHwnd())) //如果当前对话框已经销毁,则退出线程
return 0;
if (!ip_address.empty())
{
CString info;
if (ip_location.empty())
info = ip_address.c_str();
else
info.Format(_T("%s (%s)"), ip_address.c_str(), ip_location.c_str());
p_instance->m_info_list.SetItemText(15, 1, info);
}
else
{
p_instance->m_info_list.SetItemText(15, 1, CCommon::LoadText(IDS_GET_FAILED));
}
p_instance->m_ip_acquired = true;
return 0;
}
CString CNetworkInfoDlg::GetDialogName() const
{
return _T("NetworkInfoDlg");
}
void CNetworkInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INFO_LIST1, m_info_list);
}
BEGIN_MESSAGE_MAP(CNetworkInfoDlg, CBaseDialog)
ON_COMMAND(ID_COPY_TEXT, &CNetworkInfoDlg::OnCopyText)
ON_NOTIFY(NM_RCLICK, IDC_INFO_LIST1, &CNetworkInfoDlg::OnNMRClickInfoList1)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_PREVIOUS_BUTTON, &CNetworkInfoDlg::OnBnClickedPreviousButton)
ON_BN_CLICKED(IDC_NEXT_BUTTON, &CNetworkInfoDlg::OnBnClickedNextButton)
ON_WM_TIMER()
ON_WM_MOUSEWHEEL()
ON_NOTIFY(NM_DBLCLK, IDC_INFO_LIST1, &CNetworkInfoDlg::OnNMDblclkInfoList1)
END_MESSAGE_MAP()
// CNetworkInfoDlg 消息处理程序
BOOL CNetworkInfoDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_CONNECTION_DETIAL));
SetIcon(theApp.GetMenuIcon(IDI_INFO), FALSE); // 设置小图标
//重新获取IP地址
CAdapterCommon::RefreshIpAddress(m_connections);
//初始化列表控件
CRect rect;
m_info_list.GetClientRect(rect);
m_info_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0, width1;
width0 = rect.Width() / 4;
width1 = rect.Width() - width0 - theApp.DPI(20) - 1;
m_info_list.InsertColumn(0, CCommon::LoadText(IDS_ITEM), LVCFMT_LEFT, width0); //插入第0列
m_info_list.InsertColumn(1, CCommon::LoadText(IDS_VALUE), LVCFMT_LEFT, width1); //插入第1列
//向列表中插入行
m_info_list.InsertItem(0, CCommon::LoadText(IDS_INTERFACE_NAME));
m_info_list.InsertItem(1, CCommon::LoadText(IDS_INTERFACE_DESCRIPTION));
m_info_list.InsertItem(2, CCommon::LoadText(IDS_CONNECTION_TYPE));
m_info_list.InsertItem(3, CCommon::LoadText(IDS_SPEED));
m_info_list.InsertItem(4, CCommon::LoadText(IDS_ADAPTER_PHYSICAL_ADDRESS));
m_info_list.InsertItem(5, CCommon::LoadText(IDS_IP_ADDRESS));
m_info_list.InsertItem(6, CCommon::LoadText(IDS_SUBNET_MASK));
m_info_list.InsertItem(7, CCommon::LoadText(IDS_DEFAULT_GATEWAY));
m_info_list.InsertItem(8, CCommon::LoadText(IDS_OPERATIONAL_STATUS));
m_info_list.InsertItem(9, CCommon::LoadText(IDS_BYTES_RECEIVED));
m_info_list.InsertItem(10, CCommon::LoadText(IDS_BYTES_SENT));
m_info_list.InsertItem(11, CCommon::LoadText(IDS_BYTES_RECEIVED_SINCE_START));
m_info_list.InsertItem(12, CCommon::LoadText(IDS_BYTES_SENT_SINCE_START));
m_info_list.InsertItem(13, CCommon::LoadText(IDS_PROGRAM_ELAPSED_TIME));
m_info_list.InsertItem(14, CCommon::LoadText(IDS_INTERNET_IP_ADDRESS, _T(" (ipv4)")));
m_info_list.InsertItem(15, CCommon::LoadText(IDS_INTERNET_IP_ADDRESS, _T(" (ipv6)")));
//if (theApp.m_cfg_data.m_show_internet_ip)
//{
// m_info_list.SetItemText(14, 1, CCommon::LoadText(IDS_ACQUIRING, _T("...")));
// m_info_list.SetItemText(15, 1, CCommon::LoadText(IDS_ACQUIRING, _T("...")));
//}
//else
//{
m_info_list.SetItemText(14, 1, CCommon::LoadText(IDS_DOUBLE_CLICK_TO_ACQUIRE));
m_info_list.SetItemText(15, 1, CCommon::LoadText(IDS_DOUBLE_CLICK_TO_ACQUIRE));
//}
//显示列表中的信息
LOGFONT lf{};
GetFont()->GetLogFont(&lf);
lf.lfWeight = FW_BOLD;
m_font_bold.CreateFontIndirect(&lf);
ShowInfo();
GetProgramElapsedTime();
//CCommon::GetInternetIp();
//if (theApp.m_cfg_data.m_show_internet_ip)
// m_pGetIPThread = AfxBeginThread(GetInternetIPThreadFunc, this); //启动获取外网IP的线程
//SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); //取消置顶
m_info_list.GetToolTips()->SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
m_menu.LoadMenu(IDR_INFO_MENU); //装载右键菜单
SetTimer(CONNECTION_DETAIL_TIMER, 1000, NULL);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CNetworkInfoDlg::OnCopyText()
{
// TODO: 在此添加命令处理程序代码
if (!CCommon::CopyStringToClipboard(wstring(m_selected_string)))
MessageBox(CCommon::LoadText(IDS_COPY_TO_CLIPBOARD_FAILED), NULL, MB_ICONWARNING);
}
void CNetworkInfoDlg::OnNMRClickInfoList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
//获取鼠标点击处的文本
int item, sub_item;
item = pNMItemActivate->iItem;
sub_item = pNMItemActivate->iSubItem;
m_selected_string = m_info_list.GetItemText(item, sub_item);
//弹出右键菜单
CMenu* pContextMenu = m_menu.GetSubMenu(0); //获取第一个弹出菜单
CPoint point1; //定义一个用于确定光标位置的位置
GetCursorPos(&point1); //获取当前光标的位置,以便使得菜单可以跟随光标
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point1.x, point1.y, this); //在指定位置显示弹出菜单
*pResult = 0;
}
void CNetworkInfoDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//对话框关闭时强制结束获取IP地址的线程
//if(theApp.m_cfg_data.m_show_internet_ip)
// TerminateThread(m_pGetIPThread->m_hThread, 0);
CBaseDialog::OnClose();
}
void CNetworkInfoDlg::OnBnClickedPreviousButton()
{
// TODO: 在此添加控件通知处理程序代码
if (m_connections.size() > 1 && m_connection_selected > 0)
{
m_connection_selected--;
ShowInfo();
}
}
void CNetworkInfoDlg::OnBnClickedNextButton()
{
// TODO: 在此添加控件通知处理程序代码
if (m_connections.size() > 1 && m_connection_selected < m_connections.size() - 1)
{
m_connection_selected++;
ShowInfo();
}
}
BOOL CNetworkInfoDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_LEFT)
{
OnBnClickedPreviousButton();
return TRUE;
}
if (pMsg->wParam == VK_RIGHT)
{
OnBnClickedNextButton();
return TRUE;
}
}
return CBaseDialog::PreTranslateMessage(pMsg);
}
void CNetworkInfoDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == CONNECTION_DETAIL_TIMER)
{
GetProgramElapsedTime();
}
CBaseDialog::OnTimer(nIDEvent);
}
BOOL CNetworkInfoDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//通过鼠标滚轮翻页
if (zDelta > 0)
{
OnBnClickedPreviousButton();
}
if (zDelta < 0)
{
OnBnClickedNextButton();
}
return CBaseDialog::OnMouseWheel(nFlags, zDelta, pt);
}
void CNetworkInfoDlg::OnNMDblclkInfoList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
if (/*!theApp.m_cfg_data.m_show_internet_ip && */!m_ip_acquired && (pNMItemActivate->iItem == 14 || pNMItemActivate->iItem == 15)) //双击了IP地址一行时
{
m_info_list.SetItemText(14, 1, CCommon::LoadText(IDS_ACQUIRING, _T("...")));
m_info_list.SetItemText(15, 1, CCommon::LoadText(IDS_ACQUIRING, _T("...")));
m_pGetIPThread = AfxBeginThread(GetInternetIPThreadFunc, this);
}
*pResult = 0;
}
================================================
FILE: TrafficMonitor/NetworkInfoDlg.h
================================================
#pragma once
#include"Common.h"
#include "afxcmn.h"
#include "AdapterCommon.h"
#include "BaseDialog.h"
// CNetworkInfoDlg 对话框
class CNetworkInfoDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CNetworkInfoDlg)
public:
CNetworkInfoDlg(vector& adapters, MIB_IFROW* pIfRow, int connection_selected, CWnd* pParent = NULL); // 标准构造函数
virtual ~CNetworkInfoDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_NETWORK_INFO_DIALOG };
#endif
SYSTEMTIME m_start_time; //程序启动的时间
protected:
vector& m_connections;
MIB_IFROW* m_pIfRow;
int m_connection_selected; //当前对话框显示的连接
int m_current_connection; //初始选择的连接
CListCtrl m_info_list;
CMenu m_menu;
CString m_selected_string;
CFont m_font_bold; //默认字体的粗体
CWinThread* m_pGetIPThread; //获取外网IP的线程
bool m_ip_acquired{ false }; //如果已获取外网ip地址,则为true
//void GetIPAddress(); //获取IP地址
void ShowInfo();
void GetProgramElapsedTime();
MIB_IFROW& GetConnectIfTable(int connection_index); //获取当前选择的网络连接的MIB_IFROW对象。connection_index为m_connections中的索引
NetWorkConection GetConnection(int connection_index); //获取当前选择的网络连接的NetWorkConection对象。connection_index为m_connections中的索引
//获取外网IP的线程函数
static UINT GetInternetIPThreadFunc(LPVOID lpParam);
virtual CString GetDialogName() const override;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
virtual BOOL OnInitDialog();
public:
afx_msg void OnCopyText();
afx_msg void OnNMRClickInfoList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnClose();
afx_msg void OnBnClickedPreviousButton();
afx_msg void OnBnClickedNextButton();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
afx_msg void OnNMDblclkInfoList1(NMHDR* pNMHDR, LRESULT* pResult);
};
================================================
FILE: TrafficMonitor/OptionsDlg.cpp
================================================
// OptionsDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "OptionsDlg.h"
#include "afxdialogex.h"
// COptionsDlg 对话框
IMPLEMENT_DYNAMIC(COptionsDlg, CBaseDialog)
COptionsDlg::COptionsDlg(int tab, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_OPTIONS_DIALOG, pParent), m_tab_selected{ tab }
{
}
COptionsDlg::~COptionsDlg()
{
}
CString COptionsDlg::GetDialogName() const
{
return OPTION_DLG_NAME;
}
void COptionsDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TAB1, m_tab);
}
BEGIN_MESSAGE_MAP(COptionsDlg, CBaseDialog)
ON_WM_SIZE()
ON_BN_CLICKED(IDC_APPLY_BUTTON, &COptionsDlg::OnBnClickedApplyButton)
END_MESSAGE_MAP()
// COptionsDlg 消息处理程序
BOOL COptionsDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_OPTION));
SetIcon(theApp.GetMenuIcon(IDI_SETTINGS), FALSE); // 设置小图标
//创建子对话框
m_tab1_dlg.Create(IDD_MAIN_WND_SETTINGS_DIALOG, &m_tab);
m_tab2_dlg.Create(IDD_TASKBAR_SETTINGS_DIALOG, &m_tab);
m_tab3_dlg.Create(IDD_GENERAL_SETTINGS_DIALOG, &m_tab);
//保存子对话框
m_tab_vect.push_back(&m_tab1_dlg);
m_tab_vect.push_back(&m_tab2_dlg);
m_tab_vect.push_back(&m_tab3_dlg);
//获取子对话框的初始高度
for (const auto* pDlg : m_tab_vect)
{
CRect rect;
pDlg->GetWindowRect(rect);
m_tab_height.push_back(rect.Height());
}
//添加对话框
m_tab.AddWindow(&m_tab1_dlg, CCommon::LoadText(IDS_MAIN_WINDOW_SETTINGS));
m_tab.AddWindow(&m_tab2_dlg, CCommon::LoadText(IDS_TASKBAR_WINDOW_SETTINGS));
m_tab.AddWindow(&m_tab3_dlg, CCommon::LoadText(IDS_GENERAL_SETTINGS));
//为每个子窗口设置滚动信息
for (size_t i = 0; i < m_tab_vect.size(); i++)
{
m_tab_vect[i]->SetScrollbarInfo(m_tab.m_tab_rect.Height(), m_tab_height[i]);
}
//设置默认选中的标签
if (m_tab_selected < 0 || m_tab_selected >= m_tab.GetItemCount())
m_tab_selected = 0;
m_tab.SetCurTab(m_tab_selected);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void COptionsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
m_tab1_dlg.OnOK();
m_tab2_dlg.OnOK();
m_tab3_dlg.OnOK();
CBaseDialog::OnOK();
}
void COptionsDlg::OnSize(UINT nType, int cx, int cy)
{
CBaseDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (nType != SIZE_MINIMIZED)
{
//为每个子窗口设置滚动信息
for (size_t i = 0; i < m_tab_vect.size(); i++)
{
m_tab_vect[i]->ResetScroll();
m_tab_vect[i]->SetScrollbarInfo(m_tab.m_tab_rect.Height(), m_tab_height[i]);
}
}
}
void COptionsDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
m_tab3_dlg.OnCancel();
CBaseDialog::OnCancel();
}
void COptionsDlg::OnBnClickedApplyButton()
{
m_tab2_dlg.SaveColorSettingToDefaultStyle();
::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_SETTINGS_APPLIED, (WPARAM)this, 0);
}
================================================
FILE: TrafficMonitor/OptionsDlg.h
================================================
#pragma once
#include "MainWndSettingsDlg.h"
#include "TaskBarSettingsDlg.h"
#include "GeneralSettingsDlg.h"
#include "afxcmn.h"
#include "CTabCtrlEx.h"
#include "BaseDialog.h"
// COptionsDlg 对话框
#define OPTION_DLG_NAME _T("OptionsDlg")
class COptionsDlg : public CBaseDialog
{
DECLARE_DYNAMIC(COptionsDlg)
public:
COptionsDlg(int tab = 0, CWnd* pParent = NULL); // 标准构造函数
virtual ~COptionsDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_OPTIONS_DIALOG };
#endif
CMainWndSettingsDlg m_tab1_dlg{ this };
CTaskBarSettingsDlg m_tab2_dlg{ this };
CGeneralSettingsDlg m_tab3_dlg{ this };
protected:
CTabCtrlEx m_tab;
int m_tab_selected;
std::vector m_tab_vect;
std::vector m_tab_height;
virtual CString GetDialogName() const override;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
afx_msg void OnSize(UINT nType, int cx, int cy);
virtual void OnCancel();
afx_msg void OnBnClickedApplyButton();
};
================================================
FILE: TrafficMonitor/PictureStatic.cpp
================================================
#include "stdafx.h"
#include "PictureStatic.h"
CPictureStatic::CPictureStatic()
{
}
CPictureStatic::~CPictureStatic()
{
m_memDC.DeleteDC();
m_bitmap.Detach();
}
void CPictureStatic::SetPicture(UINT pic_id)
{
m_memDC.DeleteDC();
if (!m_bitmap.LoadBitmap(pic_id)) //λͼ
return;
//ȡͼʵʴС
GetObject(m_bitmap, sizeof(BITMAP), &m_bm);
CDC* pDC = GetDC();
m_memDC.CreateCompatibleDC(pDC);
m_memDC.SelectObject(&m_bitmap);
//ȡؼС
GetClientRect(m_rect);
//ֶػ
Invalidate();
}
void CPictureStatic::SetPicture(HBITMAP hBitmap)
{
m_memDC.DeleteDC();
m_bitmap.Detach();
if (!m_bitmap.Attach(hBitmap))
return;
//ȡͼʵʴС
GetObject(m_bitmap, sizeof(BITMAP), &m_bm);
CDC* pDC = GetDC();
m_memDC.CreateCompatibleDC(pDC);
m_memDC.SelectObject(&m_bitmap);
//ȡؼС
GetClientRect(m_rect);
//ֶػ
Invalidate();
}
BEGIN_MESSAGE_MAP(CPictureStatic, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CPictureStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: ڴ˴Ϣ
// ΪͼϢ CStatic::OnPaint()
if (m_bitmap.m_hObject != NULL)
{
// бͼƬʧ
dc.SetStretchBltMode(HALFTONE);
dc.SetBrushOrg(0, 0);
//ƽڴDCеͼ
dc.StretchBlt(0, 0, m_rect.Width(), m_rect.Height(), &m_memDC, 0, 0, m_bm.bmWidth, m_bm.bmHeight, SRCCOPY);
//ڷػϢ
CWnd* pParent{ GetParent() };
if (pParent != nullptr)
pParent->SendMessage(WM_CONTROL_REPAINT, (WPARAM)this, LPARAM(&dc));
}
}
================================================
FILE: TrafficMonitor/PictureStatic.h
================================================
/*һʾͼƬľ̬ؼ
ҪʱSetPicture()þ̬ͼƬ
ؼػʱڷWM_CONTROL_REPAINTϢ
ͨwParamݵǰؼCWndָ룬ͨlParamCDCָ
*/
#pragma once
#define WM_CONTROL_REPAINT (WM_USER + 1003) //ؼػϢ
class CPictureStatic : public CStatic
{
public:
CPictureStatic();
~CPictureStatic();
void SetPicture(UINT pic_id);
void SetPicture(HBITMAP hBitmap);
protected:
CDC m_memDC;
CBitmap m_bitmap;
CRect m_rect;
BITMAP m_bm;
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
};
================================================
FILE: TrafficMonitor/PluginInfoDlg.cpp
================================================
// PluginInfoDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "PluginInfoDlg.h"
#include "FilePathHelper.h"
// CPluginInfoDlg 对话框
IMPLEMENT_DYNAMIC(CPluginInfoDlg, CBaseDialog)
CPluginInfoDlg::CPluginInfoDlg(int plugin_index, CWnd* pParent /*=nullptr*/)
: CBaseDialog(IDD_NETWORK_INFO_DIALOG, pParent), m_cur_index(plugin_index)
{
if (m_cur_index < 0 || m_cur_index >= static_cast(theApp.m_plugins.GetPlugins().size()))
m_cur_index = 0;
}
CPluginInfoDlg::~CPluginInfoDlg()
{
}
void CPluginInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INFO_LIST1, m_info_list);
}
void CPluginInfoDlg::ShowInfo()
{
if (m_cur_index >= 0 && m_cur_index < static_cast(theApp.m_plugins.GetPlugins().size()))
{
auto& plugin_info = theApp.m_plugins.GetPlugins()[m_cur_index];
m_info_list.SetItemText(RI_NAME, 1, plugin_info.Property(ITMPlugin::TMI_NAME).c_str());
m_info_list.SetItemText(RI_DESCRIPTION, 1, plugin_info.Property(ITMPlugin::TMI_DESCRIPTION).c_str());
m_info_list.SetItemText(RI_FILE_NAME, 1, CFilePathHelper(plugin_info.file_path).GetFileName().c_str());
m_info_list.SetItemText(RI_FILE_PATH, 1, plugin_info.file_path.c_str());
m_info_list.SetItemText(RI_ITEM_NUM, 1, std::to_wstring(plugin_info.plugin_items.size()).c_str());
wstring item_names;
wstring item_id;
for (const auto& item : plugin_info.plugin_items)
{
item_names += item->GetItemName();
item_names += L";";
item_id += item->GetItemId();
item_id += L";";
}
if (!plugin_info.plugin_items.empty())
{
item_names.pop_back();
item_id.pop_back();
}
m_info_list.SetItemText(RI_ITEM_NAMES, 1, item_names.c_str());
m_info_list.SetItemText(RI_ITEM_ID, 1, item_id.c_str());
m_info_list.SetItemText(RI_AUTHOR, 1, plugin_info.Property(ITMPlugin::TMI_AUTHOR).c_str());
m_info_list.SetItemText(RI_COPYRIGHT, 1, plugin_info.Property(ITMPlugin::TMI_COPYRIGHT).c_str());
m_info_list.SetItemText(RI_URL, 1, plugin_info.Property(ITMPlugin::TMI_URL).c_str());
m_info_list.SetItemText(RI_VERSION, 1, plugin_info.Property(ITMPlugin::TMI_VERSION).c_str());
if (plugin_info.plugin != nullptr)
m_info_list.SetItemText(RI_API_VERSION, 1, std::to_wstring(plugin_info.plugin->GetAPIVersion()).c_str());
}
//显示当前选择指示
CString str;
str.Format(_T("%d/%d"), m_cur_index + 1, theApp.m_plugins.GetPlugins().size());
SetDlgItemText(IDC_INDEX_STATIC, str);
}
BEGIN_MESSAGE_MAP(CPluginInfoDlg, CBaseDialog)
ON_COMMAND(ID_COPY_TEXT, &CPluginInfoDlg::OnCopyText)
ON_NOTIFY(NM_RCLICK, IDC_INFO_LIST1, &CPluginInfoDlg::OnNMRClickInfoList1)
ON_BN_CLICKED(IDC_PREVIOUS_BUTTON, &CPluginInfoDlg::OnBnClickedPreviousButton)
ON_BN_CLICKED(IDC_NEXT_BUTTON, &CPluginInfoDlg::OnBnClickedNextButton)
ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()
// CPluginInfoDlg 消息处理程序
BOOL CPluginInfoDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_PLUGIN_INFO));
SetIcon(theApp.GetMenuIcon(IDI_PLUGINS), FALSE); // 设置小图标
//初始化列表控件
CRect rect;
m_info_list.GetClientRect(rect);
m_info_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0, width1;
width0 = rect.Width() / 4;
width1 = rect.Width() - width0 - theApp.DPI(20) - 1;
m_info_list.InsertColumn(0, CCommon::LoadText(IDS_ITEM), LVCFMT_LEFT, width0);
m_info_list.InsertColumn(1, CCommon::LoadText(IDS_VALUE), LVCFMT_LEFT, width1);
//向列表中插入行
for (int i = 0; i < RI_MAX; i++)
{
m_info_list.InsertItem(i, GetRowName(i));
}
//显示列表中的信息
ShowInfo();
m_info_list.GetToolTips()->SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
m_menu.LoadMenu(IDR_INFO_MENU); //装载右键菜单
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
CString CPluginInfoDlg::GetDialogName() const
{
return _T("PluginInfoDlg");
}
void CPluginInfoDlg::OnCopyText()
{
if (!CCommon::CopyStringToClipboard(wstring(m_selected_string)))
MessageBox(CCommon::LoadText(IDS_COPY_TO_CLIPBOARD_FAILED), NULL, MB_ICONWARNING);
}
void CPluginInfoDlg::OnNMRClickInfoList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
//获取鼠标点击处的文本
int item, sub_item;
item = pNMItemActivate->iItem;
sub_item = pNMItemActivate->iSubItem;
m_selected_string = m_info_list.GetItemText(item, sub_item);
//弹出右键菜单
CMenu* pContextMenu = m_menu.GetSubMenu(0); //获取第一个弹出菜单
CPoint point1; //定义一个用于确定光标位置的位置
GetCursorPos(&point1); //获取当前光标的位置,以便使得菜单可以跟随光标
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point1.x, point1.y, this); //在指定位置显示弹出菜单
*pResult = 0;
}
void CPluginInfoDlg::OnBnClickedPreviousButton()
{
if (m_cur_index > 0)
{
m_cur_index--;
ShowInfo();
}
}
void CPluginInfoDlg::OnBnClickedNextButton()
{
if (m_cur_index < static_cast(theApp.m_plugins.GetPlugins().size() - 1))
{
m_cur_index++;
ShowInfo();
}
}
BOOL CPluginInfoDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_LEFT)
{
OnBnClickedPreviousButton();
return TRUE;
}
if (pMsg->wParam == VK_RIGHT)
{
OnBnClickedNextButton();
return TRUE;
}
}
return CBaseDialog::PreTranslateMessage(pMsg);
}
BOOL CPluginInfoDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
//通过鼠标滚轮翻页
if (zDelta > 0)
{
OnBnClickedPreviousButton();
}
if (zDelta < 0)
{
OnBnClickedNextButton();
}
return CBaseDialog::OnMouseWheel(nFlags, zDelta, pt);
}
CString CPluginInfoDlg::GetRowName(int row_index)
{
switch (row_index)
{
case CPluginInfoDlg::RI_NAME:
return CCommon::LoadText(IDS_NAME);
case CPluginInfoDlg::RI_DESCRIPTION:
return CCommon::LoadText(IDS_DESCRIPTION);
case CPluginInfoDlg::RI_FILE_NAME:
return CCommon::LoadText(IDS_FILE_NAME);
case CPluginInfoDlg::RI_FILE_PATH:
return CCommon::LoadText(IDS_FILE_PATH);
case CPluginInfoDlg::RI_ITEM_NUM:
return CCommon::LoadText(IDS_ITEM_NUM);
case CPluginInfoDlg::RI_ITEM_NAMES:
return CCommon::LoadText(IDS_ITEM_NAMES);
case CPluginInfoDlg::RI_ITEM_ID:
return CCommon::LoadText(IDS_DISP_ITEM_ID);
case CPluginInfoDlg::RI_AUTHOR:
return CCommon::LoadText(IDS_AUTHOR);
case CPluginInfoDlg::RI_COPYRIGHT:
return CCommon::LoadText(IDS_COPYRIGHT);
case CPluginInfoDlg::RI_URL:
return CCommon::LoadText(IDS_URL);
case CPluginInfoDlg::RI_VERSION:
return CCommon::LoadText(IDS_VERSION);
case CPluginInfoDlg::RI_API_VERSION:
return CCommon::LoadText(IDS_PLUGIN_API_VERSION);
default:
break;
}
return CString();
}
================================================
FILE: TrafficMonitor/PluginInfoDlg.h
================================================
#pragma once
#include "BaseDialog.h"
// CPluginInfoDlg 对话框
class CPluginInfoDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CPluginInfoDlg)
public:
CPluginInfoDlg(int plugin_index, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CPluginInfoDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_NETWORK_INFO_DIALOG };
#endif
private:
int m_cur_index; //初始显示的插件索引
CListCtrl m_info_list;
CMenu m_menu;
CString m_selected_string;
//列表中的列
enum RowIndex
{
RI_FILE_NAME,
RI_FILE_PATH,
RI_NAME,
RI_DESCRIPTION,
RI_ITEM_NUM,
RI_ITEM_NAMES,
RI_ITEM_ID,
RI_AUTHOR,
RI_COPYRIGHT,
RI_URL,
RI_VERSION,
RI_API_VERSION,
RI_MAX
};
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void ShowInfo();
static CString GetRowName(int row_index); //获取行的名称
// 通过 CBaseDialog 继承
virtual CString GetDialogName() const override;
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnCopyText();
afx_msg void OnNMRClickInfoList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnBnClickedPreviousButton();
afx_msg void OnBnClickedNextButton();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
};
================================================
FILE: TrafficMonitor/PluginManager.cpp
================================================
#include "stdafx.h"
#include "PluginManager.h"
#include "Common.h"
#include "TrafficMonitor.h"
#define PLUGIN_UNSUPPORT_VERSION 0 //不被支持的插件版本
CPluginManager::CPluginManager()
{
}
CPluginManager::~CPluginManager()
{
//卸载插件
for (const auto& m : m_modules)
FreeLibrary(m.plugin_module);
}
static wstring WcharArrayToWString(const wchar_t* str)
{
if (str == nullptr)
return wstring();
else
return wstring(str);
}
void CPluginManager::LoadPlugins()
{
//从plugins目录下加载插件
wstring plugin_dir;
plugin_dir = CCommon::GetModuleDir() + L"plugins";
vector plugin_files;
CCommon::GetFiles((plugin_dir + L"\\*.dll").c_str(), plugin_files); //获取Plugins目录下所有的dll文件的文件名
for (const auto& file : plugin_files)
{
//插件信息
m_modules.push_back(PluginInfo());
PluginInfo& plugin_info{ m_modules.back() };
//插件dll的路径
plugin_info.file_path = plugin_dir + file;
//插件文件名
std::wstring file_name{ file };
if (!file_name.empty() && (file_name[0] == L'\\' || file_name[0] == L'/'))
file_name = file_name.substr(1);
//如果插件被禁用,则不加载插件
if (theApp.m_cfg_data.plugin_disabled.Contains(file_name))
{
plugin_info.state = PluginState::PS_DISABLE;
continue;
}
//载入dll
plugin_info.plugin_module = LoadLibrary(plugin_info.file_path.c_str());
if (plugin_info.plugin_module == NULL)
{
plugin_info.state = PluginState::PS_MUDULE_LOAD_FAILED;
plugin_info.error_code = GetLastError();
continue;
}
//获取函数的入口地址
pfTMPluginGetInstance TMPluginGetInstance = (pfTMPluginGetInstance)::GetProcAddress(plugin_info.plugin_module, "TMPluginGetInstance");
if (TMPluginGetInstance == NULL)
{
plugin_info.state = PluginState::PS_FUNCTION_GET_FAILED;
plugin_info.error_code = GetLastError();
continue;
}
//创建插件对象
plugin_info.plugin = TMPluginGetInstance();
if (plugin_info.plugin == nullptr)
continue;
//检查插件版本
int version = plugin_info.plugin->GetAPIVersion();
if (version <= PLUGIN_UNSUPPORT_VERSION)
{
plugin_info.state = PluginState::PS_VERSION_NOT_SUPPORT;
continue;
}
//获取插件信息
for (int i{}; i < ITMPlugin::TMI_MAX; i++)
{
ITMPlugin::PluginInfoIndex index{ static_cast(i) };
plugin_info.properties[index] = WcharArrayToWString(plugin_info.plugin->GetInfo(index));
}
//获取插件显示项目
int index = 0;
while (true)
{
IPluginItem* item = plugin_info.plugin->GetItem(index);
if (item == nullptr)
break;
plugin_info.plugin_items.push_back(item);
m_plugins.push_back(item);
m_plguin_item_map[item] = plugin_info.plugin;
index++;
}
}
//初始化所有任务栏显示项目
for (const auto& display_item : AllDisplayItems)
{
m_all_display_items_with_plugins.insert(display_item);
}
for (const auto& display_item : m_plugins)
{
m_all_display_items_with_plugins.insert(display_item);
}
}
const std::vector& CPluginManager::GetPluginItems() const
{
return m_plugins;
}
const std::vector& CPluginManager::GetPlugins() const
{
return m_modules;
}
IPluginItem* CPluginManager::GetItemById(const std::wstring& item_id)
{
for (const auto& item : m_plugins)
{
if (item->GetItemId() == item_id)
return item;
}
return nullptr;
}
IPluginItem* CPluginManager::GetItemByIndex(int index)
{
if (index >= 0 && index < static_cast(m_plugins.size()))
return m_plugins[index];
return nullptr;
}
int CPluginManager::GetItemIndex(IPluginItem* item) const
{
for (auto iter = m_plugins.begin(); iter != m_plugins.end(); ++iter)
{
if (*iter == item)
return iter - m_plugins.begin();
}
return -1;
}
ITMPlugin* CPluginManager::GetPluginByItem(IPluginItem* pItem)
{
if (pItem == nullptr)
return nullptr;
return m_plguin_item_map[pItem];
}
void CPluginManager::EnumPlugin(std::function func) const
{
for (const auto& item : m_modules)
{
if (item.plugin != nullptr)
{
func(item.plugin);
}
}
}
void CPluginManager::EnumPluginItem(std::function func) const
{
for (const auto& item : m_plugins)
{
if (item != nullptr)
{
func(item);
}
}
}
const std::set& CPluginManager::AllDisplayItemsWithPlugins()
{
return m_all_display_items_with_plugins;
}
int CPluginManager::GetItemWidth(IPluginItem* pItem, CDC* pDC)
{
int width = 0;
ITMPlugin* plugin = GetPluginByItem(pItem);
if (plugin != nullptr && plugin->GetAPIVersion() >= 3)
{
width = pItem->GetItemWidthEx(pDC->GetSafeHdc()); //优先使用GetItemWidthEx接口获取宽度
}
if (width == 0)
{
width = theApp.DPI(pItem->GetItemWidth());
}
return width;
}
std::wstring CPluginManager::PluginInfo::Property(ITMPlugin::PluginInfoIndex index) const
{
auto iter = properties.find(index);
if (iter != properties.end())
return iter->second;
return wstring();
}
================================================
FILE: TrafficMonitor/PluginManager.h
================================================
#pragma once
#include "PluginInterface.h"
#include
#include "TaskbarItemOrderHelper.h"
#include
typedef ITMPlugin* (*pfTMPluginGetInstance)();
//用于加载和管理插件
class CPluginManager
{
public:
//插件的状态
enum class PluginState
{
PS_SUCCEED, //载入成功
PS_MUDULE_LOAD_FAILED, //dll加载失败
PS_FUNCTION_GET_FAILED, //插件函数获取失败
PS_VERSION_NOT_SUPPORT, //插件版本不被支持
PS_DISABLE //已禁用
};
//插件信息
struct PluginInfo
{
wstring file_path; //文件路径
HMODULE plugin_module{}; //dll module
ITMPlugin* plugin{}; //插件对象
std::vector plugin_items; //插件提供的所有显示项目
PluginState state{}; //插件的状态
DWORD error_code{}; //错误代码(GetLastError的返回值)
std::map properties; //插件属性
std::wstring Property(ITMPlugin::PluginInfoIndex) const;
};
CPluginManager();
~CPluginManager();
void LoadPlugins();
const std::vector& GetPluginItems() const;
const std::vector& GetPlugins() const;
IPluginItem* GetItemById(const std::wstring& item_id);
IPluginItem* GetItemByIndex(int index);
int GetItemIndex(IPluginItem* item) const;
ITMPlugin* GetPluginByItem(IPluginItem* pItem);
//遍历所有插件
//func: 参数为遍历到的ITMPlugin对象
void EnumPlugin(std::function func) const;
//遍历所有插件项目
//func: 参数为遍历到的IPluginItem对象
void EnumPluginItem(std::function func) const;
const std::set& AllDisplayItemsWithPlugins();
int GetItemWidth(IPluginItem* pItem, CDC* pDC);
private:
std::vector m_plugins;
std::vector m_modules;
std::set m_all_display_items_with_plugins; //包含插件在内的所有任务栏显示项目
std::map m_plguin_item_map; //用于根据插件项目查找对应插件的map
};
================================================
FILE: TrafficMonitor/PluginManagerDlg.cpp
================================================
// PluginManagerDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "PluginManagerDlg.h"
#include "FilePathHelper.h"
#include "PluginInfoDlg.h"
// CPluginManagerDlg 对话框
IMPLEMENT_DYNAMIC(CPluginManagerDlg, CBaseDialog)
CPluginManagerDlg::CPluginManagerDlg(CWnd* pParent /*=nullptr*/)
: CBaseDialog(IDD_PLUGIN_MANAGER_DIALOG, pParent)
{
}
CPluginManagerDlg::~CPluginManagerDlg()
{
}
void CPluginManagerDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
DDX_Control(pDX, IDC_PLUGIN_DOWNLOAD_STATIC, m_plugin_download_lnk);
DDX_Control(pDX, IDC_PLUGIN_DEV_GUID_STATIC, m_plugin_dev_guide_lnk);
DDX_Control(pDX, IDC_OPEN_PLUGIN_DIR_STATIC, m_open_plugin_dir_lnk);
}
void CPluginManagerDlg::EnableControl()
{
bool enable{ IsSelectedPluginEnable() };
EnableDlgCtrl(IDC_OPTINS_BUTTON, enable);
EnableDlgCtrl(IDC_PLUGIN_INFO_BUTTON, enable);
}
bool CPluginManagerDlg::IsSelectedValid()
{
return m_item_selected >= 0 && m_item_selected < static_cast(theApp.m_plugins.GetPlugins().size());
}
bool CPluginManagerDlg::IsSelectedPluginEnable()
{
CPluginManager::PluginInfo plugin_info{};
bool plugin_loaded{ false };
if (IsSelectedValid())
{
plugin_info = theApp.m_plugins.GetPlugins()[m_item_selected];
plugin_loaded = (plugin_info.state == CPluginManager::PluginState::PS_SUCCEED);
}
return plugin_loaded;
}
CString CPluginManagerDlg::GetDialogName() const
{
return _T("PluginManagerDlg");
}
BEGIN_MESSAGE_MAP(CPluginManagerDlg, CBaseDialog)
ON_NOTIFY(NM_RCLICK, IDC_LIST1, &CPluginManagerDlg::OnNMRClickList1)
ON_NOTIFY(NM_CLICK, IDC_LIST1, &CPluginManagerDlg::OnNMClickList1)
ON_BN_CLICKED(IDC_OPTINS_BUTTON, &CPluginManagerDlg::OnBnClickedOptinsButton)
ON_BN_CLICKED(IDC_PLUGIN_INFO_BUTTON, &CPluginManagerDlg::OnBnClickedPluginInfoButton)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CPluginManagerDlg::OnNMDblclkList1)
ON_WM_INITMENU()
ON_COMMAND(ID_PLUGIN_DETAIL, &CPluginManagerDlg::OnPluginDetail)
ON_COMMAND(ID_PLUGIN_OPTIONS, &CPluginManagerDlg::OnPluginOptions)
ON_COMMAND(ID_PLUGIN_DISABLE, &CPluginManagerDlg::OnPluginDisable)
ON_MESSAGE(WM_LINK_CLICKED, &CPluginManagerDlg::OnLinkClicked)
END_MESSAGE_MAP()
// CPluginManagerDlg 消息处理程序
BOOL CPluginManagerDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_PLUGINS), FALSE);
//初始化列表控件
CRect rect;
m_list_ctrl.GetClientRect(rect);
m_list_ctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0, width1, width2;
width0 = width1 = rect.Width() / 3;
width2 = rect.Width() - width0 - width1 - theApp.DPI(20) - 1;
m_list_ctrl.InsertColumn(0, CCommon::LoadText(IDS_FILE_NAME), LVCFMT_LEFT, width0);
m_list_ctrl.InsertColumn(1, CCommon::LoadText(IDS_PLUGIN_NAME), LVCFMT_LEFT, width1);
m_list_ctrl.InsertColumn(2, CCommon::LoadText(IDS_STATUS), LVCFMT_LEFT, width2);
//向列表中插入行
for (const auto& plugin : theApp.m_plugins.GetPlugins())
{
std::wstring file_name = CFilePathHelper(plugin.file_path).GetFileName();
CString status;
switch (plugin.state)
{
case CPluginManager::PluginState::PS_SUCCEED:
status = CCommon::LoadText(IDS_PLUGIN_LOAD_SUCCEED);
break;
case CPluginManager::PluginState::PS_MUDULE_LOAD_FAILED:
status = CCommon::LoadTextFormat(IDS_PLUGIN_MODULE_LOAD_FAILED, { static_cast(plugin.error_code) });
break;
case CPluginManager::PluginState::PS_FUNCTION_GET_FAILED:
status = CCommon::LoadTextFormat(IDS_PLUGIN_FUNCTION_GET_FAILED, { static_cast(plugin.error_code) });
break;
case CPluginManager::PluginState::PS_DISABLE:
status = CCommon::LoadText(IDS_DISABLED);
break;
case CPluginManager::PluginState::PS_VERSION_NOT_SUPPORT:
status = CCommon::LoadText(IDS_PLUGIN_VERSION_NOT_SUPPORT);
break;
default:
break;
}
int index = m_list_ctrl.GetItemCount();
m_list_ctrl.InsertItem(index, file_name.c_str());
m_list_ctrl.SetItemText(index, 1, plugin.Property(ITMPlugin::TMI_NAME).c_str());
m_list_ctrl.SetItemText(index, 2, status);
}
m_plugin_download_lnk.SetURL(L"https://github.com/zhongyang219/TrafficMonitorPlugins/blob/main/download/plugin_download.md");
m_plugin_dev_guide_lnk.SetURL(L"https://github.com/zhongyang219/TrafficMonitor/wiki/%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97");
m_open_plugin_dir_lnk.SetLinkIsURL(false);
EnableControl();
m_menu.LoadMenu(IDR_PLUGIN_MANAGER_MENU); //装载右键菜单
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CPluginManagerDlg::OnNMRClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
m_item_selected = pNMItemActivate->iItem;
EnableControl();
//弹出右键菜单
CMenu* pContextMenu = m_menu.GetSubMenu(0); //获取第一个弹出菜单
CPoint point1; //定义一个用于确定光标位置的位置
GetCursorPos(&point1); //获取当前光标的位置,以便使得菜单可以跟随光标
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point1.x, point1.y, this); //在指定位置显示弹出菜单
*pResult = 0;
}
void CPluginManagerDlg::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
m_item_selected = pNMItemActivate->iItem;
EnableControl();
*pResult = 0;
}
void CPluginManagerDlg::OnBnClickedOptinsButton()
{
// TODO: 在此添加控件通知处理程序代码
if (m_item_selected >= 0 && m_item_selected < static_cast(theApp.m_plugins.GetPlugins().size()))
{
auto plugin_info = theApp.m_plugins.GetPlugins()[m_item_selected];
if (plugin_info.plugin != nullptr)
{
ITMPlugin::OptionReturn rtn = plugin_info.plugin->ShowOptionsDialog(m_hWnd);
if (rtn == ITMPlugin::OR_OPTION_NOT_PROVIDED)
MessageBox(CCommon::LoadText(IDS_PLUGIN_NO_OPTIONS_INFO), nullptr, MB_ICONINFORMATION | MB_OK);
//else if (rtn == ITMPlugin::OR_OPTION_CHANGED)
// theApp.m_pMainWnd->SendMessage(WM_REOPEN_TASKBAR_WND);
}
}
}
void CPluginManagerDlg::OnBnClickedPluginInfoButton()
{
// TODO: 在此添加控件通知处理程序代码
if (IsSelectedPluginEnable())
{
CPluginInfoDlg dlg(m_item_selected);
dlg.DoModal();
}
}
void CPluginManagerDlg::OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
m_item_selected = pNMItemActivate->iItem;
OnBnClickedPluginInfoButton();
*pResult = 0;
}
void CPluginManagerDlg::OnInitMenu(CMenu* pMenu)
{
CBaseDialog::OnInitMenu(pMenu);
// TODO: 在此处添加消息处理程序代码
bool enable{ IsSelectedPluginEnable() };
pMenu->EnableMenuItem(ID_PLUGIN_DETAIL, MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED));
pMenu->EnableMenuItem(ID_PLUGIN_OPTIONS, MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED));
pMenu->EnableMenuItem(ID_PLUGIN_DISABLE, MF_BYCOMMAND | (IsSelectedValid() ? MF_ENABLED : MF_GRAYED));
bool disabled{};
CPluginManager::PluginInfo plugin_info;
if (m_item_selected >= 0 && m_item_selected < static_cast(theApp.m_plugins.GetPlugins().size()))
{
plugin_info = theApp.m_plugins.GetPlugins()[m_item_selected];
std::wstring file_name = CFilePathHelper(plugin_info.file_path).GetFileName();
disabled = theApp.m_cfg_data.plugin_disabled.Contains(file_name);
}
pMenu->CheckMenuItem(ID_PLUGIN_DISABLE, MF_BYCOMMAND | (disabled ? MF_CHECKED : MF_UNCHECKED));
}
void CPluginManagerDlg::OnPluginDetail()
{
OnBnClickedOptinsButton();
}
void CPluginManagerDlg::OnPluginOptions()
{
OnBnClickedOptinsButton();
}
void CPluginManagerDlg::OnPluginDisable()
{
if (m_item_selected >= 0 && m_item_selected < static_cast(theApp.m_plugins.GetPlugins().size()))
{
CPluginManager::PluginInfo plugin_info = theApp.m_plugins.GetPlugins()[m_item_selected];
std::wstring file_name = CFilePathHelper(plugin_info.file_path).GetFileName();
bool disabled = theApp.m_cfg_data.plugin_disabled.Contains(file_name);
theApp.m_cfg_data.plugin_disabled.SetStrContained(file_name, !disabled);
MessageBox(CCommon::LoadText(IDS_RESTART_TO_APPLY_CHANGE_INFO), nullptr, MB_OK | MB_ICONINFORMATION);
}
}
afx_msg LRESULT CPluginManagerDlg::OnLinkClicked(WPARAM wParam, LPARAM lParam)
{
CWnd* pCtrl = (CWnd*)wParam;
//点击了“打开插件目录”
if (pCtrl == &m_open_plugin_dir_lnk)
{
wstring plugin_dir = CCommon::GetModuleDir() + L"plugins";
CreateDirectory(plugin_dir.c_str(), NULL); //如果plugins不存在,则创建它
ShellExecute(NULL, _T("open"), _T("explorer"), plugin_dir.c_str(), NULL, SW_SHOWNORMAL);
}
return 0;
}
================================================
FILE: TrafficMonitor/PluginManagerDlg.h
================================================
#pragma once
#include "BaseDialog.h"
#include "LinkStatic.h"
// CPluginManagerDlg 对话框
class CPluginManagerDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CPluginManagerDlg)
public:
CPluginManagerDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CPluginManagerDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_PLUGIN_MANAGER_DIALOG };
#endif
private:
CListCtrl m_list_ctrl;
int m_item_selected{ -1 };
CMenu m_menu;
CLinkStatic m_plugin_download_lnk;
CLinkStatic m_plugin_dev_guide_lnk;
CLinkStatic m_open_plugin_dir_lnk;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void EnableControl();
bool IsSelectedValid();
bool IsSelectedPluginEnable(); //选中插件是否可用
DECLARE_MESSAGE_MAP()
// 通过 CBaseDialog 继承
virtual CString GetDialogName() const override;
public:
virtual BOOL OnInitDialog();
afx_msg void OnNMRClickList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnBnClickedOptinsButton();
afx_msg void OnBnClickedPluginInfoButton();
afx_msg void OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnInitMenu(CMenu* pMenu);
afx_msg void OnPluginDetail();
afx_msg void OnPluginOptions();
afx_msg void OnPluginDisable();
protected:
afx_msg LRESULT OnLinkClicked(WPARAM wParam, LPARAM lParam);
};
================================================
FILE: TrafficMonitor/ReadMe.txt
================================================
================================================================================
MICROSOFT 基础类库 : TrafficMonitor 项目概述
===============================================================================
应用程序向导已为您创建了此 TrafficMonitor 应用程序。此应用程序不仅演示 Microsoft 基础类的基本使用方法,还可作为您编写应用程序的起点。
本文件概要介绍组成 TrafficMonitor 应用程序的每个文件的内容。
TrafficMonitor.vcxproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
TrafficMonitor.vcxproj.filters
这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。
TrafficMonitor.h
这是应用程序的主头文件。
其中包括其他项目特定的标头(包括 Resource.h),并声明 CTrafficMonitorApp 应用程序类。
TrafficMonitor.cpp
这是包含应用程序类 CTrafficMonitorApp 的主应用程序源文件。
TrafficMonitor.rc
这是程序使用的所有 Microsoft Windows 资源的列表。它包括 RES 子目录中存储的图标、位图和光标。此文件可以直接在 Microsoft Visual C++ 中进行编辑。项目资源包含在 2052 中。
res\TrafficMonitor.ico
这是用作应用程序图标的图标文件。此图标包括在主资源文件 TrafficMonitor.rc 中。
res\TrafficMonitor.rc2
此文件包含不在 Microsoft Visual C++ 中进行编辑的资源。您应该将不可由资源编辑器编辑的所有资源放在此文件中。
/////////////////////////////////////////////////////////////////////////////
应用程序向导创建一个对话框类:
TrafficMonitorDlg.h、TrafficMonitorDlg.cpp - 对话框
这些文件包含 CTrafficMonitorDlg 类。此类定义应用程序的主对话框的行为。对话框模板包含在 TrafficMonitor.rc 中,该文件可以在 Microsoft Visual C++ 中编辑。
/////////////////////////////////////////////////////////////////////////////
其他功能:
ActiveX 控件
该应用程序包含对使用 ActiveX 控件的支持。
/////////////////////////////////////////////////////////////////////////////
其他标准文件:
StdAfx.h, StdAfx.cpp
这些文件用于生成名为 TrafficMonitor.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。
Resource.h
这是标准头文件,可用于定义新的资源 ID。Microsoft Visual C++ 将读取并更新此文件。
TrafficMonitor.manifest
Windows XP 使用应用程序清单文件来描述特定版本的并行程序集的应用程序依赖项。加载程序使用这些信息来从程序集缓存中加载相应的程序集,并保护其不被应用程序访问。应用程序清单可能会包含在内,以作为与应用程序可执行文件安装在同一文件夹中的外部 .manifest 文件进行重新分发,它还可能以资源的形式包含在可执行文件中。
/////////////////////////////////////////////////////////////////////////////
其他注释:
应用程序向导使用“TODO:”来指示应添加或自定义的源代码部分。
如果应用程序使用共享 DLL 中的 MFC,您将需要重新分发 MFC DLL。如果应用程序所使用的语言与操作系统的区域设置不同,则还需要重新分发相应的本地化资源 mfc110XXX.DLL。
有关上述话题的更多信息,请参见 MSDN 文档中有关重新分发 Visual C++ 应用程序的部分。
/////////////////////////////////////////////////////////////////////////////
================================================
FILE: TrafficMonitor/SelectConnectionsDlg.cpp
================================================
// SelectConnectionsDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "SelectConnectionsDlg.h"
#include "afxdialogex.h"
#include "AdapterCommon.h"
// CSelectConnectionsDlg 对话框
IMPLEMENT_DYNAMIC(CSelectConnectionsDlg, CBaseDialog)
CSelectConnectionsDlg::CSelectConnectionsDlg(const StringSet& connections_hide, CWnd* pParent /*=nullptr*/)
: m_connections_hide(connections_hide), CBaseDialog(IDD_SELECT_CONNECTIONS_DIALOG, pParent)
{
std::vector connections;
CAdapterCommon::GetAdapterInfo(connections);
for (const auto& item : connections)
m_all_connection_names.push_back(CCommon::StrToUnicode(item.description.c_str()));
}
CSelectConnectionsDlg::~CSelectConnectionsDlg()
{
}
void CSelectConnectionsDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
}
CString CSelectConnectionsDlg::GetDialogName() const
{
//throw std::logic_error("The method or operation is not implemented.");
return _T("SelectConnectionsDlg");
}
BEGIN_MESSAGE_MAP(CSelectConnectionsDlg, CBaseDialog)
END_MESSAGE_MAP()
// CSelectConnectionsDlg 消息处理程序
BOOL CSelectConnectionsDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_CONNECTION), FALSE);
m_list_ctrl.SetItemHeight(0, theApp.DPI(20));
//向列表中添加项目
for (const auto& connection_name : m_all_connection_names)
{
m_list_ctrl.AddString(connection_name.c_str());
if (!m_connections_hide.Contains(connection_name))
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, TRUE);
else
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, FALSE);
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CSelectConnectionsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//保存每个项目的勾选状态
bool is_empty{ true };
for (int i = 0; i < static_cast(m_all_connection_names.size()); i++)
{
bool is_checked = (m_list_ctrl.GetCheck(i) != 0);
if (is_checked)
is_empty = false;
m_connections_hide.SetStrContained(m_all_connection_names[i], !is_checked);
}
if (is_empty)
{
MessageBox(CCommon::LoadText(IDS_SELECT_AT_LEASE_ONE_WARNING), NULL, MB_ICONWARNING | MB_OK);
return;
}
CBaseDialog::OnOK();
}
================================================
FILE: TrafficMonitor/SelectConnectionsDlg.h
================================================
#pragma once
#include "CommonData.h"
#include "BaseDialog.h"
// CSelectConnectionsDlg 对话框
class CSelectConnectionsDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CSelectConnectionsDlg)
public:
CSelectConnectionsDlg(const StringSet& connections_hide, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CSelectConnectionsDlg();
const StringSet& GetData() const { return m_connections_hide; }
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_SELECT_CONNECTIONS_DIALOG };
#endif
private:
CCheckListBox m_list_ctrl;
std::vector m_all_connection_names;
StringSet m_connections_hide;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
virtual CString GetDialogName() const override;
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
};
================================================
FILE: TrafficMonitor/SetItemOrderDlg.cpp
================================================
// SetItemOrderDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "SetItemOrderDlg.h"
#include "afxdialogex.h"
// CSetItemOrderDlg 对话框
IMPLEMENT_DYNAMIC(CSetItemOrderDlg, CBaseDialog)
CSetItemOrderDlg::CSetItemOrderDlg(CWnd* pParent /*=nullptr*/)
: CBaseDialog(IDD_SELECT_ORDER_DIALOG, pParent), m_item_order(true)
{
m_item_order.Init();
}
CSetItemOrderDlg::~CSetItemOrderDlg()
{
}
void CSetItemOrderDlg::SetItemOrder(const std::vector& item_order)
{
m_item_order.SetOrder(item_order);
}
const std::vector& CSetItemOrderDlg::GetItemOrder() const
{
return m_item_order.GetItemOrderConst();
}
void CSetItemOrderDlg::SetDisplayItem(unsigned int display_item)
{
m_display_item = display_item;
}
unsigned int CSetItemOrderDlg::GetDisplayItem() const
{
return m_display_item;
}
void CSetItemOrderDlg::SetPluginDisplayItem(const StringSet& plugin_item)
{
m_plugin_item = plugin_item;
}
const StringSet& CSetItemOrderDlg::GetPluginDisplayItem() const
{
return m_plugin_item;
}
void CSetItemOrderDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
}
BEGIN_MESSAGE_MAP(CSetItemOrderDlg, CBaseDialog)
ON_BN_CLICKED(IDC_MOVE_UP_BUTTON, &CSetItemOrderDlg::OnBnClickedMoveUpButton)
ON_BN_CLICKED(IDC_MOVE_DOWN_BUTTON, &CSetItemOrderDlg::OnBnClickedMoveDownButton)
ON_BN_CLICKED(IDC_RESTORE_DEFAULT_BUTTON, &CSetItemOrderDlg::OnBnClickedRestoreDefaultButton)
ON_LBN_SELCHANGE(IDC_LIST1, &CSetItemOrderDlg::OnLbnSelchangeList1)
ON_CLBN_CHKCHANGE(IDC_LIST1, &CSetItemOrderDlg::OnCheckChanged)
END_MESSAGE_MAP()
// CSetItemOrderDlg 消息处理程序
BOOL CSetItemOrderDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_ITEM), FALSE); // 设置小图标
m_list_ctrl.SetItemHeight(0, theApp.DPI(20));
EnableCtrl(-1);
ShowItem();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CSetItemOrderDlg::ShowItem()
{
//向列表中添加项目
m_list_ctrl.ResetContent();
m_all_displayed_item = m_item_order.GetAllDisplayItemsWithOrder();
for (const auto& item : m_all_displayed_item)
{
m_list_ctrl.AddString(CTaskbarItemOrderHelper::GetItemDisplayName(item));
if (GetItemChecked(item))
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, TRUE);
else
m_list_ctrl.SetCheck(m_list_ctrl.GetCount() - 1, FALSE);
}
}
void CSetItemOrderDlg::EnableCtrl(int list_selected)
{
EnableDlgCtrl(IDC_MOVE_UP_BUTTON, list_selected > 0 && list_selected < static_cast(m_all_displayed_item.size()));
EnableDlgCtrl(IDC_MOVE_DOWN_BUTTON, list_selected >= 0 && list_selected < static_cast(m_all_displayed_item.size()) - 1);
}
void CSetItemOrderDlg::EnableDlgCtrl(UINT id, bool enable)
{
CWnd* pCtrl = GetDlgItem(id);
if (pCtrl != nullptr)
pCtrl->EnableWindow(enable);
}
bool CSetItemOrderDlg::GetItemChecked(CommonDisplayItem item)
{
if (item.is_plugin)
{
if (item.plugin_item != nullptr)
return m_plugin_item.Contains(item.plugin_item->GetItemId());
}
else
{
return m_display_item & item.item_type;
}
return false;
}
void CSetItemOrderDlg::SaveItemChecked(CommonDisplayItem item, bool checked)
{
if (item.is_plugin)
{
if (item.plugin_item != nullptr)
m_plugin_item.SetStrContained(item.plugin_item->GetItemId(), checked);
}
else
{
if (checked)
m_display_item |= item.item_type;
else
m_display_item &= ~item.item_type;
}
}
void CSetItemOrderDlg::OnBnClickedMoveUpButton()
{
// TODO: 在此添加控件通知处理程序代码
auto& item_list{ m_item_order.GetItemOrder() };
int cur_index{ m_list_ctrl.GetCurSel() };
int item_count{ static_cast(item_list.size()) };
if (cur_index > 0 && cur_index < item_count)
{
std::swap(item_list[cur_index], item_list[cur_index - 1]);
ShowItem();
m_list_ctrl.SetCurSel(cur_index - 1);
EnableCtrl(cur_index - 1);
}
}
void CSetItemOrderDlg::OnBnClickedMoveDownButton()
{
// TODO: 在此添加控件通知处理程序代码
auto& item_list{ m_item_order.GetItemOrder() };
int cur_index{ m_list_ctrl.GetCurSel() };
int item_count{ static_cast(item_list.size()) };
if (cur_index >= 0 && cur_index < item_count - 1)
{
std::swap(item_list[cur_index], item_list[cur_index + 1]);
ShowItem();
m_list_ctrl.SetCurSel(cur_index + 1);
EnableCtrl(cur_index + 1);
}
}
void CSetItemOrderDlg::OnBnClickedRestoreDefaultButton()
{
// TODO: 在此添加控件通知处理程序代码
auto item_list{ m_item_order.GetItemOrder() };
std::sort(item_list.begin(), item_list.end());
m_item_order.SetOrder(item_list);
ShowItem();
}
CString CSetItemOrderDlg::GetDialogName() const
{
return _T("SetItemOrderDlg");
}
void CSetItemOrderDlg::OnLbnSelchangeList1()
{
// TODO: 在此添加控件通知处理程序代码
int cur_index{ m_list_ctrl.GetCurSel() };
EnableCtrl(cur_index);
}
void CSetItemOrderDlg::OnCheckChanged()
{
//当用户点击项目前面的复选框时保存该项目的勾选状态
int cur_index{ m_list_ctrl.GetCurSel() };
if (cur_index >= 0 && cur_index < static_cast(m_all_displayed_item.size()))
{
bool is_checked = (m_list_ctrl.GetCheck(cur_index) != 0);
CommonDisplayItem item = m_all_displayed_item[cur_index];
SaveItemChecked(item, is_checked);
}
}
void CSetItemOrderDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//保存每个项目的勾选状态
//auto item_list = m_item_order.GetAllDisplayItemsWithOrder();
//int i = 0;
//for (; i < static_cast(item_list.size()); i++)
//{
// bool is_checked = (m_list_ctrl.GetCheck(i) != 0);
// CommonDisplayItem item = item_list[i];
// SaveItemChecked(item, is_checked);
//}
if (m_display_item == 0)
m_display_item = TDI_UP;
CBaseDialog::OnOK();
}
================================================
FILE: TrafficMonitor/SetItemOrderDlg.h
================================================
#pragma once
#include "BaseDialog.h"
// CSetItemOrderDlg 对话框
class CSetItemOrderDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CSetItemOrderDlg)
public:
CSetItemOrderDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CSetItemOrderDlg();
//设置/获取显示顺序
void SetItemOrder(const std::vector& item_order);
const std::vector& GetItemOrder() const;
//设置/获取显示项目,使用unsigned int的每个bit表示对应项目是否显示
void SetDisplayItem(unsigned int display_item);
unsigned int GetDisplayItem() const;
void SetPluginDisplayItem(const StringSet& plugin_item);
const StringSet& GetPluginDisplayItem() const;
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_SELECT_ORDER_DIALOG };
#endif
private:
CTaskbarItemOrderHelper m_item_order; //显示项目的顺序
CCheckListBox m_list_ctrl;
unsigned int m_display_item; //使用每个bit位表示的要显示的内置项目
StringSet m_plugin_item; //要显示的插件项目
std::vector m_all_displayed_item; //在列表中显示的所有项目
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
void ShowItem();
void EnableCtrl(int list_selected);
void EnableDlgCtrl(UINT id, bool enable);
bool GetItemChecked(CommonDisplayItem item);
void SaveItemChecked(CommonDisplayItem item, bool checked);
// 通过 CBaseDialog 继承
virtual CString GetDialogName() const override;
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedMoveUpButton();
afx_msg void OnBnClickedMoveDownButton();
afx_msg void OnBnClickedRestoreDefaultButton();
afx_msg void OnLbnSelchangeList1();
afx_msg void OnCheckChanged();
virtual void OnOK();
};
================================================
FILE: TrafficMonitor/SimpleXML.cpp
================================================
#include "stdafx.h"
#include "SimpleXML.h"
CSimpleXML::CSimpleXML(const wstring & xml_path)
{
ifstream file_stream{ xml_path };
if (file_stream.fail())
{
return;
}
//ȡļ
string xml_str;
while (!file_stream.eof())
{
xml_str.push_back(file_stream.get());
}
xml_str.pop_back();
if (!xml_str.empty() && xml_str.back() != L'\n') //ȷļĩβлس
xml_str.push_back(L'\n');
//жļǷutf8
bool is_utf8;
if (xml_str.size() >= 3 && xml_str[0] == -17 && xml_str[1] == -69 && xml_str[2] == -65)
{
//UTF8BOMɾBOM
is_utf8 = true;
xml_str = xml_str.substr(3);
}
else
{
is_utf8 = false;
}
//תUnicode
m_xml_content = CCommon::StrToUnicode(xml_str.c_str(), is_utf8);
}
CSimpleXML::CSimpleXML()
{
}
CSimpleXML::~CSimpleXML()
{
}
wstring CSimpleXML::GetNode(const wchar_t * node, const wchar_t * parent) const
{
wstring node_content = _GetNode(parent, m_xml_content);
return _GetNode(node, node_content);
}
wstring CSimpleXML::GetNode(const wchar_t * node) const
{
return _GetNode(node, m_xml_content);
}
wstring CSimpleXML::_GetNode(const wchar_t * node, const wstring & content)
{
wstring result;
wstring node_start{ L'<' };
wstring node_end{ L'<' };
node_start += node;
node_start += L'>';
node_end += L'/';
node_end += node;
node_end += L'>';
size_t index_start, index_end;
index_start = content.find(node_start);
index_end = content.find(node_end);
if (index_start == wstring::npos || index_end == wstring::npos)
return wstring();
result = content.substr(index_start + node_start.size(), index_end - index_start - node_start.size());
return result;
}
================================================
FILE: TrafficMonitor/SimpleXML.h
================================================
//һXML
#pragma once
#include "Common.h"
class CSimpleXML
{
public:
CSimpleXML(const wstring& xml_path);
CSimpleXML();
~CSimpleXML();
void LoadXMLContentDirect(const wstring& xml_content) { m_xml_content = xml_content; }
wstring GetNode(const wchar_t* node, const wchar_t* parent) const;
wstring GetNode(const wchar_t* node) const;
static wstring _GetNode(const wchar_t* node, const wstring& content);
protected:
wstring m_xml_content;
};
================================================
FILE: TrafficMonitor/SkinDlg.cpp
================================================
// SkinDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "SkinDlg.h"
#include "afxdialogex.h"
// CSkinDlg 对话框
IMPLEMENT_DYNAMIC(CSkinDlg, CBaseDialog)
CSkinDlg::CSkinDlg(CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_SKIN_DIALOG, pParent)
{
}
CSkinDlg::~CSkinDlg()
{
}
CString CSkinDlg::GetDialogName() const
{
return _T("SkinDlg");
}
void CSkinDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, m_select_box);
DDX_Control(pDX, IDC_SKIN_COURSE_STATIC, m_skin_course);
DDX_Control(pDX, IDC_SKIN_DOWNLOAD_STATIC, m_skin_download);
DDX_Control(pDX, IDC_PREVIEW_GROUP_STATIC, m_preview_static);
DDX_Control(pDX, IDC_NOTIFY_STATIC, m_notify_static);
DDX_Control(pDX, IDC_OPEN_SKIN_DIR_STATIC, m_open_skin_dir_lnk);
}
void CSkinDlg::ShowPreview()
{
//载入布局数据
wstring cfg_path{ theApp.m_skin_path + m_skins[m_skin_selected] + L"\\skin.xml" };
if (!CCommon::FileExist(cfg_path.c_str()))
cfg_path = theApp.m_skin_path + m_skins[m_skin_selected] + L"\\skin.ini";
m_skin_data.Load(cfg_path);
//获取预览区大小
m_view->SetSize(m_skin_data.GetPreviewInfo().width, m_skin_data.GetPreviewInfo().height);
//刷新预览图
m_view->Invalidate();
//显示皮肤作者
SetDlgItemText(IDC_SKIN_INFO, CCommon::LoadText(IDS_SKIN_AUTHOUR, m_skin_data.GetSkinInfo().skin_author.c_str()));
//设置提示信息
bool cover_font_setting{ !m_skin_data.GetSkinInfo().font_info.name.IsEmpty() || (m_skin_data.GetSkinInfo().font_info.size >= MIN_FONT_SIZE && m_skin_data.GetSkinInfo().font_info.size <= MAX_FONT_SIZE) };
bool cover_str_setting{ !m_skin_data.GetSkinInfo().display_text.IsInvalid() };
cover_font_setting = cover_font_setting && theApp.m_general_data.allow_skin_cover_font;
cover_str_setting = cover_str_setting && theApp.m_general_data.allow_skin_cover_text;
if (cover_font_setting && cover_str_setting)
m_notify_static.SetWindowTextEx(CCommon::LoadText(IDS_OVERWRITE_FONT_TEXT_WARNING));
else if (cover_font_setting)
m_notify_static.SetWindowTextEx(CCommon::LoadText(IDS_OVERWRITE_FONT_WARNING));
else if (cover_str_setting)
m_notify_static.SetWindowTextEx(CCommon::LoadText(IDS_OVERWRITE_TEXT_WARNING));
else
m_notify_static.SetWindowTextEx(_T(""));
}
CRect CSkinDlg::CalculateViewRect()
{
CRect rect;
m_preview_static.GetWindowRect(rect); //获取“预览” group box 的位置
ScreenToClient(&rect);
CRect scroll_view_rect{ rect };
scroll_view_rect.DeflateRect(theApp.DPI(12), theApp.DPI(40));
scroll_view_rect.top = rect.top + theApp.DPI(28);
return scroll_view_rect;
}
BEGIN_MESSAGE_MAP(CSkinDlg, CBaseDialog)
ON_CBN_SELCHANGE(IDC_COMBO1, &CSkinDlg::OnCbnSelchangeCombo1)
ON_WM_SIZE()
ON_MESSAGE(WM_LINK_CLICKED, &CSkinDlg::OnLinkClicked)
END_MESSAGE_MAP()
// CSkinDlg 消息处理程序
BOOL CSkinDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(CCommon::LoadText(IDS_TITLE_CHANGE_SKIN));
SetIcon(theApp.GetMenuIcon(IDI_SKIN), FALSE); // 设置小图标
//初始化选择框
for (const auto& skin_path : m_skins)
{
wstring skin_name;
size_t index = skin_path.find_last_of(L'\\');
skin_name = skin_path.substr(index + 1);
m_select_box.AddString(skin_name.c_str());
}
m_select_box.SetCurSel(m_skin_selected);
m_select_box.SetMinVisibleItems(9);
//初始化预览视图
m_view = (CSkinPreviewView*)RUNTIME_CLASS(CSkinPreviewView)->CreateObject();
m_view->Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, CalculateViewRect(), this, 3000);
m_view->InitialUpdate();
m_view->SetSkinData(&m_skin_data);
m_view->SetFont(m_pFont);
m_view->ShowWindow(SW_SHOW);
//设置提示信息
m_notify_static.SetTextColor(RGB(252, 128, 45));
m_notify_static.SetBackColor(GetSysColor(COLOR_BTNFACE));
m_notify_static.SetWindowTextEx(_T(""));
//显示预览图片
ShowPreview();
//设置超链接
m_skin_course.SetURL(_T("https://github.com/zhongyang219/TrafficMonitor/wiki/%E7%9A%AE%E8%82%A4%E5%88%B6%E4%BD%9C%E6%95%99%E7%A8%8B"));
m_skin_download.SetURL(_T("https://github.com/zhongyang219/TrafficMonitorSkin/blob/master/皮肤下载.md"));
m_open_skin_dir_lnk.SetLinkIsURL(false);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CSkinDlg::OnCbnSelchangeCombo1()
{
// TODO: 在此添加控件通知处理程序代码
m_skin_selected = m_select_box.GetCurSel();
ShowPreview();
}
void CSkinDlg::OnSize(UINT nType, int cx, int cy)
{
CBaseDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (m_preview_static.m_hWnd != NULL && nType != SIZE_MINIMIZED && m_view != nullptr)
m_view->MoveWindow(CalculateViewRect());
}
afx_msg LRESULT CSkinDlg::OnLinkClicked(WPARAM wParam, LPARAM lParam)
{
CWnd* pCtrl = (CWnd*)wParam;
//点击了“打开皮肤目录”
if (pCtrl == &m_open_skin_dir_lnk)
{
CreateDirectory(theApp.m_skin_path.c_str(), NULL); //如果皮肤目录不存在,则创建它
ShellExecute(NULL, _T("open"), _T("explorer"), theApp.m_skin_path.c_str(), NULL, SW_SHOWNORMAL);
}
return 0;
}
================================================
FILE: TrafficMonitor/SkinDlg.h
================================================
#pragma once
#include "afxwin.h"
#include"StaticEx.h"
#include "PictureStatic.h"
#include "CSkinPreviewView.h"
#include "LinkStatic.h"
#include "BaseDialog.h"
// CSkinDlg 对话框
class CSkinDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CSkinDlg)
public:
CSkinDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CSkinDlg();
vector m_skins; //皮肤文件的路径
int m_skin_selected; //选择的皮肤
CFont* m_pFont; //预览图的字体
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_SKIN_DIALOG };
#endif
const CSkinFile& GetSkinData() const { return m_skin_data; }
protected:
virtual CString GetDialogName() const override;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
protected:
//控件变量
CComboBox m_select_box; //选择框
CLinkStatic m_skin_course; //“皮肤制作教程”超链接
CLinkStatic m_skin_download; //“更多皮肤下载”超链接
CLinkStatic m_open_skin_dir_lnk;
CSkinPreviewView* m_view; //预览区视图类
CStatic m_preview_static;
CStaticEx m_notify_static; //显示提示信息的static控件
CSkinFile m_skin_data; //皮肤数据
void ShowPreview(); //显示皮肤预览
CRect CalculateViewRect(); //根据窗口大小计算预览视图的大小
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnCbnSelchangeCombo1();
afx_msg void OnSize(UINT nType, int cx, int cy);
protected:
afx_msg LRESULT OnLinkClicked(WPARAM wParam, LPARAM lParam);
};
================================================
FILE: TrafficMonitor/SkinFile.cpp
================================================
#include "stdafx.h"
#include "SkinFile.h"
#include "Common.h"
#include "FilePathHelper.h"
#include "TrafficMonitor.h"
#include "IniHelper.h"
#include "DrawCommon.h"
CSkinFile::CSkinFile()
{
}
CSkinFile::~CSkinFile()
{
}
static CSkinFile::LayoutItem LayoutItemFromXmlNode(tinyxml2::XMLElement* ele)
{
CSkinFile::LayoutItem layout_item;
layout_item.x = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele, "x")));
layout_item.y = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele, "y")));
layout_item.width = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele, "width")));
layout_item.align = static_cast(atoi(CTinyXml2Helper::ElementAttribute(ele, "align")));
layout_item.show = CTinyXml2Helper::StringToBool(CTinyXml2Helper::ElementAttribute(ele, "show"));
return layout_item;
}
CSkinFile::Layout CSkinFile::LayoutFromXmlNode(tinyxml2::XMLElement* ele)
{
CSkinFile::Layout layout;
layout.width = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele, "width")));
layout.height = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele, "height")));
CTinyXml2Helper::IterateChildNode(ele, [&](tinyxml2::XMLElement* ele_layout_item)
{
string str_layout_item = CTinyXml2Helper::ElementName(ele_layout_item);
for (auto display_item : AllDisplayItems)
{
if (str_layout_item == CSkinFile::GetDisplayItemXmlNodeName(display_item))
{
layout.layout_items[display_item] = LayoutItemFromXmlNode(ele_layout_item);
break;
}
}
wstring plugin_id = CCommon::StrToUnicode(m_plugin_map[str_layout_item].c_str(), true);
if (!plugin_id.empty())
{
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
{
if (plugin_id == plugin_item->GetItemId())
{
layout.layout_items[plugin_item] = LayoutItemFromXmlNode(ele_layout_item);
break;
}
}
}
});
return layout;
}
void CSkinFile::DrawSkinText(CDrawCommon drawer, DrawStr draw_str, CRect rect, COLORREF color, Alignment align)
{
int text_width = drawer.GetDC()->GetTextExtent(draw_str.GetStr()).cx;
//标签和数值两端对齐
if (align == Alignment::SIDE && text_width < rect.Width()) //只有文本宽度小于矩形的宽度时才使用两端对齐
{
//绘制标签
drawer.DrawWindowText(rect, draw_str.label, color, Alignment::LEFT);
//绘制数值
drawer.DrawWindowText(rect, draw_str.value, color, Alignment::RIGHT);
}
else
{
drawer.DrawWindowText(rect, draw_str.GetStr(), color, align);
}
}
void CSkinFile::Load(const wstring& file_path)
{
CFilePathHelper file_path_helper{ file_path };
wstring ext = file_path_helper.GetFileExtension();
if (ext == L"ini")
LoadFromIni(file_path);
else
LoadFromXml(file_path);
if (m_font.m_hObject)
m_font.DeleteObject();
//创建字体对象
m_skin_info.font_info.Create(m_font);
//载入背景图片
wstring path_dir = file_path_helper.GetDir();
m_background_s.Destroy();
m_background_s.Load((path_dir + BACKGROUND_IMAGE_S).c_str());
m_background_l.Destroy();
m_background_l.Load((path_dir + BACKGROUND_IMAGE_L).c_str());
}
void CSkinFile::LoadFromXml(const wstring& file_path)
{
m_skin_info = SkinInfo();
m_layout_info = LayoutInfo();
m_preview_info = PreviewInfo();
tinyxml2::XMLDocument doc;
if (CTinyXml2Helper::LoadXmlFile(doc, file_path.c_str()))
{
CTinyXml2Helper::IterateChildNode(doc.FirstChildElement(), [this](tinyxml2::XMLElement* child)
{
string ele_name = CTinyXml2Helper::ElementName(child);
//读取皮肤信息
if (ele_name == "skin")
{
CTinyXml2Helper::IterateChildNode(child, [this](tinyxml2::XMLElement* skin_item)
{
string skin_item_name = CTinyXml2Helper::ElementName(skin_item);
//文本颜色
if (skin_item_name == "text_color")
{
string str_text_color = CTinyXml2Helper::ElementText(skin_item);
std::vector split_result;
CCommon::StringSplit(str_text_color, L',', split_result);
for (const auto& str : split_result)
{
m_skin_info.text_color.push_back(atoi(str.c_str()));
}
}
if (m_skin_info.text_color.size() < theApp.m_plugins.AllDisplayItemsWithPlugins().size())
{
COLORREF default_color{};
if (!m_skin_info.text_color.empty())
default_color = m_skin_info.text_color.front();
m_skin_info.text_color.resize(theApp.m_plugins.AllDisplayItemsWithPlugins().size(), default_color);
}
//指定每个项目的颜色
else if (skin_item_name == "specify_each_item_color")
{
m_skin_info.specify_each_item_color = CTinyXml2Helper::StringToBool(CTinyXml2Helper::ElementText(skin_item));
}
//皮肤作者
else if (skin_item_name == "skin_author")
{
m_skin_info.skin_author = CCommon::StrToUnicode(CTinyXml2Helper::ElementText(skin_item), true);
}
//字体
else if (skin_item_name == "font")
{
m_skin_info.font_info.name = CTinyXml2Helper::ElementAttribute(skin_item, "name");
m_skin_info.font_info.size = atoi(CTinyXml2Helper::ElementAttribute(skin_item, "size"));
int font_style = atoi(CTinyXml2Helper::ElementAttribute(skin_item, "style"));
m_skin_info.font_info.bold = CCommon::GetNumberBit(font_style, 0);
m_skin_info.font_info.italic = CCommon::GetNumberBit(font_style, 1);
m_skin_info.font_info.underline = CCommon::GetNumberBit(font_style, 2);
m_skin_info.font_info.strike_out = CCommon::GetNumberBit(font_style, 3);
}
else if (skin_item_name == "display_text")
{
CTinyXml2Helper::IterateChildNode(skin_item, [this](tinyxml2::XMLElement* display_text_item)
{
string item_name = CTinyXml2Helper::ElementName(display_text_item);
wstring item_text = CCommon::StrToUnicode(CTinyXml2Helper::ElementText(display_text_item), true);
for (auto display_item : AllDisplayItems)
{
if (item_name == CSkinFile::GetDisplayItemXmlNodeName(display_item))
{
m_skin_info.display_text.Get(display_item) = item_text;
break;
}
}
});
}
});
}
//布局信息
else if (ele_name == "layout")
{
m_layout_info.text_height = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(child, "text_height")));
m_layout_info.no_label = CTinyXml2Helper::StringToBool(CTinyXml2Helper::ElementAttribute(child, "no_label"));
CTinyXml2Helper::IterateChildNode(child, [this](tinyxml2::XMLElement* ele_layout)
{
string str_layout = CTinyXml2Helper::ElementName(ele_layout);
if (str_layout == "layout_l")
m_layout_info.layout_l = LayoutFromXmlNode(ele_layout);
else if (str_layout == "layout_s")
m_layout_info.layout_s = LayoutFromXmlNode(ele_layout);
});
}
//预览图
else if (ele_name == "preview")
{
m_preview_info.width = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(child, "width")));
m_preview_info.height = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(child, "height")));
CTinyXml2Helper::IterateChildNode(child, [this](tinyxml2::XMLElement* ele_priview_item)
{
string str_item_name = CTinyXml2Helper::ElementName(ele_priview_item);
if (str_item_name == "l")
{
m_preview_info.l_pos.x = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele_priview_item, "x")));
m_preview_info.l_pos.y = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele_priview_item, "y")));
}
else if (str_item_name == "s")
{
m_preview_info.s_pos.x = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele_priview_item, "x")));
m_preview_info.s_pos.y = theApp.DPI(atoi(CTinyXml2Helper::ElementAttribute(ele_priview_item, "y")));
}
});
}
//插件名称映射
else if (ele_name == "plugin_map")
{
CTinyXml2Helper::IterateChildNode(child, [this](tinyxml2::XMLElement* plugin_item)
{
string ele_name = CTinyXml2Helper::ElementName(plugin_item);
string ele_text = CTinyXml2Helper::ElementText(plugin_item);
m_plugin_map[ele_name] = ele_text;
});
}
});
}
}
void CSkinFile::LoadFromIni(const wstring& file_path)
{
m_skin_info = SkinInfo();
m_layout_info = LayoutInfo();
m_preview_info = PreviewInfo();
//获取皮肤信息
CIniHelper ini(file_path);
//获取当前皮肤的文字颜色
std::map text_colors{};
ini.LoadMainWndColors(_T("skin"), _T("text_color"), text_colors, 0);
for (const auto& item : text_colors)
{
m_skin_info.text_color.push_back(item.second);
}
m_skin_info.specify_each_item_color = ini.GetBool(_T("skin"), _T("specify_each_item_color"), false);
//获取当前皮肤的字体
FontInfo default_font{};
ini.LoadFontData(L"skin", m_skin_info.font_info, default_font);
//获取皮肤作者
m_skin_info.skin_author = ini.GetString(_T("skin"), _T("skin_author"), _T("unknow"));
//获取显示文本
m_skin_info.display_text.Get(TDI_UP) = ini.GetString(_T("skin"), _T("up_string"), NONE_STR);
m_skin_info.display_text.Get(TDI_DOWN) = ini.GetString(_T("skin"), _T("down_string"), NONE_STR);
m_skin_info.display_text.Get(TDI_CPU) = ini.GetString(_T("skin"), _T("cpu_string"), NONE_STR);
m_skin_info.display_text.Get(TDI_MEMORY) = ini.GetString(_T("skin"), _T("memory_string"), NONE_STR);
//获取预览区大小
m_preview_info.width = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_width"), 238));
m_preview_info.height = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_height"), 105));
//从ini文件读取皮肤布局,并根据DPI进行缩放
m_layout_info.text_height = theApp.DPI(ini.GetInt(_T("layout"), _T("text_height"), 20));
m_layout_info.no_label = ini.GetBool(_T("layout"), _T("no_text"), false);
m_layout_info.layout_l.width = theApp.DPI(ini.GetInt(_T("layout"), _T("width_l"), 220));
m_layout_info.layout_l.height = theApp.DPI(ini.GetInt(_T("layout"), _T("height_l"), 43));
m_layout_info.layout_l.layout_items[TDI_UP].x = theApp.DPI(ini.GetInt(_T("layout"), _T("up_x_l"), 6));
m_layout_info.layout_l.layout_items[TDI_UP].y = theApp.DPI(ini.GetInt(_T("layout"), _T("up_y_l"), 2));
m_layout_info.layout_l.layout_items[TDI_UP].width = theApp.DPI(ini.GetInt(_T("layout"), _T("up_width_l"), 108));
m_layout_info.layout_l.layout_items[TDI_UP].align = static_cast(ini.GetInt(_T("layout"), _T("up_align_l"), 0));
m_layout_info.layout_l.layout_items[TDI_DOWN].x = theApp.DPI(ini.GetInt(_T("layout"), _T("down_x_l"), 114));
m_layout_info.layout_l.layout_items[TDI_DOWN].y = theApp.DPI(ini.GetInt(_T("layout"), _T("down_y_l"), 2));
m_layout_info.layout_l.layout_items[TDI_DOWN].width = theApp.DPI(ini.GetInt(_T("layout"), _T("down_width_l"), 110));
m_layout_info.layout_l.layout_items[TDI_DOWN].align = static_cast(ini.GetInt(_T("layout"), _T("down_align_l"), 0));
m_layout_info.layout_l.layout_items[TDI_CPU].x = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_x_l"), 6));
m_layout_info.layout_l.layout_items[TDI_CPU].y = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_y_l"), 21));
m_layout_info.layout_l.layout_items[TDI_CPU].width = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_width_l"), 108));
m_layout_info.layout_l.layout_items[TDI_CPU].align = static_cast(ini.GetInt(_T("layout"), _T("cpu_align_l"), 0));
m_layout_info.layout_l.layout_items[TDI_MEMORY].x = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_x_l"), 114));
m_layout_info.layout_l.layout_items[TDI_MEMORY].y = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_y_l"), 21));
m_layout_info.layout_l.layout_items[TDI_MEMORY].width = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_width_l"), 110));
m_layout_info.layout_l.layout_items[TDI_MEMORY].align = static_cast(ini.GetInt(_T("layout"), _T("memory_align_l"), 0));
m_layout_info.layout_l.layout_items[TDI_UP].show = ini.GetBool(_T("layout"), _T("show_up_l"), true);
m_layout_info.layout_l.layout_items[TDI_DOWN].show = ini.GetBool(_T("layout"), _T("show_down_l"), true);
m_layout_info.layout_l.layout_items[TDI_CPU].show = ini.GetBool(_T("layout"), _T("show_cpu_l"), true);
m_layout_info.layout_l.layout_items[TDI_MEMORY].show = ini.GetBool(_T("layout"), _T("show_memory_l"), true);
m_preview_info.l_pos.x = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_x_l"), 0));
m_preview_info.l_pos.y = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_y_l"), 47));
m_layout_info.layout_s.width = theApp.DPI(ini.GetInt(_T("layout"), _T("width_s"), 220));
m_layout_info.layout_s.height = theApp.DPI(ini.GetInt(_T("layout"), _T("height_s"), 28));
m_layout_info.layout_s.layout_items[TDI_UP].x = theApp.DPI(ini.GetInt(_T("layout"), _T("up_x_s"), 6));
m_layout_info.layout_s.layout_items[TDI_UP].y = theApp.DPI(ini.GetInt(_T("layout"), _T("up_y_s"), 4));
m_layout_info.layout_s.layout_items[TDI_UP].width = theApp.DPI(ini.GetInt(_T("layout"), _T("up_width_s"), 108));
m_layout_info.layout_s.layout_items[TDI_UP].align = static_cast(ini.GetInt(_T("layout"), _T("up_align_s"), 0));
m_layout_info.layout_s.layout_items[TDI_DOWN].x = theApp.DPI(ini.GetInt(_T("layout"), _T("down_x_s"), 114));
m_layout_info.layout_s.layout_items[TDI_DOWN].y = theApp.DPI(ini.GetInt(_T("layout"), _T("down_y_s"), 4));
m_layout_info.layout_s.layout_items[TDI_DOWN].width = theApp.DPI(ini.GetInt(_T("layout"), _T("down_width_s"), 110));
m_layout_info.layout_s.layout_items[TDI_DOWN].align = static_cast(ini.GetInt(_T("layout"), _T("down_align_s"), 0));
m_layout_info.layout_s.layout_items[TDI_CPU].x = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_x_s"), 0));
m_layout_info.layout_s.layout_items[TDI_CPU].y = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_y_s"), 0));
m_layout_info.layout_s.layout_items[TDI_CPU].width = theApp.DPI(ini.GetInt(_T("layout"), _T("cpu_width_s"), 0));
m_layout_info.layout_s.layout_items[TDI_CPU].align = static_cast(ini.GetInt(_T("layout"), _T("cpu_align_s"), 0));
m_layout_info.layout_s.layout_items[TDI_MEMORY].x = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_x_s"), 0));
m_layout_info.layout_s.layout_items[TDI_MEMORY].y = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_y_s"), 0));
m_layout_info.layout_s.layout_items[TDI_MEMORY].width = theApp.DPI(ini.GetInt(_T("layout"), _T("memory_width_s"), 0));
m_layout_info.layout_s.layout_items[TDI_MEMORY].align = static_cast(ini.GetInt(_T("layout"), _T("memory_align_s"), 0));
m_layout_info.layout_s.layout_items[TDI_UP].show = ini.GetBool(_T("layout"), _T("show_up_s"), true);
m_layout_info.layout_s.layout_items[TDI_DOWN].show = ini.GetBool(_T("layout"), _T("show_down_s"), true);
m_layout_info.layout_s.layout_items[TDI_CPU].show = ini.GetBool(_T("layout"), _T("show_cpu_s"), false);
m_layout_info.layout_s.layout_items[TDI_MEMORY].show = ini.GetBool(_T("layout"), _T("show_memory_s"), false);
m_preview_info.s_pos.x = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_x_s"), 0));
m_preview_info.s_pos.y = theApp.DPI(ini.GetInt(_T("layout"), _T("preview_y_s"), 0));
}
void CSkinFile::DrawPreview(CDC* pDC, CRect rect)
{
CDrawCommon draw;
draw.Create(pDC, nullptr);
if (!m_skin_info.font_info.name.IsEmpty() && m_skin_info.font_info.size > 0)
draw.SetFont(&m_font);
else
draw.SetFont(theApp.m_pMainWnd->GetFont());
draw.SetDrawRect(rect);
draw.FillRect(rect, RGB(255, 255, 255));
//绘制背景
CRect rect_s(CPoint(m_preview_info.s_pos.x, m_preview_info.s_pos.y), CSize(m_layout_info.layout_s.width, m_layout_info.layout_s.height));
CRect rect_l(CPoint(m_preview_info.l_pos.x, m_preview_info.l_pos.y), CSize(m_layout_info.layout_l.width, m_layout_info.layout_l.height));
if (m_background_s.IsNull())
draw.FillRect(rect_s, RGB(230, 230, 230));
else
draw.DrawBitmap(m_background_s, rect_s.TopLeft(), rect_s.Size());
if (m_background_l.IsNull())
draw.FillRect(rect_l, RGB(230, 230, 230));
draw.DrawBitmap(m_background_l, rect_l.TopLeft(), rect_l.Size());
//获取每个项目显示的文本
std::map map_str;
for (auto iter = AllDisplayItems.begin(); iter != AllDisplayItems.end(); ++iter)
{
//wstring disp_text = m_skin_info.display_text.Get(*iter);
//if (disp_text == NONE_STR)
// disp_text = theApp.m_main_wnd_data.disp_str.Get(*iter);
DrawStr draw_str;
switch (*iter)
{
case TDI_UP:
draw_str.value = _T("88.8 KB/s");
break;
case TDI_DOWN:
draw_str.value = _T("88.9 KB/s");
break;
case TDI_TOTAL_SPEED:
draw_str.value = _T("90 KB/s");
break;
case TDI_CPU:
draw_str.value = _T("50 %");
break;
case TDI_MEMORY:
draw_str.value = _T("51 %");
break;
case TDI_CPU_TEMP: case TDI_GPU_TEMP: case TDI_HDD_TEMP: case TDI_MAIN_BOARD_TEMP:
draw_str.value = _T("40 °C");
break;
case TDI_CPU_FREQ:
draw_str.value = _T("1.0 GHz");
break;
default:
draw_str.value = _T("99");
break;
}
if (m_skin_info.display_text.Get(*iter) == NONE_STR)
m_skin_info.display_text.Get(*iter) = theApp.m_main_wnd_data.disp_str.Get(*iter);
if (!m_layout_info.no_label)
draw_str.label = m_skin_info.display_text.Get(*iter).c_str();
map_str[*iter] = draw_str;
}
//获取文本颜色
std::map text_colors{};
if (m_skin_info.specify_each_item_color)
{
int i{};
for (const auto& item : theApp.m_plugins.AllDisplayItemsWithPlugins())
{
if (i < static_cast(m_skin_info.text_color.size()))
text_colors[item] = m_skin_info.text_color[i];
i++;
}
}
else if (!m_skin_info.text_color.empty())
{
for (const auto& item : theApp.m_plugins.AllDisplayItemsWithPlugins())
{
if (!m_skin_info.text_color.empty())
text_colors[item] = m_skin_info.text_color[0];
}
}
//绘制预览图文本
auto drawPreviewText = [&](Layout& layout, const PreviewInfo::Pos& pos)
{
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
{
if (layout.layout_items[iter->first].show)
{
CPoint point;
point.SetPoint(layout.layout_items[iter->first].x, layout.layout_items[iter->first].y);
point.Offset(pos.x, pos.y);
CRect rect(point, CSize(layout.layout_items[iter->first].width, m_layout_info.text_height));
COLORREF text_color{};
text_color = text_colors[iter->first];
DrawSkinText(draw, iter->second, rect, text_color, layout.layout_items[iter->first].align);
}
}
//绘制插件项目
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
{
LayoutItem layout_item = layout.GetItem(plugin_item);
if (layout_item.show)
{
COLORREF cl{};
auto iter = text_colors.find(plugin_item);
if (iter != text_colors.end())
cl = iter->second;
else if (!text_colors.empty())
cl = text_colors.begin()->second;
//矩形区域
CPoint point;
point.SetPoint(layout_item.x, layout_item.y);
point.Offset(pos.x, pos.y);
CRect rect(point, CSize(layout_item.width, m_layout_info.text_height));
if (plugin_item->IsCustomDraw())
{
int brightness{ (GetRValue(cl) + GetGValue(cl) + GetBValue(cl)) / 2 };
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(plugin_item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
{
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(cl).c_str());
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"0");
}
draw.GetDC()->SetTextColor(cl);
plugin_item->DrawItem(draw.GetDC()->GetSafeHdc(), point.x, point.y, layout_item.width, m_layout_info.text_height, brightness >= 128);
}
else
{
//绘制文本
DrawStr draw_str;
draw_str.label = plugin_item->GetItemLableText();
draw_str.value = plugin_item->GetItemValueSampleText();
DrawSkinText(draw, draw_str, rect, cl, layout_item.align);
}
}
}
};
//绘制小预览图文本
drawPreviewText(m_layout_info.layout_s, m_preview_info.s_pos);
//绘制大预览图文本
drawPreviewText(m_layout_info.layout_l, m_preview_info.l_pos);
}
void CSkinFile::DrawInfo(CDC* pDC, bool show_more_info, CFont& font)
{
//绘制背景图
CImage& background_image{ show_more_info ? m_background_l : m_background_s };
Layout& layout{ show_more_info ? m_layout_info.layout_l : m_layout_info.layout_s };
CRect rect(CPoint(0, 0), CSize(layout.width, layout.height));
CDrawDoubleBuffer draw_double_buffer(pDC, rect);
CDrawCommon draw;
draw.Create(draw_double_buffer.GetMemDC(), nullptr);
draw.DrawBitmap(background_image, CPoint(0, 0), CSize(layout.width, layout.height));
//获取每个项目显示的文本
std::map map_str;
if (!m_layout_info.no_label)
{
map_str[TDI_UP].label = theApp.m_main_wnd_data.disp_str.Get(TDI_UP).c_str();
map_str[TDI_DOWN].label = theApp.m_main_wnd_data.disp_str.Get(TDI_DOWN).c_str();
map_str[TDI_TOTAL_SPEED].label = theApp.m_main_wnd_data.disp_str.Get(TDI_TOTAL_SPEED).c_str();
map_str[TDI_CPU].label = theApp.m_main_wnd_data.disp_str.Get(TDI_CPU).c_str();
map_str[TDI_MEMORY].label = theApp.m_main_wnd_data.disp_str.Get(TDI_MEMORY).c_str();
map_str[TDI_GPU_USAGE].label = theApp.m_main_wnd_data.disp_str.Get(TDI_GPU_USAGE).c_str();
map_str[TDI_HDD_USAGE].label = theApp.m_main_wnd_data.disp_str.Get(TDI_HDD_USAGE).c_str();
map_str[TDI_CPU_TEMP].label = theApp.m_main_wnd_data.disp_str.Get(TDI_CPU_TEMP).c_str();
map_str[TDI_CPU_FREQ].label = theApp.m_main_wnd_data.disp_str.Get(TDI_CPU_FREQ).c_str();
map_str[TDI_GPU_TEMP].label = theApp.m_main_wnd_data.disp_str.Get(TDI_GPU_TEMP).c_str();
map_str[TDI_HDD_TEMP].label = theApp.m_main_wnd_data.disp_str.Get(TDI_HDD_TEMP).c_str();
map_str[TDI_MAIN_BOARD_TEMP].label = theApp.m_main_wnd_data.disp_str.Get(TDI_MAIN_BOARD_TEMP).c_str();
}
//上传/下载
CString in_speed = CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_main_wnd_data);
CString out_speed = CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_main_wnd_data);
CString total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, theApp.m_main_wnd_data);
if (!theApp.m_main_wnd_data.hide_unit || theApp.m_main_wnd_data.speed_unit == SpeedUnit::AUTO)
{
in_speed += _T("/s");
out_speed += _T("/s");
total_speed += _T("/s");
}
map_str[TDI_UP].value = out_speed.GetString();
map_str[TDI_DOWN].value = in_speed.GetString();
map_str[TDI_TOTAL_SPEED].value = total_speed.GetString();
if (theApp.m_main_wnd_data.swap_up_down) //交换上传和下载位置
{
std::swap(map_str[TDI_UP], map_str[TDI_DOWN]);
}
//CPU/内存/显卡利用率
map_str[TDI_CPU].value = CCommon::UsageToString(theApp.m_cpu_usage, theApp.m_main_wnd_data);
map_str[TDI_CPU_FREQ].value = CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_main_wnd_data);
CString str_memory_value;
if (theApp.m_main_wnd_data.memory_display == MemoryDisplay::MEMORY_USED)
str_memory_value = CCommon::DataSizeToString(static_cast(theApp.m_used_memory) * 1024, theApp.m_main_wnd_data.separate_value_unit_with_space);
else if (theApp.m_main_wnd_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE)
str_memory_value = CCommon::DataSizeToString((static_cast(theApp.m_total_memory) - static_cast(theApp.m_used_memory)) * 1024, theApp.m_main_wnd_data.separate_value_unit_with_space);
else
str_memory_value = CCommon::UsageToString(theApp.m_memory_usage, theApp.m_main_wnd_data);
map_str[TDI_MEMORY].value = str_memory_value;
map_str[TDI_GPU_USAGE].value = CCommon::UsageToString(theApp.m_gpu_usage, theApp.m_main_wnd_data);
map_str[TDI_HDD_USAGE].value = CCommon::UsageToString(theApp.m_hdd_usage, theApp.m_main_wnd_data);
//温度
auto getTemperatureStr = [&](DisplayItem display_item, float temperature)
{
map_str[display_item].value = CCommon::TemperatureToString(temperature, theApp.m_main_wnd_data);
};
getTemperatureStr(TDI_CPU_TEMP, theApp.m_cpu_temperature);
getTemperatureStr(TDI_GPU_TEMP, theApp.m_gpu_temperature);
getTemperatureStr(TDI_HDD_TEMP, theApp.m_hdd_temperature);
getTemperatureStr(TDI_MAIN_BOARD_TEMP, theApp.m_main_board_temperature);
//获取文本颜色
std::map text_colors{};
if (theApp.m_main_wnd_data.specify_each_item_color)
{
text_colors = theApp.m_main_wnd_data.text_colors;
}
else if (!theApp.m_main_wnd_data.text_colors.empty())
{
for (const auto& item : theApp.m_plugins.AllDisplayItemsWithPlugins())
{
text_colors[item] = theApp.m_main_wnd_data.text_colors.begin()->second;
}
}
//绘制文本
draw.SetFont(&font);
//绘制文本
int index{};
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
{
const auto& layout_item = layout.GetItem(iter->first);
if (layout_item.show)
{
//矩形区域
CRect rect(CPoint(layout_item.x, layout_item.y), CSize(layout_item.width, m_layout_info.text_height));
//文本颜色
COLORREF text_color = text_colors[iter->first];
//绘制文本
DrawSkinText(draw, map_str[iter->first], rect, text_color, layout_item.align);
}
index++;
}
//绘制插件项目
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
{
const auto& layout_item = layout.GetItem(plugin_item);
if (layout_item.show)
{
//插件项目自绘
COLORREF cl{};
auto iter = text_colors.find(plugin_item);
if (iter != text_colors.end())
cl = iter->second;
else if (!text_colors.empty())
cl = text_colors.begin()->second;
if (plugin_item->IsCustomDraw())
{
int brightness{ (GetRValue(cl) + GetGValue(cl) + GetBValue(cl)) / 2 };
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(plugin_item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
{
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(cl).c_str());
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"0");
}
draw.GetDC()->SetTextColor(cl);
plugin_item->DrawItem(draw.GetDC()->GetSafeHdc(), layout_item.x, layout_item.y, layout_item.width, m_layout_info.text_height, brightness >= 128);
}
else
{
//矩形区域
CRect rect(CPoint(layout_item.x, layout_item.y), CSize(layout_item.width, m_layout_info.text_height));
//绘制文本
DrawStr draw_str;
draw_str.label = theApp.m_main_wnd_data.disp_str.Get(plugin_item).c_str();
draw_str.value = plugin_item->GetItemValueText();
DrawSkinText(draw, draw_str, rect, cl, layout_item.align);
}
}
}
}
string CSkinFile::GetDisplayItemXmlNodeName(DisplayItem display_item)
{
switch (display_item)
{
case TDI_UP:
return "up";
break;
case TDI_DOWN:
return "down";
break;
case TDI_TOTAL_SPEED:
return "total_speed";
break;
case TDI_CPU:
return "cpu";
break;
case TDI_MEMORY:
return "memory";
break;
case TDI_GPU_USAGE:
return "gpu";
break;
case TDI_CPU_TEMP:
return "cpu_temperature";
break;
case TDI_GPU_TEMP:
return "gpu_temperature";
break;
case TDI_HDD_TEMP:
return "hdd_temperature";
break;
case TDI_MAIN_BOARD_TEMP:
return "main_board_temperature";
break;
case TDI_HDD_USAGE:
return "hdd";
break;
case TDI_CPU_FREQ:
return "cpu_freq";
break;
default:
return string();
break;
}
}
================================================
FILE: TrafficMonitor/SkinFile.h
================================================
#pragma once
#include "CommonData.h"
#include "TinyXml2Helper.h"
#include "DrawCommon.h"
class CSkinFile
{
public:
CSkinFile();
~CSkinFile();
//从文件载入皮肤信息
void Load(const wstring& file_path);
//皮肤信息
struct SkinInfo
{
std::vector text_color; //文本颜色
bool specify_each_item_color{}; //是否指定每个项目的颜色
wstring skin_author; //皮肤的作者
FontInfo font_info; //字体信息
DispStrings display_text; //每一项的显示文本
COLORREF TextColor(size_t i) const
{
if (i >= 0 && i < text_color.size())
return text_color[i];
else if (!text_color.empty())
return text_color.front();
else
return 0;
}
};
//皮肤中每一项的布局信息
struct LayoutItem
{
int x{}; //X位置
int y{}; //Y位置
int width{}; //宽度
Alignment align{}; //对齐方式
bool show{}; //是否显示
};
//皮肤布局
struct Layout
{
int width{}; //宽度
int height{}; //高度
std::map layout_items; //每一项的布局信息
LayoutItem GetItem(CommonDisplayItem display_item) const
{
auto iter = layout_items.find(display_item);
if (iter != layout_items.end())
return iter->second;
return LayoutItem();
}
};
//皮肤布局信息
struct LayoutInfo
{
int text_height{}; //皮肤文本的高度
bool no_label{}; //是否不显示标签
Layout layout_l; //“显示更多信息”时的布局
Layout layout_s; //不“显示更多信息”时的布局
};
//皮肤预览图信息
struct PreviewInfo
{
struct Pos
{
int x{};
int y{};
};
int width{}; //预览图的宽度
int height{}; //预览图的高度
Pos l_pos; //“显示更多信息”时的窗口在预览图中的位置
Pos s_pos; //不“显示更多信息”时的窗口在预览图中的位置
};
const SkinInfo& GetSkinInfo() const { return m_skin_info; }
const LayoutInfo& GetLayoutInfo() const { return m_layout_info; }
const PreviewInfo& GetPreviewInfo() const { return m_preview_info; }
const CImage& GetBackgroundL() const { return m_background_l; }
const CImage& GetBackgroundS() const { return m_background_s; }
//绘制预览图
//pDC: 绘图的CDC
//rect: 绘图区域
void DrawPreview(CDC* pDC, CRect rect);
//绘制主界面
void DrawInfo(CDC* pDC, bool show_more_info, CFont& font);
static string GetDisplayItemXmlNodeName(DisplayItem display_item);
private:
void LoadFromXml(const wstring& file_path); //从xml文件读取皮肤数据
void LoadFromIni(const wstring& file_path); //从ini文件读取皮肤数据(用于兼容旧版皮肤)
CSkinFile::Layout LayoutFromXmlNode(tinyxml2::XMLElement* ele);
struct DrawStr
{
CString label;
CString value;
CString GetStr() const
{
return label + value;
}
};
static void DrawSkinText(CDrawCommon drawer, DrawStr draw_str, CRect rect, COLORREF color, Alignment align);
private:
SkinInfo m_skin_info;
LayoutInfo m_layout_info;
PreviewInfo m_preview_info;
std::map m_plugin_map; //插件名称与xml节点名称的映射关系。key是xml节点名称,value是插件ID
CFont m_font;
CImage m_background_s;
CImage m_background_l;
};
================================================
FILE: TrafficMonitor/StaticEx.cpp
================================================
#include "stdafx.h"
#include "StaticEx.h"
CStaticEx::CStaticEx()
{
}
CStaticEx::~CStaticEx()
{
}
void CStaticEx::SetWindowTextEx(LPCTSTR lpszString, Alignment align)
{
m_text = lpszString;
m_align = align;
m_color_text = true;
Invalidate();
}
void CStaticEx::SetTextColor(COLORREF textColor)
{
m_text_color = textColor;
Invalidate();
}
void CStaticEx::SetBackColor(COLORREF back_color)
{
m_back_color = back_color;
m_draw_background_color = true;
}
CString CStaticEx::GetString() const
{
return m_text;
}
LRESULT CStaticEx::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: ڴרô/û
if (message == WM_SETTEXT)
{
CRect rect;
CDC* pDC = GetDC();
GetClientRect(rect);
DrawThemeParentBackground(m_hWnd, pDC->GetSafeHdc(), &rect);
ReleaseDC(pDC);
}
return CStatic::DefWindowProc(message, wParam, lParam);
}
BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CStaticEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: ڴ˴Ϣ
// ΪͼϢ CStatic::OnPaint()
dc.SetTextColor(m_text_color);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(this->GetFont());
CRect rect;
this->GetClientRect(&rect);
if (m_draw_background_color)
dc.FillSolidRect(rect, m_back_color);
else
DrawThemeParentBackground(m_hWnd, dc.GetSafeHdc(), &rect); //ػؼԽص
CSize text_size = dc.GetTextExtent(m_text);
UINT format{ DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX }; //CDC::DrawText()ıʽ
if (text_size.cx > rect.Width()) //ıȳ˾Ŀȣ˾ʱ
{
if (m_align == Alignment::RIGHT)
format |= DT_RIGHT;
}
else
{
switch (m_align)
{
case Alignment::RIGHT: format |= DT_RIGHT; break;
case Alignment::CENTER: format |= DT_CENTER; break;
}
}
dc.DrawText(m_text, rect, format);
}
void CStaticEx::PreSubclassWindow()
{
// TODO: ڴרô/û
GetWindowText(m_text);
CStatic::PreSubclassWindow();
}
================================================
FILE: TrafficMonitor/StaticEx.h
================================================
/*
CStatic࣬ɫıؼ
SetTextColorıɫ
ҪʱSetWindowTextExÿؼı
*/
#pragma once
#include "afxwin.h"
#include "CommonData.h"
class CStaticEx :
public CStatic
{
public:
CStaticEx();
~CStaticEx();
//Staticؼɫıʱ
public:
void SetWindowTextEx(LPCTSTR lpszString, Alignment align = Alignment::LEFT); //ΪؼɫıҪSetTextColorʹã
void SetTextColor(COLORREF textColor); //ÿؼıɫ
void SetBackColor(COLORREF back_color); //ÿؼɫ
CString GetString() const; //ȡؼı
protected:
bool m_color_text{ false };
COLORREF m_text_color; //ؼɫ
COLORREF m_back_color;
CString m_text; //ؼϵı
Alignment m_align{}; //ıĶ뷽ʽ
bool m_draw_background_color{ false }; //ǷҪΪؼ䱳ɫ
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
virtual void PreSubclassWindow();
};
================================================
FILE: TrafficMonitor/TabDlg.cpp
================================================
#include "stdafx.h"
#include "TabDlg.h"
#include "TrafficMonitor.h"
IMPLEMENT_DYNAMIC(CTabDlg, CDialogEx)
CTabDlg::CTabDlg(UINT nIDTemplate, CWnd * pParent) : CDialogEx(nIDTemplate, pParent)
{
m_pParent = pParent;
}
CTabDlg::~CTabDlg()
{
}
BOOL CTabDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
//由于这是tab标签中的子对话框,因此用户按回车或ESC后不应该响应当前对话框的IDOK或IDCANCEL,
//而应该响应tab标签的父窗口的IDOK或IDCANCEL命令
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
{
if (m_pParent != nullptr)
m_pParent->SendMessage(WM_COMMAND, IDCANCEL);
return TRUE;
}
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
if (m_pParent != nullptr)
m_pParent->SendMessage(WM_COMMAND, IDOK);
return TRUE;
}
return CDialogEx::PreTranslateMessage(pMsg);
}
BOOL CTabDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
//将窗口背景设置成白色
if (theApp.m_win_version.IsWindows11OrLater())
SetBackgroundColor(RGB(249, 249, 249));
else
SetBackgroundColor(RGB(255, 255, 255));
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
CWnd* CTabDlg::GetParentWindow()
{
CWnd* pParent = GetParent();
if (pParent != nullptr)
{
return pParent->GetParent();
}
return nullptr;
}
void CTabDlg::SetScrollbarInfo(int nPage, int nMax)
{
//初始化滚动条
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
scrollinfo.nPage = nPage; //设置滑块大小
scrollinfo.nMin = 0;
scrollinfo.nMax = nMax; //设置滚动条的最大位置
if (scrollinfo.nMax < 0)
scrollinfo.nMax = 0;
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
m_scroll_enable = true;
}
void CTabDlg::ResetScroll()
{
if (m_scroll_enable)
{
m_last_pos = 0;
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
int step = scrollinfo.nPos - scrollinfo.nMin;
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
ScrollWindow(0, step);
}
}
void CTabDlg::ScrollWindowSimple(int step)
{
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
scrollinfo.nPos -= step;
if (scrollinfo.nPos < scrollinfo.nMin)
{
step = scrollinfo.nPos + step - scrollinfo.nMin; //如果向上滚动一个距离后小于滚动条的最小位置了,则修正step的值,使窗口滚动到最上方
scrollinfo.nPos = scrollinfo.nMin;
}
if (scrollinfo.nPos + scrollinfo.nPage > scrollinfo.nMax) //此处一定要注意加上滑块的长度,再作判断
{
step -= (scrollinfo.nMax - (scrollinfo.nPos + scrollinfo.nPage)); //如果向上滚动一个距离后大于滚动条的最大位置了,则修正step的值,使窗口滚动到最下方
if (step > 0)
step = 0;
scrollinfo.nPos = scrollinfo.nMax;
}
SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
ScrollWindow(0, step);
}
void CTabDlg::EnableDlgCtrl(UINT id, bool enable)
{
CWnd* pCtrl = GetDlgItem(id);
if (pCtrl != nullptr)
pCtrl->EnableWindow(enable);
}
void CTabDlg::ShowDlgCtrl(UINT id, bool show)
{
CWnd* pCtrl = GetDlgItem(id);
if (pCtrl != nullptr)
pCtrl->ShowWindow(show);
}
BEGIN_MESSAGE_MAP(CTabDlg, CDialogEx)
ON_WM_VSCROLL()
ON_WM_MOUSEWHEEL()
ON_WM_SIZE()
END_MESSAGE_MAP()
void CTabDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//参考资料:https://www.cnblogs.com/ranjiewen/p/6013922.html
if (m_scroll_enable)
{
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
int unit = 1;
int step = theApp.DPI(16);
switch (nSBCode)
{
case SB_LINEUP: //Scroll one line up
ScrollWindowSimple(unit * step);
break;
case SB_LINEDOWN: //Scroll one line down
ScrollWindowSimple(-unit * step);
break;
case SB_PAGEUP: //Scroll one page up.
ScrollWindowSimple(unit * step * 5);
break;
case SB_PAGEDOWN: //Scroll one page down
ScrollWindowSimple(-unit * step * 5);
break;
case SB_ENDSCROLL: //End scroll
break;
case SB_THUMBPOSITION: //Scroll to the absolute position. The current position is provided in nPos
break;
case SB_THUMBTRACK: //Drag scroll box to specified position. The current position is provided in nPos
{
int y_amount = (m_last_pos - nPos)*unit;
ScrollWindow(0, y_amount);
scrollinfo.nPos = nPos;
SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
}
break;
}
m_last_pos = scrollinfo.nPos;
}
CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}
BOOL CTabDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_scroll_enable)
{
int step = theApp.DPI(64);
if (zDelta > 0)
{
ScrollWindowSimple(step);
}
if (zDelta < 0)
{
ScrollWindowSimple(-step);
}
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
m_last_pos = scrollinfo.nPos;
}
return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
}
void CTabDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
CDialogEx::OnOK();
}
bool CTabDlg::IsScrollBarVisible()
{
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);
bool visible = scrollinfo.nPage < scrollinfo.nMax;
return visible;
}
void CTabDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
SetControlMouseWheelEnable(!IsScrollBarVisible()); //如果显示了滚动条,则禁止控件响应鼠标滚轮,此时鼠标滚轮用于滚动窗口
}
================================================
FILE: TrafficMonitor/TabDlg.h
================================================
//这是用于Tab标签中的子对话框类
#pragma once
class CTabDlg : public CDialogEx
{
DECLARE_DYNAMIC(CTabDlg)
public:
CTabDlg(UINT nIDTemplate, CWnd *pParent = NULL);
~CTabDlg();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual BOOL OnInitDialog();
CWnd* GetParentWindow();
void SetScrollbarInfo(int nPage, int nMax);
void ResetScroll();
virtual void OnTabEntered() {} //当标签切换到当前窗口时被调用
virtual void OnOK();
void SetScrollEnable(bool enable) { m_scroll_enable = enable; }
virtual void SetControlMouseWheelEnable(bool enable) {} //在派生类中重写此函数以设置控件是否允许响应鼠标滚轮
bool IsScrollBarVisible(); //滚动条是否可见
protected:
void ScrollWindowSimple(int step);
void EnableDlgCtrl(UINT id, bool enable);
void ShowDlgCtrl(UINT id, bool show);
protected:
CWnd* m_pParent;
bool m_scroll_enable{ false };
int m_last_pos{};
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
================================================
FILE: TrafficMonitor/TaskBarDlg.cpp
================================================
// TaskBarDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "TaskBarDlg.h"
#include "afxdialogex.h"
#include "TrafficMonitorDlg.h"
#include "WindowsSettingHelper.h"
// CTaskBarDlg 对话框
IMPLEMENT_DYNAMIC(CTaskBarDlg, CDialogEx)
CTaskBarDlg::CTaskBarDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_TASK_BAR_DIALOG, pParent)
{
}
CTaskBarDlg::~CTaskBarDlg()
{
for (auto iter = m_map_history_data.begin(); iter != m_map_history_data.end(); ++iter)
{
iter->second.clear();
}
}
void CTaskBarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CTaskBarDlg, CDialogEx)
ON_WM_RBUTTONUP()
ON_WM_INITMENU()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_TIMER()
ON_WM_PAINT()
ON_WM_CLOSE()
ON_WM_LBUTTONUP()
ON_MESSAGE(WM_EXITMENULOOP, &CTaskBarDlg::OnExitmenuloop)
END_MESSAGE_MAP()
// CTaskBarDlg 消息处理程序
void CTaskBarDlg::ShowInfo(CDC* pDC)
{
if (this->GetSafeHwnd() == NULL || pDC == nullptr || !IsWindow(this->GetSafeHwnd())) return;
if (m_rect.IsRectEmpty() || m_rect.IsRectNull()) return;
CRect draw_rect{ m_rect }; //绘图的矩形区域
draw_rect.MoveToXY(0, 0);
//设置缓冲的DC
CDrawDoubleBuffer draw_double_buffer(pDC, draw_rect);
//绘图
CDrawCommon draw;
draw.Create(draw_double_buffer.GetMemDC(), nullptr);
draw.FillRect(draw_rect, theApp.m_taskbar_data.back_color); //填充背景色
draw.SetFont(&m_font);
draw.SetBackColor(theApp.m_taskbar_data.back_color);
//计算各部分的位置
int index = 0;
CRect item_rect{};
int item_count = static_cast(m_item_widths.size()); //要显示的项目数量
auto last_iter = m_item_widths.begin();
for (auto iter = m_item_widths.begin(); iter != m_item_widths.end(); ++iter)
{
auto last_item_width = last_iter->item_width;
//任务栏在桌面顶部或底部
if (IsTasksbarOnTopOrBottom())
{
if (theApp.m_taskbar_data.horizontal_arrange) //水平排列
{
if (index > 0)
item_rect.MoveToX(item_rect.right + theApp.DPI(theApp.m_taskbar_data.item_space));
item_rect.right = item_rect.left + iter->item_width.TotalWidth();
item_rect.bottom = item_rect.top + m_window_height;
if (iter->is_plugin)
DrawPluginItem(draw, iter->plugin_item, item_rect, iter->item_width.label_width);
else
DrawDisplayItem(draw, iter->item_type, item_rect, iter->item_width.label_width);
}
else //非水平排列时,每两个一组显示
{
//在index为奇数时同时绘制两个项目
if (index % 2 == 1)
{
CRect item_rect_up; //上面一个项目的矩形区域
if (index > 0)
item_rect_up.MoveToXY(item_rect.right + theApp.DPI(theApp.m_taskbar_data.item_space), 0);
item_rect.left = item_rect_up.left;
item_rect.top = (m_window_height - TASKBAR_WND_HEIGHT / 2);
//确定窗口大小
item_rect_up.bottom = item_rect.top - 1;
item_rect.bottom = m_window_height;
int width = max(iter->item_width.TotalWidth(), last_item_width.TotalWidth());
item_rect.right = item_rect.left + width;
item_rect_up.right = item_rect_up.left + width;
//绘制信息
if (last_iter->is_plugin)
DrawPluginItem(draw, last_iter->plugin_item, item_rect_up, last_item_width.label_width);
else
DrawDisplayItem(draw, last_iter->item_type, item_rect_up, last_item_width.label_width);
if (iter->is_plugin)
DrawPluginItem(draw, iter->plugin_item, item_rect, iter->item_width.label_width);
else
DrawDisplayItem(draw, iter->item_type, item_rect, iter->item_width.label_width);
}
//要绘制的项目为奇数时绘制最后一个
else if (item_count % 2 == 1 && index == item_count - 1)
{
item_rect.MoveToXY(item_rect.right + theApp.DPI(theApp.m_taskbar_data.item_space), 0);
item_rect.bottom = TASKBAR_WND_HEIGHT;
item_rect.right = item_rect.left + iter->item_width.MaxWidth();
if (iter->is_plugin)
DrawPluginItem(draw, iter->plugin_item, item_rect, iter->item_width.label_width, true);
else
DrawDisplayItem(draw, iter->item_type, item_rect, iter->item_width.label_width, true);
}
}
}
//任务栏在桌面两侧
else
{
if (index > 0)
item_rect.MoveToXY(0, item_rect.bottom + theApp.DPI(theApp.m_taskbar_data.item_space));
item_rect.bottom = item_rect.top + TASKBAR_WND_HEIGHT / 2;
item_rect.right = item_rect.left + min(m_window_width, m_rcMin.Width() - theApp.DPI(theApp.m_taskbar_data.item_space));
if (iter->is_plugin)
DrawPluginItem(draw, iter->plugin_item, item_rect, iter->item_width.label_width);
else
DrawDisplayItem(draw, iter->item_type, item_rect, iter->item_width.label_width);
}
index++;
last_iter = iter;
}
}
void CTaskBarDlg::DrawDisplayItem(CDrawCommon& drawer, DisplayItem type, CRect rect, int label_width, bool vertical)
{
m_item_rects[type] = rect;
//设置要绘制的文本颜色
COLORREF label_color{};
COLORREF text_color{};
if (theApp.m_taskbar_data.specify_each_item_color)
{
label_color = theApp.m_taskbar_data.text_colors[type].label;
text_color = theApp.m_taskbar_data.text_colors[type].value;
}
else if (!theApp.m_taskbar_data.text_colors.empty())
{
label_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
text_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
}
//设置标签和数值的矩形区域
CRect rect_label{ rect };
CRect rect_value{ rect };
if (!vertical)
{
rect_label.right = rect_label.left + label_width;
rect_value.left = rect_label.right;
}
else if (label_width != 0)
{
rect_label.bottom = rect_label.top + (rect_label.Height() / 2);
rect_value.top = rect_label.bottom;
}
// 绘制状态条
if (type == TDI_CPU || type == TDI_MEMORY || type == TDI_GPU_USAGE || type == TDI_CPU_TEMP
|| type == TDI_GPU_TEMP || type == TDI_HDD_TEMP || type == TDI_MAIN_BOARD_TEMP || type == TDI_HDD_USAGE
|| type == TDI_UP || type == TDI_DOWN || type == TDI_TOTAL_SPEED/* ||type==TDI_CPU_FREQ*/)
{
int figure_value{};
switch (type)
{
case TDI_CPU:
figure_value = theApp.m_cpu_usage;
break;
case TDI_MEMORY:
figure_value = theApp.m_memory_usage;
break;
case TDI_GPU_USAGE:
figure_value = theApp.m_gpu_usage;
break;
case TDI_CPU_TEMP:
figure_value = theApp.m_cpu_temperature;
break;
case TDI_GPU_TEMP:
figure_value = theApp.m_gpu_temperature;
break;
case TDI_HDD_TEMP:
figure_value = theApp.m_hdd_temperature;
break;
case TDI_MAIN_BOARD_TEMP:
figure_value = theApp.m_main_board_temperature;
break;
case TDI_HDD_USAGE:
figure_value = theApp.m_hdd_usage;
break;
//case TDI_CPU_FREQ:
// figure_value = theApp.m_cpu_freq;
// break;
case TDI_UP:
figure_value = CalculateNetspeedPercent(theApp.m_out_speed);
break;
case TDI_DOWN:
figure_value = CalculateNetspeedPercent(theApp.m_in_speed);
break;
case TDI_TOTAL_SPEED:
figure_value = CalculateNetspeedPercent(theApp.m_in_speed + theApp.m_out_speed);
break;
default:
break;
}
if ((type != TDI_UP && type != TDI_DOWN && type != TDI_TOTAL_SPEED) && theApp.m_taskbar_data.show_status_bar
|| (type == TDI_UP || type == TDI_DOWN || type == TDI_TOTAL_SPEED) && theApp.m_taskbar_data.show_netspeed_figure)
{
if (theApp.m_taskbar_data.cm_graph_type)
{
AddHisToList(type, figure_value);
TryDrawGraph(drawer, rect, type);
}
else
{
TryDrawStatusBar(drawer, rect, figure_value);
}
}
}
//绘制标签
if (label_width > 0)
{
wstring str_label = theApp.m_taskbar_data.disp_str.Get(type);
//if (theApp.m_taskbar_data.swap_up_down)
//{
// if (type == TDI_UP)
// str_label = theApp.m_taskbar_data.disp_str.Get(TDI_DOWN);
// else if (type == TDI_DOWN)
// str_label = theApp.m_taskbar_data.disp_str.Get(TDI_UP);
//}
drawer.DrawWindowText(rect_label, str_label.c_str(), label_color, (vertical ? Alignment::CENTER : Alignment::LEFT));
}
//绘制数值
CString str_value;
Alignment value_alignment{ theApp.m_taskbar_data.value_right_align ? Alignment::RIGHT : Alignment::LEFT }; //数值的对齐方式
if (vertical)
value_alignment = Alignment::CENTER;
//绘制上传或下载速度
if (type == TDI_UP || type == TDI_DOWN || type == TDI_TOTAL_SPEED)
{
CString format_str;
if (theApp.m_taskbar_data.hide_unit && theApp.m_taskbar_data.speed_unit != SpeedUnit::AUTO)
format_str = _T("%s");
else
format_str = _T("%s/s");
CString str_in_speed = CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_taskbar_data);
CString str_out_speed = CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_taskbar_data);
CString str_total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, theApp.m_taskbar_data);
//if (theApp.m_taskbar_data.swap_up_down)
// std::swap(str_in_speed, str_out_speed);
if (type == TDI_UP)
{
str_value.Format(format_str, str_out_speed.GetString());
}
else if (type == TDI_DOWN)
{
str_value.Format(format_str, str_in_speed.GetString());
}
else
{
str_value.Format(format_str, str_total_speed.GetString());
}
}
//当内存显示为已使用内存或可用内存时
if (type == TDI_MEMORY && (theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_USED || theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE))
{
if (theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_USED)
str_value = CCommon::DataSizeToString(static_cast(theApp.m_used_memory) * 1024, theApp.m_taskbar_data.separate_value_unit_with_space);
else
str_value = CCommon::DataSizeToString((static_cast(theApp.m_total_memory) - static_cast(theApp.m_used_memory)) * 1024, theApp.m_taskbar_data.separate_value_unit_with_space);
}
//绘制CPU或内存利用率
else if (type == TDI_CPU || type == TDI_MEMORY || type == TDI_GPU_USAGE || type == TDI_HDD_USAGE)
{
int usage{};
switch (type)
{
case TDI_CPU:
usage = theApp.m_cpu_usage;
break;
case TDI_MEMORY:
usage = theApp.m_memory_usage;
break;
case TDI_GPU_USAGE:
usage = theApp.m_gpu_usage;
break;
case TDI_HDD_USAGE:
usage = theApp.m_hdd_usage;
break;
default:
break;
}
str_value = CCommon::UsageToString(usage, theApp.m_taskbar_data);
//如果CPU或内存利用率达到100%,会导致显示不全,此时将绘图区域向右扩展一些
int text_width = m_pDC->GetTextExtent(str_value).cx;
if (usage >= 100 && rect_value.Width() < text_width)
rect_value.right = rect_value.left + text_width;
}
//绘制温度
else if (type == TDI_CPU_TEMP || type == TDI_GPU_TEMP || type == TDI_HDD_TEMP || type == TDI_MAIN_BOARD_TEMP)
{
int temperature{};
switch (type)
{
case TDI_CPU_TEMP:
temperature = theApp.m_cpu_temperature;
break;
case TDI_GPU_TEMP:
temperature = theApp.m_gpu_temperature;
break;
case TDI_HDD_TEMP:
temperature = theApp.m_hdd_temperature;
break;
case TDI_MAIN_BOARD_TEMP:
temperature = theApp.m_main_board_temperature;
break;
default:
break;
}
str_value = CCommon::TemperatureToString(temperature, theApp.m_taskbar_data);
}
else if (type == TDI_CPU_FREQ) {
str_value = CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_taskbar_data);
}
drawer.DrawWindowText(rect_value, str_value, text_color, value_alignment);
}
void CTaskBarDlg::DrawPluginItem(CDrawCommon& drawer, IPluginItem* item, CRect rect, int label_width, bool vertical)
{
if (item == nullptr)
return;
m_item_rects[item] = rect;
//设置要绘制的文本颜色
COLORREF label_text_color{};
COLORREF value_text_color{};
if (theApp.m_taskbar_data.specify_each_item_color)
{
label_text_color = theApp.m_taskbar_data.text_colors[item].label;
value_text_color = theApp.m_taskbar_data.text_colors[item].value;
}
else if (!theApp.m_taskbar_data.text_colors.empty())
{
label_text_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
value_text_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
}
if (item->IsCustomDraw())
{
//根据背景色的亮度判断深色还是浅色模式
const COLORREF& bk{ theApp.m_taskbar_data.back_color };
int background_brightness{ (GetRValue(bk) + GetGValue(bk) + GetBValue(bk)) / 3 };
//由插件自绘
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
{
plugin->OnExtenedInfo(ITMPlugin::EI_LABEL_TEXT_COLOR, std::to_wstring(label_text_color).c_str());
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(value_text_color).c_str());
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"1");
}
drawer.GetDC()->SetTextColor(value_text_color);
item->DrawItem(drawer.GetDC()->GetSafeHdc(), rect.left, rect.top, rect.Width(), rect.Height(), background_brightness < 128);
}
else
{
CRect rect_label, rect_value;
rect_label = rect_value = rect;
if (label_width > 0)
{
if (!vertical)
{
rect_label = rect_value = rect;
rect_label.right = rect_label.left + label_width;
rect_value.left = rect_label.right;
}
else
{
rect_label.bottom = rect_label.top + rect.Height() / 2;
rect_value.top = rect_label.bottom;
}
}
//画标签
CString lable_text = theApp.m_taskbar_data.disp_str.Get(item).c_str();
lable_text += L' ';
drawer.DrawWindowText(rect_label, lable_text, label_text_color, (vertical ? Alignment::CENTER : Alignment::LEFT));
//画数值
Alignment value_alignment{ theApp.m_taskbar_data.value_right_align ? Alignment::RIGHT : Alignment::LEFT }; //数值的对齐方式
if (vertical)
value_alignment = Alignment::CENTER;
drawer.DrawWindowText(rect_value, item->GetItemValueText(), value_text_color, value_alignment);
}
}
void CTaskBarDlg::MoveWindow(CRect rect)
{
if (IsWindow(GetSafeHwnd()))
{
::MoveWindow(GetSafeHwnd(), rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
}
}
void CTaskBarDlg::TryDrawStatusBar(CDrawCommon& drawer, const CRect& rect_bar, int usage_percent)
{
CSize fill_size = CSize(rect_bar.Width() * usage_percent / 100, rect_bar.Height());
CRect rect_fill(rect_bar.TopLeft(), fill_size);
if (theApp.m_taskbar_data.show_graph_dashed_box)
drawer.DrawRectOutLine(rect_bar, theApp.m_taskbar_data.status_bar_color, 1, true);
drawer.FillRect(rect_fill, theApp.m_taskbar_data.status_bar_color);
}
bool CTaskBarDlg::AdjustWindowPos()
{
if (this->GetSafeHwnd() == NULL || !IsWindow(this->GetSafeHwnd()))
return false;
::GetWindowRect(m_hMin, m_rcMin); //获得最小化窗口的区域
::GetWindowRect(m_hBar, m_rcBar); //获得二级容器的区域
static bool last_taskbar_on_top_or_bottom;
CheckTaskbarOnTopOrBottom();
if (m_taskbar_on_top_or_bottom != last_taskbar_on_top_or_bottom)
{
CalculateWindowSize();
last_taskbar_on_top_or_bottom = m_taskbar_on_top_or_bottom;
}
if (m_taskbar_on_top_or_bottom) //当任务栏在桌面顶部或底部时
{
//设置窗口大小
m_rect.right = m_rect.left + m_window_width;
m_rect.bottom = m_rect.top + m_window_height;
if (m_rcMin.Width() != m_min_bar_width) //如果最小化窗口的宽度改变了,重新设置任务栏窗口的位置
{
m_rcMinOri = m_rcMin;
m_left_space = m_rcMin.left - m_rcBar.left;
m_min_bar_width = m_rcMin.Width() - m_rect.Width(); //保存最小化窗口宽度
if (!theApp.m_taskbar_data.tbar_wnd_on_left)
{
if (theApp.m_is_windows11_taskbar)
{
if (!theApp.m_taskbar_data.tbar_wnd_snap)
m_rect.MoveToX(m_rcBar.Width() - m_rect.Width() + 2);
else
m_rect.MoveToX(m_rcMin.right + 2);
}
else
{
::MoveWindow(m_hMin, m_left_space, 0, m_rcMin.Width() - m_rect.Width(), m_rcMin.Height(), TRUE); //设置最小化窗口的位置
m_rect.MoveToX(m_left_space + m_rcMin.Width() - m_rect.Width() + 2);
}
}
else
{
if (theApp.m_is_windows11_taskbar)
{
if (CWindowsSettingHelper::IsTaskbarCenterAlign())
{
if (theApp.m_taskbar_data.tbar_wnd_snap)
{
int taskbar_btn_num{ 1 }; //Win11任务栏“运行中的程序”左侧4个按钮(开始、搜索、任务视图、聊天)有几个显示。(“开始”按钮总是显示)
if (CWindowsSettingHelper::IsTaskbarSearchBtnShown())
taskbar_btn_num++;
if (CWindowsSettingHelper::IsTaskbarTaskViewBtnShown())
taskbar_btn_num++;
if (CWindowsSettingHelper::IsTaskbarChartBtnShown())
taskbar_btn_num++;
m_rect.MoveToX(m_rcMin.left - m_rect.Width() - 2 - theApp.DPI(44) * taskbar_btn_num); //每个按钮44像素
}
else
{
if (CWindowsSettingHelper::IsTaskbarWidgetsBtnShown())
m_rect.MoveToX(2 + theApp.DPI(theApp.m_cfg_data.taskbar_left_space_win11));
else
m_rect.MoveToX(2);
}
}
else
{
m_rect.MoveToX(2);
}
}
else
{
::MoveWindow(m_hMin, m_left_space + m_rect.Width(), 0, m_rcMin.Width() - m_rect.Width(), m_rcMin.Height(), TRUE);
m_rect.MoveToX(m_left_space);
}
}
m_rect.MoveToY((m_rcBar.Height() - m_rect.Height()) / 2);
if (theApp.m_taskbar_data.horizontal_arrange && theApp.m_win_version.IsWindows7())
m_rect.MoveToY(m_rect.top + theApp.DPI(1));
MoveWindow(m_rect);
}
}
else //当任务栏在屏幕在左侧或右侧时
{
//设置窗口大小
if (m_rcMin.Height() != m_min_bar_height) //如果最小化窗口的高度改变了,重新设置任务栏窗口的位置
{
m_rcMinOri = m_rcMin;
m_top_space = m_rcMin.top - m_rcBar.top;
m_min_bar_height = m_rcMin.Height() - m_rect.Height(); //保存最小化窗口高度
if (!theApp.m_taskbar_data.tbar_wnd_on_left)
{
::MoveWindow(m_hMin, 0, m_top_space, m_rcMin.Width(), m_rcMin.Height() - m_rect.Height(), TRUE); //设置最小化窗口的位置
m_rect.MoveToY(m_top_space + m_rcMin.Height() - m_rect.Height() + 2);
}
else
{
::MoveWindow(m_hMin, 0, m_top_space + m_rect.Height(), m_rcMin.Width(), m_rcMin.Height() - m_rect.Height(), TRUE); //设置最小化窗口的位置
m_rect.MoveToY(m_top_space);
}
m_rect.MoveToX((m_rcMin.Width() - m_window_width) / 2);
if (m_rect.left < theApp.DPI(2))
m_rect.MoveToX(theApp.DPI(2));
MoveWindow(m_rect);
}
}
CRect rect{ m_rect };
//如果窗口没有被成功嵌入到任务栏,窗口移动到了基于屏幕左上角的绝对位置,则修正窗口的位置
if (m_connot_insert_to_task_bar)
{
rect.MoveToXY(rect.left + m_rcBar.left, rect.top + m_rcBar.top);
this->MoveWindow(rect);
}
if (m_connot_insert_to_task_bar && ::GetForegroundWindow() == m_hTaskbar) //在窗口无法嵌入任务栏时,如果焦点设置在了任务栏上,则让窗口置顶
{
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); //设置置顶
}
return true;
}
void CTaskBarDlg::ApplyWindowTransparentColor()
{
#ifndef COMPILE_FOR_WINXP
if (theApp.m_is_windows11_taskbar) //Windows11下背景色不使用纯黑色,以解决深色模式下右键菜单无法弹出的问题
{
if (theApp.m_taskbar_data.transparent_color == 0 && theApp.m_taskbar_data.back_color == 0)
{
theApp.m_taskbar_data.transparent_color = 1;
theApp.m_taskbar_data.back_color = 1;
}
}
if ((theApp.m_taskbar_data.transparent_color != 0) && theApp.m_taksbar_transparent_color_enable)
{
SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(theApp.m_taskbar_data.transparent_color, 0, LWA_COLORKEY);
}
else
{
SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
}
#endif // !COMPILE_FOR_WINXP
}
void CTaskBarDlg::CheckTaskbarOnTopOrBottom()
{
CRect rect;
CRect rcMin;
CRect rcBar;
if (m_hTaskbar != 0)
{
::GetWindowRect(m_hMin, rcMin); //获得最小化窗口的区域
::GetWindowRect(m_hBar, rcBar); //获得二级容器的区域
if (m_left_space == 0)
m_left_space = rcMin.left - rcBar.left;
if (m_top_space == 0)
m_top_space = rcMin.top - rcBar.top;
::GetWindowRect(m_hTaskbar, rect); //获取任务栏的矩形区域
m_taskbar_on_top_or_bottom = (rect.Width() >= rect.Height()); //如果任务栏的宽度大于高度,则任务在屏幕的顶部或底部
}
else
{
m_taskbar_on_top_or_bottom = true;
}
}
CString CTaskBarDlg::GetMouseTipsInfo()
{
CString tip_info;
CString temp;
temp.Format(_T("%s: %s\r\n (%s: %s/%s: %s)"), CCommon::LoadText(IDS_TRAFFIC_USED_TODAY),
CCommon::KBytesToString((theApp.m_today_up_traffic + theApp.m_today_down_traffic) / 1024u),
CCommon::LoadText(IDS_UPLOAD), CCommon::KBytesToString(theApp.m_today_up_traffic / 1024u),
CCommon::LoadText(IDS_DOWNLOAD), CCommon::KBytesToString(theApp.m_today_down_traffic / 1024u)
);
tip_info += temp;
if (!IsItemShow(TDI_UP))
{
temp.Format(_T("\r\n%s: %s/s"), CCommon::LoadText(IDS_UPLOAD),
CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_main_wnd_data));
tip_info += temp;
}
if (!IsItemShow(TDI_DOWN))
{
temp.Format(_T("\r\n%s: %s/s"), CCommon::LoadText(IDS_DOWNLOAD),
CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_main_wnd_data));
tip_info += temp;
}
if (!IsItemShow(TDI_CPU))
{
temp.Format(_T("\r\n%s: %d %%"), CCommon::LoadText(IDS_CPU_USAGE), theApp.m_cpu_usage);
tip_info += temp;
}
if (!IsShowCpuMemory())
{
temp.Format(_T("\r\n%s: %s/%s"),
CCommon::LoadText(IDS_MEMORY_USAGE),
CCommon::KBytesToString(theApp.m_used_memory), CCommon::KBytesToString(theApp.m_total_memory));
}
if (!IsItemShow(TDI_MEMORY))
{
temp.Format(_T("\r\n%s: %s/%s (%d %%)"), CCommon::LoadText(IDS_MEMORY_USAGE),
CCommon::KBytesToString(theApp.m_used_memory),
CCommon::KBytesToString(theApp.m_total_memory), theApp.m_memory_usage);
tip_info += temp;
}
else
{
temp.Format(_T("\r\n%s: %s/%s"), CCommon::LoadText(IDS_MEMORY_USAGE),
CCommon::KBytesToString(theApp.m_used_memory),
CCommon::KBytesToString(theApp.m_total_memory));
tip_info += temp;
}
#ifndef WITHOUT_TEMPERATURE
CTrafficMonitorDlg* pMainWnd = dynamic_cast(theApp.m_pMainWnd);
if (pMainWnd->IsTemperatureNeeded())
{
if (!IsItemShow(TDI_GPU_USAGE) && theApp.m_gpu_usage >= 0)
{
temp.Format(_T("\r\n%s: %d %%"), CCommon::LoadText(IDS_GPU_USAGE), theApp.m_gpu_usage);
tip_info += temp;
}
if (!IsItemShow(TDI_CPU_TEMP) && theApp.m_cpu_temperature > 0)
{
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_CPU_TEMPERATURE), CCommon::TemperatureToString(theApp.m_cpu_temperature, theApp.m_taskbar_data));
tip_info += temp;
}
if (!IsItemShow(TDI_CPU_FREQ) && theApp.m_cpu_freq > 0)
{
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_CPU_FREQ), CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_taskbar_data));
tip_info += temp;
}
if (!IsItemShow(TDI_GPU_TEMP) && theApp.m_gpu_temperature > 0)
{
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_GPU_TEMPERATURE), CCommon::TemperatureToString(theApp.m_gpu_temperature, theApp.m_taskbar_data));
tip_info += temp;
}
if (!IsItemShow(TDI_HDD_TEMP) && theApp.m_hdd_temperature > 0)
{
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_HDD_TEMPERATURE), CCommon::TemperatureToString(theApp.m_hdd_temperature, theApp.m_taskbar_data));
tip_info += temp;
}
if (!IsItemShow(TDI_MAIN_BOARD_TEMP) && theApp.m_main_board_temperature > 0)
{
temp.Format(_T("\r\n%s: %s"), CCommon::LoadText(IDS_MAINBOARD_TEMPERATURE), CCommon::TemperatureToString(theApp.m_main_board_temperature, theApp.m_taskbar_data));
tip_info += temp;
}
if (!IsItemShow(TDI_HDD_USAGE) && theApp.m_hdd_usage >= 0)
{
temp.Format(_T("\r\n%s: %d %%"), CCommon::LoadText(IDS_HDD_USAGE), theApp.m_hdd_usage);
tip_info += temp;
}
}
#endif
//添加插件项目的鼠标提示
tip_info += theApp.GetPlauginTooltipInfo().c_str();
return tip_info;
}
void CTaskBarDlg::SetTextFont()
{
//如果m_font已经关联了一个字体资源对象,则释放它
if (m_font.m_hObject)
{
m_font.DeleteObject();
}
//创建新的字体
theApp.m_taskbar_data.font.Create(m_font, theApp.GetDpi());
}
void CTaskBarDlg::ApplySettings()
{
SetTextFont();
CalculateWindowSize();
}
void CTaskBarDlg::CalculateWindowSize()
{
bool horizontal_arrange = theApp.m_taskbar_data.horizontal_arrange && m_taskbar_on_top_or_bottom;
if (theApp.m_taskbar_data.m_tbar_display_item == 0)
theApp.m_taskbar_data.m_tbar_display_item |= TDI_UP; //至少显示一项
m_item_widths.clear();
//显示项目的宽度
std::map item_widths;
m_pDC->SelectObject(&m_font);
//计算标签宽度
//const auto& item_map = theApp.m_taskbar_data.disp_str.GetAllItems();
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
if (iter->is_plugin)
{
auto plugin = iter->plugin_item;
if (plugin != nullptr)
{
int& label_width{ item_widths[*iter].label_width };
if (plugin->IsCustomDraw())
{
label_width = 0;
}
else
{
CString lable_text = theApp.m_taskbar_data.disp_str.Get(plugin).c_str();
if (!lable_text.IsEmpty())
lable_text += L' ';
label_width = m_pDC->GetTextExtent(lable_text).cx;
}
}
}
else
{
item_widths[*iter].label_width = m_pDC->GetTextExtent(theApp.m_taskbar_data.disp_str.Get(*iter).c_str()).cx;
}
}
//计算数值部分宽度
//计算显示上传下载部分所需要的宽度
CString sample_str;
int value_width{};
wstring digits(theApp.m_taskbar_data.digits_number, L'8'); //根据数据位数生成指定个数的“8”
bool hide_unit{ theApp.m_taskbar_data.hide_unit && theApp.m_taskbar_data.speed_unit != SpeedUnit::AUTO };
if (theApp.m_taskbar_data.speed_short_mode)
{
if (hide_unit)
sample_str.Format(_T("%s."), digits.c_str());
else
sample_str.Format(_T("%s.M/s"), digits.c_str());
}
else
{
if (hide_unit)
sample_str.Format(_T("%s.8"), digits.c_str());
else
sample_str.Format(_T("%s.8MB/s"), digits.c_str());
}
if (!hide_unit && theApp.m_taskbar_data.separate_value_unit_with_space)
sample_str += _T(' ');
if (theApp.m_taskbar_data.speed_short_mode && !theApp.m_taskbar_data.unit_byte && !theApp.m_taskbar_data.hide_unit)
sample_str += _T('b');
value_width = m_pDC->GetTextExtent(sample_str).cx; //计算使用当前字体显示文本需要的宽度值
item_widths[TDI_UP].value_width = value_width;
item_widths[TDI_DOWN].value_width = value_width;
item_widths[TDI_TOTAL_SPEED].value_width = value_width;
//计算显示CPU、内存部分所需要的宽度
CString str;
if (theApp.m_taskbar_data.hide_percent)
{
str = _T("99");
}
else if (theApp.m_taskbar_data.separate_value_unit_with_space)
{
str = _T("99 %");
}
else
{
str = _T("99%");
}
value_width = m_pDC->GetTextExtent(str).cx;
//内存显示的宽度
int memory_width{ value_width };
if (theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_USED || theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE)
{
if (theApp.m_taskbar_data.separate_value_unit_with_space)
str = _T("19.99 GB");
else
str = _T("19.99GB");
memory_width = m_pDC->GetTextExtent(str).cx;
}
item_widths[TDI_CPU].value_width = value_width;
item_widths[TDI_MEMORY].value_width = memory_width;
item_widths[TDI_GPU_USAGE].value_width = value_width;
item_widths[TDI_HDD_USAGE].value_width = value_width;
item_widths[TDI_CPU_FREQ].value_width = m_pDC->GetTextExtent(_T("1.00 GHz")).cx;
//计算温度显示的宽度
if (theApp.m_taskbar_data.separate_value_unit_with_space)
str = _T("99 °C");
else
str = _T("99°C");
value_width = m_pDC->GetTextExtent(str).cx;
value_width += theApp.DPI(2);
item_widths[TDI_CPU_TEMP].value_width = value_width;
item_widths[TDI_GPU_TEMP].value_width = value_width;
item_widths[TDI_HDD_TEMP].value_width = value_width;
item_widths[TDI_MAIN_BOARD_TEMP].value_width = value_width;
//计算插件项目的宽度
for (const auto& plugin : theApp.m_plugins.GetPluginItems())
{
int& value_width{ item_widths[plugin].value_width };
if (plugin != nullptr && theApp.m_taskbar_data.plugin_display_item.Contains(plugin->GetItemId()))
{
if (plugin->IsCustomDraw())
{
value_width = theApp.m_plugins.GetItemWidth(plugin, m_pDC);
}
else
{
value_width = m_pDC->GetTextExtent(plugin->GetItemValueSampleText()).cx;
}
}
}
auto item_order{ theApp.m_taskbar_data.item_order.GetAllDisplayItemsWithOrder() };
for (const auto& item : item_order)
{
if (theApp.IsTaksbarItemDisplayed(item))
{
ItemWidthInfo width_info = item;
width_info.item_width = item_widths[item];
m_item_widths.push_back(width_info);
}
}
int item_count = static_cast(m_item_widths.size());
//计算窗口总宽度
if (IsTasksbarOnTopOrBottom()) //任务栏在桌面的顶部或底部时
{
m_window_width = 0;
if (theApp.m_taskbar_data.horizontal_arrange) //水平排列时
{
for (auto iter = m_item_widths.begin(); iter != m_item_widths.end(); ++iter)
{
m_window_width += iter->item_width.TotalWidth();
}
m_window_width += theApp.DPI(theApp.m_taskbar_data.item_space) * item_count; //加上每个标签间的空隙
}
else //非水平排列时,每两个一组排列
{
int index = 0;
int width0;
for (auto iter = m_item_widths.begin(); iter != m_item_widths.end(); ++iter)
{
if (index % 2 == 0)
{
width0 = iter->item_width.TotalWidth();
}
else
{
m_window_width += max(width0, iter->item_width.TotalWidth());
}
if (item_count % 2 == 1 && index == item_count - 1) //项目数为奇数时加上最后一个的宽度
{
m_window_width += iter->item_width.MaxWidth();
}
index++;
}
m_window_width += theApp.DPI(theApp.m_taskbar_data.item_space) * ((item_count + 1) / 2 + 1); //加上每个标签间的空隙
}
}
else //任务栏在桌面两侧时
{
m_window_width = 0;
//所有标签中最大的宽度即为窗口宽度
for (auto iter = m_item_widths.begin(); iter != m_item_widths.end(); ++iter)
{
if (m_window_width < iter->item_width.TotalWidth())
m_window_width = iter->item_width.TotalWidth();
}
}
//计算窗口高度
if (IsTasksbarOnTopOrBottom())
{
if (!horizontal_arrange)
m_window_height = TASKBAR_WND_HEIGHT;
else
m_window_height = TASKBAR_WND_HEIGHT * 2 / 3;
}
else
{
m_window_height = TASKBAR_WND_HEIGHT / 2 * item_count;
m_window_height += (theApp.DPI(theApp.m_taskbar_data.item_space) * item_count); //加上每个标签间的空隙
}
m_rect.right = m_rect.left + m_window_width;
m_rect.bottom = m_rect.top + m_window_height;
}
void CTaskBarDlg::SetToolTipsTopMost()
{
m_tool_tips.SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
void CTaskBarDlg::UpdateToolTips()
{
if (theApp.m_taskbar_data.show_tool_tip && IsWindow(m_tool_tips.GetSafeHwnd()))
{
CString tip_info;
tip_info = GetMouseTipsInfo();
m_tool_tips.UpdateTipText(tip_info, this);
}
}
bool CTaskBarDlg::IsItemShow(DisplayItem item)
{
return (theApp.m_taskbar_data.m_tbar_display_item & item);
}
bool CTaskBarDlg::IsShowCpuMemory()
{
return ((theApp.m_taskbar_data.m_tbar_display_item & TDI_CPU) || (theApp.m_taskbar_data.m_tbar_display_item & TDI_MEMORY));
}
bool CTaskBarDlg::IsShowNetSpeed()
{
return ((theApp.m_taskbar_data.m_tbar_display_item & TDI_UP) || (theApp.m_taskbar_data.m_tbar_display_item & TDI_DOWN));
}
BOOL CTaskBarDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
//设置隐藏任务栏图标
ModifyStyleEx(0, WS_EX_TOOLWINDOW);
m_pDC = GetDC();
//设置字体
SetTextFont();
m_pDC->SelectObject(&m_font);
m_hTaskbar = ::FindWindow(L"Shell_TrayWnd", NULL); //寻找类名是Shell_TrayWnd的窗口句柄
m_hBar = ::FindWindowEx(m_hTaskbar, 0, L"ReBarWindow32", NULL); //寻找二级容器的句柄
m_hMin = ::FindWindowEx(m_hBar, 0, L"MSTaskSwWClass", NULL); //寻找最小化窗口的句柄
//在“Shell_TrayWnd”的子窗口找到类名为“Windows.UI.Composition.DesktopWindowContentBridge”的窗口则认为是Windows11的任务栏
if (theApp.m_win_version.IsWindows11OrLater())
{
theApp.m_is_windows11_taskbar = (::FindWindowExW(m_hTaskbar, 0, L"Windows.UI.Composition.DesktopWindowContentBridge", NULL) != NULL);
}
//设置窗口透明色
ApplyWindowTransparentColor();
::GetWindowRect(m_hMin, m_rcMin); //获得最小化窗口的区域
::GetWindowRect(m_hBar, m_rcBar); //获得二级容器的区域
m_left_space = m_rcMin.left - m_rcBar.left;
m_top_space = m_rcMin.top - m_rcBar.top;
CheckTaskbarOnTopOrBottom();
CalculateWindowSize();
m_rect.SetRectEmpty();
m_rect.bottom = m_window_height;
m_rect.right = m_rect.left + m_window_width;
m_connot_insert_to_task_bar = !(::SetParent(this->m_hWnd, m_hBar)); //把程序窗口设置成任务栏的子窗口
m_error_code = GetLastError();
AdjustWindowPos();
SetBackgroundColor(theApp.m_taskbar_data.back_color);
//初始化鼠标提示
if (IsWindow(GetSafeHwnd()) && m_tool_tips.Create(this, TTS_ALWAYSTIP) && IsWindow(m_tool_tips.GetSafeHwnd()))
{
m_tool_tips.SetMaxTipWidth(600);
m_tool_tips.AddTool(this, _T(""));
SetToolTipsTopMost(); //设置提示信息总是置顶
}
//SetTimer(TASKBAR_TIMER, 100, NULL);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CTaskBarDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
//SaveConfig();
//关闭所有以任务栏窗口为父窗口的窗口
for (const auto& item : CBaseDialog::AllUniqueHandels())
{
HWND parent = ::GetParent(item.second);
if (parent == GetSafeHwnd())
{
::SendMessage(item.second, WM_COMMAND, IDCANCEL, 0);
}
}
DestroyWindow();
//程序关闭的时候,把最小化窗口的width恢复回去
CheckTaskbarOnTopOrBottom();
if (m_taskbar_on_top_or_bottom)
::MoveWindow(m_hMin, m_left_space, 0, m_rcMinOri.Width(), m_rcMinOri.Height(), TRUE);
else
::MoveWindow(m_hMin, 0, m_top_space, m_rcMinOri.Width(), m_rcMinOri.Height(), TRUE);
//CDialogEx::OnCancel();
}
void CTaskBarDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_menu_popuped = true;
m_tool_tips.Pop();
if (CheckClickedItem(point) && m_clicked_item.is_plugin && m_clicked_item.plugin_item != nullptr)
{
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(m_clicked_item.plugin_item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 3)
{
if (m_clicked_item.plugin_item->OnMouseEvent(IPluginItem::MT_RCLICKED, point.x, point.y, (void*)GetSafeHwnd(), IPluginItem::MF_TASKBAR_WND) != 0)
return;
}
}
CPoint point1; //定义一个用于确定光标位置的位置
GetCursorPos(&point1); //获取当前光标的位置,以便使得菜单可以跟随光标
CMenu* pMenu = theApp.m_taskbar_menu.GetSubMenu(0);
if (pMenu != nullptr)
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point1.x, point1.y, this); //在指定位置显示弹出菜单
CDialogEx::OnRButtonUp(nFlags, point1);
}
void CTaskBarDlg::OnInitMenu(CMenu* pMenu)
{
CDialogEx::OnInitMenu(pMenu);
// TODO: 在此处添加消息处理程序代码
pMenu->CheckMenuItem(ID_SHOW_MAIN_WND, MF_BYCOMMAND | (!theApp.m_cfg_data.m_hide_main_window ? MF_CHECKED : MF_UNCHECKED));
pMenu->EnableMenuItem(ID_SELECT_ALL_CONNECTION, MF_BYCOMMAND | (theApp.m_general_data.show_all_interface ? MF_GRAYED : MF_ENABLED));
pMenu->EnableMenuItem(ID_CHECK_UPDATE, MF_BYCOMMAND | (theApp.IsCheckingForUpdate() ? MF_GRAYED : MF_ENABLED));
//pMenu->SetDefaultItem(ID_NETWORK_INFO);
//设置默认菜单项
switch (theApp.m_taskbar_data.double_click_action)
{
case DoubleClickAction::CONNECTION_INFO:
pMenu->SetDefaultItem(ID_NETWORK_INFO);
break;
case DoubleClickAction::HISTORY_TRAFFIC:
pMenu->SetDefaultItem(ID_TRAFFIC_HISTORY);
break;
case DoubleClickAction::SHOW_MORE_INFO:
pMenu->SetDefaultItem(ID_SHOW_CPU_MEMORY2);
break;
case DoubleClickAction::OPTIONS:
pMenu->SetDefaultItem(ID_OPTIONS2);
break;
case DoubleClickAction::TASK_MANAGER:
pMenu->SetDefaultItem(ID_OPEN_TASK_MANAGER);
break;
default:
pMenu->SetDefaultItem(-1);
break;
}
::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_TASKBAR_MENU_POPED_UP, 0, 0); //通知主窗口菜单已弹出
}
BOOL CTaskBarDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
//屏蔽按回车键和ESC键退出
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE;
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) return TRUE;
if (theApp.m_taskbar_data.show_tool_tip && !m_menu_popuped && IsWindow(m_tool_tips.GetSafeHwnd()) && (pMsg->message == WM_LBUTTONDOWN ||
pMsg->message == WM_LBUTTONUP ||
pMsg->message == WM_MOUSEMOVE))
{
m_tool_tips.RelayEvent(pMsg);
}
return CDialogEx::PreTranslateMessage(pMsg);
}
void CTaskBarDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnMouseMove(nFlags, point);
}
void CTaskBarDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (CheckClickedItem(point) && m_clicked_item.is_plugin && m_clicked_item.plugin_item != nullptr)
{
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(m_clicked_item.plugin_item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 3)
{
if (m_clicked_item.plugin_item->OnMouseEvent(IPluginItem::MT_DBCLICKED, point.x, point.y, (void*)GetSafeHwnd(), IPluginItem::MF_TASKBAR_WND) != 0)
return;
}
}
switch (theApp.m_taskbar_data.double_click_action)
{
case DoubleClickAction::CONNECTION_INFO:
SendMessage(WM_COMMAND, ID_NETWORK_INFO); //双击后弹出“连接详情”对话框
break;
case DoubleClickAction::HISTORY_TRAFFIC:
SendMessage(WM_COMMAND, ID_TRAFFIC_HISTORY); //双击后弹出“历史流量统计”对话框
break;
case DoubleClickAction::SHOW_MORE_INFO:
PostMessage(WM_COMMAND, ID_SHOW_CPU_MEMORY2); //切换显示CPU和内存利用率
break;
case DoubleClickAction::OPTIONS:
SendMessage(WM_COMMAND, ID_OPTIONS2); //双击后弹出“选项设置”对话框
break;
case DoubleClickAction::TASK_MANAGER:
ShellExecuteW(NULL, _T("open"), (theApp.m_system_dir + L"\\Taskmgr.exe").c_str(), NULL, NULL, SW_NORMAL); //打开任务管理器
break;
case DoubleClickAction::SEPCIFIC_APP:
ShellExecuteW(NULL, _T("open"), (theApp.m_taskbar_data.double_click_exe).c_str(), NULL, NULL, SW_NORMAL); //打开指定程序,默认任务管理器
break;
default:
break;
}
//CDialogEx::OnLButtonDblClk(nFlags, point);
}
void CTaskBarDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//if (nIDEvent == TASKBAR_TIMER)
//{
// AdjustWindowPos();
// //ShowInfo();
// Invalidate(FALSE);
//}
CDialogEx::OnTimer(nIDEvent);
}
BOOL CTaskBarDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
UINT uMsg = LOWORD(wParam);
if (uMsg == ID_SELECT_ALL_CONNECTION || uMsg == ID_SELETE_CONNECTION
|| (uMsg > ID_SELECT_ALL_CONNECTION && uMsg <= ID_SELETE_CONNECTION_MAX))
{
::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_COMMAND, wParam, lParam); //如果点击了“选择网络连接”子菜单项,将消息转发到主窗口
return TRUE;
}
//选择了“显示项目”中的插件项目
if (uMsg >= ID_SHOW_PLUGIN_ITEM_START && uMsg <= ID_SHOW_PLUGIN_ITEM_MAX)
{
IPluginItem* item = theApp.m_plugins.GetItemByIndex(uMsg - ID_SHOW_PLUGIN_ITEM_START);
if (item != nullptr)
{
bool displayed = theApp.m_taskbar_data.plugin_display_item.Contains(item->GetItemId());
theApp.m_taskbar_data.plugin_display_item.SetStrContained(item->GetItemId(), !displayed);
::PostMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_REOPEN_TASKBAR_WND, 0, 0);
}
}
return CDialogEx::OnCommand(wParam, lParam);
}
void CTaskBarDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialogEx::OnPaint()
ShowInfo(&dc);
}
void CTaskBarDlg::AddHisToList(DisplayItem item_type, int current_usage_percent)
{
int& data_count{ m_history_data_count[item_type] };
std::list& list = m_map_history_data[item_type];
//将数累加到加链表的头部,直到添加的数据数量达到TASKBAR_GRAPH_STEP的倍数时计算平均数
if (data_count % TASKBAR_GRAPH_STEP == 0)
{
//计算前面累加的TASKBAR_GRAPH_STEP个数据的平均数
if (!list.empty())
list.front() /= TASKBAR_GRAPH_STEP;
//将新的数据添加到末尾
list.push_front(current_usage_percent);
}
else
{
//数累加到加链表的头部
list.front() += current_usage_percent;
}
size_t graph_max_length = m_item_rects[item_type].Width();
//判断是否超过最大长度,如果超过,将链表尾部数据移除
if (list.size() > graph_max_length)
{
list.pop_back();
}
data_count++;
}
int CTaskBarDlg::CalculateNetspeedPercent(unsigned __int64 net_speed)
{
int percet = 0;
unsigned __int64 max_value{ theApp.m_taskbar_data.GetNetspeedFigureMaxValueInBytes() };
if (net_speed >= max_value)
percet = 100;
else if (max_value > 0)
percet = net_speed * 100 / max_value;
return percet;
}
bool CTaskBarDlg::CheckClickedItem(CPoint point)
{
for (const auto& item : m_item_rects)
{
if (item.second.PtInRect(point))
{
m_clicked_item = item.first;
return true;
}
}
m_clicked_item = TDI_UP;
return false;
}
void CTaskBarDlg::TryDrawGraph(CDrawCommon& drawer, const CRect& value_rect, DisplayItem item_type)
{
std::list& list = m_map_history_data[item_type];
if (theApp.m_taskbar_data.show_graph_dashed_box)
drawer.DrawRectOutLine(value_rect, theApp.m_taskbar_data.status_bar_color, 1, true);
int i{ -1 };
for (const auto& item : list)
{
i++;
if (i == 0) //不绘制链表头部的数据,因为在累加中,还未取平均数
continue;
if (i >= value_rect.Width())
break;
//从右往左画线
CPoint start_point = CPoint(value_rect.right - i, value_rect.bottom);
int height = item * value_rect.Height() / 100;
drawer.DrawLine(start_point, height, theApp.m_taskbar_data.status_bar_color);
}
}
void CTaskBarDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_TASKBAR_WND_CLOSED, 0, 0);
CDialogEx::OnClose();
}
void CTaskBarDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (CheckClickedItem(point) && m_clicked_item.is_plugin && m_clicked_item.plugin_item != nullptr)
{
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(m_clicked_item.plugin_item);
if (plugin != nullptr && plugin->GetAPIVersion() >= 3)
{
if (m_clicked_item.plugin_item->OnMouseEvent(IPluginItem::MT_LCLICKED, point.x, point.y, (void*)GetSafeHwnd(), IPluginItem::MF_TASKBAR_WND) != 0)
return;
}
}
CDialogEx::OnLButtonUp(nFlags, point);
}
afx_msg LRESULT CTaskBarDlg::OnExitmenuloop(WPARAM wParam, LPARAM lParam)
{
m_menu_popuped = false;
return 0;
}
================================================
FILE: TrafficMonitor/TaskBarDlg.h
================================================
#pragma once
#include "Common.h"
#include "afxwin.h"
#include "DrawCommon.h"
#include "IniHelper.h"
#include "CommonData.h"
#include "TaskbarItemOrderHelper.h"
#include
// CTaskBarDlg 对话框
#define TASKBAR_WND_HEIGHT theApp.DPI(32) //任务栏窗口的高度
#define WM_TASKBAR_MENU_POPED_UP (WM_USER + 1004) //定义任务栏窗口右键菜单弹出时发出的消息
//#define TASKBAR_GRAPH_MAX_LEN 600 //历史数据存储最大长度
#define TASKBAR_GRAPH_STEP 5 //几秒钟画一条线
class CTaskBarDlg : public CDialogEx
{
DECLARE_DYNAMIC(CTaskBarDlg)
public:
CTaskBarDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CTaskBarDlg();
CToolTipCtrl m_tool_tips;
void ShowInfo(CDC* pDC); //将信息绘制到控件上
void TryDrawStatusBar(CDrawCommon& drawer, const CRect& rect_bar, int usage_percent); //绘制CPU/内存状态条
void TryDrawGraph(CDrawCommon& drawer, const CRect& value_rect, DisplayItem item_type); // 绘制CPU/内存动态图
bool AdjustWindowPos(); //设置窗口在任务栏中的位置
void ApplyWindowTransparentColor();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TASK_BAR_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
HWND m_hTaskbar; //任务栏窗口句柄
HWND m_hBar; //任务栏窗口二级容器的句柄
HWND m_hMin; //最小化窗口的句柄
CRect m_rcBar; //初始状态时任务栏窗口的矩形区域
CRect m_rcMin; //最小化窗口的矩形区域
CRect m_rcMinOri; //初始状态时最小化窗口的矩形区域
CRect m_rect; //当前窗口的矩形区域
int m_window_width{};
int m_window_height{};
//任务栏各个部分的宽度
struct ItemWidth
{
int label_width{}; //标签部分宽度
int value_width{}; //数值部分宽度
int TotalWidth() const //总宽度
{
return label_width + value_width;
}
int MaxWidth() const //最大宽度
{
return max(label_width, value_width);
}
};
struct ItemWidthInfo : public CommonDisplayItem
{
ItemWidth item_width;
ItemWidthInfo()
{}
ItemWidthInfo(const CommonDisplayItem& item)
: CommonDisplayItem(item)
{}
};
std::vector m_item_widths; //任务栏窗口每个部分的宽度
std::map m_item_rects; //任务栏窗口每个部分的矩形区域
CommonDisplayItem m_clicked_item; //鼠标点击的任务栏项目
int m_min_bar_width; //最小化窗口缩小宽度后的宽度
int m_min_bar_height; //最小化窗口缩小高度后的高度(用于任务栏在屏幕左侧或右侧时)
std::map> m_map_history_data; //保存各项数据历史数据的链表,链表保存按照时间顺序,越靠近头部数据越新
std::map m_history_data_count; //统计添加到历史数据链表的次数
int m_left_space{}; //最小化窗口和二级窗口窗口左侧的边距
int m_top_space{}; //最小化窗口和二级窗口窗口顶部的边距(用于任务栏在屏幕左侧或右侧时)
bool m_connot_insert_to_task_bar{ false }; //如果窗口无法嵌入任务栏,则为true
bool m_taskbar_on_top_or_bottom{ true }; //如果任务栏在屏幕顶部或底部,则为ture
int m_error_code{};
bool m_menu_popuped{ false }; //指示当前是否有菜单处于弹出状态
CFont m_font; //字体
CDC* m_pDC{}; //窗口的DC,用来计算窗口的宽度
void CheckTaskbarOnTopOrBottom(); //检查任务栏是否在屏幕的顶部或底部,并将结果保存在m_taskbar_on_top_or_bottom中
CString GetMouseTipsInfo(); //获取鼠标提示
void AddHisToList(DisplayItem item_type, int current_usage_percent); //将当前利用率数值添加进链表
int CalculateNetspeedPercent(unsigned __int64 net_speed); //计算网速占网速占用图的最大值的百分比
//判断一个点在哪个显示项目的区域内,并保存到m_clicked_item。如果返回false,则该点不在任何一个项目的区域内,否则返回true
bool CheckClickedItem(CPoint point);
//绘制任务栏窗口中的一个显示项目
// drawer: 绘图类的对象
// type: 项目的类型
// rect: 绘制矩形区域
// label_width: 标签区域的宽度
// vertical: 如果为true,则标签和数值上下显示
void DrawDisplayItem(CDrawCommon& drawer, DisplayItem type, CRect rect, int label_width, bool vertical = false);
//绘制任务栏窗口中的一个插件项目
// drawer: 绘图类的对象
// item: 插件显示项目的指针
// rect: 绘制矩形区域
// label_width: 标签区域的宽度
// vertical: 如果为true,则标签和数值上下显示
void DrawPluginItem(CDrawCommon& drawer, IPluginItem* item, CRect rect, int label_width, bool vertical = false);
void MoveWindow(CRect rect);
public:
void SetTextFont();
void ApplySettings();
void CalculateWindowSize(); //计算窗口每部分的大小,及各个部分的宽度。窗口大小保存到m_window_width和m_window_height中,各部分宽度保存到m_item_widths中
void SetToolTipsTopMost(); //设置鼠标提示置顶
void UpdateToolTips();
bool GetCannotInsertToTaskBar() const { return m_connot_insert_to_task_bar; }
int GetErrorCode() const { return m_error_code; }
bool IsTasksbarOnTopOrBottom() { return m_taskbar_on_top_or_bottom; }
static bool IsItemShow(DisplayItem item);
static bool IsShowCpuMemory();
static bool IsShowNetSpeed();
CommonDisplayItem GetClickedItem() const { return m_clicked_item; }
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnCancel();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
//afx_msg void OnSetBackgroundColor();
//afx_msg void OnSetTextColor();
afx_msg void OnInitMenu(CMenu* pMenu);
virtual BOOL PreTranslateMessage(MSG* pMsg);
//afx_msg void OnSetFont();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT_PTR nIDEvent);
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
afx_msg void OnPaint();
afx_msg void OnClose();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
protected:
afx_msg LRESULT OnExitmenuloop(WPARAM wParam, LPARAM lParam);
};
================================================
FILE: TrafficMonitor/TaskBarSettingsDlg.cpp
================================================
// TaskBarSettingsDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "TaskBarSettingsDlg.h"
#include "afxdialogex.h"
#include "CMFCColorDialogEx.h"
#include "CAutoAdaptSettingsDlg.h"
#include "DisplayTextSettingDlg.h"
#include "SetItemOrderDlg.h"
#include "WindowsSettingHelper.h"
// CTaskBarSettingsDlg 对话框
IMPLEMENT_DYNAMIC(CTaskBarSettingsDlg, CTabDlg)
CTaskBarSettingsDlg::CTaskBarSettingsDlg(CWnd* pParent /*=NULL*/)
: CTabDlg(IDD_TASKBAR_SETTINGS_DIALOG, pParent)
{
}
CTaskBarSettingsDlg::~CTaskBarSettingsDlg()
{
}
bool CTaskBarSettingsDlg::IsStyleModified()
{
bool modified{};
modified |= (theApp.m_taskbar_data.text_colors != m_data.text_colors);
modified |= (theApp.m_taskbar_data.back_color != m_data.back_color);
modified |= (theApp.m_taskbar_data.transparent_color != m_data.transparent_color);
modified |= (theApp.m_taskbar_data.status_bar_color != m_data.status_bar_color);
modified |= (theApp.m_taskbar_data.specify_each_item_color != m_data.specify_each_item_color);
return modified && m_style_modified;
}
void CTaskBarSettingsDlg::DrawStaticColor()
{
//CCommon::FillStaticColor(m_text_color_static, m_data.text_color);
//CCommon::FillStaticColor(m_back_color_static, m_data.back_color);
if (m_data.specify_each_item_color)
{
int color_num{};
#ifdef WITHOUT_TEMPERATURE
color_num = 8;
#else
color_num = 16;
#endif
int i{};
m_text_color_static.SetColorNum(color_num);
for (const auto& item : m_data.text_colors)
{
m_text_color_static.SetFillColor(i, item.second.label);
m_text_color_static.SetFillColor(i + 1, item.second.value);
i += 2;
}
m_text_color_static.Invalidate();
}
else if (!m_data.text_colors.empty())
{
m_text_color_static.SetFillColor(m_data.text_colors.begin()->second.label);
}
m_back_color_static.SetFillColor(m_data.back_color);
//m_trans_color_static.SetFillColor(m_data.transparent_color);
m_status_bar_color_static.SetFillColor(m_data.status_bar_color);
}
void CTaskBarSettingsDlg::IniUnitCombo()
{
m_unit_combo.ResetContent();
m_unit_combo.AddString(CCommon::LoadText(IDS_AUTO));
if (m_data.unit_byte)
{
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" KB/s")));
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" MB/s")));
}
else
{
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" Kb/s")));
m_unit_combo.AddString(CCommon::LoadText(IDS_FIXED_AS, _T(" Mb/s")));
}
m_unit_combo.SetCurSel(static_cast(m_data.speed_unit));
}
void CTaskBarSettingsDlg::ApplyDefaultStyle(int index)
{
theApp.m_taskbar_default_style.ApplyDefaultStyle(index, m_data);
DrawStaticColor();
((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->SetCheck(m_data.specify_each_item_color);
m_background_transparent_chk.SetCheck(IsTaskbarTransparent());
}
void CTaskBarSettingsDlg::ModifyDefaultStyle(int index)
{
theApp.m_taskbar_default_style.ModifyDefaultStyle(index, m_data);
}
void CTaskBarSettingsDlg::EnableControl()
{
bool exe_path_enable = (m_data.double_click_action == DoubleClickAction::SEPCIFIC_APP);
ShowDlgCtrl(IDC_EXE_PATH_STATIC, exe_path_enable);
ShowDlgCtrl(IDC_EXE_PATH_EDIT, exe_path_enable);
ShowDlgCtrl(IDC_BROWSE_BUTTON, exe_path_enable);
EnableDlgCtrl(IDC_AUTO_ADAPT_SETTINGS_BUTTON, m_data.auto_adapt_light_theme);
EnableDlgCtrl(IDC_SHOW_DASHED_BOX, m_data.show_status_bar || m_data.show_netspeed_figure);
m_status_bar_color_static.EnableWindow(m_data.show_status_bar || m_data.show_netspeed_figure);
EnableDlgCtrl(IDC_CM_GRAPH_BAR_RADIO, m_data.show_status_bar || m_data.show_netspeed_figure);
EnableDlgCtrl(IDC_CM_GRAPH_PLOT_RADIO, m_data.show_status_bar || m_data.show_netspeed_figure);
EnableDlgCtrl(IDC_NET_SPEED_FIGURE_MAX_VALUE_EDIT, m_data.show_netspeed_figure);
EnableDlgCtrl(IDC_NET_SPEED_FIGURE_MAX_VALUE_UNIT_COMBO, m_data.show_netspeed_figure);
//EnableDlgCtrl(IDC_TASKBAR_WND_SNAP_CHECK, theApp.m_win_version.IsWindows11OrLater() && !m_data.tbar_wnd_on_left);
}
void CTaskBarSettingsDlg::SetTaskabrTransparent(bool transparent)
{
CTaskbarDefaultStyle::SetTaskabrTransparent(transparent, m_data);
}
bool CTaskBarSettingsDlg::IsTaskbarTransparent()
{
return CTaskbarDefaultStyle::IsTaskbarTransparent(m_data);
}
void CTaskBarSettingsDlg::SetControlMouseWheelEnable(bool enable)
{
m_unit_combo.SetMouseWheelEnable(enable);
m_double_click_combo.SetMouseWheelEnable(enable);
m_digit_number_combo.SetMouseWheelEnable(enable);
m_font_size_edit.SetMouseWheelEnable(enable);
m_memory_display_combo.SetMouseWheelEnable(enable);
m_item_space_edit.SetMouseWheelEnable(enable);
m_net_speed_figure_max_val_edit.SetMouseWheelEnable(enable);
m_net_speed_figure_max_val_unit_combo.SetMouseWheelEnable(enable);
}
void CTaskBarSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_TEXT_COLOR_STATIC1, m_text_color_static);
DDX_Control(pDX, IDC_TEXT_COLOR_STATIC2, m_back_color_static);
DDX_Control(pDX, IDC_TEXT_COLOR_STATIC3, m_status_bar_color_static);
CTabDlg::DoDataExchange(pDX);
DDX_Control(pDX, IDC_UNIT_COMBO, m_unit_combo);
DDX_Control(pDX, IDC_HIDE_UNIT_CHECK, m_hide_unit_chk);
DDX_Control(pDX, IDC_FONT_SIZE_EDIT1, m_font_size_edit);
DDX_Control(pDX, IDC_DOUBLE_CLICK_COMBO, m_double_click_combo);
DDX_Control(pDX, IDC_DIGIT_NUMBER_COMBO, m_digit_number_combo);
//DDX_Control(pDX, IDC_TRANSPARENT_COLOR_STATIC, m_trans_color_static);
DDX_Control(pDX, IDC_BACKGROUND_TRANSPARENT_CHECK, m_background_transparent_chk);
DDX_Control(pDX, IDC_AUTO_ADAPT_LIGHT_THEME_CHECK, m_atuo_adapt_light_theme_chk);
DDX_Control(pDX, IDC_AUTO_SET_BACK_COLOR_CHECK, m_auto_set_back_color_chk);
DDX_Control(pDX, IDC_MEMORY_DISPLAY_COMBO, m_memory_display_combo);
DDX_Control(pDX, IDC_ITEM_SPACE_EDIT, m_item_space_edit);
DDX_Control(pDX, IDC_NET_SPEED_FIGURE_MAX_VALUE_EDIT, m_net_speed_figure_max_val_edit);
DDX_Control(pDX, IDC_NET_SPEED_FIGURE_MAX_VALUE_UNIT_COMBO, m_net_speed_figure_max_val_unit_combo);
}
BEGIN_MESSAGE_MAP(CTaskBarSettingsDlg, CTabDlg)
ON_BN_CLICKED(IDC_SET_FONT_BUTTON1, &CTaskBarSettingsDlg::OnBnClickedSetFontButton1)
ON_BN_CLICKED(IDC_TASKBAR_WND_ON_LEFT_CHECK, &CTaskBarSettingsDlg::OnBnClickedTaskbarWndOnLeftCheck)
ON_BN_CLICKED(IDC_SPEED_SHORT_MODE_CHECK, &CTaskBarSettingsDlg::OnBnClickedSpeedShortModeCheck)
ON_CBN_SELCHANGE(IDC_UNIT_COMBO, &CTaskBarSettingsDlg::OnCbnSelchangeUnitCombo)
ON_BN_CLICKED(IDC_HIDE_UNIT_CHECK, &CTaskBarSettingsDlg::OnBnClickedHideUnitCheck)
ON_BN_CLICKED(IDC_VALUE_RIGHT_ALIGN_CHECK, &CTaskBarSettingsDlg::OnBnClickedValueRightAlignCheck)
ON_BN_CLICKED(IDC_HIDE_PERCENTAGE_CHECK, &CTaskBarSettingsDlg::OnBnClickedHidePercentageCheck)
ON_MESSAGE(WM_STATIC_CLICKED, &CTaskBarSettingsDlg::OnStaticClicked)
ON_BN_CLICKED(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK, &CTaskBarSettingsDlg::OnBnClickedSpecifyEachItemColorCheck)
ON_CBN_SELCHANGE(IDC_DOUBLE_CLICK_COMBO, &CTaskBarSettingsDlg::OnCbnSelchangeDoubleClickCombo)
ON_BN_CLICKED(IDC_HORIZONTAL_ARRANGE_CHECK, &CTaskBarSettingsDlg::OnBnClickedHorizontalArrangeCheck)
ON_BN_CLICKED(IDC_SHOW_STATUS_BAR_CHECK, &CTaskBarSettingsDlg::OnBnClickedShowStatusBarCheck)
ON_BN_CLICKED(IDC_SEPARATE_VALUE_UNIT_CHECK, &CTaskBarSettingsDlg::OnBnClickedSeparateValueUnitCheck)
ON_BN_CLICKED(IDC_UNIT_BYTE_RADIO, &CTaskBarSettingsDlg::OnBnClickedUnitByteRadio)
ON_BN_CLICKED(IDC_UNIT_BIT_RADIO, &CTaskBarSettingsDlg::OnBnClickedUnitBitRadio)
ON_BN_CLICKED(IDC_SHOW_TOOL_TIP_CHK, &CTaskBarSettingsDlg::OnBnClickedShowToolTipChk)
ON_BN_CLICKED(IDC_DEFAULT_STYLE_BUTTON, &CTaskBarSettingsDlg::OnBnClickedDefaultStyleButton)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BROWSE_BUTTON, &CTaskBarSettingsDlg::OnBnClickedBrowseButton)
ON_BN_CLICKED(IDC_CM_GRAPH_BAR_RADIO, &CTaskBarSettingsDlg::OnBnClickedCMGraphBarRadio)
ON_BN_CLICKED(IDC_CM_GRAPH_PLOT_RADIO, &CTaskBarSettingsDlg::OnBnClickedCMGraphPLOTRadio)
ON_BN_CLICKED(IDC_BACKGROUND_TRANSPARENT_CHECK, &CTaskBarSettingsDlg::OnBnClickedBackgroundTransparentCheck)
ON_BN_CLICKED(IDC_AUTO_ADAPT_SETTINGS_BUTTON, &CTaskBarSettingsDlg::OnBnClickedAutoAdaptSettingsButton)
ON_BN_CLICKED(IDC_AUTO_ADAPT_LIGHT_THEME_CHECK, &CTaskBarSettingsDlg::OnBnClickedAutoAdaptLightThemeCheck)
ON_BN_CLICKED(IDC_AUTO_SET_BACK_COLOR_CHECK, &CTaskBarSettingsDlg::OnBnClickedAutoSetBackColorCheck)
ON_BN_CLICKED(IDC_DISPLAY_TEXT_SETTING_BUTTON, &CTaskBarSettingsDlg::OnBnClickedDisplayTextSettingButton)
ON_CBN_SELCHANGE(IDC_MEMORY_DISPLAY_COMBO, &CTaskBarSettingsDlg::OnCbnSelchangeMemoryDisplayCombo)
ON_BN_CLICKED(IDC_SHOW_DASHED_BOX, &CTaskBarSettingsDlg::OnBnClickedShowDashedBox)
ON_BN_CLICKED(IDC_SET_ORDER_BUTTON, &CTaskBarSettingsDlg::OnBnClickedSetOrderButton)
ON_BN_CLICKED(IDC_TASKBAR_WND_SNAP_CHECK, &CTaskBarSettingsDlg::OnBnClickedTaskbarWndSnapCheck)
ON_EN_CHANGE(IDC_ITEM_SPACE_EDIT, &CTaskBarSettingsDlg::OnEnChangeItemSpaceEdit)
ON_BN_CLICKED(IDC_SHOW_NET_SPEED_FIGURE_CHECK, &CTaskBarSettingsDlg::OnBnClickedShowNetSpeedFigureCheck)
ON_CBN_SELCHANGE(IDC_NET_SPEED_FIGURE_MAX_VALUE_UNIT_COMBO, &CTaskBarSettingsDlg::OnCbnSelchangeNetSpeedFigureMaxValueUnitCombo)
ON_EN_CHANGE(IDC_NET_SPEED_FIGURE_MAX_VALUE_EDIT, &CTaskBarSettingsDlg::OnEnChangeNetSpeedFigureMaxValueEdit)
END_MESSAGE_MAP()
// CTaskBarSettingsDlg 消息处理程序
BOOL CTaskBarSettingsDlg::OnInitDialog()
{
CTabDlg::OnInitDialog();
// TODO: 在此添加额外的初始化
theApp.m_taskbar_default_style.LoadConfig();
//初始化各控件状态
SetDlgItemText(IDC_FONT_NAME_EDIT1, m_data.font.name);
//wchar_t buff[16];
//swprintf_s(buff, L"%d", m_data.font_size);
//SetDlgItemText(IDC_FONT_SIZE_EDIT1, buff);
m_font_size_edit.SetRange(5, 72);
m_font_size_edit.SetValue(m_data.font.size);
//SetDlgItemText(IDC_UPLOAD_EDIT1, m_data.disp_str.Get(TDI_UP).c_str());
//SetDlgItemText(IDC_DOWNLOAD_EDIT1, m_data.disp_str.Get(TDI_DOWN).c_str());
//SetDlgItemText(IDC_CPU_EDIT1, m_data.disp_str.Get(TDI_CPU).c_str());
//SetDlgItemText(IDC_MEMORY_EDIT1, m_data.disp_str.Get(TDI_MEMORY).c_str());
//((CButton*)GetDlgItem(IDC_SWITCH_UP_DOWN_CHECK1))->SetCheck(m_data.swap_up_down);
((CButton*)GetDlgItem(IDC_TASKBAR_WND_ON_LEFT_CHECK))->SetCheck(m_data.tbar_wnd_on_left);
((CButton*)GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK))->SetCheck(m_data.speed_short_mode);
((CButton*)GetDlgItem(IDC_VALUE_RIGHT_ALIGN_CHECK))->SetCheck(m_data.value_right_align);
((CButton*)GetDlgItem(IDC_HORIZONTAL_ARRANGE_CHECK))->SetCheck(m_data.horizontal_arrange);
((CButton*)GetDlgItem(IDC_SHOW_STATUS_BAR_CHECK))->SetCheck(m_data.show_status_bar);
((CButton*)GetDlgItem(IDC_SEPARATE_VALUE_UNIT_CHECK))->SetCheck(m_data.separate_value_unit_with_space);
((CButton*)GetDlgItem(IDC_SHOW_TOOL_TIP_CHK))->SetCheck(m_data.show_tool_tip);
EnableDlgCtrl(IDC_TASKBAR_WND_SNAP_CHECK, theApp.m_win_version.IsWindows11OrLater());
CheckDlgButton(IDC_TASKBAR_WND_SNAP_CHECK, m_data.tbar_wnd_snap);
m_text_color_static.SetLinkCursor();
m_back_color_static.SetLinkCursor();
//m_trans_color_static.SetLinkCursor();
m_status_bar_color_static.SetLinkCursor();
DrawStaticColor();
#ifdef COMPILE_FOR_WINXP
m_background_transparent_chk.EnableWindow(FALSE);
#endif // COMPILE_FOR_WINXP
if (theApp.m_win_version.IsWindows7())
m_background_transparent_chk.EnableWindow(FALSE);
m_toolTip.Create(this);
m_toolTip.SetMaxTipWidth(theApp.DPI(300));
m_toolTip.AddTool(GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK), CCommon::LoadText(IDS_SPEED_SHORT_MODE_TIP));
m_toolTip.AddTool(&m_atuo_adapt_light_theme_chk, CCommon::LoadText(IDS_AUTO_ADAPT_TIP_INFO));
m_toolTip.AddTool(GetDlgItem(IDC_SHOW_STATUS_BAR_CHECK), CCommon::LoadText(IDS_SHOW_RESOURCE_USAGE_GRAPH_TIP));
m_toolTip.AddTool(GetDlgItem(IDC_SHOW_NET_SPEED_FIGURE_CHECK), CCommon::LoadText(IDS_SHOW_NET_SPEED_GRAPH_TIP));
if (m_data.unit_byte)
((CButton*)GetDlgItem(IDC_UNIT_BYTE_RADIO))->SetCheck(TRUE);
else
((CButton*)GetDlgItem(IDC_UNIT_BIT_RADIO))->SetCheck(TRUE);
IniUnitCombo();
m_hide_unit_chk.SetCheck(m_data.hide_unit);
if (m_data.speed_unit == SpeedUnit::AUTO)
{
m_hide_unit_chk.SetCheck(FALSE);
m_data.hide_unit = false;
m_hide_unit_chk.EnableWindow(FALSE);
}
((CButton*)GetDlgItem(IDC_HIDE_PERCENTAGE_CHECK))->SetCheck(m_data.hide_percent);
((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->SetCheck(m_data.specify_each_item_color);
m_background_transparent_chk.SetCheck(IsTaskbarTransparent());
m_atuo_adapt_light_theme_chk.SetCheck(m_data.auto_adapt_light_theme);
m_auto_set_back_color_chk.SetCheck(m_data.auto_set_background_color);
m_auto_set_back_color_chk.EnableWindow(theApp.m_win_version.IsWindows8OrLater());
if (theApp.m_win_version.GetMajorVersion() < 10)
{
m_data.auto_adapt_light_theme = false;
m_atuo_adapt_light_theme_chk.EnableWindow(FALSE);
}
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_CONNECTION_DETIAL));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_HISTORICAL_TRAFFIC));
m_double_click_combo.AddString(CCommon::LoadText(IDS_SHOW_HIDE_CPU_MEMORY));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_OPTION_SETTINGS));
m_double_click_combo.AddString(CCommon::LoadText(IDS_OPEN_TASK_MANAGER));
m_double_click_combo.AddString(CCommon::LoadText(IDS_SPECIFIC_APP));
m_double_click_combo.AddString(CCommon::LoadText(IDS_NONE));
m_double_click_combo.SetCurSel(static_cast(m_data.double_click_action));
m_digit_number_combo.AddString(_T("3"));
m_digit_number_combo.AddString(_T("4"));
m_digit_number_combo.AddString(_T("5"));
m_digit_number_combo.AddString(_T("6"));
m_digit_number_combo.AddString(_T("7"));
m_digit_number_combo.SetCurSel(m_data.digits_number - 3);
SetDlgItemText(IDC_EXE_PATH_EDIT, m_data.double_click_exe.c_str());
EnableControl();
//m_default_style_menu.LoadMenu(IDR_TASKBAR_STYLE_MENU);
if (m_data.cm_graph_type)
CheckDlgButton(IDC_CM_GRAPH_PLOT_RADIO, TRUE);
else
CheckDlgButton(IDC_CM_GRAPH_BAR_RADIO, TRUE);
CheckDlgButton(IDC_SHOW_DASHED_BOX, m_data.show_graph_dashed_box);
m_item_space_edit.SetRange(0, 32);
m_item_space_edit.SetValue(m_data.item_space);
//初始化内存显示方式下拉列表
m_memory_display_combo.AddString(CCommon::LoadText(IDS_USAGE_PERCENTAGE));
m_memory_display_combo.AddString(CCommon::LoadText(IDS_MEMORY_USED));
m_memory_display_combo.AddString(CCommon::LoadText(IDS_MEMORY_AVAILABLE));
m_memory_display_combo.SetCurSel(static_cast(m_data.memory_display));
CheckDlgButton(IDC_SHOW_NET_SPEED_FIGURE_CHECK, m_data.show_netspeed_figure);
m_net_speed_figure_max_val_edit.SetRange(1, 1024);
m_net_speed_figure_max_val_edit.SetValue(m_data.netspeed_figure_max_value);
m_net_speed_figure_max_val_unit_combo.AddString(_T("KB"));
m_net_speed_figure_max_val_unit_combo.AddString(_T("MB"));
m_net_speed_figure_max_val_unit_combo.SetCurSel(m_data.netspeed_figure_max_value_unit);
((CButton*)GetDlgItem(IDC_SET_ORDER_BUTTON))->SetIcon(theApp.GetMenuIcon(IDI_ITEM));
//初始化“预设方案”菜单
m_default_style_menu.CreatePopupMenu();
m_modify_default_style_menu.CreatePopupMenu();
for (int i{ 0 }; i < TASKBAR_DEFAULT_STYLE_NUM; i++)
{
CString item_name;
item_name.Format(_T("%s %d"), CCommon::LoadText(IDS_PRESET).GetString(), i + 1);
m_default_style_menu.AppendMenu(MF_STRING | MF_ENABLED, ID_DEFAULT_STYLE1 + i, item_name);
m_modify_default_style_menu.AppendMenu(MF_STRING | MF_ENABLED, ID_MODIFY_DEFAULT_STYLE1 + i, item_name);
}
m_default_style_menu.AppendMenu(MF_SEPARATOR);
m_default_style_menu.AppendMenu(MF_POPUP | MF_STRING, (UINT)m_modify_default_style_menu.m_hMenu, CCommon::LoadText(IDS_MODIFY_PRESET));
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CTaskBarSettingsDlg::OnBnClickedSetFontButton1()
{
// TODO: 在此添加控件通知处理程序代码
LOGFONT lf{};
lf.lfHeight = FontSizeToLfHeight(m_data.font.size);
lf.lfWeight = (m_data.font.bold ? FW_BOLD : FW_NORMAL);
lf.lfItalic = m_data.font.italic;
lf.lfUnderline = m_data.font.underline;
lf.lfStrikeOut = m_data.font.strike_out;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
//wcsncpy_s(lf.lfFaceName, m_data.font.name.GetString(), 32);
CCommon::WStringCopy(lf.lfFaceName, 32, m_data.font.name.GetString());
CCommon::NormalizeFont(lf);
CFontDialog fontDlg(&lf); //构造字体对话框,初始选择字体为之前字体
if (IDOK == fontDlg.DoModal()) // 显示字体对话框
{
//获取字体信息
m_data.font.name = fontDlg.GetFaceName();
m_data.font.size = fontDlg.GetSize() / 10;
m_data.font.bold = (fontDlg.IsBold() != FALSE);
m_data.font.italic = (fontDlg.IsItalic() != FALSE);
m_data.font.underline = (fontDlg.IsUnderline() != FALSE);
m_data.font.strike_out = (fontDlg.IsStrikeOut() != FALSE);
//将字体信息显示出来
SetDlgItemText(IDC_FONT_NAME_EDIT1, m_data.font.name);
wchar_t buff[16];
swprintf_s(buff, L"%d", m_data.font.size);
SetDlgItemText(IDC_FONT_SIZE_EDIT1, buff);
}
}
void CTaskBarSettingsDlg::OnBnClickedTaskbarWndOnLeftCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.tbar_wnd_on_left = (((CButton*)GetDlgItem(IDC_TASKBAR_WND_ON_LEFT_CHECK))->GetCheck() != 0);
EnableControl();
}
void CTaskBarSettingsDlg::OnBnClickedSpeedShortModeCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.speed_short_mode = (((CButton*)GetDlgItem(IDC_SPEED_SHORT_MODE_CHECK))->GetCheck() != 0);
}
BOOL CTaskBarSettingsDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_MOUSEMOVE)
m_toolTip.RelayEvent(pMsg);
return CTabDlg::PreTranslateMessage(pMsg);
}
void CTaskBarSettingsDlg::OnCbnSelchangeUnitCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.speed_unit = static_cast(m_unit_combo.GetCurSel());
if (m_data.speed_unit == SpeedUnit::AUTO)
{
m_hide_unit_chk.SetCheck(FALSE);
m_data.hide_unit = false;
m_hide_unit_chk.EnableWindow(FALSE);
}
else
{
m_hide_unit_chk.EnableWindow(TRUE);
}
}
void CTaskBarSettingsDlg::OnBnClickedHideUnitCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hide_unit = (m_hide_unit_chk.GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//获取字体设置
int font_size;
font_size = m_font_size_edit.GetValue();
if (font_size > MAX_FONT_SIZE || font_size < MIN_FONT_SIZE)
{
CString info;
info.Format(CCommon::LoadText(IDS_FONT_SIZE_WARNING), MIN_FONT_SIZE, MAX_FONT_SIZE);
MessageBox(info, NULL, MB_OK | MB_ICONWARNING);
}
else
{
m_data.font.size = font_size;
}
GetDlgItemText(IDC_FONT_NAME_EDIT1, m_data.font.name);
//获取数据位数的设置
m_data.digits_number = m_digit_number_combo.GetCurSel() + 3;
bool is_taskbar_transparent_checked = (m_background_transparent_chk.GetCheck() != 0);
SetTaskabrTransparent(is_taskbar_transparent_checked);
SaveColorSettingToDefaultStyle();
CTabDlg::OnOK();
}
void CTaskBarSettingsDlg::SaveColorSettingToDefaultStyle()
{
//如果开启了自动适应Windows10深色/浅色模式功能时,自动将当前配置保存到对应预设
if (theApp.m_taskbar_data.auto_save_taskbar_color_settings_to_preset && m_data.auto_adapt_light_theme && IsStyleModified())
{
int default_style_saved{ CWindowsSettingHelper::IsWindows10LightTheme() ? m_data.light_default_style : m_data.dark_default_style };
ModifyDefaultStyle(default_style_saved);
}
}
void CTaskBarSettingsDlg::OnBnClickedValueRightAlignCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.value_right_align = (((CButton*)GetDlgItem(IDC_VALUE_RIGHT_ALIGN_CHECK))->GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnBnClickedHidePercentageCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.hide_percent = (((CButton*)GetDlgItem(IDC_HIDE_PERCENTAGE_CHECK))->GetCheck() != 0);
}
afx_msg LRESULT CTaskBarSettingsDlg::OnStaticClicked(WPARAM wParam, LPARAM lParam)
{
switch (::GetDlgCtrlID(((CWnd*)wParam)->m_hWnd))
{
case IDC_TEXT_COLOR_STATIC1: //点击“文本颜色”时
{
//设置文本颜色
if (m_data.specify_each_item_color)
{
CTaskbarColorDlg colorDlg(m_data.text_colors);
if (colorDlg.DoModal() == IDOK)
{
m_data.text_colors = colorDlg.GetColors();
DrawStaticColor();
m_style_modified = true;
}
}
else if (!m_data.text_colors.empty())
{
CMFCColorDialogEx colorDlg(m_data.text_colors.begin()->second.label, 0, this);
if (colorDlg.DoModal() == IDOK)
{
m_data.text_colors.begin()->second.label = colorDlg.GetColor();
if (m_data.back_color == m_data.text_colors.begin()->second.label)
MessageBox(CCommon::LoadText(IDS_SAME_TEXT_BACK_COLOR_WARNING), NULL, MB_ICONWARNING);
DrawStaticColor();
m_style_modified = true;
}
}
break;
}
case IDC_TEXT_COLOR_STATIC2: //点击“背景颜色”时
{
//设置背景颜色
CMFCColorDialogEx colorDlg(m_data.back_color, 0, this);
if (colorDlg.DoModal() == IDOK)
{
bool background_transparent = IsTaskbarTransparent();
m_data.back_color = colorDlg.GetColor();
if (m_data.back_color == m_data.text_colors.begin()->second.label)
MessageBox(CCommon::LoadText(IDS_SAME_BACK_TEXT_COLOR_WARNING), NULL, MB_ICONWARNING);
if (background_transparent)
{
CCommon::TransparentColorConvert(m_data.back_color);
//如果当前设置了背景透明,则更改了背景色后同时将透明色设置成和背景色一样的颜色,以保持背景透明
m_data.transparent_color = m_data.back_color;
}
DrawStaticColor();
m_style_modified = true;
}
break;
}
//case IDC_TRANSPARENT_COLOR_STATIC: //点击“透明色”时
//{
// CMFCColorDialogEx colorDlg(m_data.transparent_color, 0, this);
// if (colorDlg.DoModal() == IDOK)
// {
// m_data.transparent_color = colorDlg.GetColor();
// DrawStaticColor();
// }
// break;
//}
case IDC_TEXT_COLOR_STATIC3: //点击“状态条颜色”时
{
CMFCColorDialogEx colorDlg(m_data.status_bar_color, 0, this);
if (colorDlg.DoModal() == IDOK)
{
m_data.status_bar_color = colorDlg.GetColor();
DrawStaticColor();
m_style_modified = true;
}
break;
}
default:
break;
}
return 0;
}
void CTaskBarSettingsDlg::OnBnClickedSpecifyEachItemColorCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.specify_each_item_color = (((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->GetCheck() != 0);
DrawStaticColor();
m_style_modified = true;
}
void CTaskBarSettingsDlg::OnCbnSelchangeDoubleClickCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.double_click_action = static_cast(m_double_click_combo.GetCurSel());
EnableControl();
}
void CTaskBarSettingsDlg::OnBnClickedHorizontalArrangeCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.horizontal_arrange = (((CButton*)GetDlgItem(IDC_HORIZONTAL_ARRANGE_CHECK))->GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnBnClickedShowStatusBarCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_status_bar = (((CButton*)GetDlgItem(IDC_SHOW_STATUS_BAR_CHECK))->GetCheck() != 0);
EnableControl();
}
void CTaskBarSettingsDlg::OnBnClickedSeparateValueUnitCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.separate_value_unit_with_space = (((CButton*)GetDlgItem(IDC_SEPARATE_VALUE_UNIT_CHECK))->GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnBnClickedUnitByteRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.unit_byte = true;
IniUnitCombo();
}
void CTaskBarSettingsDlg::OnBnClickedUnitBitRadio()
{
// TODO: 在此添加控件通知处理程序代码
m_data.unit_byte = false;
IniUnitCombo();
}
void CTaskBarSettingsDlg::OnBnClickedShowToolTipChk()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_tool_tip = (((CButton*)GetDlgItem(IDC_SHOW_TOOL_TIP_CHK))->GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnBnClickedDefaultStyleButton()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pBtn = GetDlgItem(IDC_DEFAULT_STYLE_BUTTON);
CPoint point;
if (pBtn != nullptr)
{
CRect rect;
pBtn->GetWindowRect(rect);
point.x = rect.left;
point.y = rect.bottom;
m_default_style_menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
void CTaskBarSettingsDlg::OnDestroy()
{
CTabDlg::OnDestroy();
// TODO: 在此处添加消息处理程序代码
theApp.m_taskbar_default_style.SaveConfig();
}
void CTaskBarSettingsDlg::OnBnClickedBrowseButton()
{
// TODO: 在此添加控件通知处理程序代码
CString szFilter = CCommon::LoadText(IDS_EXE_FILTER);
CFileDialog fileDlg(TRUE, NULL, NULL, 0, szFilter, this);
if (IDOK == fileDlg.DoModal())
{
m_data.double_click_exe = fileDlg.GetPathName();
SetDlgItemText(IDC_EXE_PATH_EDIT, m_data.double_click_exe.c_str());
}
}
void CTaskBarSettingsDlg::OnBnClickedCMGraphPLOTRadio()
{
m_data.cm_graph_type = true;
}
void CTaskBarSettingsDlg::OnBnClickedCMGraphBarRadio()
{
m_data.cm_graph_type = false;
}
void CTaskBarSettingsDlg::OnBnClickedBackgroundTransparentCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = (m_background_transparent_chk.GetCheck() != 0);
SetTaskabrTransparent(checked);
m_style_modified = true;
}
void CTaskBarSettingsDlg::OnBnClickedAutoAdaptSettingsButton()
{
// TODO: 在此添加控件通知处理程序代码
CAutoAdaptSettingsDlg dlg(m_data);
dlg.DoModal();
}
void CTaskBarSettingsDlg::OnBnClickedAutoAdaptLightThemeCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.auto_adapt_light_theme = (m_atuo_adapt_light_theme_chk.GetCheck() != 0);
EnableControl();
}
void CTaskBarSettingsDlg::OnBnClickedAutoSetBackColorCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.auto_set_background_color = (m_auto_set_back_color_chk.GetCheck() != 0);
}
void CTaskBarSettingsDlg::OnBnClickedDisplayTextSettingButton()
{
// TODO: 在此添加控件通知处理程序代码
CDisplayTextSettingDlg dlg(m_data.disp_str);
dlg.DoModal();
}
void CTaskBarSettingsDlg::OnCbnSelchangeMemoryDisplayCombo()
{
// TODO: 在此添加控件通知处理程序代码
m_data.memory_display = static_cast(m_memory_display_combo.GetCurSel());
}
void CTaskBarSettingsDlg::OnBnClickedShowDashedBox()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_graph_dashed_box = (IsDlgButtonChecked(IDC_SHOW_DASHED_BOX) != 0);
}
void CTaskBarSettingsDlg::OnBnClickedSetOrderButton()
{
// TODO: 在此添加控件通知处理程序代码
CSetItemOrderDlg dlg;
dlg.SetItemOrder(m_data.item_order.GetItemOrderConst());
dlg.SetDisplayItem(m_data.m_tbar_display_item);
dlg.SetPluginDisplayItem(m_data.plugin_display_item);
if (dlg.DoModal() == IDOK)
{
m_data.item_order.SetOrder(dlg.GetItemOrder());
m_data.m_tbar_display_item = dlg.GetDisplayItem();
m_data.plugin_display_item = dlg.GetPluginDisplayItem();
}
}
void CTaskBarSettingsDlg::OnBnClickedTaskbarWndSnapCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.tbar_wnd_snap = (IsDlgButtonChecked(IDC_TASKBAR_WND_SNAP_CHECK) != 0);
}
void CTaskBarSettingsDlg::OnEnChangeItemSpaceEdit()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CTabDlg::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
m_data.item_space = m_item_space_edit.GetValue();
m_data.ValidItemSpace();
}
BOOL CTaskBarSettingsDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
UINT cmd = LOWORD(wParam);
if (cmd >= ID_DEFAULT_STYLE1 && cmd < ID_DEFAULT_STYLE_MAX)
{
int default_style = cmd - ID_DEFAULT_STYLE1;
ApplyDefaultStyle(default_style);
}
if (cmd >= ID_MODIFY_DEFAULT_STYLE1 && cmd < ID_MODIFY_DEFAULT_STYLE_MAX)
{
int default_style = cmd - ID_MODIFY_DEFAULT_STYLE1;
if (MessageBox(CCommon::LoadTextFormat(IDS_SAVE_DEFAULT_STYLE_INQUIRY, { default_style + 1 }), NULL, MB_ICONQUESTION | MB_YESNO) == IDYES)
{
ModifyDefaultStyle(default_style);
}
}
return CTabDlg::OnCommand(wParam, lParam);
}
void CTaskBarSettingsDlg::OnBnClickedShowNetSpeedFigureCheck()
{
m_data.show_netspeed_figure = (IsDlgButtonChecked(IDC_SHOW_NET_SPEED_FIGURE_CHECK) != 0);
EnableControl();
}
void CTaskBarSettingsDlg::OnCbnSelchangeNetSpeedFigureMaxValueUnitCombo()
{
m_data.netspeed_figure_max_value_unit = m_net_speed_figure_max_val_unit_combo.GetCurSel();
}
void CTaskBarSettingsDlg::OnEnChangeNetSpeedFigureMaxValueEdit()
{
m_data.netspeed_figure_max_value = m_net_speed_figure_max_val_edit.GetValue();
}
================================================
FILE: TrafficMonitor/TaskBarSettingsDlg.h
================================================
#pragma once
#include "ColorStatic.h"
#include "afxwin.h"
#include "SpinEdit.h"
#include "TabDlg.h"
#include "TaskbarColorDlg.h"
#include "ComboBox2.h"
// CTaskBarSettingsDlg 对话框
class CTaskBarSettingsDlg : public CTabDlg
{
DECLARE_DYNAMIC(CTaskBarSettingsDlg)
public:
CTaskBarSettingsDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CTaskBarSettingsDlg();
bool IsStyleModified();
//如果开启了自动适应Windows10深色/浅色模式功能时,将当前配置保存到对应预设
void SaveColorSettingToDefaultStyle();
//选项设置数据
TaskBarSettingData m_data;
CWinVersionHelper m_win_version; //当前Windows的版本
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TASKBAR_SETTINGS_DIALOG };
#endif
protected:
//控件变量
CColorStatic m_text_color_static;
CColorStatic m_back_color_static;
CColorStatic m_trans_color_static;
CColorStatic m_status_bar_color_static;
CToolTipCtrl m_toolTip;
CComboBox2 m_unit_combo;
CButton m_hide_unit_chk;
CSpinEdit m_font_size_edit;
CComboBox2 m_double_click_combo;
CComboBox2 m_digit_number_combo;
CMenu m_default_style_menu;
CMenu m_modify_default_style_menu;
CButton m_background_transparent_chk;
CButton m_atuo_adapt_light_theme_chk;
CButton m_auto_set_back_color_chk;
CComboBox2 m_memory_display_combo;
CSpinEdit m_item_space_edit;
CSpinEdit m_net_speed_figure_max_val_edit;
CComboBox2 m_net_speed_figure_max_val_unit_combo;
bool m_style_modified{};
protected:
void DrawStaticColor();
void IniUnitCombo();
void ApplyDefaultStyle(int index); //应用一个预设方案
public:
void ModifyDefaultStyle(int index); //将当前颜色设置保存到一个预设方案
protected:
void EnableControl();
void SetTaskabrTransparent(bool transparent);
bool IsTaskbarTransparent();
virtual void SetControlMouseWheelEnable(bool enable) override;
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedSetFontButton1();
afx_msg void OnBnClickedTaskbarWndOnLeftCheck();
afx_msg void OnBnClickedSpeedShortModeCheck();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnCbnSelchangeUnitCombo();
afx_msg void OnBnClickedHideUnitCheck();
virtual void OnOK();
afx_msg void OnBnClickedValueRightAlignCheck();
afx_msg void OnBnClickedHidePercentageCheck();
protected:
afx_msg LRESULT OnStaticClicked(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnBnClickedSpecifyEachItemColorCheck();
afx_msg void OnCbnSelchangeDoubleClickCombo();
afx_msg void OnBnClickedHorizontalArrangeCheck();
afx_msg void OnBnClickedShowStatusBarCheck();
afx_msg void OnBnClickedSeparateValueUnitCheck();
afx_msg void OnBnClickedUnitByteRadio();
afx_msg void OnBnClickedUnitBitRadio();
afx_msg void OnBnClickedShowToolTipChk();
afx_msg void OnBnClickedDefaultStyleButton();
afx_msg void OnDestroy();
afx_msg void OnBnClickedBrowseButton();
afx_msg void OnBnClickedCMGraphBarRadio();
afx_msg void OnBnClickedCMGraphPLOTRadio();
afx_msg void OnBnClickedBackgroundTransparentCheck();
afx_msg void OnBnClickedAutoAdaptSettingsButton();
afx_msg void OnBnClickedAutoAdaptLightThemeCheck();
afx_msg void OnBnClickedAutoSetBackColorCheck();
afx_msg void OnBnClickedDisplayTextSettingButton();
afx_msg void OnCbnSelchangeMemoryDisplayCombo();
afx_msg void OnBnClickedShowDashedBox();
afx_msg void OnBnClickedSetOrderButton();
afx_msg void OnBnClickedTaskbarWndSnapCheck();
afx_msg void OnEnChangeItemSpaceEdit();
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
afx_msg void OnBnClickedShowNetSpeedFigureCheck();
afx_msg void OnCbnSelchangeNetSpeedFigureMaxValueUnitCombo();
afx_msg void OnEnChangeNetSpeedFigureMaxValueEdit();
};
================================================
FILE: TrafficMonitor/TaskbarColorDlg.cpp
================================================
// TaskbarColorDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "TaskbarColorDlg.h"
#include "afxdialogex.h"
#include "CMFCColorDialogEx.h"
// CTaskbarColorDlg 对话框
IMPLEMENT_DYNAMIC(CTaskbarColorDlg, CBaseDialog)
CTaskbarColorDlg::CTaskbarColorDlg(const std::map& colors, CWnd* pParent /*=NULL*/)
: CBaseDialog(IDD_TASKBAR_COLOR_DIALOG, pParent), m_colors(colors)
{
}
CTaskbarColorDlg::~CTaskbarColorDlg()
{
}
CString CTaskbarColorDlg::GetDialogName() const
{
return _T("TaskbarColorDlg");
}
void CTaskbarColorDlg::DoDataExchange(CDataExchange* pDX)
{
CBaseDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list_ctrl);
}
BEGIN_MESSAGE_MAP(CTaskbarColorDlg, CBaseDialog)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CTaskbarColorDlg::OnNMDblclkList1)
END_MESSAGE_MAP()
// CTaskbarColorDlg 消息处理程序
BOOL CTaskbarColorDlg::OnInitDialog()
{
CBaseDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetIcon(theApp.GetMenuIcon(IDI_TASKBAR_WINDOW), FALSE); // 设置小图标
//初始化列表控件
CRect rect;
m_list_ctrl.GetClientRect(rect);
m_list_ctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
int width0 = rect.Width() /2;
int width1 = (rect.Width() - width0 - theApp.DPI(20) - 1) / 2;
int width2 = rect.Width() - width0 - width1 - theApp.DPI(20) - 1;
m_list_ctrl.InsertColumn(0, CCommon::LoadText(IDS_ITEM), LVCFMT_LEFT, width0);
m_list_ctrl.InsertColumn(1, CCommon::LoadText(IDS_COLOR_LABEL), LVCFMT_LEFT, width1);
m_list_ctrl.InsertColumn(2, CCommon::LoadText(IDS_COLOR_VALUE), LVCFMT_LEFT, width2);
m_list_ctrl.SetDrawItemRangMargin(theApp.DPI(2));
//向列表中插入行
for (auto iter = theApp.m_plugins.AllDisplayItemsWithPlugins().begin(); iter != theApp.m_plugins.AllDisplayItemsWithPlugins().end(); ++iter)
{
CString item_name = iter->GetItemName();
if (!item_name.IsEmpty())
{
int index = m_list_ctrl.GetItemCount();
m_list_ctrl.InsertItem(index, item_name);
m_list_ctrl.SetItemColor(index, 1, m_colors[*iter].label);
m_list_ctrl.SetItemColor(index, 2, m_colors[*iter].value);
m_list_ctrl.SetItemData(index, (DWORD_PTR)&(*iter));
}
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CTaskbarColorDlg::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
int index = pNMItemActivate->iItem;
int col = pNMItemActivate->iSubItem;
if (col == 1 || col == 2)
{
COLORREF color = m_list_ctrl.GetItemColor(index, col);
CMFCColorDialogEx colorDlg(color, 0, this);
if (colorDlg.DoModal() == IDOK)
{
color = colorDlg.GetColor();
m_list_ctrl.SetItemColor(index, col, color);
CommonDisplayItem* item = (CommonDisplayItem*)(m_list_ctrl.GetItemData(index));
if (col == 1)
m_colors[*item].label = color;
else
m_colors[*item].value = color;
}
}
*pResult = 0;
}
================================================
FILE: TrafficMonitor/TaskbarColorDlg.h
================================================
#pragma once
#include "ColorStatic.h"
#include "afxwin.h"
#include "ColorSettingListCtrl.h"
#include "BaseDialog.h"
// CTaskbarColorDlg 对话框
class CTaskbarColorDlg : public CBaseDialog
{
DECLARE_DYNAMIC(CTaskbarColorDlg)
public:
CTaskbarColorDlg(const std::map& colors, CWnd* pParent = NULL); // 标准构造函数
virtual ~CTaskbarColorDlg();
const std::map& GetColors() const { return m_colors; }
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TASKBAR_COLOR_DIALOG };
#endif
protected:
std::map m_colors;
CColorSettingListCtrl m_list_ctrl;
virtual CString GetDialogName() const override;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult);
};
================================================
FILE: TrafficMonitor/TaskbarDefaultStyle.cpp
================================================
#include "stdafx.h"
#include "TaskbarDefaultStyle.h"
#include "IniHelper.h"
#include "TrafficMonitor.h"
#include "WindowsSettingHelper.h"
CTaskbarDefaultStyle::CTaskbarDefaultStyle()
{
}
CTaskbarDefaultStyle::~CTaskbarDefaultStyle()
{
SaveConfig();
}
void CTaskbarDefaultStyle::LoadConfig()
{
CIniHelper ini{ theApp.m_config_path };
for (int i = 0; i < TASKBAR_DEFAULT_STYLE_NUM; i++)
{
COLORREF default_text_color = (i == TASKBAR_DEFAULT_LIGHT_STYLE_INDEX ? RGB(0, 0, 0) : RGB(255, 255, 255));
COLORREF default_back_color = (i == TASKBAR_DEFAULT_LIGHT_STYLE_INDEX ? RGB(210, 210, 211) : 0);
COLORREF default_transparent_color = (i == TASKBAR_DEFAULT_LIGHT_STYLE_INDEX ? RGB(210, 210, 211) : 0);
COLORREF default_status_bar_color = (i == TASKBAR_DEFAULT_LIGHT_STYLE_INDEX ? RGB(165, 165, 165) : 0x005A5A5A);
wchar_t buff[64];
swprintf_s(buff, L"default%d_", i + 1);
wstring key_name = buff;
ini.LoadTaskbarWndColors(L"taskbar_default_style", (key_name + L"text_color").c_str(), m_default_style[i].text_colors, default_text_color);
m_default_style[i].back_color = ini.GetInt(L"taskbar_default_style", (key_name + L"back_color").c_str(), default_back_color);
m_default_style[i].transparent_color = ini.GetInt(L"taskbar_default_style", (key_name + L"transparent_color").c_str(), default_transparent_color);
m_default_style[i].status_bar_color = ini.GetInt(L"taskbar_default_style", (key_name + L"status_bar_color").c_str(), default_status_bar_color);
m_default_style[i].specify_each_item_color = ini.GetBool(L"taskbar_default_style", (key_name + L"specify_each_item_color").c_str(), false);
}
}
void CTaskbarDefaultStyle::SaveConfig() const
{
CIniHelper ini{ theApp.m_config_path };
for (int i = 0; i < TASKBAR_DEFAULT_STYLE_NUM; i++)
{
wchar_t buff[64];
swprintf_s(buff, L"default%d_", i + 1);
wstring key_name = buff;
if (IsTaskBarStyleDataValid(m_default_style[i])) //保存前检查当前颜色预设是否有效
{
ini.SaveTaskbarWndColors(L"taskbar_default_style", (key_name + L"text_color").c_str(), m_default_style[i].text_colors);
ini.WriteInt(L"taskbar_default_style", (key_name + L"back_color").c_str(), m_default_style[i].back_color);
ini.WriteInt(L"taskbar_default_style", (key_name + L"transparent_color").c_str(), m_default_style[i].transparent_color);
ini.WriteInt(L"taskbar_default_style", (key_name + L"status_bar_color").c_str(), m_default_style[i].status_bar_color);
ini.WriteBool(L"taskbar_default_style", (key_name + L"specify_each_item_color").c_str(), m_default_style[i].specify_each_item_color);
}
else
{
//写入日志
CString log_str;
log_str.Format(_T("在保存预设%d时检测到背景色和文字颜色都为黑色,该预设未被保存。"), i);
CCommon::WriteLog(log_str, theApp.m_log_path.c_str());
return;
}
}
ini.Save();
}
void CTaskbarDefaultStyle::ApplyDefaultStyle(int index, TaskBarSettingData & data) const
{
/*if (index == TASKBAR_DEFAULT_LIGHT_STYLE_INDEX)
{
ApplyDefaultLightStyle(data);
}
else */if (index >= 0 && index < TASKBAR_DEFAULT_STYLE_NUM)
{
if (!IsTaskBarStyleDataValid(m_default_style[index]))
return;
data.text_colors = m_default_style[index].text_colors;
data.back_color = m_default_style[index].back_color;
data.transparent_color = m_default_style[index].transparent_color;
data.status_bar_color = m_default_style[index].status_bar_color;
data.specify_each_item_color = m_default_style[index].specify_each_item_color;
if (data.transparent_color == data.back_color)
{
CCommon::TransparentColorConvert(data.back_color);
CCommon::TransparentColorConvert(data.transparent_color);
}
}
}
//void CTaskbarDefaultStyle::ApplyDefaultLightStyle(TaskBarSettingData& data)
//{
// for (auto& item : data.text_colors)
// {
// item.second.label = RGB(0, 0, 0);
// item.second.value = RGB(0, 0, 0);
// }
// data.back_color = RGB(210, 210, 211);
// data.transparent_color = RGB(210, 210, 211);
// data.status_bar_color = RGB(165, 165, 165);
//}
void CTaskbarDefaultStyle::ModifyDefaultStyle(int index, TaskBarSettingData & data)
{
if (index < 0 || index >= TASKBAR_DEFAULT_STYLE_NUM)
return;
m_default_style[index].text_colors = data.text_colors;
m_default_style[index].back_color = data.back_color;
m_default_style[index].transparent_color = data.transparent_color;
m_default_style[index].status_bar_color = data.status_bar_color;
m_default_style[index].specify_each_item_color = data.specify_each_item_color;
}
bool CTaskbarDefaultStyle::IsTaskbarTransparent(const TaskBarSettingData& data)
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
return (data.transparent_color == data.back_color);
else
return data.transparent_color == 0;
}
void CTaskbarDefaultStyle::SetTaskabrTransparent(bool transparent, TaskBarSettingData& data)
{
if (transparent)
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
{
//浅色模式下要设置任务栏窗口透明,只需将透明色设置成和背景色一样即可
CCommon::TransparentColorConvert(data.back_color);
data.transparent_color = data.back_color;
}
else
{
//深色模式下,背景色透明将透明色设置成黑色
data.transparent_color = 0;
}
}
else
{
//要设置任务栏窗口不透明,只需将透明色设置成和背景色不一样即可
if (data.back_color != TASKBAR_TRANSPARENT_COLOR1)
data.transparent_color = TASKBAR_TRANSPARENT_COLOR1;
else
data.transparent_color = TASKBAR_TRANSPARENT_COLOR2;
}
}
bool CTaskbarDefaultStyle::IsTaskBarStyleDataValid(const TaskBarStyleData& data)
{
for (const auto& item : data.text_colors)
{
if (item.second.label != data.back_color || item.second.value != data.back_color)
return true;
}
return false; //如果文本颜色全部等于背景颜色,则该颜色预设无效
}
================================================
FILE: TrafficMonitor/TaskbarDefaultStyle.h
================================================
#pragma once
#include "CommonData.h"
#define TASKBAR_DEFAULT_STYLE_NUM 4
#define TASKBAR_DEFAULT_LIGHT_STYLE_INDEX 3
class CTaskbarDefaultStyle
{
public:
struct TaskBarStyleData
{
std::map text_colors{};
COLORREF back_color{};
COLORREF transparent_color{};
COLORREF status_bar_color{};
bool specify_each_item_color{};
};
CTaskbarDefaultStyle();
~CTaskbarDefaultStyle();
void LoadConfig();
void SaveConfig() const;
void ApplyDefaultStyle(int index, TaskBarSettingData& data) const; //应用一个颜色预设
//static void ApplyDefaultLightStyle(TaskBarSettingData& data); //应用默认的浅色模式预设
void ModifyDefaultStyle(int index, TaskBarSettingData& data); //将当前颜色设置保存到一个预设方案
static bool IsTaskbarTransparent(const TaskBarSettingData& data);
static void SetTaskabrTransparent(bool transparent, TaskBarSettingData& data);
static bool IsTaskBarStyleDataValid(const TaskBarStyleData& data); //判断一个颜色模式是否有效
private:
TaskBarStyleData m_default_style[TASKBAR_DEFAULT_STYLE_NUM]; //预设样式
};
================================================
FILE: TrafficMonitor/TaskbarItemOrderHelper.cpp
================================================
#include "stdafx.h"
#include "TaskbarItemOrderHelper.h"
#include "Common.h"
#include "TrafficMonitor.h"
CommonDisplayItem::CommonDisplayItem(DisplayItem item)
{
is_plugin = false;
item_type = item;
plugin_item = nullptr;
}
CommonDisplayItem::CommonDisplayItem(IPluginItem* item)
{
is_plugin = true;
plugin_item = item;
}
bool CommonDisplayItem::operator<(const CommonDisplayItem& item) const
{
if (is_plugin && !item.is_plugin)
return false;
else if (!is_plugin && item.is_plugin)
return true;
else if (!is_plugin)
return item_type < item.item_type;
else
return theApp.m_plugins.GetItemIndex(plugin_item) < theApp.m_plugins.GetItemIndex(item.plugin_item);
}
CString CommonDisplayItem::GetItemName() const
{
CString item_name;
if (is_plugin)
{
if (plugin_item != nullptr)
item_name = plugin_item->GetItemName();
}
else
{
switch (item_type)
{
case TDI_UP:
item_name = CCommon::LoadText(IDS_UPLOAD);
break;
case TDI_DOWN:
item_name = CCommon::LoadText(IDS_DOWNLOAD);
break;
case TDI_TOTAL_SPEED:
item_name = CCommon::LoadText(IDS_TOTAL_NET_SPEED);
break;
case TDI_CPU:
item_name = CCommon::LoadText(IDS_CPU_USAGE);
break;
case TDI_MEMORY:
item_name = CCommon::LoadText(IDS_MEMORY_USAGE);
break;
#ifndef WITHOUT_TEMPERATURE
case TDI_GPU_USAGE:
item_name = CCommon::LoadText(IDS_GPU_USAGE);
break;
case TDI_CPU_TEMP:
item_name = CCommon::LoadText(IDS_CPU_TEMPERATURE);
break;
case TDI_GPU_TEMP:
item_name = CCommon::LoadText(IDS_GPU_TEMPERATURE);
break;
case TDI_HDD_TEMP:
item_name = CCommon::LoadText(IDS_HDD_TEMPERATURE);
break;
case TDI_MAIN_BOARD_TEMP:
item_name = CCommon::LoadText(IDS_MAINBOARD_TEMPERATURE);
break;
case TDI_HDD_USAGE:
item_name = CCommon::LoadText(IDS_HDD_USAGE);
break;
case TDI_CPU_FREQ:
item_name = CCommon::LoadText(IDS_CPU_FREQ);
break;
#endif
default:
break;
}
}
return item_name;
}
bool CommonDisplayItem::operator==(const CommonDisplayItem& item) const
{
if (is_plugin != item.is_plugin)
return false;
else if (!is_plugin)
return item_type == item.item_type;
else
return plugin_item == item.plugin_item;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
CTaskbarItemOrderHelper::CTaskbarItemOrderHelper(bool displayed_only)
: m_displayed_only(displayed_only)
{
}
void CTaskbarItemOrderHelper::Init()
{
for (const auto& item : theApp.m_plugins.AllDisplayItemsWithPlugins())
{
m_all_item_in_default_order.push_back(item);
}
}
std::vector CTaskbarItemOrderHelper::GetAllDisplayItemsWithOrder() const
{
std::vector items;
for (auto i : m_item_order)
{
if (i >= 0 && i < static_cast(m_all_item_in_default_order.size()))
{
if (m_displayed_only && !IsItemDisplayed(m_all_item_in_default_order[i]))
{
continue;
}
items.push_back(m_all_item_in_default_order[i]);
}
}
return items;
}
void CTaskbarItemOrderHelper::FromString(const std::wstring& str)
{
m_item_order.clear();
std::vector str_list;
CCommon::StringSplit(str, L',', str_list);
for (const auto& s : str_list)
{
m_item_order.push_back(_wtoi(s.c_str()));
}
NormalizeItemOrder();
}
std::wstring CTaskbarItemOrderHelper::ToString() const
{
std::wstring result;
for (int i : m_item_order)
{
result += std::to_wstring(i);
result.push_back(L',');
}
if (!m_item_order.empty())
result.pop_back();
return result;
}
void CTaskbarItemOrderHelper::SetOrder(const vector& item_order)
{
m_item_order = item_order;
NormalizeItemOrder();
}
const vector& CTaskbarItemOrderHelper::GetItemOrderConst() const
{
return m_item_order;
}
vector& CTaskbarItemOrderHelper::GetItemOrder()
{
return m_item_order;
}
CString CTaskbarItemOrderHelper::GetItemDisplayName(CommonDisplayItem item)
{
if (item.is_plugin)
{
if (item.plugin_item != nullptr)
return item.plugin_item->GetItemName();
else
return CString();
}
else
{
switch (item.item_type)
{
case TDI_UP:
return CCommon::LoadText(IDS_UPLOAD);
case TDI_DOWN:
return CCommon::LoadText(IDS_DOWNLOAD);
case TDI_TOTAL_SPEED:
return CCommon::LoadText(IDS_TOTAL_NET_SPEED);
case TDI_CPU:
return CCommon::LoadText(IDS_CPU_USAGE);
case TDI_MEMORY:
return CCommon::LoadText(IDS_MEMORY_USAGE);
case TDI_GPU_USAGE:
return CCommon::LoadText(IDS_GPU_USAGE);
case TDI_CPU_TEMP:
return CCommon::LoadText(IDS_CPU_TEMPERATURE);
case TDI_GPU_TEMP:
return CCommon::LoadText(IDS_GPU_TEMPERATURE);
case TDI_HDD_TEMP:
return CCommon::LoadText(IDS_HDD_TEMPERATURE);
case TDI_MAIN_BOARD_TEMP:
return CCommon::LoadText(IDS_MAINBOARD_TEMPERATURE);
case TDI_HDD_USAGE:
return CCommon::LoadText(IDS_HDD_USAGE);
case TDI_CPU_FREQ:
return CCommon::LoadText(IDS_CPU_FREQ);
default:
break;
}
}
return CString();
}
bool CTaskbarItemOrderHelper::IsItemDisplayed(CommonDisplayItem item)
{
bool displayed = true;
if (!item.is_plugin)
{
if ((item == TDI_CPU_TEMP || item == TDI_CPU_FREQ) && !theApp.m_general_data.IsHardwareEnable(HI_CPU))
displayed = false;
if ((item == TDI_GPU_TEMP || item == TDI_GPU_USAGE) && !theApp.m_general_data.IsHardwareEnable(HI_GPU))
displayed = false;
if ((item == TDI_HDD_TEMP || item == TDI_HDD_USAGE) && !theApp.m_general_data.IsHardwareEnable(HI_HDD))
displayed = false;
if (item == TDI_MAIN_BOARD_TEMP && !theApp.m_general_data.IsHardwareEnable(HI_MBD))
displayed = false;
}
return displayed;
}
void CTaskbarItemOrderHelper::NormalizeItemOrder()
{
//检查是否有超出范围的序号
int item_num = static_cast(theApp.m_plugins.AllDisplayItemsWithPlugins().size());
for (auto iter = m_item_order.begin(); iter != m_item_order.end();)
{
if (*iter < 0 || *iter >= item_num)
iter = m_item_order.erase(iter);
else
++iter;
}
//删除不显示的序号
if (m_displayed_only)
{
for (auto iter = m_item_order.begin(); iter != m_item_order.end();)
{
if (*iter >= 0 && *iter < static_cast(m_all_item_in_default_order.size()))
{
CommonDisplayItem item = m_all_item_in_default_order[*iter];
if (!IsItemDisplayed(item))
{
iter = m_item_order.erase(iter);
continue;
}
}
++iter;
}
}
//删除重复的序号
CCommon::RemoveVectorDuplicateItem(m_item_order);
//检查是否有缺少的序号
for (int i = 0; i < item_num; i++)
{
auto iter = std::find(m_item_order.begin(), m_item_order.end(), i);
if (iter == m_item_order.end())
m_item_order.push_back(i);
}
}
================================================
FILE: TrafficMonitor/TaskbarItemOrderHelper.h
================================================
#pragma once
#include "PluginInterface.h"
//显示的项目
enum DisplayItem
{
TDI_UP = 1 << 0,
TDI_DOWN = 1 << 1,
TDI_CPU = 1 << 2,
TDI_MEMORY = 1 << 3,
TDI_GPU_USAGE = 1 << 4,
TDI_CPU_TEMP = 1 << 5,
TDI_GPU_TEMP = 1 << 6,
TDI_HDD_TEMP = 1 << 7,
TDI_MAIN_BOARD_TEMP = 1 << 8,
TDI_HDD_USAGE = 1 << 9,
TDI_TOTAL_SPEED = 1 << 10,
TDI_CPU_FREQ = 1 << 11
};
//显示的项目
struct CommonDisplayItem
{
bool is_plugin{}; //是否为插件项目
DisplayItem item_type{}; //内建的显示项目
IPluginItem* plugin_item{}; //插件显示项目
CommonDisplayItem() {}
CommonDisplayItem(DisplayItem item);
CommonDisplayItem(IPluginItem* item);
bool operator<(const CommonDisplayItem&) const;
bool operator==(const CommonDisplayItem&) const;
CString GetItemName() const;
};
//所有显示项目的集合
const std::set AllDisplayItems
{
TDI_UP, TDI_DOWN, TDI_CPU, TDI_MEMORY
#ifndef WITHOUT_TEMPERATURE
, TDI_GPU_USAGE, TDI_CPU_TEMP, TDI_GPU_TEMP, TDI_HDD_TEMP, TDI_MAIN_BOARD_TEMP, TDI_HDD_USAGE,TDI_CPU_FREQ
#endif
, TDI_TOTAL_SPEED
};
class CTaskbarItemOrderHelper
{
public:
CTaskbarItemOrderHelper(bool displayed_only = false);
void Init();
//根据设定的顺序获取任务窗口每个显示项目
std::vector GetAllDisplayItemsWithOrder() const;
void FromString(const std::wstring& str);
std::wstring ToString() const;
void SetOrder(const vector& item_order);
const vector& GetItemOrderConst() const;
vector& GetItemOrder();
static CString GetItemDisplayName(CommonDisplayItem item);
static bool IsItemDisplayed(CommonDisplayItem item);
private:
//规范m_item_order里的项目,如果m_item_order里有序号超过了显示项目的个数,则将其移除,并在后面添加缺少的项目的序号
void NormalizeItemOrder();
private:
vector m_item_order; //保存每个项目的顺序
vector m_all_item_in_default_order; //以默认顺序保存的所有显示项目
bool m_displayed_only;
};
================================================
FILE: TrafficMonitor/Test.cpp
================================================
#include "stdafx.h"
#include "Test.h"
#include "Common.h"
#include "SkinFile.h"
#include "TrafficMonitor.h"
CTest::CTest()
{
}
CTest::~CTest()
{
}
void CTest::Test()
{
//TestHttpQequest();
//TestGetLicense();
//TestSkin();
//TestCrash();
//TestDate();
}
void CTest::TestCommand()
{
//TestPlugin();
}
void CTest::TestHttpQequest()
{
wstring result;
bool rtn = CCommon::GetURL(L"https://v4.yinghualuo.cn/bejson", result, true, L"TrafficMonitor_V1.78");
int a = 0;
}
void CTest::TestGetLicense()
{
CString license_str;
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_LICENSE), _T("TEXT"));
if (hRes != NULL)
{
HGLOBAL hglobal = LoadResource(NULL, hRes);
if (hglobal != NULL)
{
license_str = CCommon::StrToUnicode((const char*)hglobal, true).c_str();
int a = 0;
}
}
}
void CTest::TestSkin()
{
CSkinFile skin;
//skin.Load(L"C:\\Users\\zhong\\OneDrive\\文档\\TrafficMonitorSkin.xml");
skin.Load(L"D:\\Projects\\GitHub Project\\TrafficMonitor\\TrafficMonitor\\skins\\0默认皮肤\\skin.ini");
int a = 0;
}
void CTest::TestCrash()
{
CString* pStr = nullptr;
int a = pStr->GetLength();
printf("%d", a);
}
void CTest::TestPlugin()
{
if (!theApp.m_plugins.GetPlugins().empty())
{
theApp.m_plugins.GetPlugins()[0].plugin->ShowOptionsDialog(theApp.m_pMainWnd->m_hWnd);
}
}
void CTest::TestDate()
{
Date d;
d.year = 2021;
d.month = 1;
d.day = 4;
int week = d.week();
int a = 0;
}
================================================
FILE: TrafficMonitor/Test.h
================================================
#pragma once
class CTest
{
public:
CTest();
~CTest();
static void Test();
static void TestCommand();
private:
static void TestHttpQequest();
static void TestGetLicense();
static void TestSkin();
static void TestCrash();
static void TestPlugin();
static void TestDate();
};
================================================
FILE: TrafficMonitor/TinyXml2Helper.cpp
================================================
#include "stdafx.h"
#include "TinyXml2Helper.h"
#include "Common.h"
bool CTinyXml2Helper::LoadXmlFile(tinyxml2::XMLDocument& doc, const wchar_t* file_path)
{
//由于XMLDocument::LoadFile函数不支持Unicode,因此这里自行读取文件内容,并调用XMLDocument::Parse函数解析
size_t length;
const char* xml_contents = CCommon::GetFileContent(file_path, length);
auto err = doc.Parse(xml_contents, length);
delete[] xml_contents;
return err == tinyxml2::XML_SUCCESS;
}
void CTinyXml2Helper::IterateChildNode(tinyxml2::XMLElement* ele, std::function fun)
{
if (ele == nullptr)
return;
tinyxml2::XMLElement* child = ele->FirstChildElement();
if (child == nullptr)
return;
fun(child);
while (true)
{
child = child->NextSiblingElement();
if (child != nullptr)
fun(child);
else
break;
}
}
const char * CTinyXml2Helper::ElementAttribute(tinyxml2::XMLElement * ele, const char * attr)
{
if (ele != nullptr)
{
const char* str = ele->Attribute(attr);
if (str != nullptr)
return str;
}
return "";
}
const char* CTinyXml2Helper::ElementName(tinyxml2::XMLElement* ele)
{
if (ele != nullptr)
{
const char* str = ele->Name();
if (str != nullptr)
return str;
}
return "";
}
const char* CTinyXml2Helper::ElementText(tinyxml2::XMLElement* ele)
{
if (ele != nullptr)
{
const char* str = ele->GetText();
if (str != nullptr)
return str;
}
return "";
}
bool CTinyXml2Helper::StringToBool(const char* str)
{
string str_text{ str };
return (!str_text.empty() && str_text != "0");
}
================================================
FILE: TrafficMonitor/TinyXml2Helper.h
================================================
#pragma once
#include "tinyxml2/tinyxml2.h"
#include
#include
class CTinyXml2Helper
{
public:
//从文件读取XML内容
static bool LoadXmlFile(tinyxml2::XMLDocument& doc, const wchar_t* file_path);
//遍历一个XML节点
//fun: 一个函数对象,遍历到一个节点时被调用
static void IterateChildNode(tinyxml2::XMLElement* ele, std::function fun);
//获取一个节点的属性(返回值不会为空指针,如果找不到则返回空字符串)
static const char* ElementAttribute(tinyxml2::XMLElement* ele, const char* attr);
//获取一个节点的名称(返回值不会为空指针,如果找不到则返回空字符串)
static const char* ElementName(tinyxml2::XMLElement* ele);
//获取一个节点的文本(返回值不会为空指针,如果找不到则返回空字符串)
static const char* ElementText(tinyxml2::XMLElement* ele);
static bool StringToBool(const char* str);
};
================================================
FILE: TrafficMonitor/TrafficMonitor.cpp
================================================
// TrafficMonitor.cpp : 定义应用程序的类行为。
//
#include "stdafx.h"
#include "TrafficMonitor.h"
#include "TrafficMonitorDlg.h"
#include "crashtool.h"
#include "UpdateHelper.h"
#include "Test.h"
#include "WIC.h"
#include "auto_start_helper.h"
#include "AppAlreadyRuningDlg.h"
#include "WindowsSettingHelper.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTrafficMonitorApp
BEGIN_MESSAGE_MAP(CTrafficMonitorApp, CWinApp)
//ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
ON_COMMAND(ID_HELP, &CTrafficMonitorApp::OnHelp)
ON_COMMAND(ID_FREQUENTY_ASKED_QUESTIONS, &CTrafficMonitorApp::OnFrequentyAskedQuestions)
ON_COMMAND(ID_UPDATE_LOG, &CTrafficMonitorApp::OnUpdateLog)
END_MESSAGE_MAP()
CTrafficMonitorApp* CTrafficMonitorApp::self = NULL;
// CTrafficMonitorApp 构造
CTrafficMonitorApp::CTrafficMonitorApp()
{
self = this;
// 支持重新启动管理器
//m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
CRASHREPORT::StartCrashReport();
}
void CTrafficMonitorApp::LoadConfig()
{
CIniHelper ini{ m_config_path };
//常规设置
m_general_data.check_update_when_start = ini.GetBool(_T("general"), _T("check_update_when_start"), true);
m_general_data.allow_skin_cover_font = ini.GetBool(_T("general"), _T("allow_skin_cover_font"), true);
m_general_data.allow_skin_cover_text = ini.GetBool(_T("general"), _T("allow_skin_cover_text"), true);
m_general_data.language = static_cast(ini.GetInt(_T("general"), _T("language"), 0));
m_general_data.show_all_interface = ini.GetBool(L"general", L"show_all_interface", false);
bool is_chinese_language{}; //当前语言是否为简体中文
if (m_general_data.language == Language::FOLLOWING_SYSTEM)
is_chinese_language = CCommon::LoadText(IDS_LANGUAGE_CODE) == _T("2");
else
is_chinese_language = (m_general_data.language == Language::SIMPLIFIED_CHINESE);
m_general_data.update_source = ini.GetInt(L"general", L"update_source", is_chinese_language ? 1 : 0); //如果当前语言为简体,则默认更新源为Gitee,否则为GitHub
//载入获取CPU利用率的方式,默认使用GetSystemTimes获取
m_general_data.m_get_cpu_usage_by_cpu_times = ini.GetBool(L"general", L"get_cpu_usage_by_cpu_times", /*m_win_version.GetMajorVersion() < 10*/ true);
m_general_data.monitor_time_span = ini.GetInt(L"general", L"monitor_time_span", 1000);
if (m_general_data.monitor_time_span < MONITOR_TIME_SPAN_MIN || m_general_data.monitor_time_span > MONITOR_TIME_SPAN_MAX)
m_general_data.monitor_time_span = 1000;
m_general_data.hard_disk_name = ini.GetString(L"general", L"hard_disk_name", L"");
m_general_data.cpu_core_name = ini.GetString(L"general", L"cpu_core_name", L"Core Average");
m_general_data.hardware_monitor_item = ini.GetInt(L"general", L"hardware_monitor_item", 0);
std::vector connections_hide;
ini.GetStringList(L"general", L"connections_hide", connections_hide, std::vector{});
m_general_data.connections_hide.FromVector(connections_hide);
//Windows10颜色模式设置
bool is_windows10_light_theme = CWindowsSettingHelper::IsWindows10LightTheme();
if (is_windows10_light_theme)
CCommon::SetColorMode(ColorMode::Light);
else
CCommon::SetColorMode(ColorMode::Default);
//主窗口设置
m_cfg_data.m_transparency = ini.GetInt(_T("config"), _T("transparency"), 80);
m_main_wnd_data.m_always_on_top = ini.GetBool(_T("config"), _T("always_on_top"), true);
m_main_wnd_data.m_lock_window_pos = ini.GetBool(_T("config"), _T("lock_window_pos"), false);
m_general_data.show_notify_icon = ini.GetBool(_T("config"), _T("show_notify_icon"), true);
m_cfg_data.m_show_more_info = ini.GetBool(_T("config"), _T("show_cpu_memory"), false);
m_main_wnd_data.m_mouse_penetrate = ini.GetBool(_T("config"), _T("mouse_penetrate"), false);
m_cfg_data.m_show_task_bar_wnd = ini.GetBool(_T("config"), _T("show_task_bar_wnd"), false);
m_cfg_data.m_position_x = ini.GetInt(_T("config"), _T("position_x"), -1);
m_cfg_data.m_position_y = ini.GetInt(_T("config"), _T("position_y"), -1);
m_cfg_data.m_auto_select = ini.GetBool(_T("connection"), _T("auto_select"), true);
m_cfg_data.m_select_all = ini.GetBool(_T("connection"), _T("select_all"), false);
//判断皮肤是否存在
std::vector skin_files;
CCommon::GetFiles((theApp.m_skin_path + L"\\*").c_str(), skin_files);
bool is_skin_exist = (!skin_files.empty());
ini.LoadMainWndColors(_T("config"), _T("text_color"), m_main_wnd_data.text_colors, (is_skin_exist ? 16384 : 16777215)); //根据皮肤是否存在来设置默认的文本颜色,皮肤文件不存在时文本颜色默认为白色
m_main_wnd_data.specify_each_item_color = ini.GetBool(_T("config"), _T("specify_each_item_color"), false);
m_cfg_data.m_hide_main_window = ini.GetBool(_T("config"), _T("hide_main_window"), false);
m_cfg_data.m_connection_name = CCommon::UnicodeToStr(ini.GetString(L"connection", L"connection_name", L"").c_str());
m_cfg_data.m_skin_name = ini.GetString(_T("config"), _T("skin_selected"), _T(""));
if (m_cfg_data.m_skin_name.substr(0, 8) == L".\\skins\\") //如果读取到的皮肤名称前面有".\\skins\\",则把它删除。(用于和前一个版本保持兼容性)
m_cfg_data.m_skin_name = m_cfg_data.m_skin_name.substr(7);
m_cfg_data.m_notify_icon_selected = ini.GetInt(_T("config"), _T("notify_icon_selected"), (m_win_version.IsWindows7() || m_win_version.IsWindows8Or8point1() ? 2 : m_cfg_data.m_dft_notify_icon)); //Win7/8/8.1默认使用蓝色通知区图标,因为隐藏通知区图标后白色图标会看不清,其他系统默认使用白色图标
m_cfg_data.m_notify_icon_auto_adapt = ini.GetBool(_T("config"), _T("notify_icon_auto_adapt"), true);
if (m_cfg_data.m_notify_icon_auto_adapt)
AutoSelectNotifyIcon();
m_main_wnd_data.swap_up_down = ini.GetBool(_T("config"), _T("swap_up_down"), false);
m_main_wnd_data.hide_main_wnd_when_fullscreen = ini.GetBool(_T("config"), _T("hide_main_wnd_when_fullscreen"), true);
FontInfo default_font{};
default_font.name = CCommon::LoadText(IDS_DEFAULT_FONT);
default_font.size = 10;
ini.LoadFontData(_T("config"), m_main_wnd_data.font, default_font);
//m_main_wnd_data.font.name = ini.GetString(_T("config"), _T("font_name"), CCommon::LoadText(IDS_MICROSOFT_YAHEI)).c_str();
//m_main_wnd_data.font.size = ini.GetInt(_T("config"), _T("font_size"), 10);
//载入显示文本设置
m_main_wnd_data.disp_str.Get(TDI_UP) = ini.GetString(_T("config"), L"up_string", CCommon::LoadText(IDS_UPLOAD_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_DOWN) = ini.GetString(L"config", L"down_string", CCommon::LoadText(IDS_DOWNLOAD_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_TOTAL_SPEED) = ini.GetString(L"config", L"total_speed_string", _T("↑↓: $"));
m_main_wnd_data.disp_str.Get(TDI_CPU) = ini.GetString(L"config", L"cpu_string", L"CPU: $");
m_main_wnd_data.disp_str.Get(TDI_CPU_FREQ) = ini.GetString(L"config", L"cpu_freq_string", CCommon::LoadText(IDS_CPU_FREQ, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_MEMORY) = ini.GetString(L"config", L"memory_string", CCommon::LoadText(IDS_MEMORY_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_GPU_USAGE) = ini.GetString(L"config", L"gpu_string", CCommon::LoadText(IDS_GPU_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_CPU_TEMP) = ini.GetString(L"config", L"cpu_temp_string", L"CPU: $");
m_main_wnd_data.disp_str.Get(TDI_GPU_TEMP) = ini.GetString(L"config", L"gpu_temp_string", CCommon::LoadText(IDS_GPU_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_HDD_TEMP) = ini.GetString(L"config", L"hdd_temp_string", CCommon::LoadText(IDS_HDD_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_MAIN_BOARD_TEMP) = ini.GetString(L"config", L"main_board_temp_string", CCommon::LoadText(IDS_MAINBOARD_DISP, _T(": $")));
m_main_wnd_data.disp_str.Get(TDI_HDD_USAGE) = ini.GetString(L"config", L"hdd_string", CCommon::LoadText(IDS_HDD_DISP, _T(": $")));
//载入插件项目的显示文本设置
ini.LoadPluginDisplayStr(true);
m_main_wnd_data.speed_short_mode = ini.GetBool(_T("config"), _T("speed_short_mode"), false);
m_main_wnd_data.separate_value_unit_with_space = ini.GetBool(_T("config"), _T("separate_value_unit_with_space"), true);
m_main_wnd_data.show_tool_tip = ini.GetBool(_T("config"), _T("show_tool_tip"), true);
m_main_wnd_data.memory_display = static_cast(ini.GetInt(L"config", L"memory_display", static_cast(MemoryDisplay::USAGE_PERCENTAGE)));
m_main_wnd_data.unit_byte = ini.GetBool(_T("config"), _T("unit_byte"), true);
m_main_wnd_data.speed_unit = static_cast(ini.GetInt(_T("config"), _T("speed_unit"), 0));
m_main_wnd_data.hide_unit = ini.GetBool(_T("config"), _T("hide_unit"), false);
m_main_wnd_data.hide_percent = ini.GetBool(_T("config"), _T("hide_percent"), false);
m_main_wnd_data.double_click_action = static_cast(ini.GetInt(_T("config"), _T("double_click_action"), 0));
m_main_wnd_data.double_click_exe = ini.GetString(L"config", L"double_click_exe", (theApp.m_system_dir + L"\\Taskmgr.exe").c_str());
m_main_wnd_data.m_alow_out_of_border = ini.GetBool(_T("config"), _T("alow_out_of_border"), false);
m_general_data.traffic_tip_enable = ini.GetBool(L"notify_tip", L"traffic_tip_enable", false);
m_general_data.traffic_tip_value = ini.GetInt(L"notify_tip", L"traffic_tip_value", 200);
m_general_data.traffic_tip_unit = ini.GetInt(L"notify_tip", L"traffic_tip_unit", 0);
m_general_data.memory_usage_tip.enable = ini.GetBool(L"notify_tip", L"memory_usage_tip_enable", false);
m_general_data.memory_usage_tip.tip_value = ini.GetInt(L"notify_tip", L"memory_tip_value", 80);
m_general_data.cpu_temp_tip.enable = ini.GetBool(L"notify_tip", L"cpu_temperature_tip_enable", false);
m_general_data.cpu_temp_tip.tip_value = ini.GetInt(L"notify_tip", L"cpu_temperature_tip_value", 80);
m_general_data.gpu_temp_tip.enable = ini.GetBool(L"notify_tip", L"gpu_temperature_tip_enable", false);
m_general_data.gpu_temp_tip.tip_value = ini.GetInt(L"notify_tip", L"gpu_temperature_tip_value", 80);
m_general_data.hdd_temp_tip.enable = ini.GetBool(L"notify_tip", L"hdd_temperature_tip_enable", false);
m_general_data.hdd_temp_tip.tip_value = ini.GetInt(L"notify_tip", L"hdd_temperature_tip_value", 80);
m_general_data.mainboard_temp_tip.enable = ini.GetBool(L"notify_tip", L"mainboard_temperature_tip_enable", false);
m_general_data.mainboard_temp_tip.tip_value = ini.GetInt(L"notify_tip", L"mainboard_temperature_tip_value", 80);
//任务栏窗口设置
m_taskbar_data.back_color = ini.GetInt(_T("task_bar"), _T("task_bar_back_color"), m_taskbar_data.dft_back_color);
m_taskbar_data.transparent_color = ini.GetInt(_T("task_bar"), _T("transparent_color"), m_taskbar_data.dft_transparent_color);
if (CTaskbarDefaultStyle::IsTaskbarTransparent(m_taskbar_data)) //如果任务栏背景透明,则需要将颜色转换一下
{
CCommon::TransparentColorConvert(m_taskbar_data.back_color);
CCommon::TransparentColorConvert(m_taskbar_data.transparent_color);
}
m_taskbar_data.status_bar_color = ini.GetInt(_T("task_bar"), _T("status_bar_color"), m_taskbar_data.dft_status_bar_color);
//m_taskbar_data.text_color = GetPrivateProfileInt(_T("task_bar"), _T("task_bar_text_color"), 0x00ffffffU, m_config_path.c_str());
ini.LoadTaskbarWndColors(_T("task_bar"), _T("task_bar_text_color"), m_taskbar_data.text_colors, m_taskbar_data.dft_text_colors);
m_taskbar_data.specify_each_item_color = ini.GetBool(L"task_bar", L"specify_each_item_color", false);
//m_cfg_data.m_tbar_show_cpu_memory = ini.GetBool(_T("task_bar"), _T("task_bar_show_cpu_memory"), false);
m_taskbar_data.m_tbar_display_item = ini.GetInt(L"task_bar", L"tbar_display_item", TDI_UP | TDI_DOWN);
//不含温度监控的版本,不显示温度监控相关项目
#ifdef WITHOUT_TEMPERATURE
m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_USAGE;
m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_TEMP;
m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_TEMP;
m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_TEMP;
m_taskbar_data.m_tbar_display_item &= ~TDI_MAIN_BOARD_TEMP;
m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_USAGE;
#endif
//如果选项设置中关闭了某个硬件监控,则不显示对应的温度监控相关项目
if (!m_general_data.IsHardwareEnable(HI_CPU))
m_taskbar_data.m_tbar_display_item &= ~TDI_CPU_TEMP;
if (!m_general_data.IsHardwareEnable(HI_GPU))
{
m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_USAGE;
m_taskbar_data.m_tbar_display_item &= ~TDI_GPU_TEMP;
}
if (!m_general_data.IsHardwareEnable(HI_HDD))
{
m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_TEMP;
m_taskbar_data.m_tbar_display_item &= ~TDI_HDD_USAGE;
}
if (!m_general_data.IsHardwareEnable(HI_MBD))
m_taskbar_data.m_tbar_display_item &= ~TDI_MAIN_BOARD_TEMP;
//m_taskbar_data.swap_up_down = ini.GetBool(_T("task_bar"), _T("task_bar_swap_up_down"), false);
if (m_taskbar_data.back_color == 0 && !m_taskbar_data.text_colors.empty() && m_taskbar_data.text_colors.begin()->second.label == 0) //万一读取到的背景色和文本颜色都为0(黑色),则将文本色和背景色设置成默认颜色
{
m_taskbar_data.back_color = m_taskbar_data.dft_back_color;
m_taskbar_data.text_colors.begin()->second.label = m_taskbar_data.dft_text_colors;
}
//m_taskbar_data.font.name = ini.GetString(_T("task_bar"), _T("tack_bar_font_name"), CCommon::LoadText(IDS_MICROSOFT_YAHEI)).c_str();
//m_taskbar_data.font.size = ini.GetInt(_T("task_bar"), _T("tack_bar_font_size"), 9);
default_font = FontInfo{};
default_font.name = CCommon::LoadText(IDS_DEFAULT_FONT);
default_font.size = 9;
ini.LoadFontData(_T("task_bar"), m_taskbar_data.font, default_font);
m_taskbar_data.disp_str.Get(TDI_UP) = ini.GetString(L"task_bar", L"up_string", L"↑: $");
m_taskbar_data.disp_str.Get(TDI_DOWN) = ini.GetString(L"task_bar", L"down_string", L"↓: $");
m_taskbar_data.disp_str.Get(TDI_TOTAL_SPEED) = ini.GetString(L"task_bar", L"total_speed_string", L"↑↓: $");
m_taskbar_data.disp_str.Get(TDI_CPU) = ini.GetString(L"task_bar", L"cpu_string", L"CPU: $");
m_taskbar_data.disp_str.Get(TDI_MEMORY) = ini.GetString(L"task_bar", L"memory_string", CCommon::LoadText(IDS_MEMORY_DISP, _T(": $")));
m_taskbar_data.disp_str.Get(TDI_GPU_USAGE) = ini.GetString(L"task_bar", L"gpu_string", CCommon::LoadText(IDS_GPU_DISP, _T(": $")));
m_taskbar_data.disp_str.Get(TDI_CPU_TEMP) = ini.GetString(L"task_bar", L"cpu_temp_string", L"CPU: $");
m_taskbar_data.disp_str.Get(TDI_GPU_TEMP) = ini.GetString(L"task_bar", L"gpu_temp_string", CCommon::LoadText(IDS_GPU_DISP, _T(": ")));
m_taskbar_data.disp_str.Get(TDI_HDD_TEMP) = ini.GetString(L"task_bar", L"hdd_temp_string", CCommon::LoadText(IDS_HDD_DISP, _T(": ")));
m_taskbar_data.disp_str.Get(TDI_MAIN_BOARD_TEMP) = ini.GetString(L"task_bar", L"main_board_temp_string", CCommon::LoadText(IDS_MAINBOARD_DISP, _T(": ")));
m_taskbar_data.disp_str.Get(TDI_HDD_USAGE) = ini.GetString(L"task_bar", L"hdd_string", CCommon::LoadText(IDS_HDD_DISP, _T(": ")));
m_taskbar_data.disp_str.Get(TDI_CPU_FREQ) = ini.GetString(L"task_bar", L"cpu_freq_string", CCommon::LoadText(IDS_CPU_FREQ, _T(": $")));
ini.LoadPluginDisplayStr(false);
m_taskbar_data.tbar_wnd_on_left = ini.GetBool(_T("task_bar"), _T("task_bar_wnd_on_left"), false);
m_taskbar_data.speed_short_mode = ini.GetBool(_T("task_bar"), _T("task_bar_speed_short_mode"), false);
m_taskbar_data.tbar_wnd_snap = ini.GetBool(_T("task_bar"), _T("task_bar_wnd_snap"), false);
m_taskbar_data.unit_byte = ini.GetBool(_T("task_bar"), _T("unit_byte"), true);
m_taskbar_data.speed_unit = static_cast(ini.GetInt(_T("task_bar"), _T("task_bar_speed_unit"), 0));
m_taskbar_data.hide_unit = ini.GetBool(_T("task_bar"), _T("task_bar_hide_unit"), false);
m_taskbar_data.hide_percent = ini.GetBool(_T("task_bar"), _T("task_bar_hide_percent"), false);
m_taskbar_data.value_right_align = ini.GetBool(_T("task_bar"), _T("value_right_align"), false);
m_taskbar_data.horizontal_arrange = ini.GetBool(_T("task_bar"), _T("horizontal_arrange"), false);
m_taskbar_data.show_status_bar = ini.GetBool(_T("task_bar"), _T("show_status_bar"), false);
m_taskbar_data.separate_value_unit_with_space = ini.GetBool(_T("task_bar"), _T("separate_value_unit_with_space"), true);
m_taskbar_data.show_tool_tip = ini.GetBool(_T("task_bar"), _T("show_tool_tip"), true);
m_taskbar_data.digits_number = ini.GetInt(_T("task_bar"), _T("digits_number"), 4);
m_taskbar_data.memory_display = static_cast(ini.GetInt(L"task_bar", L"memory_display", static_cast(MemoryDisplay::USAGE_PERCENTAGE)));
m_taskbar_data.double_click_action = static_cast(ini.GetInt(_T("task_bar"), _T("double_click_action"), 0));
m_taskbar_data.double_click_exe = ini.GetString(L"task_bar", L"double_click_exe", (theApp.m_system_dir + L"\\Taskmgr.exe").c_str());
m_taskbar_data.cm_graph_type = ini.GetBool(_T("task_bar"), _T("cm_graph_type"), true);
m_taskbar_data.show_graph_dashed_box = ini.GetBool(L"task_bar", L"show_graph_dashed_box", false);
m_taskbar_data.item_space = ini.GetInt(L"task_bar", L"item_space", 4);
m_taskbar_data.ValidItemSpace();
if (m_win_version.IsWindows10OrLater()) //只有Win10才支持自动适应系统深色/浅色主题
m_taskbar_data.auto_adapt_light_theme = ini.GetBool(L"task_bar", L"auto_adapt_light_theme", false);
else
m_taskbar_data.auto_adapt_light_theme = false;
m_taskbar_data.dark_default_style = ini.GetInt(L"task_bar", L"dark_default_style", 0);
m_taskbar_data.light_default_style = ini.GetInt(L"task_bar", L"light_default_style", TASKBAR_DEFAULT_LIGHT_STYLE_INDEX);
if (m_win_version.IsWindows8OrLater())
m_taskbar_data.auto_set_background_color = ini.GetBool(L"task_bar", L"auto_set_background_color", false);
else
m_taskbar_data.auto_set_background_color = false;
m_taskbar_data.item_order.Init();
m_taskbar_data.item_order.FromString(ini.GetString(L"task_bar", L"item_order", L""));
m_taskbar_data.plugin_display_item.FromString(ini.GetString(L"task_bar", L"plugin_display_item", L""));
m_taskbar_data.auto_save_taskbar_color_settings_to_preset = ini.GetBool(L"task_bar", L"auto_save_taskbar_color_settings_to_preset", true);
m_taskbar_data.show_netspeed_figure = ini.GetBool(L"task_bar", L"show_netspeed_figure", false);
m_taskbar_data.netspeed_figure_max_value = ini.GetInt(L"task_bar", L"netspeed_figure_max_value", 512);
m_taskbar_data.netspeed_figure_max_value_unit = ini.GetInt(L"task_bar", L"netspeed_figure_max_value_unit", 0);
//其他设置
//m_cfg_data.m_show_internet_ip = ini.GetBool(L"connection_details", L"show_internet_ip", false);
m_cfg_data.m_use_log_scale = ini.GetBool(_T("histroy_traffic"), _T("use_log_scale"), true);
m_cfg_data.m_sunday_first = ini.GetBool(_T("histroy_traffic"), _T("sunday_first"), true);
m_cfg_data.m_view_type = static_cast(ini.GetInt(_T("histroy_traffic"), _T("view_type"), static_cast(HistoryTrafficViewType::HV_DAY)));
m_no_multistart_warning = ini.GetBool(_T("other"), _T("no_multistart_warning"), false);
m_notify_interval = ini.GetInt(_T("other"), _T("notify_interval"), 60);
m_exit_when_start_by_restart_manager = ini.GetBool(_T("other"), _T("exit_when_start_by_restart_manager"), true);
m_debug_log = ini.GetBool(_T("other"), _T("debug_log"), false);
//由于Win7系统中设置任务栏窗口透明色会导致任务栏窗口不可见,因此默认在Win7中禁用透明色的设定
m_taksbar_transparent_color_enable = ini.GetBool(L"other", L"taksbar_transparent_color_enable", !m_win_version.IsWindows7());
m_last_light_mode = ini.GetBool(L"other", L"last_light_mode", CWindowsSettingHelper::IsWindows10LightTheme());
m_show_mouse_panetrate_tip = ini.GetBool(L"other", L"show_mouse_panetrate_tip", true);
m_show_dot_net_notinstalled_tip = ini.GetBool(L"other", L"show_dot_net_notinstalled_tip", true);
m_cfg_data.taskbar_left_space_win11 = ini.GetInt(L"task_bar", L"taskbar_left_space_win11", 160);
}
void CTrafficMonitorApp::SaveConfig()
{
CIniHelper ini{ m_config_path };
//常规设置
ini.WriteBool(_T("general"), _T("check_update_when_start"), m_general_data.check_update_when_start);
ini.WriteBool(_T("general"), _T("allow_skin_cover_font"), m_general_data.allow_skin_cover_font);
ini.WriteBool(_T("general"), _T("allow_skin_cover_text"), m_general_data.allow_skin_cover_text);
ini.WriteInt(_T("general"), _T("language"), static_cast(m_general_data.language));
ini.WriteBool(L"general", L"show_all_interface", m_general_data.show_all_interface);
ini.WriteBool(L"general", L"get_cpu_usage_by_cpu_times", m_general_data.m_get_cpu_usage_by_cpu_times);
ini.WriteInt(L"general", L"monitor_time_span", m_general_data.monitor_time_span);
ini.WriteString(L"general", L"hard_disk_name", m_general_data.hard_disk_name);
ini.WriteString(L"general", L"cpu_core_name", m_general_data.cpu_core_name);
ini.WriteInt(L"general", L"hardware_monitor_item", m_general_data.hardware_monitor_item);
ini.WriteStringList(L"general", L"connections_hide", m_general_data.connections_hide.ToVector());
//主窗口设置
ini.WriteInt(L"config", L"transparency", m_cfg_data.m_transparency);
ini.WriteBool(L"config", L"always_on_top", m_main_wnd_data.m_always_on_top);
ini.WriteBool(L"config", L"lock_window_pos", m_main_wnd_data.m_lock_window_pos);
ini.WriteBool(L"config", L"show_notify_icon", m_general_data.show_notify_icon);
ini.WriteBool(L"config", L"show_cpu_memory", m_cfg_data.m_show_more_info);
ini.WriteBool(L"config", L"mouse_penetrate", m_main_wnd_data.m_mouse_penetrate);
ini.WriteBool(L"config", L"show_task_bar_wnd", m_cfg_data.m_show_task_bar_wnd);
ini.WriteInt(L"config", L"position_x", m_cfg_data.m_position_x);
ini.WriteInt(L"config", L"position_y", m_cfg_data.m_position_y);
ini.WriteBool(L"connection", L"auto_select", m_cfg_data.m_auto_select);
ini.WriteBool(L"connection", L"select_all", m_cfg_data.m_select_all);
ini.SaveMainWndColors(L"config", L"text_color", m_main_wnd_data.text_colors);
ini.WriteBool(_T("config"), _T("specify_each_item_color"), m_main_wnd_data.specify_each_item_color);
ini.WriteInt(L"config", L"hide_main_window", m_cfg_data.m_hide_main_window);
ini.WriteString(L"connection", L"connection_name", CCommon::StrToUnicode(m_cfg_data.m_connection_name.c_str()));
ini.WriteString(_T("config"), _T("skin_selected"), m_cfg_data.m_skin_name.c_str());
ini.WriteInt(L"config", L"notify_icon_selected", m_cfg_data.m_notify_icon_selected);
ini.WriteBool(L"config", L"notify_icon_auto_adapt", m_cfg_data.m_notify_icon_auto_adapt);
ini.SaveFontData(L"config", m_main_wnd_data.font);
ini.WriteBool(L"config", L"swap_up_down", m_main_wnd_data.swap_up_down);
ini.WriteBool(L"config", L"hide_main_wnd_when_fullscreen", m_main_wnd_data.hide_main_wnd_when_fullscreen);
ini.WriteString(_T("config"), _T("up_string"), m_main_wnd_data.disp_str.Get(TDI_UP));
ini.WriteString(_T("config"), _T("down_string"), m_main_wnd_data.disp_str.Get(TDI_DOWN));
ini.WriteString(_T("config"), _T("total_speed_string"), m_main_wnd_data.disp_str.Get(TDI_TOTAL_SPEED));
ini.WriteString(_T("config"), _T("cpu_string"), m_main_wnd_data.disp_str.Get(TDI_CPU));
ini.WriteString(_T("config"), _T("memory_string"), m_main_wnd_data.disp_str.Get(TDI_MEMORY));
ini.WriteString(_T("config"), _T("gpu_string"), m_main_wnd_data.disp_str.Get(TDI_GPU_USAGE));
ini.WriteString(_T("config"), _T("cpu_temp_string"), m_main_wnd_data.disp_str.Get(TDI_CPU_TEMP));
ini.WriteString(_T("config"), _T("cpu_freq_string"), m_main_wnd_data.disp_str.Get(TDI_CPU_FREQ));
ini.WriteString(_T("config"), _T("gpu_temp_string"), m_main_wnd_data.disp_str.Get(TDI_GPU_TEMP));
ini.WriteString(_T("config"), _T("hdd_temp_string"), m_main_wnd_data.disp_str.Get(TDI_HDD_TEMP));
ini.WriteString(_T("config"), _T("main_board_temp_string"), m_main_wnd_data.disp_str.Get(TDI_MAIN_BOARD_TEMP));
ini.WriteString(_T("config"), _T("hdd_string"), m_main_wnd_data.disp_str.Get(TDI_HDD_USAGE));
ini.SavePluginDisplayStr(true);
ini.WriteBool(L"config", L"speed_short_mode", m_main_wnd_data.speed_short_mode);
ini.WriteBool(L"config", L"separate_value_unit_with_space", m_main_wnd_data.separate_value_unit_with_space);
ini.WriteBool(L"config", L"show_tool_tip", m_main_wnd_data.show_tool_tip);
ini.WriteInt(L"config", L"memory_display", static_cast(m_main_wnd_data.memory_display));
ini.WriteBool(L"config", L"unit_byte", m_main_wnd_data.unit_byte);
ini.WriteInt(L"config", L"speed_unit", static_cast(m_main_wnd_data.speed_unit));
ini.WriteBool(L"config", L"hide_unit", m_main_wnd_data.hide_unit);
ini.WriteBool(L"config", L"hide_percent", m_main_wnd_data.hide_percent);
ini.WriteInt(L"config", L"double_click_action", static_cast(m_main_wnd_data.double_click_action));
ini.WriteString(L"config", L"double_click_exe", m_main_wnd_data.double_click_exe);
ini.WriteInt(L"config", L"alow_out_of_border", m_main_wnd_data.m_alow_out_of_border);
ini.WriteBool(L"notify_tip", L"traffic_tip_enable", m_general_data.traffic_tip_enable);
ini.WriteInt(L"notify_tip", L"traffic_tip_value", m_general_data.traffic_tip_value);
ini.WriteInt(L"notify_tip", L"traffic_tip_unit", m_general_data.traffic_tip_unit);
ini.WriteBool(L"notify_tip", L"memory_usage_tip_enable", m_general_data.memory_usage_tip.enable);
ini.WriteInt(L"notify_tip", L"memory_tip_value", m_general_data.memory_usage_tip.tip_value);
ini.WriteBool(L"notify_tip", L"cpu_temperature_tip_enable", m_general_data.cpu_temp_tip.enable);
ini.WriteInt(L"notify_tip", L"cpu_temperature_tip_value", m_general_data.cpu_temp_tip.tip_value);
ini.WriteBool(L"notify_tip", L"gpu_temperature_tip_enable", m_general_data.gpu_temp_tip.enable);
ini.WriteInt(L"notify_tip", L"gpu_temperature_tip_value", m_general_data.gpu_temp_tip.tip_value);
ini.WriteBool(L"notify_tip", L"hdd_temperature_tip_enable", m_general_data.hdd_temp_tip.enable);
ini.WriteInt(L"notify_tip", L"hdd_temperature_tip_value", m_general_data.hdd_temp_tip.tip_value);
ini.WriteBool(L"notify_tip", L"mainboard_temperature_tip_enable", m_general_data.mainboard_temp_tip.enable);
ini.WriteInt(L"notify_tip", L"mainboard_temperature_tip_value", m_general_data.mainboard_temp_tip.tip_value);
//任务栏窗口设置
ini.WriteInt(L"task_bar", L"task_bar_back_color", m_taskbar_data.back_color);
ini.WriteInt(L"task_bar", L"transparent_color", m_taskbar_data.transparent_color);
ini.WriteInt(L"task_bar", L"status_bar_color", m_taskbar_data.status_bar_color);
ini.SaveTaskbarWndColors(L"task_bar", L"task_bar_text_color", m_taskbar_data.text_colors);
ini.WriteBool(L"task_bar", L"specify_each_item_color", m_taskbar_data.specify_each_item_color);
//ini.WriteBool(L"task_bar", L"task_bar_show_cpu_memory", m_cfg_data.m_tbar_show_cpu_memory);
ini.WriteInt(L"task_bar", L"tbar_display_item", m_taskbar_data.m_tbar_display_item);
ini.SaveFontData(L"task_bar", m_taskbar_data.font);
//ini.WriteBool(L"task_bar", L"task_bar_swap_up_down", m_taskbar_data.swap_up_down);
ini.WriteString(_T("task_bar"), _T("up_string"), m_taskbar_data.disp_str.Get(TDI_UP));
ini.WriteString(_T("task_bar"), _T("down_string"), m_taskbar_data.disp_str.Get(TDI_DOWN));
ini.WriteString(_T("task_bar"), _T("total_speed_string"), m_taskbar_data.disp_str.Get(TDI_TOTAL_SPEED));
ini.WriteString(_T("task_bar"), _T("cpu_string"), m_taskbar_data.disp_str.Get(TDI_CPU));
ini.WriteString(_T("task_bar"), _T("memory_string"), m_taskbar_data.disp_str.Get(TDI_MEMORY));
ini.WriteString(_T("task_bar"), _T("gpu_string"), m_taskbar_data.disp_str.Get(TDI_GPU_USAGE));
ini.WriteString(_T("task_bar"), _T("cpu_temp_string"), m_taskbar_data.disp_str.Get(TDI_CPU_TEMP));
ini.WriteString(_T("task_bar"), _T("cpu_freq_string"), m_taskbar_data.disp_str.Get(TDI_CPU_FREQ));
ini.WriteString(_T("task_bar"), _T("gpu_temp_string"), m_taskbar_data.disp_str.Get(TDI_GPU_TEMP));
ini.WriteString(_T("task_bar"), _T("hdd_temp_string"), m_taskbar_data.disp_str.Get(TDI_HDD_TEMP));
ini.WriteString(_T("task_bar"), _T("main_board_temp_string"), m_taskbar_data.disp_str.Get(TDI_MAIN_BOARD_TEMP));
ini.WriteString(_T("task_bar"), _T("hdd_string"), m_taskbar_data.disp_str.Get(TDI_HDD_USAGE));
ini.SavePluginDisplayStr(false);
ini.WriteBool(L"task_bar", L"task_bar_wnd_on_left", m_taskbar_data.tbar_wnd_on_left);
ini.WriteBool(L"task_bar", L"task_bar_wnd_snap", m_taskbar_data.tbar_wnd_snap);
ini.WriteBool(L"task_bar", L"task_bar_speed_short_mode", m_taskbar_data.speed_short_mode);
ini.WriteBool(L"task_bar", L"unit_byte", m_taskbar_data.unit_byte);
ini.WriteInt(L"task_bar", L"task_bar_speed_unit", static_cast(m_taskbar_data.speed_unit));
ini.WriteBool(L"task_bar", L"task_bar_hide_unit", m_taskbar_data.hide_unit);
ini.WriteBool(L"task_bar", L"task_bar_hide_percent", m_taskbar_data.hide_percent);
ini.WriteBool(L"task_bar", L"value_right_align", m_taskbar_data.value_right_align);
ini.WriteBool(L"task_bar", L"horizontal_arrange", m_taskbar_data.horizontal_arrange);
ini.WriteBool(L"task_bar", L"show_status_bar", m_taskbar_data.show_status_bar);
ini.WriteBool(L"task_bar", L"separate_value_unit_with_space", m_taskbar_data.separate_value_unit_with_space);
ini.WriteBool(L"task_bar", L"show_tool_tip", m_taskbar_data.show_tool_tip);
ini.WriteInt(L"task_bar", L"digits_number", m_taskbar_data.digits_number);
ini.WriteInt(L"task_bar", L"memory_display", static_cast