SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)(idCr[LOWORD(wP)-10]));


/**Ouvi a correção, não a rejeiteis e sede sábios. Provérbios 8:33*/

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

#define ID_TIMER 1

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

TCHAR szAppName[] = TEXT("Menu");
static TCHAR nm[] = "CheckMenuItem(hMenu, iSelection, MF_CHECKED);";
static TCHAR aj[] = "MENUITEM \"&Ajuda..\",           IDM_APP_HELP";
static TCHAR sb[] = "MENUITEM \"&Sobre..\",           IDM_APP_ABOUT";

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, PSTR szCmdLine, int iCS) {
    HWND     hwnd;
    MSG      msg;
    WNDCLASS wndclass;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hI;
    wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName  = szAppName;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass (&wndclass)) {
        MessageBox(NULL, TEXT("Error!"), szAppName, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindow(
        szAppName, nm, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hI, NULL
    );
    ShowWindow(hwnd, iCS);
    UpdateWindow(hwnd);

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

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lParam) {
    static int idCr[5] = { 11, 14, 2, 3, 17 };
    static int iSelection = IDM_COR_1;
    HMENU      hMenu;

    switch(msg) {
        case WM_COMMAND:
            hMenu = GetMenu(hwnd);
            switch(LOWORD(wP)) {
                case IDM_FILE_NEW:
                case IDM_FILE_OPEN:
                case IDM_FILE_SAVE:
                case IDM_FILE_SAVE_AS:
                    MessageBeep(0);
                    return 0;

                case IDM_APP_EXIT:
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                    return 0;

                case IDM_EDIT_UNDO:
                case IDM_EDIT_CUT:
                case IDM_EDIT_COPY:
                case IDM_EDIT_PASTE:
                case IDM_EDIT_CLEAR:
                    MessageBeep(0);
                    return 0;
                case IDM_COR_1:
                case IDM_COR_2:
                case IDM_COR_3:
                case IDM_COR_4:
                case IDM_COR_5:
                    CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
                    iSelection = LOWORD(wP);
                    CheckMenuItem(hMenu, iSelection, MF_CHECKED);
                    SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)(idCr[LOWORD(wP)-10]));
                    InvalidateRect(hwnd, NULL, TRUE);
                    return 0;
                case IDM_TIMER_START:
                    if(SetTimer(hwnd, ID_TIMER, 1000, NULL)) {
                        EnableMenuItem(hMenu,IDM_TIMER_START,MF_GRAYED);
                        EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_ENABLED);
                    }
                    return 0;
                case IDM_TIMER_STOP:
                    KillTimer(hwnd, ID_TIMER);
                    EnableMenuItem(hMenu,IDM_TIMER_START,MF_ENABLED);
                    EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_GRAYED);
                    return 0;
                case IDM_APP_HELP:
                    MessageBox(hwnd, aj, szAppName, 0x30 | MB_OK);
                    return 0;
                case IDM_APP_ABOUT:
                    MessageBox(hwnd, sb, szAppName, 64 | MB_OK);
                    return 0;
            }
            break;
            case WM_TIMER:
                MessageBeep(0);
                return 0;
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
    }
    return DefWindowProc(hwnd, msg, wP, lParam);
}



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

#define IDM_FILE_NEW                    1
#define IDM_FILE_OPEN                   2
#define IDM_FILE_SAVE                   3
#define IDM_FILE_SAVE_AS                4

#define IDM_APP_EXIT                    5

#define IDM_EDIT_UNDO                   6
#define IDM_EDIT_CUT                    7
#define IDM_EDIT_COPY                   8
#define IDM_EDIT_PASTE                  9
#define IDM_EDIT_CLEAR                  10

#define IDM_COR_1                       11
#define IDM_COR_2                       12
#define IDM_COR_3                       13
#define IDM_COR_4                       14
#define IDM_COR_5                       15

#define IDM_TIMER_START                 16
#define IDM_TIMER_STOP                  17

#define IDM_APP_HELP                    18
#define IDM_APP_ABOUT                   19



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

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

Menu MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&New",                IDM_FILE_NEW
        MENUITEM "&Open",               IDM_FILE_OPEN
        MENUITEM "&Save",               IDM_FILE_SAVE
        MENUITEM "Save &As...",         IDM_FILE_SAVE_AS
        MENUITEM SEPARATOR
        MENUITEM "E&xit",               IDM_APP_EXIT
    END
    POPUP "&Edit"
    BEGIN
        MENUITEM "&Undo",               IDM_EDIT_UNDO
        MENUITEM SEPARATOR
        MENUITEM "C&ut",                IDM_EDIT_CUT
        MENUITEM "&Copy",               IDM_EDIT_COPY
        MENUITEM "&Paste",              IDM_EDIT_PASTE
        MENUITEM "De&lete",             IDM_EDIT_CLEAR
    END
    POPUP "&Background"
    BEGIN
        MENUITEM "Primeira Cor",        IDM_COR_1, CHECKED
        MENUITEM "Segunda Cor",         IDM_COR_2
        MENUITEM "Terceira Cor",        IDM_COR_3
        MENUITEM "Quarta Cor",          IDM_COR_4
        MENUITEM "Quinta Cor",          IDM_COR_5
    END
    POPUP "&Timer"
    BEGIN
        MENUITEM "&Start",              IDM_TIMER_START
        MENUITEM "S&top",               IDM_TIMER_STOP, GRAYED
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&Ajuda..",            IDM_APP_HELP
        MENUITEM "&Sobre..",            IDM_APP_ABOUT
    END
END

Comentários