优化构建
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -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* | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										27
									
								
								DeviceManager/Utils/LogManager.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								DeviceManager/Utils/LogManager.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								DeviceManager/Utils/LogManager.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								DeviceManager/Utils/LogManager.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| // | ||||
| // Created by zyz on 2023/10/17. | ||||
| // | ||||
|  | ||||
| #ifndef DEVICEMANAGER_LOGMANAGER_H | ||||
| #define DEVICEMANAGER_LOGMANAGER_H | ||||
|  | ||||
|  | ||||
| #include <QtMsgHandler> | ||||
| #include <QString> | ||||
|  | ||||
| class LogManager { | ||||
| public: | ||||
|     static void CustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); | ||||
| }; | ||||
|  | ||||
|  | ||||
| #endif //DEVICEMANAGER_LOGMANAGER_H | ||||
| @@ -1,12 +1,26 @@ | ||||
| #include "mainwindow.h" | ||||
|  | ||||
| #include <QApplication> | ||||
| #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(); | ||||
|   | ||||
| @@ -13,8 +13,31 @@ | ||||
|   <property name="windowTitle"> | ||||
|    <string>MainWindow</string> | ||||
|   </property> | ||||
|   <widget class="QWidget" name="centralwidget"/> | ||||
|   <widget class="QMenuBar" name="menubar"/> | ||||
|   <widget class="QWidget" name="centralwidget"> | ||||
|    <widget class="QPushButton" name="pushButton"> | ||||
|     <property name="geometry"> | ||||
|      <rect> | ||||
|       <x>150</x> | ||||
|       <y>160</y> | ||||
|       <width>80</width> | ||||
|       <height>24</height> | ||||
|      </rect> | ||||
|     </property> | ||||
|     <property name="text"> | ||||
|      <string>PushButton</string> | ||||
|     </property> | ||||
|    </widget> | ||||
|   </widget> | ||||
|   <widget class="QMenuBar" name="menubar"> | ||||
|    <property name="geometry"> | ||||
|     <rect> | ||||
|      <x>0</x> | ||||
|      <y>0</y> | ||||
|      <width>800</width> | ||||
|      <height>21</height> | ||||
|     </rect> | ||||
|    </property> | ||||
|   </widget> | ||||
|   <widget class="QStatusBar" name="statusbar"/> | ||||
|  </widget> | ||||
|  <resources/> | ||||
|   | ||||
| @@ -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 | ||||
| ) | ||||
| ) | ||||
|  | ||||
| # 设置库文件输出目录相对于 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) | ||||
|   | ||||
| @@ -1,10 +1,10 @@ | ||||
| #include "ScreenManager.h" | ||||
| #include <Windows.h> | ||||
| #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; | ||||
|   | ||||
| @@ -3,20 +3,23 @@ | ||||
| #include <map> | ||||
|  | ||||
| #include <Mmdeviceapi.h> | ||||
| #include <dxgi1_6.h> | ||||
|  | ||||
| class ScreenManager | ||||
| { | ||||
| public: | ||||
| 	ScreenManager(); | ||||
| 	~ScreenManager(); | ||||
| 	void UpdateDisplayInfo(); | ||||
|     void UpdateDisplayInfo(); | ||||
|  | ||||
|     std::vector<DISPLAY_DEVICE> _displayDeviceList; | ||||
|     std::map<HMONITOR, MONITORINFOEX> _hMonitorInfoMap; | ||||
|     std::vector<IDXGIAdapter*> _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<DISPLAY_DEVICE> _displayDeviceList; | ||||
| 	std::map<HMONITOR, MONITORINFOEX> _hMonitorInfoMap; | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user