// 该程序为子窗口的各种处理子过程,主要是消息处理过程,是实现子窗口功能自定义的过程
// 包括自定义头文件
#include "myCalculator.h"
// 外部定义的全局变量引用
extern HINSTANCE hInst;
HBITMAP hBitmap;
double arr[3];
int nSelected;
int i=0;
int m=0;
double temp[1]={0};
// 定义全局消息ID号与子窗口消息处理过程指针查找表
const MessageProc calculatorMessages[]={
WM_CREATE, goCreateCalculatorWnd,
WM_PAINT, goPaintCalculatorWnd,
WM_CTLCOLORSTATIC,goCtlcolorCalculatorWnd,
WM_CTLCOLORBTN, goCtlcolorCalculatorWnd,
WM_COMMAND, goCommandCalculatorWnd,
};
// 定义七个控件窗口的创建属性
CONTROLSTRUCT controlWnd[]={
{TEXT(""),IDC_EDITDATA,TEXT("EDIT"),ES_RIGHT | WS_BORDER ,60,30,150,25},
{TEXT("7"),IDC_SEVEN,TEXT("BUTTON"),BS_PUSHBUTTON,60,60,30,30},
{TEXT("8"),IDC_EIGHT,TEXT("BUTTON"),BS_PUSHBUTTON,100,60,30,30},
{TEXT("9"),IDC_NINE,TEXT("BUTTON"),BS_PUSHBUTTON,140,60,30,30},
{TEXT("4"),IDC_FOUR,TEXT("BUTTON"),BS_PUSHBUTTON,60,100,30,30},
{TEXT("5"),IDC_FIVE,TEXT("BUTTON"),BS_PUSHBUTTON,100,100,30,30},
{TEXT("6"),IDC_SIX,TEXT("BUTTON"),BS_PUSHBUTTON,140,100,30,30},
{TEXT("1"),IDC_ONE,TEXT("BUTTON"),BS_PUSHBUTTON ,60,140,30,30},
{TEXT("2"),IDC_TWO,TEXT("BUTTON"),BS_PUSHBUTTON,100,140,30,30},
{TEXT("3"),IDC_THREE,TEXT("BUTTON"),BS_PUSHBUTTON,140,140,30,30},
{TEXT("0"),IDC_ZERO,TEXT("BUTTON"),BS_PUSHBUTTON,60,180,30,30},
{TEXT("+"),IDC_PLUS,TEXT("BUTTON"),BS_PUSHBUTTON,180,180,30,30},
{TEXT("-"),IDC_MINUS,TEXT("BUTTON"),BS_PUSHBUTTON,180,140,30,30},
{TEXT("*"),IDC_MULTIPLY,TEXT("BUTTON"),BS_PUSHBUTTON,180,100,30,30},
{TEXT("/"),IDC_DIVIDE,TEXT("BUTTON"),BS_PUSHBUTTON,180,60,30,30},
{TEXT("c"),IDC_CLEAR,TEXT("BUTTON"),BS_PUSHBUTTON,100,180,30,30},
{TEXT(" = "),IDC_PUSHBTN,TEXT("BUTTON"),BS_PUSHBUTTON,140,180,30,30},
};
// 注册子窗口类
int registerCalculator(HINSTANCE hInstance)
{
// 定义窗口类结构体对象
WNDCLASS wc;
// 填充结构体中的各元素
wc.hInstance = hInstance;
wc.lpszClassName = strSinChildClassName;
wc.lpfnWndProc = calculatorWndProc;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.style = CS_DBLCLKS;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
// 注册窗口类
if(!RegisterClass(&wc))
return 0;
return 1;
}
// 子窗口过程体,必须为回调函数,当窗口收到消息后,会告知Windows CE系统执行对应的消息处理过程
LRESULT CALLBACK calculatorWndProc(HWND hWnd, UINT msgCode, WPARAM wParam, LPARAM lParam)
{
INT i;
// 查找对应的消息ID号,并执行相应的消息处理过程
for(i=0; i < dim(calculatorMessages); i++)
{
if(msgCode == calculatorMessages[i].uCode)
return (*calculatorMessages[i].functionName)(hWnd, msgCode, wParam, lParam);
}
// 对于不在消息查找表中的消息,调用Windows CE的缺省窗口过程
return DefWindowProc(hWnd, msgCode, wParam, lParam);
}
// 子窗口收到WM_CREATE消息后的处理过程体
LRESULT goCreateCalculatorWnd(HWND hWnd, UINT msgCode, WPARAM wParam, LPARAM lParam)
{
int i;
if(hBitmap==0)
{
hBitmap = (HBITMAP)LoadImage(hInst,MAKEINTRESOURCE(ID_BITMAP),IMAGE_BITMAP,0,0,0);
}
// 在子窗口中创建八个控件
for(i=0;i<dim(controlWnd);i++)
{
CreateWindow(controlWnd[i].szClass,controlWnd[i].szTitle,
controlWnd[i].dwStyle | WS_VISIBLE | WS_CHILD,
controlWnd[i].x,controlWnd[i].y,
controlWnd[i].xw,controlWnd[i].yh,
hWnd,(HMENU)controlWnd[i].nID,hInst,NULL);
}
// 选中单选钮组中的第一个
//CheckRadioButton(hWnd,IDC_PLUS,IDC_MINUS,IDC_MULTIPLY, IDC_DIVIDE);
return 0;
}
// 子窗口收到WM_PAINT消息后的处理过程体
LRESULT goPaintCalculatorWnd(HWND hWnd, UINT msgCode, WPARAM wParam, LPARAM lParam)
{
// 定义画板结构体对象
PAINTSTRUCT ps;
// 定义矩形结构体对象
RECT rect;
BITMAP bmp;
HBRUSH hOldBrush;
// 定义设备环境句柄(或称设备上下文,一般指触摸屏)和内存映像设备
HDC hdc,hdcMem;
// 获得客户区矩形大小
GetClientRect(hWnd,&rect);
// 获得设备环境,必须与EndPaint配对使用
hdc=BeginPaint(hWnd,&ps);
hdcMem = CreateCompatibleDC(hdc);
hOldBrush = (HBRUSH)SelectObject(hdcMem,hBitmap);
GetObject(hBitmap,sizeof(BITMAP),&bmp);
BitBlt(hdc,rect.left,rect.top,bmp.bmWidth,bmp.bmHeight,hdcMem,0,0,SRCCOPY);
SelectObject(hdcMem,hOldBrush);
DeleteDC(hdcMem);
SetBkMode(hdc,TRANSPARENT);
// 释放设备环境资源
EndPaint(hWnd,&ps);
return 0;
}
// 控件显示前引发的消息WM_CTLCOLORxx,处理该消息可以改变控件的显示样式,如背景色和字体等
LRESULT goCtlcolorCalculatorWnd(HWND hWnd, UINT msgCode, WPARAM wParam, LPARAM lParam)
{
// 设置控件为透明背景
SetBkMode(HDC(wParam),TRANSPARENT);
// 提取出控件的ID号,为对应的控件背景着色,按钮着金黄色,其他为白色
switch(GetDlgCtrlID((HWND)lParam))
{
case IDC_PLUS:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_MINUS:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_MULTIPLY:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_DIVIDE:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_PUSHBTN:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_EDITDATA:
case IDC_CLEAR:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_ONE:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_TWO:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_THREE:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_FOUR:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_FIVE:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_SIX:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_SEVEN:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_EIGHT:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_NINE:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
case IDC_ZERO:
return (LRESULT)CreateSolidBrush(RGB(200,200,70));
return 0;
break;
default:
return 0;
break;
}
}
// 子窗口收到点击控件引发的WM_COMMAND消息后的处理过程体
LRESULT goCommandCalculatorWnd(HWND hWnd, UINT msgCode, WPARAM wParam, LPARAM lParam)
{
// 定义字符串,保存计算结果
TCHAR szOut[128];
// 定义字符串变量
TCHAR szEdit[64],*stopStr;
/*if(GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit))!=0)
a=wcstod(szEdit,&stopStr);*/
// LOWORD(wParam)为控件ID号,据此识别被按下去的控件
switch(LOWORD(wParam))
{
case IDC_PUSHBTN:
if(i==1){
GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
arr[0]=wcstod(szEdit,&stopStr);
arr[2]=calculator(nSelected,arr[0],arr[1]);
wsprintf(szOut,TEXT("%.5lf"),arr[2]);
// 把计算结果显示在EDIT控件IDC_EDITRESULT中
SendDlgItemMessage(hWnd,IDC_EDITDATA,WM_SETTEXT,(WPARAM)0,(LPARAM)szOut);
i=0;
}
else{
//GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
//arr[1]=wcstod(szEdit,&stopStr);
//wsprintf(szOut,TEXT("%.5lf"),arr[1]);
//// 把计算结果显示在EDIT控件IDC_EDITRESULT中
//SendDlgItemMessage(hWnd,IDC_EDITDATA,WM_SETTEXT,(WPARAM)0,(LPARAM)szOut);
}
break;
case IDC_PLUS:
//GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
//x=wcstod(szEdit,&stopStr);
nSelected= 1;
i=1;
m=0;
temp[0]=0;
GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
arr[0]=wcstod(szEdit,&stopStr);
if(arr[0]!=0){
arr[1]=arr[0];
SetDlgItemText(hWnd,IDC_EDITDATA,NULL);
}
break;
case IDC_MINUS:
nSelected= 2;
i=1;
m=0;
temp[0]=0;
GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
arr[0]=wcstod(szEdit,&stopStr);
if(arr[0]!=0){
arr[1]=arr[0];
SetDlgItemText(hWnd,IDC_EDITDATA,NULL);
}
break;
case IDC_MULTIPLY:
nSelected= 3;
i=1;
m=0;
temp[0]=0;
GetDlgItemText(hWnd,IDC_EDITDATA,szEdit,sizeof(szEdit));
arr[0]=wcstod(szEdit,&stopStr);
if(arr[0]!=0){
arr[1]=arr[0];
SetDlgItemText(hWnd,IDC_EDITDATA,NULL);
}
break;
case IDC_DIVIDE:
nSelected= 4;
i=1;
m=0;
temp[0]=0;
Ge
评论0
最新资源