Janela, Menus, Controles e Eventos

João 3:16
#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include <locale.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "resource.h"

#define ID_BTN   1
#define INTERNET 2

static HWND hCaption1;

//static HWND button1;

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

void addMenus(HWND); /**Adciona Menus*/
void addControls(HWND);

HMENU hMenu;         /**Menu de Eventos*/

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

int btn1() {
    int btn = MessageBox(NULL, "Botão Internet", "Desenvolvedor APL", MB_OK);
    return btn;
}

int versao() {
    int vs = MessageBox(NULL, "Windows Version", "Error", MB_ICONEXCLAMATION);;
    return vs;
}

int ajuda() {
    int h = MessageBox(NULL, "Ok para Sair", "Menu Ajuda:", MB_OKCANCEL);
    return h;
}

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(NULL, IDI_INFORMATION);
    wincl.hIconSm      = LoadIcon(hThisInstance, IDI_QUESTION);
    wincl.hCursor      = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra   = 0;
    wincl.cbWndExtra   = 0;
    wincl.hbrBackground = GetSysColorBrush(COLOR_GRADIENTINACTIVECAPTION);

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

    hwnd = CreateWindowEx (
           0, szClassName, _T("Abrindo um Arquivo em C através de uma página WEB"),       /* Titulo da Janela */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT, CW_USEDEFAULT, 650, 320,
           HWND_DESKTOP, NULL, hThisInstance, NULL
    );

    ShowWindow (hwnd, nCmdShow);

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

    setlocale(LC_ALL, "ptb");

    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        case WM_COMMAND:
            if(LOWORD(wParam) == 2) { /**Propriedades*/
                int len = GetWindowTextLength(hCaption1) + 1;
                static char title[500];

                GetWindowText(hCaption1, title, len);

                MessageBox(hwnd, title, "", MB_OK);
            }
            switch(wParam) {
            case 0:
                //system("notepad");
                break;

            case 1:
                //ajuda();
                break;

            }

            if(LOWORD(wParam) == INTERNET) {     /**Windows Version*/
                versao();
                return 0;
            }
            if(LOWORD(wParam) == 1) {
                ajuda();
                return 0;
            }
            if(LOWORD(wParam) == ID_BTN) { //Internet Button
                btn1();
                return 0;
            }
            break;
        case WM_CREATE:
            addMenus(hwnd);
            addControls(hwnd);
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

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

    HMENU hFileMenu = CreateMenu();

    AppendMenu(hMenu, MF_STRING, 0, "Arquivo");
    AppendMenu(hMenu, MF_STRING, 1, "Ajuda");
    AppendMenu(hMenu, MF_STRING, 2, "Propriedade");
    AppendMenu(hMenu, MF_STRING, NULL, "Gravar");

    SetMenu(hwnd, hMenu);
}

void addControls(HWND hwnd) {
    CreateWindowW(
        L"static", L"1970 - Surge a Linguagem C baseada no BCPL e, em 1971 a Intel lanca o Intel 4004",
        WS_VISIBLE | WS_CHILD | WS_BORDER,
        10, 10, 560, 20,
        hwnd, NULL, NULL, NULL
    );

    hCaption1 = CreateWindowW(
        L"static", L"1981 - Primeiro Sistema Operacional MS-DOS",
        WS_VISIBLE | WS_CHILD | WS_BORDER,
        10, 50, 360, 20,
        hwnd, NULL, NULL, NULL
    );

    CreateWindowW(
        L"static", L"1985 - Primeira Interface Grafica do Windows",
        WS_VISIBLE | WS_CHILD | WS_BORDER,
        10, 90, 360, 20,
        hwnd, NULL, NULL, NULL
    );

    CreateWindowW(
        L"static", L"1993 - A Microsoft lanca o Windows NT para Desktop",
        WS_VISIBLE | WS_CHILD | WS_BORDER,
        10, 130, 360, 20,
        hwnd, NULL, NULL, NULL
    );

    CreateWindowW(
        L"Edit", L"Caixa de Texto..", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
        10, 170, 540, 40,
        hwnd, NULL, NULL, NULL
    );


    CreateWindowW(
        L"Button", L"Internet", WS_VISIBLE | WS_CHILD | WS_BORDER,
        10, 230, 160, 20,
        hwnd, (HMENU) INTERNET, NULL, NULL
    );

    CreateWindow(
        TEXT("button"), TEXT("Windows Version"), WS_VISIBLE | WS_CHILD,
        200, 230, 160, 20,
        hwnd, (HMENU) ID_BTN, NULL, NULL
    );

    CreateWindow(
        TEXT("button"), TEXT("Status do Sistema"), WS_VISIBLE | WS_CHILD,
        390, 230, 160, 20,
        hwnd, NULL, NULL, NULL
    );
}

Comentários