#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <chrono>
#include <ctime>
#include <thread>
constexpr int WINDOW_X = 10;
constexpr int WINDOW_Y = 10;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void DisplayCurrentDateTime(HWND hwnd, int x, int y);
void Waku(HWND hwnd);
int main() {
// ウィンドウクラスの登録
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 255)); // 背景色を青に設定
wc.lpszClassName = "MyWindowClass";
RegisterClass(&wc);
// フルスクリーンのサイズ取得
//int screenWidth = GetSystemMetrics(SM_CXSCREEN);
//int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// 画面のサイズを取得
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// ウィンドウのサイズ
int windowWidth = 1000;
int windowHeight = 800;
// ウィンドウ位置を画面の中央に設定
int posX = (screenWidth - windowWidth) / 2;
int posY = (screenHeight - windowHeight) / 2;
// ウィンドウ作成
HWND hwnd = CreateWindow("MyWindowClass", "タイトル", WS_OVERLAPPEDWINDOW & ~(WS_SYSMENU), posX, posY, windowWidth, windowHeight, NULL, NULL, GetModuleHandle(NULL), NULL); //サイズ設定
//HWND hwnd = CreateWindow("MyWindowClass", "My Window", WS_OVERLAPPEDWINDOW & ~(WS_SYSMENU), CW_USEDEFAULT, CW_USEDEFAULT, screenWidth, screenHeight, NULL, NULL, GetModuleHandle(NULL), NULL); // フルサイズ
// ウィンドウ表示
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// スレッドを起動して日時を表示
//std::thread dateTimeThread(DisplayCurrentDateTime, hwnd, WINDOW_X, WINDOW_Y);
//std::thread dateTimeThread(DisplayCurrentDateTime, hwnd);
// ウィンドウの枠を表示
Waku(hwnd);
// カーソル非表示
ShowCursor(FALSE);
// メッセージループ
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE) { //ESCキーを押すとスレッド終了
break;
}
}
// スレッドを終了
//dateTimeThread.detach();
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) // ESCキーでウィンドウを閉じる
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// リアルタイム日時
void DisplayCurrentDateTime(HWND hwnd, int x, int y) {
while (true) {
HDC hdc = GetDC(hwnd);
RECT rect = { x, y, x + 200, y + 100 }; // 文字の表示位置(left, top, right ,bottom)
// フォント作成 高さ, 幅, 傾き(反時計回り), 文字の向き, 太字サイズ, Italic, 下線, 打消し線
HFONT hFont = CreateFont(24, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
SetBkColor(hdc, RGB(0, 0, 255)); // 文字の背景色
SetTextColor(hdc, RGB(0, 255, 255)); // 文字色
time_t now = time(nullptr);
struct tm* localTime = localtime(&now);
char timeStr[128];
strftime(timeStr, sizeof(timeStr), "%Y/%m/%d %H:%M:%S", localTime);
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
DrawTextA(hdc, timeStr, -1, &rect, DT_LEFT | DT_TOP | DT_SINGLELINE); // 日時を描画
ReleaseDC(hwnd, hdc);
// 1秒待機
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
// 枠の描画
void Waku(HWND hwnd) {
HDC hdc = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd, &rect);
// 太く水色の線枠を描画
HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 255)); // 枠の背景色
HPEN hPen = CreatePen(PS_SOLID, 3, RGB(0, 255, 255)); // 太い枠線
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
Rectangle(hdc, 4, 4, 780, 40);
Rectangle(hdc, 4, 44, 780, 386);
Rectangle(hdc, 4, 390, 780, 430);
// 枠の描画
FrameRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
ReleaseDC(hwnd, hdc);
}