From ac0eca92a954dfc877008fc2a3a8d87186bf477a Mon Sep 17 00:00:00 2001 From: DevWiki Date: Tue, 24 Oct 2023 20:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ DeviceManager/CMakeLists.txt | 32 +++++++++++++++++++-------- DeviceManager/Utils/LogManager.cpp | 27 ++++++++++++++++++++++ DeviceManager/Utils/LogManager.h | 18 +++++++++++++++ DeviceManager/main.cpp | 18 +++++++++++++-- DeviceManager/mainwindow.ui | 27 ++++++++++++++++++++-- WinDevice/CMakeLists.txt | 26 +++++++++++++++++++++- WinDevice/src/Video/ScreenManager.cpp | 32 ++++++++++++++++++++++++++- WinDevice/src/Video/ScreenManager.h | 11 +++++---- 9 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 DeviceManager/Utils/LogManager.cpp create mode 100644 DeviceManager/Utils/LogManager.h diff --git a/.gitignore b/.gitignore index 5dc8a41..5a23e7d 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ CMakeLists.txt.user* *.opensdf *.vcxproj *vcxproj.* +*.user # MinGW generated files *.Debug @@ -76,3 +77,5 @@ CMakeLists.txt.user* .idea .vs */out +*/lib +*/cmake* diff --git a/DeviceManager/CMakeLists.txt b/DeviceManager/CMakeLists.txt index 71d4d4f..6efc417 100644 --- a/DeviceManager/CMakeLists.txt +++ b/DeviceManager/CMakeLists.txt @@ -20,6 +20,8 @@ set(PROJECT_SOURCES mainwindow.cpp mainwindow.h mainwindow.ui + Utils/LogManager.cpp + Utils/LogManager.h ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) @@ -46,15 +48,27 @@ else() endif() target_link_libraries(DeviceManager PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) -# 添加WinDevice项目 -add_subdirectory(../WinDevice WinDevice) -# 链接WinDevice项目 -target_link_libraries(DeviceManager PRIVATE WinDevice) -# 链接WinDevice项目 -target_include_directories(DeviceManager PRIVATE - ../WinDevice/src - include/third_lib -) + +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + # Debug 模式下,添加 WinDevice 项目作为子项目 + add_subdirectory(../WinDevice WinDevice) + # 链接WinDevice项目 + target_include_directories(DeviceManager PRIVATE + ../WinDevice/src + include/third_lib + ) + target_link_libraries(DeviceManager PRIVATE WinDevice) +else() + # Release 模式下,链接 WinDevice 生成的库文件 + target_link_libraries(DeviceManager PRIVATE WinDevice) + # 在 Release 模式下,添加 WinDevice 的头文件路径 + target_include_directories(DeviceManager PRIVATE + ../WinDevice/lib + ) +endif() + + +## target_link_libraries(DeviceManager PRIVATE dxgi) set_target_properties(DeviceManager PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com diff --git a/DeviceManager/Utils/LogManager.cpp b/DeviceManager/Utils/LogManager.cpp new file mode 100644 index 0000000..5b110ac --- /dev/null +++ b/DeviceManager/Utils/LogManager.cpp @@ -0,0 +1,27 @@ +// +// Created by zyz on 2023/10/17. +// + +#include "LogManager.h" + +void LogManager::CustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + QByteArray localMsg = msg.toUtf8(); + switch (type) { + case QtDebugMsg: + fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + break; + case QtInfoMsg: + fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + break; + case QtWarningMsg: + fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + break; + case QtCriticalMsg: + fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + break; + case QtFatalMsg: + fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + abort(); + } +} diff --git a/DeviceManager/Utils/LogManager.h b/DeviceManager/Utils/LogManager.h new file mode 100644 index 0000000..9a01e44 --- /dev/null +++ b/DeviceManager/Utils/LogManager.h @@ -0,0 +1,18 @@ +// +// Created by zyz on 2023/10/17. +// + +#ifndef DEVICEMANAGER_LOGMANAGER_H +#define DEVICEMANAGER_LOGMANAGER_H + + +#include +#include + +class LogManager { +public: + static void CustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); +}; + + +#endif //DEVICEMANAGER_LOGMANAGER_H diff --git a/DeviceManager/main.cpp b/DeviceManager/main.cpp index 053c8b0..53bd123 100644 --- a/DeviceManager/main.cpp +++ b/DeviceManager/main.cpp @@ -1,12 +1,26 @@ #include "mainwindow.h" #include -#include "Utils/CmdUtil.h" +#include "Utils/LogManager.h" +#include "Video/ScreenManager.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); - CmdUtil::ExecuteCommand(""); + qInstallMessageHandler(LogManager::CustomMessageHandler); + + ScreenManager screenManager; + screenManager.UpdateDisplayInfo(); + + HRESULT hr = S_OK; + for (size_t i = 0; i < screenManager._displayAdapterList.size(); i++) { + DXGI_ADAPTER_DESC adapterDesc; + hr = screenManager._displayAdapterList[i]->GetDesc(&adapterDesc); + if (SUCCEEDED(hr)) { + qDebug("Adapter: %s", adapterDesc.Description); + } + } + MainWindow w; w.show(); return a.exec(); diff --git a/DeviceManager/mainwindow.ui b/DeviceManager/mainwindow.ui index b232854..e8f429d 100644 --- a/DeviceManager/mainwindow.ui +++ b/DeviceManager/mainwindow.ui @@ -13,8 +13,31 @@ MainWindow - - + + + + + 150 + 160 + 80 + 24 + + + + PushButton + + + + + + + 0 + 0 + 800 + 21 + + + diff --git a/WinDevice/CMakeLists.txt b/WinDevice/CMakeLists.txt index 569c75c..854ddcf 100644 --- a/WinDevice/CMakeLists.txt +++ b/WinDevice/CMakeLists.txt @@ -4,6 +4,9 @@ cmake_minimum_required(VERSION 3.12) # 设置项目名称和版本,以及编程语言 project(WinDevice VERSION 0.1 LANGUAGES CXX) +# 允许 Windows 导出所有符号 +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + # 设置C++标准 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -49,4 +52,25 @@ set(THIRD_LIB_DIR ${CMAKE_SOURCE_DIR}/include/third_lib) target_include_directories(WinDevice PRIVATE include/third_lib src -) \ No newline at end of file +) + +# 设置库文件输出目录相对于 WinDevice 项目的根目录 +set(LIB_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/lib") +# 设置库的输出路径 +set_target_properties(WinDevice PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${LIB_OUTPUT_DIR} # 设置静态库输出路径 + LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_DIR} # 设置动态链接库输出路径 +) + +# 设置发布目录,例如 "install" 文件夹 +set(INSTALL_DIR "${CMAKE_SOURCE_DIR}/install") + +# 在发布阶段将库文件安装到指定目录 +install(TARGETS WinDevice + ARCHIVE DESTINATION "${INSTALL_DIR}/lib" # 安装静态库文件 + LIBRARY DESTINATION "${INSTALL_DIR}/lib" # 安装动态链接库文件 +) + +target_link_libraries(WinDevice PRIVATE dxguid) + +target_compile_definitions(WinDevice PRIVATE -DWINDEVICE_EXPORTS) diff --git a/WinDevice/src/Video/ScreenManager.cpp b/WinDevice/src/Video/ScreenManager.cpp index 24da773..e86d85d 100644 --- a/WinDevice/src/Video/ScreenManager.cpp +++ b/WinDevice/src/Video/ScreenManager.cpp @@ -1,10 +1,10 @@ #include "ScreenManager.h" +#include #include "spdlog/spdlog.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" #include "Utils/StringUtil.h" - ScreenManager::ScreenManager() { @@ -19,6 +19,7 @@ void ScreenManager::UpdateDisplayInfo() { _UpdateDisplayDeviceList(); _UpdateMonitorInfoMap(); + _UpdateDisplayAdapterList(); } void ScreenManager::_UpdateDisplayDeviceList() @@ -39,6 +40,35 @@ void ScreenManager::_UpdateDisplayDeviceList() spdlog::info("=====GetInfoByEnumDisplayDevices end====="); } +void ScreenManager::_UpdateDisplayAdapterList() +{ + spdlog::info("=====_UpdateDisplayAdapterList start====="); + HRESULT hr = S_OK; + hr = CoInitialize(nullptr); + if (FAILED(hr)) { + spdlog::error("_UpdateDisplayAdapterList CoInitialize failed"); + return; + } + + IDXGIFactory7* dxgiFactory = nullptr; + hr = CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)); + if (FAILED(hr)) { + spdlog::error("CreateDXGIFactory1 failed"); + CoUninitialize(); + return; + } + + UINT adapterIndex = 0; + IDXGIAdapter* adapter = nullptr; + + while (dxgiFactory->EnumAdapters(adapterIndex, &adapter) != DXGI_ERROR_NOT_FOUND) { + _displayAdapterList.push_back(adapter); // 将 IDXGIAdapter 指针存储在容器中 + adapterIndex++; + } + dxgiFactory->Release(); + CoUninitialize(); +} + BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) { MONITORINFOEX monitorInfo; diff --git a/WinDevice/src/Video/ScreenManager.h b/WinDevice/src/Video/ScreenManager.h index d50e25f..5e1280c 100644 --- a/WinDevice/src/Video/ScreenManager.h +++ b/WinDevice/src/Video/ScreenManager.h @@ -3,20 +3,23 @@ #include #include +#include class ScreenManager { public: ScreenManager(); ~ScreenManager(); - void UpdateDisplayInfo(); + void UpdateDisplayInfo(); + + std::vector _displayDeviceList; + std::map _hMonitorInfoMap; + std::vector _displayAdapterList; private: void _UpdateDisplayDeviceList(); void _UpdateMonitorInfoMap(); + void _UpdateDisplayAdapterList(); static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData); BOOL _EnumMonitorProc(HMONITOR hMonitor); - - std::vector _displayDeviceList; - std::map _hMonitorInfoMap; };