// calculator03.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "calculator03.h"
#include "Counter.h"
using namespace std ;
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
BOOL OnButton(int xMouse, int yMouse, int xLeft, int yTop, int xRight, int yBottom);//判断鼠标在Button上
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_CALCULATOR03, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CALCULATOR03));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CALCULATOR03));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);//(HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = MAKEINTRESOURCE(IDC_CALCULATOR03);
wndclass.lpszClassName = szWindowClass;
wndclass.hIconSm = LoadIcon(wndclass.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wndclass);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ,
500, 300, 250, 220, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent, xMouse, yMouse;
PAINTSTRUCT ps;
HDC hdc;
static std::wstring psText = L"0";
//static int yText = 230 - 8*wcslen(psText);
static wstring inStr ;
static CCounter counter ;
//RECT rect;
switch (message)
{
case WM_KEYDOWN:
switch ( wParam )
{
case VK_DELETE :
counter = CCounter() ;
inStr.clear () ;
//SetWindowText( hEditResult, L"0") ;
psText = L"0";
InvalidateRect (hWnd, NULL, TRUE) ;
break;
}
return 0 ;
case WM_CHAR:
switch( LOWORD( wParam ) )
{
case '1' : case '2' : case '3' :
case '4' : case '5' : case '6' :
case '7' : case '8' : case '9' :
case '0' :
if( inStr .size () == 1 && inStr[ 0 ] == '0' )
inStr[ 0 ] = LOWORD( wParam ) ;
else inStr += LOWORD( wParam ) ;
//wstrcpy( psText, inStr.c_str());
case '.' :
psText = inStr;
if( LOWORD( wParam ) == '.' )
{
if( inStr.empty () )
{
//inStr = SetWindowText( hEditResult, L"0") ;
psText = L"0";
InvalidateRect (hWnd, NULL, TRUE) ;
}
else if( inStr.find_first_of( '.' ) == wstring::npos )
inStr += '.';
psText = inStr;
}
// 如果这前的操作符是以下值,这时又输入了数字,当清空之前记录的值 ;
if( counter.GetOper () == '~' || counter.GetOper () == 'S' ||
counter.GetOper () == '=' || counter.GetSum() == CCounter::ErrDiviend )
counter = CCounter() ;
if( !inStr.empty () )
//strcpy(showEdit,inStr.c_str());
//SetWindowText( hEditResult, inStr.c_str() ) ;
{
InvalidateRect (hWnd, NULL, TRUE) ;
/*hdc = GetDC(hWnd);
TextOut(hdc, 228 - 9*inStr.size(), 9, inStr.c_str(), inStr.size());
ReleaseDC(hWnd, hdc);*/
}
else
//SetWindowText( hEditResult, L"0") ;
{
psText = L"0";
InvalidateRect (hWnd, NULL, TRUE) ;
}
break;
case '\b' ://Space键
if( LOWORD( wParam ) == '\b' )
if( !inStr.empty () )
{
inStr.resize( inStr.size () - 1 ) ;
psText = inStr;
if(inStr.size() == 0)
{
psText = L"0";
}
InvalidateRect (hWnd, NULL, TRUE) ;
/* hdc = GetDC(hWnd);
TextOut(hdc, 228 - 9*inStr.size(), 9, inStr.c_str(), inStr.size());
ReleaseDC(hWnd, hdc);*/
}
else
{
if(inStr.size() == 0)
{
psText = L"0";
}
InvalidateRect (hWnd, NULL, TRUE) ;
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
{
wstring Result ;
CCounter::DouToStr( Result,
counter.GetResult( LOWORD( wParam ), inStr ) ) ;
psText = Result;
InvalidateRect (hWnd, NULL, TRUE) ;
inStr.clear () ;
}
break;
case '~' :
case 0x0073 ://"S"键sqrt
case '=' :
{
wstring Result ;
CCounter::DouToStr( Result,
counter.GetResult( LOWORD( wParam ), inStr ) );
//SetWindowText( hEditResult, Result.c_str() ) ;
InvalidateRect (hWnd, NULL, TRUE) ;
psText = Result;
/*hdc = GetDC(hWnd);
TextOut(hdc, 228 - 9*Result.size(), 9, Result.c_str(), Result.size());
ReleaseDC (hWnd, hdc);*/
inStr.clear () ;
}
break;
}
return 0 ;
case WM_COMMAND:
//case WM_LBUTTONDOWN:
// SetFocus(hWnd);
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
MessageBox(hWnd,L" 计算器V1.02 GDI版\n\n\n版权所有(C) 2011 JONSEN_CHEN",L"关于",MB_OK | MB_ICONINFORMATION);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
xMouse = LOWORD(lParam);
yMouse = HIWORD(lParam);
if (OnButton(xMouse, yMouse, 10, 35, 50, 60))
{
SendMessage(hWnd,WM_CHAR,'7',0); //如果鼠标在Button‘7’上就向窗口发送一个WM_CHAR消息
}
if (OnButton(xMouse, yMouse, 55, 35, 95, 60))//
{
SendMessage(hWnd,WM_CHAR,'8',0);
}
if (OnButton(xMouse, yMouse, 100, 35, 140, 60))//
{
SendMessage(hWnd,WM_CHAR,'9',0);
}
if (OnButton(xMouse, yMouse, 145, 35, 185, 60))//
{
SendMessage(hWnd,WM_CHAR,'+',0);
}
if (OnButton(xMouse, yMouse, 190, 35, 230, 60))//space
{
SendMessage(hWnd,WM_CHAR, 0x8, 0);
}
if (OnButton(xMouse, yMouse, 10, 65, 50, 90))//
{
SendMessage(hWnd,WM_CHAR,'4',0);
}
if (OnButton(xMouse, yMouse, 55, 65, 95, 90))//
{
SendMessage(hWnd,WM_CHAR,'5',0);
}
if (OnButton(xMouse, yMouse, 100, 65, 140, 90))//
{
SendMessage(hWnd,WM_CHAR,'6',0);
}
if (OnButton(xMouse,