IMC - Linguagem C/C++

main.html
#include <windows.h>

#define ID_BTN_1 5

void Controles(HWND);

static HWND r, massa, altura;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
    switch(msg) {
        case WM_CREATE:
            Controles(hwnd);
            return 0;
        case WM_COMMAND:
            if(LOWORD(wP)==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;
        /* Upon destruction, tell the main thread to stop */
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, msg, wP, lP);
    }
    return 0;
}

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
    );
}

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR szCL, int iCS) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG msg; /* A temporary location for all messages */

    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize		 = sizeof(WNDCLASSEX);
    wc.lpfnWndProc	 = WndProc;
    wc.hInstance	 = hI;
    wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetSysColorBrush(COLOR_MENUHILIGHT);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc)) {
        MessageBox(
            NULL,
            "Window Registration Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE, "WindowClass", "IMC",
        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        300, /* width */
        220, /* height */
        NULL, NULL, hI, NULL
    );

    if(hwnd == NULL) {
        MessageBox(
            NULL,
            "Window Creation Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }
	
    while(GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

Comentários