Last active
January 30, 2023 08:28
-
-
Save Myoga1012/0810a125613c9dd0f671 to your computer and use it in GitHub Desktop.
C++とDXライブラリでカレンダーを出力するソースコードです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka) | |
// Twitter : https://twitter.com/Myoga1012 | |
#include <stdio.h> | |
#include "DxLib.h" | |
// Zellerの公式でddの1日目の曜日を求めます。 | |
int Zeller( DATEDATA& dd ) { | |
return ( dd.Year + dd.Year / 4 - dd.Year / 100 + dd.Year / 400 + ( 13 * dd.Mon + 8 ) / 5 + 1 ) % 7; | |
} | |
// ddの月末日を求めます。 | |
int DaysInMonth( DATEDATA& dd ) { | |
if( dd.Mon == 2 ) | |
return dd.Year % 4 == 0 ? 29 : 28; | |
else | |
return ( dd.Mon <= 7 && dd.Mon % 2 == 1 || dd.Mon >= 8 && dd.Mon % 2 == 0 ) ? 31 : 30; | |
} | |
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { | |
SetMainWindowText( "カレンダー" ); // ウインドウタイトルの変更 | |
ChangeWindowMode( TRUE ); // ウィンドウモード | |
SetGraphMode( 320, 240, 16 ); // ウィンドウサイズの変更 | |
SetOutApplicationLogValidFlag( false ); // ログを出力しない | |
// DXライブラリの初期化を行います。 | |
if( DxLib_Init() == -1 ) return -1; | |
// 描画先をリアバッファーに設定します。 | |
SetDrawScreen( DX_SCREEN_BACK ); | |
// ここから開始 | |
DATEDATA now; | |
GetDateTime( &now ); // 現在の日時を取得します。 | |
int last = DaysInMonth( now ); // 当月末日を求めます。 | |
// YYYY年MM月とタイトルバーに表示します。 | |
char ds[64]; | |
sprintf_s( ds, "カレンダー - %4d年%02d月", now.Year, now.Mon ); | |
SetMainWindowText( ds ); | |
// カレンダーを出力します。 | |
const int XArea = 30, YArea = 20; | |
for( int seq = Zeller( now ), day = 1; day <= last; seq++, day++ ) { | |
DrawFormatString( | |
10 + ( seq % 7 ) * XArea, 10 + ( seq / 7 ) * YArea, | |
// 日曜は赤系、土曜は青系、それ以外は白です。 | |
seq % 7 == 0 ? 0xFF00CC : ( seq % 7 == 6 ? 0x00CCFF : 0xFFFFFF ), | |
"%2d", day | |
); | |
} | |
ScreenFlip(); // リアバッファーに描画した内容をウィンドウに反映します。 | |
WaitKey(); // キーボードの入力を待ちます。 | |
// DXライブラリ終了処理を行います。 | |
DxLib_End(); | |
return 0; | |
} | |
// Calendar.cpp ( C++ with DXライブラリ ) | |
// Copyright (c) 2014 Myoga-TN.net All Rights Reserved. | |
// This software is released under the MIT License. | |
// http://opensource.org/licenses/mit-license.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment