Linguagem C Média Escolar - Post modificado em 04/06/2024

main.html
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "resource.h"

#pragma comment(lib, "user32.lib")

#define BTN1 1
#define BTN2 2
#define BTN3 3
#define DCS  4

void addMenus(HWND);

static HWND nome, nome2, num1, num2, num3, num4, resultado;

HMENU hMenu;

char txt[] = {"Média Escolar"};
char descricao[] = {
    " C para Windows\n C para Linux\n C para App Móveis\n C Padrão ANSI"
};
char descricao2[] = {
    "Doação Voluntária - PIX: marcio_br@protonmail.com"
};

TCHAR szBuffer[20], szBuffer2[20];

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lParam) {
    static COLORREF cr, crLast;
    static HDC hdcScreen;
    HDC hdc;
    PAINTSTRUCT ps;
    POINT pt;
    RECT rc;
    TCHAR szBuffer[16];
    
    int vCores[3] = {COLOR_WINDOW, COLOR_ACTIVECAPTION, COLOR_INFOBK};
    DWORD CorPadraoDoSistema[3];
    
    switch(msg) {
        case WM_CREATE:
            addMenus(hwnd);
            nome = CreateWindowW(
                L"static", L"Calcular a Media de 4 Notas", WS_VISIBLE | WS_CHILD,
                10, 20, 260, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindow(
                TEXT("button"), TEXT("Dados Cor de Fundo"), WS_VISIBLE | WS_CHILD,
                10, 90, 150, 40,
                hwnd, (HMENU) BTN1, NULL, NULL
            );
            CreateWindow(
                TEXT("button"), TEXT("Sair"), WS_VISIBLE | WS_CHILD,
                180, 90, 90, 40,
                hwnd, (HMENU) BTN2, NULL, NULL
            );
            nome2 = CreateWindow(
                TEXT("static"), descricao2, WS_VISIBLE | WS_CHILD,
                10, 180, 380, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindow(TEXT("static"), descricao, WS_VISIBLE | WS_CHILD,
                         340, 20, 180, 80,
                         hwnd, NULL, NULL, NULL);

            CreateWindowW(
                L"static", L"1 Nota: ", WS_VISIBLE | WS_CHILD,
                10, 220, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            num1 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                74, 220, 50, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindowW(
                L"static", L"2 Nota: ", WS_VISIBLE | WS_CHILD,
                130, 220, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            num2 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                196, 220, 50, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindowW(
                L"static", L"3 Nota: ", WS_VISIBLE | WS_CHILD,
                256, 220, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            num3 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                322, 220, 50, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindowW(
                L"static", L"4 Nota: ", WS_VISIBLE | WS_CHILD,
                386, 220, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            num4 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
                452, 220, 50, 20,
                hwnd, NULL, NULL, NULL
            );
            CreateWindow(
                TEXT("button"), TEXT("Calcular"), WS_VISIBLE | WS_CHILD | WS_BORDER,
                10, 270, 100, 20,
                hwnd, (HMENU) BTN3, NULL, NULL
            );
            CreateWindow(
                TEXT("static"), TEXT("Resultado:"), WS_VISIBLE | WS_CHILD,
                280, 270, 100, 20,
                hwnd, NULL, NULL, NULL
            );
            resultado = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
                404, 270, 100, 20,
                hwnd, NULL, NULL, NULL
            );
            return 0;
        case WM_COMMAND:
            if(LOWORD(wP)==BTN1) {
                //int len = GetWindowTextLength(nome)+1;
                //static char title[500];
                //etWindowText(nome, title, len);
                //MessageBox(NULL, title, "", MB_OK);
                CorPadraoDoSistema[1] = GetSysColor(vCores[1]);
            	wsprintf(
                    szBuffer2, TEXT("Cor de fundo: %02X %02X %02X "),
                    GetRValue(CorPadraoDoSistema[1]),
                    GetGValue(CorPadraoDoSistema[1]),
                    GetBValue(CorPadraoDoSistema[1])
                );
            	MessageBox(NULL, szBuffer2, "Cor Padrão", MB_OK);
                return 0;
            }
            if(LOWORD(wP)==BTN2) {
                exit(0);
            }
            if(LOWORD(wP)==BTN3) {
                int n1 = GetWindowTextLength(num1)+1;
                int n2 = GetWindowTextLength(num2)+1;
                int n3 = GetWindowTextLength(num3)+1;
                int n4 = GetWindowTextLength(num4)+1;

                static char t1[50], t2[50], t3[50], t4[50];

                GetWindowText(num1, t1, n1);
                GetWindowText(num2, t2, n2);
                GetWindowText(num3, t3, n3);
                GetWindowText(num4, t4, n4);

                float res = (atof(t1)+atof(t2)+atof(t3)+atof(t4))/4;

                char tam[30];
                //Imprime com limite de duas casas depois da vígula(ponto)
                sprintf(tam, "%.2f", res);

                //ltoa(res, tam, 10);

                SetWindowText(resultado, tam);

                FILE *arq;
                arq=fopen("Teste.txt", "w+");
                fwrite(tam, sizeof(arq),10,arq);
                fclose(arq);
            }
            if(LOWORD(wP)==DCS) {
            	CorPadraoDoSistema[0] = GetSysColor(vCores[0]);
            	SetSysColors(3, vCores, CorPadraoDoSistema);
            	wsprintf(
                    szBuffer, TEXT(" %02X %02X %02X "),
                    GetRValue(CorPadraoDoSistema[0]),
                    GetGValue(CorPadraoDoSistema[0]),
                    GetBValue(CorPadraoDoSistema[0])
                );
                MessageBox(NULL, szBuffer, "Cor Padrão", MB_OK);
            }
            return 0;
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
		
        default:
            return DefWindowProc(hwnd, msg, wP, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR szCL, int iCS) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd;
    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_ACTIVECAPTION);
    wc.lpszClassName = "WindowClass";
    wc.hIcon		 = LoadIcon(hI, ICONE);
    wc.hIconSm		 = LoadIcon(hI, ICONE); 

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

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,"WindowClass",txt,WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 580, 375,
        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) { /* If no error is received... */
        TranslateMessage(&msg); /* Translate key codes to chars if present */
        DispatchMessage(&msg); /* Send it to WndProc */
    }
    return msg.wParam;
}

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

    HMENU hFileMenu = CreateMenu();

    AppendMenu(hMenu, MF_STRING, NULL, "Arquivo");
    AppendMenu(hMenu, MF_STRING, NULL, "Ajuda");
    AppendMenu(hMenu, MF_STRING, NULL, "Propriedade");
    AppendMenu(hMenu, MF_STRING, DCS, "Default");
    AppendMenu(hMenu, MF_STRING, NULL, "Keyboard");

    SetMenu(hwnd, hMenu);

}

resource.html


/* -- Arquivo: resource.h -- */


#define ICONE 17

resourcerc.html


/* -- Arquivo: resource.rc -- */


#include "resource.h"
#include "afxres.h"

ICONE ICON "project1.ico"

Comentários