Linguagem C - ((x =1 até o valor 3) - m) ao quadrado, windows.h

Linguagem C - Somatório de x menos a variável m ao quadrado
#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include "resource.h"

#define ID_BUTTON  1

static HWND num1, num2, ResSomatorio;

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

void addMenus(HWND);
HMENU MenuPrincipal;

TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {
    HWND       hwnd;
    MSG        messages;
    WNDCLASSEX wincl;

    wincl.hInstance     = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc   = WindowProcedure;
    wincl.style         = CS_DBLCLKS;
    wincl.cbSize        = sizeof(WNDCLASSEX);
    wincl.hIcon         = LoadIcon (hThisInstance, IDI_ICON);
    wincl.hIconSm       = LoadIcon (NULL, IDI_ICON);
    wincl.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName  = NULL;
    wincl.cbClsExtra    = 0;
    wincl.cbWndExtra    = 0;
    wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE);

    if (!RegisterClassEx(&wincl)) {
        return 0;
    }

    hwnd = CreateWindowEx (
        0, szClassName, _T("((1+2+3) - m)*((1+2+3) - m)"), WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 220,
        HWND_DESKTOP, NULL, hThisInstance, NULL
    );

    ShowWindow (hwnd, nCmdShow);

    while (GetMessage (&messages, NULL, 0, 0)) {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CREATE:
            addMenus(hwnd);

            CreateWindow(
                TEXT("static"),TEXT("Resultado = ((3 + 2 + 1) - m)²"),WS_VISIBLE | WS_CHILD,
                30, 20, 480, 20,
                hwnd, NULL, NULL, NULL
            );

            num1 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                30, 100, 80, 20,
                hwnd, NULL, NULL, NULL
            );

            num2 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                130, 100, 80, 20,
                hwnd, NULL, NULL, NULL
            );

            CreateWindow(
                TEXT("button"), TEXT("Calcular"), WS_VISIBLE | WS_CHILD | WS_BORDER,
                220, 100, 80, 20,
                hwnd, (HMENU) ID_BUTTON, NULL, NULL
            );

            ResSomatorio = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                320, 100, 80, 20,
                hwnd, NULL, NULL, NULL
            );
            break;

        case WM_COMMAND:
            if(LOWORD(wParam)==ID_BUTTON) {
                unsigned long somatorio(int n) {
                    int i, x;
                    for(i=1, x=0; i<=n; i++) {
                        x = x+i;
                    }
                    return (x);
                }
                int n1 = GetWindowTextLength(num1)+1; /**Valor de num1*/
                int n2 = GetWindowTextLength(num2)+1; /**Valor de num2*/

                static char t1[40], t2[40];

                GetWindowText(num1, t1, n1);
                GetWindowText(num2, t2, n2);

                int resultado, valor = atoi(t1);

                resultado = (somatorio(valor)-atoi(t2)) * (somatorio(valor)-atoi(t2));

                static char tamanho[40];

                itoa(resultado, tamanho, 10);

                SetWindowText(ResSomatorio, tamanho);
                return 0;
            }
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

void addMenus(HWND hwnd) {
    MenuPrincipal = CreateMenu();

    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Arquivo");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Editar");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Copiar");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Desligar");

    SetMenu(hwnd, MenuPrincipal);
}

Comentários