Linguagem C/C++ IMC em modo Janela

IMC

#include <windows.h>
#include <stdio.h>

#define ID_BTN_1 1

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void Controles(HWND);

static HWND r, massa, altura;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    static   TCHAR szAppName[] = TEXT("IMC");
    HWND     hwnd;
    MSG      msg;
    WNDCLASS wndclass;

    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 = GetSysColorBrush(COLOR_INACTIVECAPTION);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(
        szAppName, TEXT("IMC:"), WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 280, 220,
        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) {
    switch(message) {
        case WM_SIZE:
            Controles(hwnd);
            return 0;

        case WM_COMMAND:

            if(LOWORD(wParam)==ID_BTN_1) {
                int ms = GetWindowTextLength(massa)+1;
                int at = GetWindowTextLength(altura)+1;
                static char c1[50], c2[50];
                GetWindowText(massa, c1, ms);
                GetWindowText(altura, c2, at);
                float res = (atof(c1) / (atof(c2)*atof(c2)));
                static char resultado[500];
                sprintf(resultado, "%.2f", res);
                SetWindowText(r, resultado);
            }
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

void Controles(HWND hwnd) {
    CreateWindow(
        TEXT("static"), TEXT("Peso"), WS_VISIBLE | WS_CHILD,
        10, 40, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    massa = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
        140, 40, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindow(
        TEXT("static"), TEXT("Altura"), WS_VISIBLE | WS_CHILD,
        10, 80, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    altura = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
        140, 80, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindow(
        TEXT("button"), TEXT("Calcular"), WS_VISIBLE | WS_CHILD,
        10, 120, 100, 20,
        hwnd, (HMENU) ID_BTN_1, NULL, NULL
    );
    r = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
        140, 120, 100, 20,
        hwnd, NULL, NULL, NULL
    );
}

Comentários