diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..04714d8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,118 @@ +--- +BasedOnStyle: WebKit +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveBitFields: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: DontAlign +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BinPackParameters: false +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: Always + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterStruct: true + AfterUnion: true + AfterExternBlock: false + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: true + BeforeWhile: true + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 100 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +FixNamespaceComments: true +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*Private[.]h"' + Priority: 0 + - Regex: '^".*[.]pb[.]h"' + Priority: 2 + - Regex: '^".*[.]h"' + Priority: 1 + - Regex: '^' + Priority: 5 + - Regex: '^<.*([.]h|[.]hpp)>' + Priority: 3 + - Regex: '^' + Priority: 6 + - Regex: '^' + Priority: 4 + - Regex: '^<.*>' + Priority: 6 +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: NoIndent +IndentGotoLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +Language: Cpp +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: None +PenaltyExcessCharacter: 10 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++14 +StatementMacros: ['Q_UNUSED'] +TabWidth: 4 +UseCRLF: false +UseTab: ForIndentation + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90058ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +cmake-build-* +.idea diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ec6808d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.1) +project(screen-resolution-changer CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") + +add_executable(screen-resolution-changer WIN32 main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d4d4b06 --- /dev/null +++ b/main.cpp @@ -0,0 +1,115 @@ +#include + +#include +#include + +std::optional setScreenResolution(int width, int height) +{ + DEVMODE prevMode; + DEVMODE devMode; + + ZeroMemory(&devMode, sizeof(DEVMODE)); + devMode.dmSize = sizeof(DEVMODE); + + if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode)) + return std::nullopt; + + CopyMemory(&prevMode, &devMode, sizeof(DEVMODE)); + devMode.dmPelsWidth = width; + devMode.dmPelsHeight = height; + + if (ChangeDisplaySettingsEx(NULL, &devMode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL) + return prevMode; + else + return std::nullopt; +} + +bool restoreScreenResolution(DEVMODE& mode) +{ + return ChangeDisplaySettingsEx(NULL, &mode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL; +} + +bool executeProgram(std::string command) +{ + STARTUPINFO si; + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + + PROCESS_INFORMATION pi; + ZeroMemory(&pi, sizeof(pi)); + + if (CreateProcess(NULL, command.data(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) + { + WaitForSingleObject(pi.hProcess, INFINITE); + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + return true; + } + else + return false; +} + +void showError(const char* message) +{ + MessageBox(NULL, message, "Error", MB_OK); +} + +void showUsage() +{ + showError( + "Usage:\n" + "screen-resolution-changer.exe [args...]"); +} + +int main(int argc, char* argv[]) +{ + if (argc < 4) + { + showUsage(); + return 1; + } + + unsigned int width = 0; + unsigned int height = 0; + + try + { + width = std::stoi(argv[1]); + height = std::stoi(argv[2]); + } + catch (const std::exception& e) + { + showUsage(); + return 1; + } + + std::string command = argv[3]; + for (int i = 4; i < argc; i++) + command.append(" ").append(argv[i]); + + auto prevMode = setScreenResolution(width, height); + if (prevMode) + { + int result = 1; + if (executeProgram(command)) + result = 0; + else + { + result = 1; + showError("Failed to start application"); + } + + if (restoreScreenResolution(*prevMode)) + return result; + else + { + showError("Failed to restore screen resolution"); + return 1; + } + } + else + { + showError("Failed to change screen resolution"); + return 1; + } +}