├── .gitattributes ├── .github └── workflows │ └── build_and_release.yaml ├── .gitignore ├── DisplayDataChannelTest ├── DisplayDataChannelTest.c ├── DisplayDataChannelTest.vcxproj └── DisplayDataChannelTest.vcxproj.filters ├── LICENSE.md ├── LibDisplayDataChannel ├── DllMain.c ├── Framework.h ├── LibDisplayDataChannel.c ├── LibDisplayDataChannel.h ├── LibDisplayDataChannel.vcxproj ├── LibDisplayDataChannel.vcxproj.filters └── cpp.hint ├── MonitorBrightnessAdjuster.sln ├── MonitorBrightnessAdjuster ├── AboutUtil.cs ├── Adjusters │ ├── AdjusterSingletons.cs │ ├── DdcAdjuster.cs │ ├── IAdjuster.cs │ └── WmiAdjuster.cs ├── App.xaml ├── App.xaml.cs ├── AssemblyInfo.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── MainWindowViewModel.cs ├── MonitorBrightnessAdjuster.csproj ├── MonitorBrightnessModel.cs ├── Resources │ ├── Icon.ico │ └── Manifest.xml └── packages.lock.json ├── Previews └── Preview1.png ├── README-CN.md └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.github/workflows/build_and_release.yaml: -------------------------------------------------------------------------------- 1 | name: Build And Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - "v*.*.*" 7 | 8 | permissions: 9 | contents: write 10 | 11 | jobs: 12 | build_and_release: 13 | runs-on: windows-latest 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v4 17 | 18 | - name: Setup MSBuild 19 | uses: microsoft/setup-msbuild@v2 20 | with: 21 | msbuild-architecture: x64 22 | 23 | - name: Cache NuGet packages 24 | uses: actions/cache@v4 25 | id: cache 26 | with: 27 | path: ~/.nuget/packages 28 | key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} 29 | 30 | - name: Build 31 | run: msbuild -m -t:Restore,Build -p:Configuration=Release -p:Platform=x64 -p:RestoreLockedMode=true 32 | 33 | - name: Package 34 | working-directory: MonitorBrightnessAdjuster/bin/Release/net472 35 | run: Get-ChildItem | Compress-Archive -CompressionLevel Optimal -DestinationPath MonitorBrightnessAdjuster.zip 36 | 37 | - name: Release 38 | uses: softprops/action-gh-release@v2 39 | with: 40 | fail_on_unmatched_files: true 41 | files: MonitorBrightnessAdjuster/bin/Release/net472/MonitorBrightnessAdjuster.zip 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /DisplayDataChannelTest/DisplayDataChannelTest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../LibDisplayDataChannel/LibDisplayDataChannel.h" 3 | 4 | int main() { 5 | void* handle = DdcInitialize(); 6 | 7 | uint32_t count; 8 | if (!DdcGetAvailableCount(handle, &count)) { 9 | puts("ERROR"); 10 | return -1; 11 | } 12 | 13 | uint32_t currentBrightness = 0; 14 | uint32_t minimumBrightness = 0; 15 | uint32_t maximumBrightness = 0; 16 | printf_s("COUNT: %d\n\nGET\n", count); 17 | for (uint32_t i = 0; i < (uint32_t) count; i++) { 18 | if (DdcGetBrightness(handle, i, ¤tBrightness, &minimumBrightness, &maximumBrightness)) { 19 | printf_s("NUM: %u, CUR: %u, MIN: %u, MAX: %u\n", i, currentBrightness, minimumBrightness, maximumBrightness); 20 | } else { 21 | printf_s("NUM: %u, N/A\n", i); 22 | } 23 | } 24 | 25 | printf_s("\nSET\nNUM: "); 26 | uint32_t num; 27 | scanf_s("%u", &num); 28 | uint32_t percent; 29 | printf_s("PER: "); 30 | scanf_s("%u", &percent); 31 | if (DdcSetBrightness(handle, num, (uint32_t) ((float) (maximumBrightness - minimumBrightness) * percent / 100 + minimumBrightness))) { 32 | puts("\nOK"); 33 | } 34 | 35 | DdcDestroy(handle); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /DisplayDataChannelTest/DisplayDataChannelTest.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 17.0 23 | Win32Proj 24 | {5f8427ff-0357-4aba-90b5-88e5f9e254f8} 25 | DisplayDataChannelTest 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | 80 | 81 | Console 82 | true 83 | $(TargetDir)\LibDisplayDataChannel.lib;%(AdditionalDependencies) 84 | 85 | 86 | 87 | 88 | Level3 89 | true 90 | true 91 | true 92 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 93 | true 94 | 95 | 96 | Console 97 | true 98 | true 99 | true 100 | $(TargetDir)\LibDisplayDataChannel.lib;%(AdditionalDependencies) 101 | 102 | 103 | 104 | 105 | Level3 106 | true 107 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 108 | true 109 | 110 | 111 | Console 112 | true 113 | $(TargetDir)\LibDisplayDataChannel.lib;%(AdditionalDependencies) 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | 125 | 126 | Console 127 | true 128 | true 129 | true 130 | $(TargetDir)\LibDisplayDataChannel.lib;%(AdditionalDependencies) 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /DisplayDataChannelTest/DisplayDataChannelTest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | **Copyright © 2023 YukiIsait** 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/DllMain.c: -------------------------------------------------------------------------------- 1 | #include "Framework.h" 2 | 3 | BOOL APIENTRY DllMain(HMODULE module, DWORD reasonForCall, LPVOID reserved) { 4 | return TRUE; 5 | } 6 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/Framework.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define WIN32_LEAN_AND_MEAN 6 | #include 7 | 8 | #include 9 | #include 10 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/LibDisplayDataChannel.c: -------------------------------------------------------------------------------- 1 | #include "Framework.h" 2 | #include "LibDisplayDataChannel.h" 3 | 4 | typedef struct DdcDataStruct { 5 | DWORD monitorCount; 6 | DWORD availableMonitorCount; 7 | LPHANDLE availablePhysicalMonitorHandles; 8 | LPPHYSICAL_MONITOR physicalMonitors; 9 | } DdcData, *PDdcData; 10 | 11 | typedef struct MonitorEnumProcParamStruct { 12 | BOOL countOnly; 13 | PDdcData ddcData; 14 | } MonitorEnumProcParam, *PMonitorEnumProcParam; 15 | 16 | static BOOL CALLBACK MonitorEnumProc(HMONITOR monitor, HDC dc, LPRECT rect, LPARAM data) { 17 | PMonitorEnumProcParam param = (PMonitorEnumProcParam) data; 18 | PDdcData ddcData = param->ddcData; 19 | // 当前监视器数量 20 | DWORD monitorCount = 0; 21 | // 统计每个监视器句柄对应的多个物理监视器数量 22 | if (!GetNumberOfPhysicalMonitorsFromHMONITOR(monitor, &monitorCount)) { 23 | return FALSE; 24 | } 25 | ddcData->monitorCount += monitorCount; 26 | // 仅计数则直接返回 27 | if (param->countOnly) { 28 | return TRUE; 29 | } 30 | // 当前监视器数组位置 31 | LPPHYSICAL_MONITOR currentPhysicalMonitors = &ddcData->physicalMonitors[ddcData->monitorCount - monitorCount]; 32 | // 获取对应的物理监视器句柄 33 | if (!GetPhysicalMonitorsFromHMONITOR(monitor, monitorCount, currentPhysicalMonitors)) { 34 | return FALSE; 35 | } 36 | // 统计支持亮度调节的监视器 37 | for (DWORD i = 0; i < monitorCount; i++) { 38 | DWORD currentBrightness; 39 | DWORD minimumBrightness; 40 | DWORD maximumBrightness; 41 | if (GetMonitorBrightness(currentPhysicalMonitors[i].hPhysicalMonitor, 42 | &minimumBrightness, 43 | ¤tBrightness, 44 | &maximumBrightness)) { 45 | // 记录支持亮度调节的监视器句柄 46 | ddcData->availablePhysicalMonitorHandles[ddcData->availableMonitorCount++] = currentPhysicalMonitors[i].hPhysicalMonitor; 47 | } 48 | } 49 | return TRUE; 50 | } 51 | 52 | void* DdcInitialize() { 53 | HANDLE processHeap = GetProcessHeap(); 54 | // 初始化变量 55 | PDdcData ddcData = (PDdcData) HeapAlloc(processHeap, HEAP_ZERO_MEMORY, sizeof(DdcData)); 56 | if (!ddcData) { 57 | DdcDestroy(ddcData); 58 | return NULL; 59 | } 60 | // 首次遍历获取监视器数量 61 | MonitorEnumProcParam param = { 62 | .countOnly = TRUE, 63 | .ddcData = ddcData 64 | }; 65 | if (!EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM) ¶m)) { 66 | DdcDestroy(ddcData); 67 | return NULL; 68 | } 69 | // 初始化监视器数组和可用标记数组 70 | ddcData->physicalMonitors = (LPPHYSICAL_MONITOR) HeapAlloc(processHeap, 0, ddcData->monitorCount * sizeof(PHYSICAL_MONITOR)); 71 | ddcData->availablePhysicalMonitorHandles = (LPHANDLE) HeapAlloc(processHeap, 0, ddcData->monitorCount * sizeof(HANDLE)); 72 | ddcData->monitorCount = 0; 73 | ddcData->availableMonitorCount = 0; 74 | if (!ddcData->physicalMonitors || !ddcData->availablePhysicalMonitorHandles) { 75 | DdcDestroy(ddcData); 76 | return NULL; 77 | } 78 | // 获取所有监视器以及是否可用的状态 79 | param.countOnly = FALSE; 80 | if (!EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM) ¶m)) { 81 | DdcDestroy(ddcData); 82 | return NULL; 83 | } 84 | return ddcData; 85 | } 86 | 87 | void DdcDestroy(void* handle) { 88 | if (!handle) { 89 | return; 90 | } 91 | HANDLE processHeap = GetProcessHeap(); 92 | PDdcData ddcData = (PDdcData) handle; 93 | DestroyPhysicalMonitors(ddcData->monitorCount, ddcData->physicalMonitors); 94 | HeapFree(processHeap, 0, ddcData->physicalMonitors); 95 | HeapFree(processHeap, 0, ddcData->availablePhysicalMonitorHandles); 96 | HeapFree(processHeap, 0, ddcData); 97 | } 98 | 99 | bool DdcGetAvailableCount(void* handle, uint32_t* count) { 100 | if (!handle || !count) { 101 | return false; 102 | } 103 | PDdcData ddcData = (PDdcData) handle; 104 | *count = ddcData->availableMonitorCount; 105 | return true; 106 | } 107 | 108 | bool DdcGetBrightness(void* handle, 109 | uint32_t monitorIndex, 110 | uint32_t* currentBrightness, 111 | uint32_t* minimumBrightness, 112 | uint32_t* maximumBrightness) { 113 | if (!handle || !currentBrightness || !minimumBrightness || !maximumBrightness) { 114 | return false; 115 | } 116 | PDdcData ddcData = (PDdcData) handle; 117 | if (monitorIndex >= ddcData->availableMonitorCount) { 118 | return false; 119 | } 120 | HANDLE physicalMonitorHandle = ddcData->availablePhysicalMonitorHandles[monitorIndex]; 121 | return GetMonitorBrightness(physicalMonitorHandle, minimumBrightness, currentBrightness, maximumBrightness); 122 | } 123 | 124 | bool DdcSetBrightness(void* handle, uint32_t monitorIndex, uint32_t brightness) { 125 | if (!handle) { 126 | return false; 127 | } 128 | PDdcData ddcData = (PDdcData) handle; 129 | if (monitorIndex >= ddcData->availableMonitorCount) { 130 | return false; 131 | } 132 | HANDLE physicalMonitorHandle = ddcData->availablePhysicalMonitorHandles[monitorIndex]; 133 | return SetMonitorBrightness(physicalMonitorHandle, brightness); 134 | } 135 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/LibDisplayDataChannel.h: -------------------------------------------------------------------------------- 1 | #ifdef LIBDISPLAYDATACHANNEL_EXPORTS 2 | #define LIBDISPLAYDATACHANNEL_API __declspec(dllexport) 3 | #else 4 | #define LIBDISPLAYDATACHANNEL_API __declspec(dllimport) 5 | #endif 6 | 7 | #ifdef __cplusplus 8 | #include 9 | #include 10 | extern "C" { 11 | #else 12 | #include 13 | #include 14 | #endif 15 | 16 | LIBDISPLAYDATACHANNEL_API void* DdcInitialize(); 17 | LIBDISPLAYDATACHANNEL_API void DdcDestroy(void* handle); 18 | 19 | LIBDISPLAYDATACHANNEL_API bool DdcGetAvailableCount(void* handle, 20 | uint32_t* count); 21 | LIBDISPLAYDATACHANNEL_API bool DdcGetBrightness(void* handle, 22 | uint32_t monitorIndex, 23 | uint32_t* currentBrightness, 24 | uint32_t* minimumBrightness, 25 | uint32_t* maximumBrightness); 26 | LIBDISPLAYDATACHANNEL_API bool DdcSetBrightness(void* handle, 27 | uint32_t monitorIndex, 28 | uint32_t brightness); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/LibDisplayDataChannel.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 17.0 23 | Win32Proj 24 | {999e5ba4-37e7-4662-ad0e-ddb3d7230ea1} 25 | LibDisplayDataChannel 26 | 10.0 27 | 28 | 29 | 30 | DynamicLibrary 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | DynamicLibrary 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | DynamicLibrary 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | DynamicLibrary 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | LibDisplayDataChannel 75 | false 76 | 77 | 78 | LibDisplayDataChannel 79 | false 80 | 81 | 82 | LibDisplayDataChannel 83 | false 84 | 85 | 86 | LibDisplayDataChannel 87 | false 88 | 89 | 90 | 91 | Level3 92 | false 93 | WIN32;_DEBUG;LIBDISPLAYDATACHANNEL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) 94 | true 95 | NotUsing 96 | pch.h 97 | stdcpp17 98 | stdc17 99 | false 100 | Default 101 | 102 | 103 | Windows 104 | true 105 | false 106 | Dxva2.lib;%(AdditionalDependencies) 107 | DllMain 108 | 109 | 110 | 111 | 112 | Level3 113 | true 114 | true 115 | false 116 | WIN32;NDEBUG;LIBDISPLAYDATACHANNEL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) 117 | true 118 | NotUsing 119 | pch.h 120 | stdcpp17 121 | stdc17 122 | false 123 | 124 | 125 | Windows 126 | true 127 | true 128 | true 129 | false 130 | Dxva2.lib;%(AdditionalDependencies) 131 | DllMain 132 | 133 | 134 | 135 | 136 | Level3 137 | false 138 | _DEBUG;LIBDISPLAYDATACHANNEL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) 139 | true 140 | NotUsing 141 | pch.h 142 | stdcpp17 143 | stdc17 144 | false 145 | Default 146 | 147 | 148 | Windows 149 | true 150 | false 151 | Dxva2.lib;%(AdditionalDependencies) 152 | DllMain 153 | 154 | 155 | 156 | 157 | Level3 158 | true 159 | true 160 | false 161 | NDEBUG;LIBDISPLAYDATACHANNEL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) 162 | true 163 | NotUsing 164 | pch.h 165 | stdcpp17 166 | stdc17 167 | false 168 | 169 | 170 | Windows 171 | true 172 | true 173 | true 174 | false 175 | Dxva2.lib;%(AdditionalDependencies) 176 | DllMain 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/LibDisplayDataChannel.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /LibDisplayDataChannel/cpp.hint: -------------------------------------------------------------------------------- 1 | #define LIBDISPLAYDATACHANNEL_API __declspec(dllexport) 2 | #define LIBDISPLAYDATACHANNEL_API __declspec(dllimport) 3 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.8.34408.163 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibDisplayDataChannel", "LibDisplayDataChannel\LibDisplayDataChannel.vcxproj", "{999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DisplayDataChannelTest", "DisplayDataChannelTest\DisplayDataChannelTest.vcxproj", "{5F8427FF-0357-4ABA-90B5-88E5F9E254F8}" 9 | ProjectSection(ProjectDependencies) = postProject 10 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1} = {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1} 11 | EndProjectSection 12 | EndProject 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonitorBrightnessAdjuster", "MonitorBrightnessAdjuster\MonitorBrightnessAdjuster.csproj", "{6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}" 14 | ProjectSection(ProjectDependencies) = postProject 15 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1} = {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1} 16 | EndProjectSection 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Debug|x64 = Debug|x64 22 | Debug|x86 = Debug|x86 23 | Release|Any CPU = Release|Any CPU 24 | Release|x64 = Release|x64 25 | Release|x86 = Release|x86 26 | EndGlobalSection 27 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 28 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|Any CPU.ActiveCfg = Debug|x64 29 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|Any CPU.Build.0 = Debug|x64 30 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|x64.ActiveCfg = Debug|x64 31 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|x64.Build.0 = Debug|x64 32 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|x86.ActiveCfg = Debug|Win32 33 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Debug|x86.Build.0 = Debug|Win32 34 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|Any CPU.ActiveCfg = Release|x64 35 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|Any CPU.Build.0 = Release|x64 36 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|x64.ActiveCfg = Release|x64 37 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|x64.Build.0 = Release|x64 38 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|x86.ActiveCfg = Release|Win32 39 | {999E5BA4-37E7-4662-AD0E-DDB3D7230EA1}.Release|x86.Build.0 = Release|Win32 40 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|Any CPU.ActiveCfg = Debug|x64 41 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|Any CPU.Build.0 = Debug|x64 42 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|x64.ActiveCfg = Debug|x64 43 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|x64.Build.0 = Debug|x64 44 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|x86.ActiveCfg = Debug|Win32 45 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Debug|x86.Build.0 = Debug|Win32 46 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Release|Any CPU.ActiveCfg = Release|x64 47 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Release|x64.ActiveCfg = Release|x64 48 | {5F8427FF-0357-4ABA-90B5-88E5F9E254F8}.Release|x86.ActiveCfg = Release|Win32 49 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 50 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|Any CPU.Build.0 = Debug|Any CPU 51 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|x64.ActiveCfg = Debug|Any CPU 52 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|x64.Build.0 = Debug|Any CPU 53 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|x86.ActiveCfg = Debug|Any CPU 54 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Debug|x86.Build.0 = Debug|Any CPU 55 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|Any CPU.Build.0 = Release|Any CPU 57 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|x64.ActiveCfg = Release|Any CPU 58 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|x64.Build.0 = Release|Any CPU 59 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|x86.ActiveCfg = Release|Any CPU 60 | {6DE9F24C-4328-4D6D-8A4B-7073ADB90FD7}.Release|x86.Build.0 = Release|Any CPU 61 | EndGlobalSection 62 | GlobalSection(SolutionProperties) = preSolution 63 | HideSolutionNode = FALSE 64 | EndGlobalSection 65 | GlobalSection(ExtensibilityGlobals) = postSolution 66 | SolutionGuid = {D83977A6-A581-482B-B797-84965B1DE1B7} 67 | EndGlobalSection 68 | EndGlobal 69 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/AboutUtil.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Text; 3 | 4 | namespace MonitorBrightnessAdjuster { 5 | public static class AboutUtil { 6 | public static string GetAboutInformation() { 7 | Assembly assembly = Assembly.GetExecutingAssembly(); 8 | AssemblyProductAttribute product = (AssemblyProductAttribute) Attribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute)); 9 | AssemblyFileVersionAttribute version = (AssemblyFileVersionAttribute) Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)); 10 | AssemblyDescriptionAttribute description = (AssemblyDescriptionAttribute) Attribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute)); 11 | AssemblyCopyrightAttribute copyright = (AssemblyCopyrightAttribute) Attribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute)); 12 | 13 | StringBuilder sb = new(); 14 | sb.Append(product.Product) 15 | .Append(' ') 16 | .Append(version.Version) 17 | .Append(Environment.NewLine) 18 | .Append(copyright.Copyright) 19 | .Append(Environment.NewLine) 20 | .Append(Environment.NewLine) 21 | .Append(description.Description); 22 | return sb.ToString(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Adjusters/AdjusterSingletons.cs: -------------------------------------------------------------------------------- 1 | namespace MonitorBrightnessAdjuster.Adjusters { 2 | public class AdjusterSingletons { 3 | private static readonly IAdjuster wmiAdjuster = new WmiAdjuster(); 4 | private static readonly IAdjuster ddcAdjuster = new DdcAdjuster(); 5 | 6 | public static IAdjuster WmiAdjusterInstance { 7 | get => wmiAdjuster; 8 | } 9 | 10 | public static IAdjuster DdcAdjusterInstance { 11 | get => ddcAdjuster; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Adjusters/DdcAdjuster.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace MonitorBrightnessAdjuster.Adjusters { 5 | public sealed class DdcAdjuster: IAdjuster, IDisposable { 6 | private UIntPtr handle; 7 | 8 | public DdcAdjuster() { 9 | handle = DdcInitialize(); 10 | if (handle == UIntPtr.Zero) { 11 | throw new Win32Exception(); 12 | } 13 | } 14 | 15 | ~DdcAdjuster() { 16 | Destroy(); 17 | } 18 | 19 | public void Dispose() { 20 | Destroy(); 21 | GC.SuppressFinalize(this); 22 | } 23 | 24 | private void Destroy() { 25 | if (handle != UIntPtr.Zero) { 26 | DdcDestroy(handle); 27 | handle = UIntPtr.Zero; 28 | } 29 | } 30 | 31 | public int GetNumberOfMonitors() { 32 | if (!DdcGetAvailableCount(handle, out uint count)) { 33 | throw new Win32Exception(); 34 | } 35 | return (int) count; 36 | } 37 | 38 | public int GetBrightnessPercentage(int monitorIndex) { 39 | if (monitorIndex < 0) { 40 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 41 | } 42 | if (!DdcGetBrightness(handle, (uint) monitorIndex, out uint currentBrightness, out uint minimumBrightness, out uint maximumBrightness)) { 43 | throw new Win32Exception(); 44 | } 45 | // 计算当前亮度在亮度区间的百分比 46 | return (int) ((float) currentBrightness / (maximumBrightness - minimumBrightness) * 100); 47 | } 48 | 49 | public void SetBrightnessPercentage(int monitorIndex, int brightness) { 50 | if (monitorIndex < 0) { 51 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 52 | } 53 | if (brightness < 0 || brightness > 100) { 54 | throw new ArgumentOutOfRangeException(nameof(brightness)); 55 | } 56 | if (!DdcGetBrightness(handle, (uint) monitorIndex, out _, out uint minimumBrightness, out uint maximumBrightness)) { 57 | throw new Win32Exception(); 58 | } 59 | // 将亮度百分比在亮度区间做映射 60 | uint newBrightness = (uint) ((float) (maximumBrightness - minimumBrightness) * brightness / 100 + minimumBrightness); 61 | if (!DdcSetBrightness(handle, (uint) monitorIndex, newBrightness)) { 62 | throw new Win32Exception(); 63 | } 64 | } 65 | 66 | [DllImport("LibDisplayDataChannel.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 67 | private static extern UIntPtr DdcInitialize(); 68 | 69 | [DllImport("LibDisplayDataChannel.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 70 | private static extern void DdcDestroy(UIntPtr handle); 71 | 72 | [DllImport("LibDisplayDataChannel.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 73 | private static extern bool DdcGetAvailableCount(UIntPtr handle, out uint count); 74 | 75 | [DllImport("LibDisplayDataChannel.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 76 | private static extern bool DdcGetBrightness(UIntPtr handle, uint monitorIndex, out uint currentBrightness, out uint minimumBrightness, out uint maximumBrightness); 77 | 78 | [DllImport("LibDisplayDataChannel.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] 79 | private static extern bool DdcSetBrightness(UIntPtr handle, uint monitorIndex, uint brightness); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Adjusters/IAdjuster.cs: -------------------------------------------------------------------------------- 1 | namespace MonitorBrightnessAdjuster.Adjusters { 2 | public interface IAdjuster { 3 | public int GetNumberOfMonitors(); 4 | public int GetBrightnessPercentage(int monitorIndex); 5 | public void SetBrightnessPercentage(int monitorIndex, int brightness); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Adjusters/WmiAdjuster.cs: -------------------------------------------------------------------------------- 1 | using System.Management; 2 | 3 | namespace MonitorBrightnessAdjuster.Adjusters { 4 | public sealed class WmiAdjuster: IAdjuster, IDisposable { 5 | private readonly ManagementClass monitorBrightnessClass; 6 | private readonly ManagementClass monitorBrightnessMethodsClass; 7 | 8 | public WmiAdjuster() { 9 | monitorBrightnessClass = new("WmiMonitorBrightness") { 10 | Scope = new(@"\\.\root\wmi") 11 | }; 12 | monitorBrightnessMethodsClass = new("WmiMonitorBrightnessMethods") { 13 | Scope = new(@"\\.\root\wmi") 14 | }; 15 | } 16 | 17 | public void Dispose() { 18 | monitorBrightnessClass.Dispose(); 19 | monitorBrightnessMethodsClass.Dispose(); 20 | } 21 | 22 | private IEnumerable> GetActiveMonitorBrightnessInstances() { 23 | IEnumerable monitorBrightnessInstances = monitorBrightnessClass 24 | .GetInstances() 25 | .Cast(); 26 | IEnumerable monitorBrightnessMethodsInstances = monitorBrightnessMethodsClass 27 | .GetInstances() 28 | .Cast(); 29 | return monitorBrightnessInstances 30 | .Zip(monitorBrightnessMethodsInstances, (instance, methodInstance) => 31 | new KeyValuePair(instance, methodInstance)) 32 | .Where(current => current.Key.GetPropertyValue("Active") as bool? == true); 33 | } 34 | 35 | public int GetNumberOfMonitors() { 36 | return GetActiveMonitorBrightnessInstances().Count(); 37 | } 38 | 39 | public int GetBrightnessPercentage(int monitorIndex) { 40 | IEnumerable> monitorBrightnessInstances = GetActiveMonitorBrightnessInstances(); 41 | if (monitorIndex >= monitorBrightnessInstances.Count() || monitorIndex < 0) { 42 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 43 | } 44 | ManagementObject currentInstance = monitorBrightnessInstances 45 | .Skip(monitorIndex) 46 | .First() 47 | .Key; 48 | // 获取所有的可用亮度级别和当前的亮度 49 | byte[]? brightnessLevel = currentInstance.GetPropertyValue("Level") as byte[]; 50 | byte? currentBrightness = currentInstance.GetPropertyValue("CurrentBrightness") as byte?; 51 | if (brightnessLevel == null || brightnessLevel.Length <= 0) { 52 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 53 | } 54 | // 查找当前亮度级别在所有级别的的位置并计算百分比 55 | int index = Array.IndexOf(brightnessLevel, currentBrightness); 56 | if (index < 0) { 57 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 58 | } 59 | return (int) ((float) (index + 1) / brightnessLevel.Length * 100); 60 | } 61 | 62 | public void SetBrightnessPercentage(int monitorIndex, int brightness) { 63 | IEnumerable> monitorBrightnessInstances = GetActiveMonitorBrightnessInstances(); 64 | if (monitorIndex >= monitorBrightnessInstances.Count() || monitorIndex < 0) { 65 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 66 | } 67 | if (brightness < 0 || brightness > 100) { 68 | throw new ArgumentOutOfRangeException(nameof(brightness)); 69 | } 70 | KeyValuePair currentInstance = monitorBrightnessInstances 71 | .Skip(monitorIndex) 72 | .First(); 73 | // 获取所有的可用亮度级别 74 | byte[]? brightnessLevel = currentInstance.Key.GetPropertyValue("Level") as byte[] ?? 75 | throw new ArgumentOutOfRangeException(nameof(monitorIndex)); 76 | // 查找距离给定亮度百分比最近的亮度级别 77 | byte newBrightness = brightnessLevel 78 | .Select((value, index) => 79 | new KeyValuePair( 80 | Math.Abs((int) ((float) (index + 1) / brightnessLevel.Length * 100) - brightness), 81 | value 82 | ) 83 | ) // 计算百分比及距离 84 | .OrderBy(pair => pair.Key) // 最短距离 85 | .First() 86 | .Value; 87 | currentInstance.Value.InvokeMethod("WmiSetBrightness", new object[] { 1, newBrightness }); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/App.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace MonitorBrightnessAdjuster { 4 | public partial class App: Application { } 5 | } 6 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | [assembly: ThemeInfo( 4 | ResourceDictionaryLocation.None, 5 | ResourceDictionaryLocation.SourceAssembly 6 | )] 7 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | using System.Windows.Controls; 3 | using System.Windows.Input; 4 | 5 | namespace MonitorBrightnessAdjuster { 6 | public partial class MainWindow: Window { 7 | public MainWindow() { 8 | InitializeComponent(); 9 | DataContext = new MainWindowViewModel(); 10 | } 11 | 12 | private void Slider_MouseWheel(object sender, MouseWheelEventArgs e) { 13 | if (sender is Slider slider) { 14 | slider.Value += e.Delta / 120; 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/MainWindowViewModel.cs: -------------------------------------------------------------------------------- 1 | using CommunityToolkit.Mvvm.ComponentModel; 2 | using CommunityToolkit.Mvvm.Input; 3 | 4 | using MonitorBrightnessAdjuster.Adjusters; 5 | 6 | using System.Collections.ObjectModel; 7 | using System.Windows; 8 | 9 | namespace MonitorBrightnessAdjuster { 10 | public partial class MainWindowViewModel: ObservableObject { 11 | [ObservableProperty] 12 | private bool enabledWmiChannel = true; 13 | 14 | [ObservableProperty] 15 | private bool enabledDdcChannel = true; 16 | 17 | [ObservableProperty] 18 | private bool showNoSupportedMonitorsFound = false; 19 | 20 | public ObservableCollection MonitorBrightnessModels { get; set; } 21 | 22 | public MainWindowViewModel() { 23 | MonitorBrightnessModels = new ObservableCollection(); 24 | Refresh(); 25 | } 26 | 27 | [RelayCommand] 28 | private void Refresh() { 29 | MonitorBrightnessModels.Clear(); 30 | if (EnabledWmiChannel) { 31 | try { 32 | for (int i = 0; i < AdjusterSingletons.WmiAdjusterInstance.GetNumberOfMonitors(); i++) { 33 | MonitorBrightnessModels.Add(new MonitorBrightnessModel() { 34 | Channel = MonitorBrightnessChannel.WMI, 35 | Index = i, 36 | Brightness = AdjusterSingletons.WmiAdjusterInstance.GetBrightnessPercentage(i) 37 | }); 38 | } 39 | } catch { } 40 | } 41 | if (EnabledDdcChannel) { 42 | try { 43 | for (int i = 0; i < AdjusterSingletons.DdcAdjusterInstance.GetNumberOfMonitors(); i++) { 44 | MonitorBrightnessModels.Add(new MonitorBrightnessModel() { 45 | Channel = MonitorBrightnessChannel.DDC, 46 | Index = i, 47 | Brightness = AdjusterSingletons.DdcAdjusterInstance.GetBrightnessPercentage(i) 48 | }); 49 | } 50 | } catch { } 51 | } 52 | ShowNoSupportedMonitorsFound = MonitorBrightnessModels.Count == 0; 53 | } 54 | 55 | [RelayCommand] 56 | private void About() { 57 | MessageBox.Show(AboutUtil.GetAboutInformation(), "About", MessageBoxButton.OK, MessageBoxImage.Information); 58 | } 59 | 60 | partial void OnEnabledWmiChannelChanged(bool _) { 61 | Refresh(); 62 | } 63 | 64 | partial void OnEnabledDdcChannelChanged(bool _) { 65 | Refresh(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/MonitorBrightnessAdjuster.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | WinExe 5 | net472 6 | 10 7 | enable 8 | enable 9 | true 10 | x64 11 | true 12 | 1.4.2.0 13 | 1.4.2.0 14 | A utility application designed to help you control the brightness of your monitors. 15 | https://github.com/YukiIsait/MonitorBrightnessAdjuster 16 | Copyright © 2025 YukiIsait 17 | Monitor Brightness Adjuster 18 | Resources\Icon.ico 19 | Resources\Manifest.xml 20 | 21 | 22 | 23 | none 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/MonitorBrightnessModel.cs: -------------------------------------------------------------------------------- 1 | using CommunityToolkit.Mvvm.ComponentModel; 2 | 3 | using MonitorBrightnessAdjuster.Adjusters; 4 | 5 | namespace MonitorBrightnessAdjuster { 6 | public enum MonitorBrightnessChannel { 7 | WMI, 8 | DDC 9 | } 10 | 11 | public partial class MonitorBrightnessModel: ObservableObject { 12 | [ObservableProperty] 13 | private MonitorBrightnessChannel channel; 14 | 15 | [ObservableProperty] 16 | private int index; 17 | 18 | [ObservableProperty] 19 | private int brightness; 20 | 21 | partial void OnBrightnessChanging(int brightness) { 22 | switch (Channel) { 23 | case MonitorBrightnessChannel.WMI: 24 | AdjusterSingletons.WmiAdjusterInstance.SetBrightnessPercentage(Index, brightness); 25 | break; 26 | case MonitorBrightnessChannel.DDC: 27 | AdjusterSingletons.DdcAdjusterInstance.SetBrightnessPercentage(Index, brightness); 28 | break; 29 | default: 30 | throw new ArgumentException(nameof(Channel)); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Resources/Icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YukiIsait/MonitorBrightnessAdjuster/fefb2fd2f616732027b9a277eb47a97375819ef3/MonitorBrightnessAdjuster/Resources/Icon.ico -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/Resources/Manifest.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MonitorBrightnessAdjuster/packages.lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "dependencies": { 4 | ".NETFramework,Version=v4.7.2": { 5 | "CommunityToolkit.Mvvm": { 6 | "type": "Direct", 7 | "requested": "[8.4.0, )", 8 | "resolved": "8.4.0", 9 | "contentHash": "tqVU8yc/ADO9oiTRyTnwhFN68hCwvkliMierptWOudIAvWY1mWCh5VFh+guwHJmpMwfg0J0rY+yyd5Oy7ty9Uw==", 10 | "dependencies": { 11 | "Microsoft.Bcl.AsyncInterfaces": "8.0.0", 12 | "System.ComponentModel.Annotations": "5.0.0", 13 | "System.Memory": "4.6.0", 14 | "System.Runtime.CompilerServices.Unsafe": "6.1.0" 15 | } 16 | }, 17 | "Microsoft.Bcl.AsyncInterfaces": { 18 | "type": "Transitive", 19 | "resolved": "8.0.0", 20 | "contentHash": "3WA9q9yVqJp222P3x1wYIGDAkpjAku0TMUaaQV22g6L67AI0LdOIrVS7Ht2vJfLHGSPVuqN94vIr15qn+HEkHw==", 21 | "dependencies": { 22 | "System.Threading.Tasks.Extensions": "4.5.4" 23 | } 24 | }, 25 | "System.Buffers": { 26 | "type": "Transitive", 27 | "resolved": "4.6.0", 28 | "contentHash": "lN6tZi7Q46zFzAbRYXTIvfXcyvQQgxnY7Xm6C6xQ9784dEL1amjM6S6Iw4ZpsvesAKnRVsM4scrDQaDqSClkjA==" 29 | }, 30 | "System.ComponentModel.Annotations": { 31 | "type": "Transitive", 32 | "resolved": "5.0.0", 33 | "contentHash": "dMkqfy2el8A8/I76n2Hi1oBFEbG1SfxD2l5nhwXV3XjlnOmwxJlQbYpJH4W51odnU9sARCSAgv7S3CyAFMkpYg==" 34 | }, 35 | "System.Memory": { 36 | "type": "Transitive", 37 | "resolved": "4.6.0", 38 | "contentHash": "OEkbBQoklHngJ8UD8ez2AERSk2g+/qpAaSWWCBFbpH727HxDq5ydVkuncBaKcKfwRqXGWx64dS6G1SUScMsitg==", 39 | "dependencies": { 40 | "System.Buffers": "4.6.0", 41 | "System.Numerics.Vectors": "4.6.0", 42 | "System.Runtime.CompilerServices.Unsafe": "6.1.0" 43 | } 44 | }, 45 | "System.Numerics.Vectors": { 46 | "type": "Transitive", 47 | "resolved": "4.6.0", 48 | "contentHash": "t+SoieZsRuEyiw/J+qXUbolyO219tKQQI0+2/YI+Qv7YdGValA6WiuokrNKqjrTNsy5ABWU11bdKOzUdheteXg==" 49 | }, 50 | "System.Runtime.CompilerServices.Unsafe": { 51 | "type": "Transitive", 52 | "resolved": "6.1.0", 53 | "contentHash": "5o/HZxx6RVqYlhKSq8/zronDkALJZUT2Vz0hx43f0gwe8mwlM0y2nYlqdBwLMzr262Bwvpikeb/yEwkAa5PADg==" 54 | }, 55 | "System.Threading.Tasks.Extensions": { 56 | "type": "Transitive", 57 | "resolved": "4.5.4", 58 | "contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==", 59 | "dependencies": { 60 | "System.Runtime.CompilerServices.Unsafe": "4.5.3" 61 | } 62 | } 63 | }, 64 | ".NETFramework,Version=v4.7.2/win-x64": {} 65 | } 66 | } -------------------------------------------------------------------------------- /Previews/Preview1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YukiIsait/MonitorBrightnessAdjuster/fefb2fd2f616732027b9a277eb47a97375819ef3/Previews/Preview1.png -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | # 屏幕亮度调节器 2 | 3 | 🌍 **[English](README.md) | [简体中文](README-CN.md)** 4 | 5 | 一款用来帮助您控制显示器屏幕亮度的小巧程序。无论您是在深夜工作还是需要微调显示亮度,该工具都提供了一种便捷的方式来调整亮度水平。 6 | 7 | ## 软件特性 8 | 9 | - 支持调节多个屏幕的亮度 10 | - 支持通过 WMI 和 DDC 协议调节屏幕的亮度(包括笔记本屏幕和外接显示器) 11 | 12 | ## 运行需求 13 | 14 | - Windows 7 及以上操作系统 15 | - .NET Framework 4.7.2 及以上版本框架 16 | 17 | ## 预览图片 18 | 19 | ![Preview1](Previews/Preview1.png) 20 | 21 | ## 开源许可 22 | 23 | 此项目根据 MIT 许可证授权,详见 [LICENSE](LICENSE.md) 文件。 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Monitor Brightness Adjuster 2 | 3 | 🌍 **[简体中文](README-CN.md) | [English](README.md)** 4 | 5 | A utility application designed to help you control the brightness of your monitors. Whether you’re working late at night or need to fine-tune your display settings, this tool provides a convenient way to adjust brightness levels. 6 | 7 | ## Features 8 | 9 | - Multi-Monitor Support: Works seamlessly with multiple monitors. 10 | - Multi-Protocol Support: Adjust brightness via WMI and DDC protocols, supporting both laptop screens and external monitors. 11 | 12 | ## System Requirements 13 | 14 | - Operating System: Windows (compatible with Windows 7 and later) 15 | - Framework: .NET Framework 4.7.2 or higher 16 | 17 | ## Previews 18 | 19 | ![Preview1](Previews/Preview1.png) 20 | 21 | ## License 22 | 23 | This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details. 24 | --------------------------------------------------------------------------------