Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525325
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-17 00:16:04

也不知道现在是几点了, 总之, 好累, 代码先放在这里, 注释哪天再写吧,今天晚上实在不行了。
这个简单的计算器增加了键盘接口, 所以长了点, 再说是用API写的, 长点正常:
我尽量做到与标准计算器一样的功能,但是还是有点小问题:精确度不高, 这点以后再想吧, 其他基本达到了:这是源代码,没有注释, 过几天再添上吧!!!!

其实几年前我就用别的语言实现过这个功能, 用API写有点不好写吧,哈哈


#include <windows.h>
#include <stdlib.h>

HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    static TCHAR szAppName[] = TEXT("dirDemo");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    hInst = hInstance;
    
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = COLOR_BTNFACE + 1;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    
    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }
    
    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        500,
        300,
        300,
        260,
        NULL,
        NULL,
        hInstance,
        NULL);
    
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    
    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static HWND hwndNumber[16], hwndStatic,
        hwndClear, hwndEqual;
    static cxChar, cyChar;
    int i, col, row;
    static double leftNumber, rightNumber;
    static char left[20], right[20], buffer[20];
    static char operators = '\0';
    static BOOL bOperators, bEqual;
    HBRUSH hBrush;
    HDC hdc;
    TCHAR* bufferIcon[16] = {
        "7",
            "8",
            "9",
            "/",
            "4",
            "5",
            "6",
            "*",
            "1",
            "2",
            "3",
            "-",
            "0",
            "+/-",
            ".",
            "+"
    };
    
    switch(message) {
    case WM_CREATE:
        cxChar = LOWORD(GetDialogBaseUnits());
        cyChar = HIWORD(GetDialogBaseUnits());
        col = row = 0;
        left[0] = ' ';
        right[0] = ' ';
        for(i = 0; i < 16; i++) {
            row = i / 4;
            col = i % 4;
            hwndNumber[i] = CreateWindow(TEXT("button"),
                bufferIcon[i],
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                13 + col * (60 + 8), 80 + row * (30 + 8),
                60, 30,
                hwnd,
                (HMENU)i,
                hInst,
                NULL);
        }
        hwndStatic = CreateWindow(TEXT("static"),
            TEXT("0"),
            WS_CHILD | WS_VISIBLE | SS_RIGHT,
            13, 8,
            270, 20,
            hwnd,
            (HMENU)16,
            hInst,
            NULL);
        hwndClear = CreateWindow(TEXT("button"),
            TEXT("C"),
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            13, 46,
            128, 30,
            hwnd,
            (HMENU)17,
            hInst,
            NULL);
        
        hwndEqual = CreateWindow(TEXT("button"),
            TEXT("="),
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            149, 46,
            128, 30,
            hwnd,
            (HMENU)18,
            hInst,
            NULL);
        return 0;
        
    case WM_COMMAND:
        switch(LOWORD(wParam)) {
        case 0:
            if(!bOperators) {
                strcat(left, "7");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "7");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 1:
            if(!bOperators) {
                strcat(left, "8");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "8");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 2:
            if(!bOperators) {
                strcat(left, "9");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "9");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 3:
            bOperators = TRUE;
            operators = '/';
            break;
        case 4:
            if(!bOperators) {
                lstrcat(left, "4");
                SetWindowText(hwndStatic, left);
            } else {
                lstrcat(right, "4");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 5:
            if(!bOperators) {
                lstrcat(left, "5");
                SetWindowText(hwndStatic, left);
            } else {
                lstrcat(right, "5");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 6:
            if(!bOperators) {
                lstrcat(left, "6");
                SetWindowText(hwndStatic, left);
            } else {
                lstrcat(right, "6");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 7:
            bOperators = TRUE;
            operators = '*';
            
            break;
        case 8:
            if(!bOperators) {
                strcat(left, "1");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "1");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 9:
            if(!bOperators) {
                strcat(left, "2");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "2");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 10:
            if(!bOperators) {
                strcat(left, "3");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "3");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 11:
            bOperators = TRUE;
            operators = '-';
            break;
        case 12:
            if(!bOperators) {
                strcat(left, "0");
                SetWindowText(hwndStatic, left);
            } else {
                strcat(right, "0");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 13:
            if(!bOperators) {
                if(left[0] == '-') {
                    left[0] = ' ';
                    SetWindowText(hwndStatic, left);
                } else {
                    left[0] = '-';
                    SetWindowText(hwndStatic, left);
                }
            }
            else {
                if(right[0] == '-') {
                    right[0] = ' ';
                    SetWindowText(hwndStatic, right);
                } else {
                    right[0] = '-';
                    SetWindowText(hwndStatic, right);
                }
            }
            break;
        case 14:
            if(!bOperators) {
                strcat(left, ".");
                SetWindowText(hwndStatic, left);
            } else {
                lstrcat(right, ".");
                SetWindowText(hwndStatic, right);
            }
            break;
        case 15:
            
            bOperators = TRUE;
            operators = '+';
            break;
            
            /*
            * case 16:
            * static control, we needn't handle it
            */

        case 17:
            left[0] = ' ';
            right[0] = ' ';
            memset(left + 1, '\0', sizeof(left));
            memset(right + 1, '\0', sizeof(right));
            bOperators = FALSE;
            SetWindowText(hwndStatic, "0");
            operators = '\0';
            bEqual = FALSE;
            break;
        case 18:
            
            leftNumber = atof(left);
            rightNumber = atof(right);
            bEqual = TRUE;
            right[0] = ' ';
            memset(right + 1, '\0', sizeof(right));
            bOperators = FALSE;
            
            switch(operators) {
            case '+':
                leftNumber = leftNumber + rightNumber;
                sprintf(buffer, "%f", leftNumber);
                SetWindowText(hwndStatic, buffer);
                if(leftNumber >= 0)
                    strcpy(left + 1, buffer);
                else
                    strcpy(left, buffer);
                break;
            case '-':
                leftNumber = leftNumber - rightNumber;
                sprintf(buffer, "%f", leftNumber);
                SetWindowText(hwndStatic, buffer);
                if(leftNumber >= 0)
                    strcpy(left + 1, buffer);
                else
                    strcpy(left, buffer);
                break;
            case '*':
                leftNumber = leftNumber * rightNumber;
                sprintf(buffer, "%f", leftNumber);
                SetWindowText(hwndStatic, buffer);
                if(leftNumber >= 0)
                    strcpy(left + 1, buffer);
                else
                    strcpy(left, buffer);
                break;
            case '/':
                if(rightNumber == 0) {
                    MessageBox(NULL, TEXT("郁闷,你竟然让我除以0, 我不活了!"),
                        TEXT("我在抱怨"), MB_ICONINFORMATION);
                    SendMessage(hwnd, WM_COMMAND,(WPARAM)17, 0);
                    break;
                }
                leftNumber = leftNumber / rightNumber;
                sprintf(buffer, "%f", leftNumber);
                SetWindowText(hwndStatic, buffer);
                if(leftNumber >= 0)
                    strcpy(left + 1, buffer);
                else
                    strcpy(left, buffer);
                break;
            default:
                break;
            }
            break;
        }
        SetFocus(hwnd);
        return 0;

        case WM_CTLCOLORSTATIC:
            
            SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
            SetBkColor(hdc, RGB(255, 255, 255));
            hBrush = CreateSolidBrush(RGB(255, 255, 255));
            return (LRESULT)hBrush;

        case WM_CHAR:
            for(i = 0; i < LOWORD(lParam); i++) {
                switch(wParam) {
                case '7':
                    SendMessage(hwnd, WM_COMMAND, 0, 0);
                    break;
                case '8':
                    SendMessage(hwnd, WM_COMMAND, 1, 0);
                    break;
                case '9':
                    SendMessage(hwnd, WM_COMMAND, 2, 0);
                    break;
                case '/':
                    SendMessage(hwnd, WM_COMMAND, 3, 0);
                    break;
                case '4':
                    SendMessage(hwnd, WM_COMMAND, 4, 0);
                    break;
                case '5':
                    SendMessage(hwnd, WM_COMMAND, 5, 0);
                    break;
                case '6':
                    SendMessage(hwnd, WM_COMMAND, 6, 0);
                    break;
                case '*':
                    SendMessage(hwnd, WM_COMMAND, 7, 0);
                    break;
                case '1':
                    SendMessage(hwnd, WM_COMMAND, 8, 0);
                    break;
                case '2':
                    SendMessage(hwnd, WM_COMMAND, 9, 0);
                    break;
                case '3':
                    SendMessage(hwnd, WM_COMMAND, 10, 0);
                    break;
                case '-':
                    SendMessage(hwnd, WM_COMMAND, 11, 0);
                    break;
                case '0':
                    SendMessage(hwnd, WM_COMMAND, 12, 0);
                    break;
                case '.':
                    SendMessage(hwnd, WM_COMMAND, 14, 0);
                    break;
                case '+':
                    SendMessage(hwnd, WM_COMMAND, 15, 0);
                    break;
                case '\r':
                case '=':
                    SendMessage(hwnd, WM_COMMAND, 18, 0);
                    break;
                default:
                    break;
                }
            }
            return 0;
        
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(898) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~