Skip to content

Instantly share code, notes, and snippets.

@Nia-TN1012
Forked from Myoga1012/Calendar.cpp
Last active January 30, 2023 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nia-TN1012/541fddec7f65e67a569d3c3dd5f5d030 to your computer and use it in GitHub Desktop.
Save Nia-TN1012/541fddec7f65e67a569d3c3dd5f5d030 to your computer and use it in GitHub Desktop.
C++(Win32フォームアプリ(Win32 API))でカレンダーを出力するソースコードです。
// 名前 : Nia Tomonaka
// Twitter : https://twitter.com/nia_tn1012
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#define MAX_LOADSTRING 100
TCHAR szTitle[MAX_LOADSTRING] = _T( "Calendar" ); // タイトル バーのテキスト
TCHAR szWindowClass[MAX_LOADSTRING] = _T( "Calendar" ); // メイン ウィンドウ クラス名
// ウィンドウクラスの作成と初期化
int InitWindow( HINSTANCE, int );
#define WC_INIT_COMPLETED 0
#define WC_INIT_FAILED -1
// ウィンドウ プロシージャ関数
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
// カレンダー用
struct Cal { int First, Last; };
Cal GetCal();
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
// ウィンドウの初期化を行います。
if( InitWindow( hInstance, nCmdShow ) ) {
MessageBox( NULL, _T( "初期化に失敗したためアプリを終了します。" ), szTitle, MB_ICONEXCLAMATION | MB_OK );
return 1;
}
// メインのループです。
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return 0;
}
// ウィンドウクラスの作成と初期化
int InitWindow( HINSTANCE hInstance, int nCmdShow ) {
// ウィンドウクラスを作成し、設定します。
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( IDI_APPLICATION ) );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon( wcex.hInstance, MAKEINTRESOURCE( IDI_APPLICATION ) );
// ウインドウクラスを登録します。
RegisterClassEx( &wcex );
// ウィンドウを作成します。
HWND hWnd = CreateWindow( szWindowClass, szTitle,
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_DLGFRAME | WS_MINIMIZEBOX,
CW_USEDEFAULT, 0, 320, 240, NULL, NULL, hInstance, NULL );
if( !hWnd )
return WC_INIT_FAILED;
// ウインドウを表示します。
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
return WC_INIT_COMPLETED;
}
// ウィンドウ プロシージャ関数
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
PAINTSTRUCT ps;
HDC hdc;
// 以下はカレンダー出力用オブジェクトです。
tagRECT rect = { 0, 0, 0, 0 };
Cal cal = GetCal();
const int XArea = 40, YArea = 30, XOFF = 2, YOFF = 10;
TCHAR days[64] = { _T( '\0' ) };
switch( message ) {
case WM_PAINT:
hdc = BeginPaint( hWnd, &ps );
// カレンダーを出力します。
SetBkMode( hdc, TRANSPARENT );
for( int seq = cal.First, day = 1; day <= cal.Last; seq++, day++ ) {
rect.left = ( seq % 7 ) * XArea + XOFF, rect.top = ( seq / 7 ) * YArea + YOFF;
rect.right = ( seq % 7 + 1 ) * XArea + XOFF, rect.bottom = ( seq / 7 + 1 ) * YArea + YOFF;
wsprintf( days, _T( "%2d" ), day );
SetTextColor( hdc, seq % 7 == 0 ? RGB( 0xFF, 0, 0xFF ) : ( seq % 7 == 6 ? RGB( 0, 0xCC, 0xFF ) : RGB( 0xFF, 0xFF, 0xFF ) ) );
DrawText( hdc, days, -1, &rect, DT_RIGHT );
}
EndPaint( hWnd, &ps );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
break;
}
return 0;
}
// 現在の日時から当月1日の曜日と当月末日を求めます。
Cal GetCal() {
SYSTEMTIME now;
GetLocalTime( &now );
Cal cal;
cal.First = ( ( 7 - ( now.wDay % 7 + 6 - now.wDayOfWeek ) % 7 ) % 7 );
if( now.wMonth == 2 )
cal.Last = now.wYear % 4 == 0 ? 29 : 28;
else
cal.Last = ( now.wMonth <= 7 && now.wMonth % 2 == 1 || now.wMonth >= 8 && now.wMonth % 2 == 0 ) ? 31 : 30;
return cal;
}
// Calendar.cpp ( フォームアプリ ( Win32 API ) )
// Copyright (c) 2014-2023 Nia T.N. Tech Lab. / Chronoir.net.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
@Nia-TN1012
Copy link
Author

GitHubのアカウント統合のため、Myoga1012→Nia-TN1012に移行しました。

旧URL: https://gist.github.com/Myoga1012/5bdaf22c6c4fcfb228bb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment