コード:
////////////////////////
// //
// ヘッダファイル //
// //
////////////////////////
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
////////////////////
// //
// マクロ定義 //
// //
////////////////////
#define CLASS_NAME "サンプル" // ウィンドウクラスの名前
#define WINDOW_NAME "ウィンドウ表示処理" // ウィンドウの名前
#define SCREEN_WIDTH ( 800 ) // ウィンドウの幅
#define SCREEN_HEIGHT ( 600 ) // ウィンドウの高さ
#define ID_EDIT_000 // エディットボックス
#define ID_BUTTON_000 // 0ボタン
#define ID_BUTTON_001 // 1ボタン
#define ID_BUTTON_002 // 2ボタン
#define ID_BUTTON_003 // 3ボタン
#define ID_BUTTON_004 // 4ボタン
#define ID_BUTTON_005 // 5ボタン
#define ID_BUTTON_006 // 6ボタン
#define ID_BUTTON_007 // 7ボタン
#define ID_BUTTON_008 // 8ボタン
#define ID_BUTTON_009 // 9ボタン
#define ID_BUTTON_010 // Cボタン
#define ID_BUTTON_011 // +ボタン
#define ID_BUTTON_012 // -ボタン
#define ID_BUTTON_013 // *ボタン
#define ID_BUTTON_014 // /ボタン
#define ID_BUTTON_015 // =ボタン
//////////////////////////
// //
// プロトタイプ宣言 //
// //
//////////////////////////
LRESULT CALLBACK WndProc ( HWND hWnd , UINT uMsg , WPARAM wParam , LPARAM lParam );
////////////////////
// //
// メイン処理 //
// //
////////////////////
int APIENTRY WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow )
{
UNREFERENCED_PARAMETER ( hPrevInstance );
UNREFERENCED_PARAMETER ( lpCmdLine );
WNDCLASSEX wcex =
{
sizeof ( WNDCLASSEX ) ,
CS_CLASSDC ,
WndProc ,
0 ,
0 ,
hInstance ,
NULL ,
LoadCursor ( NULL , IDC_ARROW ) ,
( HBRUSH ) ( COLOR_WINDOW + 1 ) ,
NULL ,
CLASS_NAME ,
NULL
};
// ウィンドウクラスの登録
RegisterClassEx ( &wcex );
static HWND hWnd; // ウィンドウハンドル
static MSG msg;
// ウィンドウスタイル定義
const int wstyle = WS_OVERLAPPEDWINDOW &~ WS_MINIMIZEBOX &~ WS_MAXIMIZEBOX &~ WS_THICKFRAME ;
RECT cr = { 0 , 0 , SCREEN_WIDTH , SCREEN_HEIGHT };
AdjustWindowRect ( &cr , wstyle , FALSE );
int ww = cr.right - cr.left; // 画面(ウィンドウ表示)幅
int wh = cr.bottom - cr.top; // 画面(ウィンドウ表示)高
// 初期ウィンドウ位置を真ん中に設定
RECT dr;
GetWindowRect ( GetDesktopWindow() , &dr );
int dw = dr.right - dr.left;
int dh = dr.bottom - dr.top;
int wx = ww > dw ? 0: ( dw - ww ) / 2;
int wy = wh > dh ? 0: ( dh - wh ) / 2;
// ウィンドウを作成
hWnd = CreateWindowEx ( 0 ,
CLASS_NAME ,
WINDOW_NAME ,
wstyle ,
wx,
wy,
ww ,
wh ,
NULL ,
NULL ,
hInstance ,
NULL
);
// ウィンドウの表示
ShowWindow ( hWnd , nCmdShow );
UpdateWindow ( hWnd );
while ( GetMessage ( &msg , NULL , 0 , 0 ) )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
return ( int ) msg.wParam;
}
LRESULT CALLBACK WndProc ( HWND hWnd , UINT uMsg , WPARAM wParam , LPARAM lParam )
{
static HDC hDC;
static HWND hWndEdit; // エディットボックス
static HWND hWndButton_0; // 0ボタン
static HWND hWndButton_1; // 1ボタン
static HWND hWndButton_2; // 2ボタン
static HWND hWndButton_3; // 3ボタン
static HWND hWndButton_4; // 4ボタン
static HWND hWndButton_5; // 5ボタン
static HWND hWndButton_6; // 6ボタン
static HWND hWndButton_7; // 7ボタン
static HWND hWndButton_8; // 8ボタン
static HWND hWndButton_9; // 9ボタン
static HWND hWndButton_10; // Cボタン
static HWND hWndButton_11; // +ボタン
static HWND hWndButton_12; // -ボタン
static HWND hWndButton_13; // *ボタン
static HWND hWndButton_14; // /ボタン
static HWND hWndButton_15; // =ボタン
static char EditBoxLength; // 表示文字数カウント
static char NumLength [ 15 ]; // 文字連結前の文字数
static char NumBox [ 2 ][ 14 ] ; // 文字連結後の文字数
static int Mode = 1; // 符号の前後判定
static int CalculateMark; // 計算符号判定
enum { Plus , Minus , Multi , Div }; // 符号の状態
static double Ans; // 計算結果
static char AnsLength [14]; // 計算結果の表示数
switch ( uMsg )
{
case WM_CREATE:
hWndEdit = CreateWindowEx ( 0 , "EDIT" , "" , WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT | ES_READONLY , 300 , 100 , 150 , 30 , hWnd ,
( HMENU ) ID_EDIT_000 (201) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_0 = CreateWindowEx ( 0 , "BUTTON" , "0" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 170 , 500 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_000 (101) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_1 = CreateWindowEx ( 0 , "BUTTON" , "1" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 170 , 430 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_001 (102) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_2 = CreateWindowEx ( 0 , "BUTTON" , "2" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 240 , 430 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_002 (103) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_3 = CreateWindowEx ( 0 , "BUTTON" , "3" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 310 , 430 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_003 (104) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_4 = CreateWindowEx ( 0 , "BUTTON" , "4" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 170 , 360 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_004 (105) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_5 = CreateWindowEx ( 0 , "BUTTON" , "5" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 240 , 360 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_005 (106) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_6 = CreateWindowEx ( 0 , "BUTTON" , "6" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 310 , 360 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_006 (107) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_7 = CreateWindowEx ( 0 , "BUTTON" , "7" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 170 , 290 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_007 (108) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_8 = CreateWindowEx ( 0 , "BUTTON" , "8" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 240 , 290 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_008 (109) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_9 = CreateWindowEx ( 0 , "BUTTON" , "9" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 310 , 290 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_009 (110) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_10 = CreateWindowEx ( 0 , "BUTTON" , "C" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 240 , 500 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_010 (111) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_11 = CreateWindowEx ( 0 , "BUTTON" , "+" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 380 , 290 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_011 (112) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_12 = CreateWindowEx ( 0 , "BUTTON" , "-" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 380 , 360 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_012 (113) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_13 = CreateWindowEx ( 0 , "BUTTON" , "*" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 380 , 430 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_013 (114) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_14 = CreateWindowEx ( 0 , "BUTTON" , "/" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 380 , 500 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_014 (115) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
hWndButton_15 = CreateWindowEx ( 0 , "BUTTON" , "=" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 310 , 500 , 70 , 70 , hWnd ,
( HMENU ) ID_BUTTON_015 (116) , ( HINSTANCE ) GetWindowLong ( hWnd , GWL_HINSTANCE ) , NULL );
break;
case WM_COMMAND:
// 0ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_000 (101) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "0" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s0" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 1ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_001 (102) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 1 ][ 0 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "1" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s1" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 2ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_002 (103) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "2" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s2" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 3ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_003 (104) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "3" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s3" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 4ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_004 (105) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "4" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s4" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 5ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_005 (106) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "5" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s5" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 6ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_006 (107) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "6" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s6" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 7ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_007 (108) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "7" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s7" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 8ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_008 (109) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "8" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s8" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// 9ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_009 (110) )
{
EditBoxLength = GetWindowTextLength ( hWndEdit ); // 表示文字数カウント
// Cボタンを押した後なら
if ( NumBox [ 0 ][ 1 ] == '\0' )
{
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
// 表示文字数が0なら
if ( EditBoxLength == 0 )
{
SetWindowText ( hWndEdit , "9" );
}
// 表示文字数が0より大きいなら
else if ( EditBoxLength > 0 )
{
GetWindowText ( hWndEdit , NumLength , 15 );
sprintf ( NumBox [ Mode ] , "%s9" , &NumLength [ 0 ] ); // 文字列の連結
SetWindowText ( hWndEdit , NumBox [ Mode ] );
}
}
// Cボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_010 (111) )
{
// 表示文字列を0にする
memset ( NumBox [ Mode ] , '\0' , sizeof ( NumBox [ Mode ] ) );
SetWindowText ( hWndEdit , "0" );
}
// +ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_011 (112) )
{
// 表示文字列をリセット
memset ( NumBox [ Mode ] , '\0' , sizeof ( NumBox [ Mode ] ) );
CalculateMark = Plus;
Mode = 2;
}
// -ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_012 (113) )
{
// 表示文字列をリセット
memset ( NumBox [ Mode ] , '\0' , sizeof ( NumBox [ Mode ] ) );
CalculateMark = Minus;
Mode = 2;
}
// *ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_013 (114) )
{
// 表示文字列をリセット
memset ( NumBox [ Mode ] , '\0' , sizeof ( NumBox [ Mode ] ) );
CalculateMark = Multi;
Mode = 2;
}
// /ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_014 (115) )
{
// 表示文字列をリセット
memset ( NumBox [ Mode ] , '\0' , sizeof ( NumBox [ Mode ] ) );
CalculateMark = Div;
Mode = 2;
}
// =ボタンを押したら
if ( LOWORD ( wParam ) == ID_BUTTON_015 (116) )
{
switch ( CalculateMark )
{
case Plus:
Ans = atof ( NumBox [ 1 ] ) + atof ( NumBox [ 2 ] );
break;
case Minus:
Ans = atof ( NumBox [ 1 ] ) - atof ( NumBox [ 2 ] );
break;
case Multi:
Ans = atof ( NumBox [ 1 ] ) * atof ( NumBox [ 2 ] );
break;
case Div:
Ans = atof ( NumBox [ 1 ] ) / atof ( NumBox [ 2 ] );
break;
}
sprintf ( &AnsLength [ 0 ] , "%s" , Ans );
SetWindowText ( hWndEdit , &AnsLength [ 0 ] );
memset ( NumBox , '\0' , sizeof ( NumBox ) );
Mode = 1;
}
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
{
UINT nID = MessageBox ( NULL , "電卓を終了しますか?" , "Message" , MB_YESNO | MB_ICONEXCLAMATION );
if ( nID == IDYES )
{
DestroyWindow ( hWnd );
}
}
break;
case WM_CLOSE:
MessageBox ( NULL , "ウィンドウを閉じます" , "Message" , MB_OK | MB_ICONEXCLAMATION );
DestroyWindow ( hWnd );
break;
case WM_DESTROY:
PostQuitMessage ( 0 );
break;
}
return DefWindowProc ( hWnd , uMsg , wParam , lParam );
}
/////////////// EOF ///////////////