优化构建

This commit is contained in:
DevWiki 2023-10-24 20:25:03 +08:00
parent 4305a8cf1e
commit ac0eca92a9
9 changed files with 175 additions and 19 deletions

3
.gitignore vendored
View File

@ -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*

View File

@ -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

View 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();
}
}

View 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

View File

@ -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();

View File

@ -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/>

View File

@ -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)

View File

@ -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;

View File

@ -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;
};