C++関連その2

■MAIN.cpp

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <shellapi.h>
#include <conio.h>
#include "support.h"

int main() {
    //コンソール画面を中央に表示
    console_setup();

    std::cout << "1.サブ1 [1]\n" << std::endl;
    std::cout << "2.サブ2 [2]\n" << std::endl;
    std::cout << "E.終了  [E]\n" << std::endl;
    std::cout << "1~2またはEのキーを教えてください。" << std::endl;
    char key;

    do {
        key = _getch();

        if (key == '1') {
            // 実行するファイルのパスを指定
            LPCSTR filePath = "C:\\Users\\sannp\\Desktop\\メイン\\sub1\\SUB1\\Release\\SUB1.exe"; // 実行したい別の実行ファイルのパスを指定

            // 別の実行ファイルを開く
            HINSTANCE hInstance = ShellExecuteA(NULL, "open", filePath, NULL, NULL, SW_SHOWNORMAL);

            // エラーチェック
            if ( ( int ) hInstance <= 32) {
                std::cerr << "Error launching executable." << std::endl;
                return EXIT_FAILURE;
            }
        }
        else if (key == '2') {
            // 実行するファイルのパスを指定
            LPCSTR filePath = "C:\\Users\\sannp\\Desktop\\メイン\\sub2\\SUB2\\Release\\SUB2.exe"; // 実行したい別の実行ファイルのパスを指定

            // 別の実行ファイルを開く
            HINSTANCE hInstance = ShellExecuteA(NULL, "open", filePath, NULL, NULL, SW_SHOWNORMAL);

            // エラーチェック
            if ( ( int ) hInstance <= 32) {
                std::cerr << "Error launching executable." << std::endl;
                return EXIT_FAILURE;
            }
        }
        else if (key == 'e') {
            exit(EXIT_SUCCESS);
        }
    } while (key != '1' && key != '2' && key != 'e');

 

    return EXIT_SUCCESS;
}

 

■support.cpp

#include <windows.h>
void console_setup() {
    //コンソール画面を中央に表示
    HWND consoleWindow = GetConsoleWindow();
    SetConsoleTitle(TEXT("sharkC1 誘導弾試験"));
    RECT desktopRect;
    GetWindowRect(GetDesktopWindow(), &desktopRect);
    int screenWidth = desktopRect.right;
    int screenHeight = desktopRect.bottom;
    RECT consoleRect;
    GetWindowRect(consoleWindow, &consoleRect);
    int consoleWidth = consoleRect.right - consoleRect.left;
    int consoleHeight = consoleRect.bottom - consoleRect.top;
    int posX = (screenWidth - consoleWidth) / 2;
    int posY = (screenHeight - consoleHeight) / 2;
    SetWindowPos(consoleWindow, NULL, posX, posY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

 

■support.h

#ifndef MAIN_H
#define MAIN_H
void console_setup();
#endif

 

■SUB1.cpp

#include "mylib.h"
#include <iostream>

int main()
{
    console_setup();
    printHello();
}

■sub1_1.cpp

#include "mylib.h"
#include <cstdio>
#include <Windows.h>
 
void printHello() {
 
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
 
SetConsoleTextAttribute(h, YELLOW);
printf("sub1 !!\n");
Sleep(2000);
printf("SCCUESS !!\n");
 
//gotoXY1(1, 1);
//printf("ここだよ XY1 !!\n");
//gotoXY2(1, 1);
//printf("ここだよ XY2 !!\n");
Sleep(2000);
 
return;
}
 
void gotoXY1(int x, int y)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 
_COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hConsole, pos);
}
 
void gotoXY2(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
 
void console_setup() {
//コンソール画面を中央に表示
HWND consoleWindow = GetConsoleWindow();
SetConsoleTitle(TEXT("sharkC1 SUB1 電波1型性能試験"));
RECT desktopRect;
GetWindowRect(GetDesktopWindow(), &desktopRect);
int screenWidth = desktopRect.right;
int screenHeight = desktopRect.bottom;
RECT consoleRect;
GetWindowRect(consoleWindow, &consoleRect);
int consoleWidth = consoleRect.right - consoleRect.left;
int consoleHeight = consoleRect.bottom - consoleRect.top;
int posX = (screenWidth - consoleWidth) / 2;
int posY = (screenHeight - consoleHeight) / 2;
SetWindowPos(consoleWindow, NULL, posX, posY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
 
■mylib.h

#ifndef SUB1_H
#define SBU1_H

void printHello();
void gotoXY2(int x, int y);
void console_setup();

#define BLACK        0x00
#define DARK_BLUE   0x01
#define DARK_GREEN    0x02
#define DARK_CYAN   0x03
#define DARK_RED    0x04
#define DARK_VIOLET    0x05
#define DARK_YELLOW 0x06
#define GRAY        0x07
#define LIGHT_GRAY  0x08
#define BLUE        0x09
#define GREEN        0x0a
#define CYAN        0x0b
#define RED            0x0c
#define VIOLET        0x0d
#define YELLOW        0x0e
#define WHITE        0x0f
#define INTENSITY    0x08 // 高輝度マスク
#define RED_MASK    0x04 // 赤色ビット
#define GREEN_MASK    0x02 // 緑色ビット
#define BLUE_MASK   0x01 // 青色ビット


#endif