Cálculo de Adição

Calculo da Adição
#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

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

#define ID_BUTTON  1

static HWND num1, num2;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND       hwnd;         /** Evento de Janela */
    MSG        messages;     /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /** Dados e struturas para Classe de Janela */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_EXCLAMATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_EXCLAMATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE);

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /** Nome da Classe */
           _T("Aplicação Gráfica em C - Somar Conteúdo"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           500,                 /** Largura do Programa */
           200,                 /** Altura em pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /** Nenhum dado de Criação da janela */
    );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message,
                                  WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_CREATE:
            CreateWindow(
                TEXT("static"),
                TEXT("Cálculo da Adição em Linguagem C, Aplicação Totalmente Gráfica"),
                WS_VISIBLE | WS_CHILD,
                30, 20, 480, 20,
                hwnd, NULL, NULL, NULL
            );

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

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

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

            CreateWindow(
                TEXT("static"), TEXT("http://wwww.esav-rj.com/HOME/download/"),
                WS_VISIBLE | WS_CHILD,
                30, 90, 280, 20,
                hwnd, NULL, NULL, NULL
            );
            break;

        case WM_COMMAND:
            if(LOWORD(wParam)==ID_BUTTON) {
                int n1 = GetWindowTextLength(num1)+1;
                int n2 = GetWindowTextLength(num2)+1;
                static char t1[40];
                static char t2[40];
                GetWindowText(num1, t1, n1);
                GetWindowText(num2, t2, n2);
                int res = atoi(t1)+atoi(t2); /**Resultado da Soma*/
                char tam[40];
                itoa(res, tam, 10); /**Converte inteiro para String*/
                MessageBox(NULL, tam, "Resultado: ", MB_OK);
                return 0;
            }
            break;

        case WM_DESTROY:
            PostQuitMessage (0);  /** Saída padrão do Sistema */
            break;
        default:                  /** for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Comentários