Texto2 CURSOR "texto1.cur"


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

#define BUFFER(x,y) *(pBuffer + y * xBuf +x)


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

HFONT hFonte;
int   bold;
BOOL  itc;

char name[] = "Texto2";

int WINAPI WinMain(HINSTANCE hC, HINSTANCE hCA, LPSTR szLinhaCmd, int iCS) {
    HWND     hjan;
    MSG      msg;
    WNDCLASS classejan;
    HACCEL   hAccel;

    classejan.style         = CS_HREDRAW | CS_VREDRAW;
    classejan.lpfnWndProc   = ProcJan;
    classejan.cbClsExtra    = 0;
    classejan.cbWndExtra    = 0;
    classejan.hInstance     = hC;
    classejan.hIcon         = LoadIcon(hC, IDI_ICON);
    classejan.hCursor       = LoadCursor(hC, name);
    classejan.hbrBackground = GetStockObject(WHITE_BRUSH);
    classejan.lpszMenuName  = name;
    classejan.lpszClassName = name;

    if(!RegisterClass(&classejan)) {
        return 0;
    }

    hjan = CreateWindow(
        name, "Editando texto na Janela:", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        HWND_DESKTOP, NULL, hC, NULL
    );

    hAccel = LoadAccelerators(hC, name);

    ShowWindow(hjan, iCS);
    UpdateWindow(hjan);

    while(GetMessage(&msg, NULL, 0, 0)) {
        if(!TranslateAccelerator(hjan, hAccel, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage (&msg);
            FreeConsole();
        }
    }
    return msg.wParam;
}

LRESULT CALLBACK ProcJan(HWND hjan, UINT msg, WPARAM wParam, LPARAM lParam) {
    static char *pBuffer = NULL;
    static int  xPi, yPi, xBuf, yBuf, xCar, yCar, cxCliente, cyCliente;
    HDC         hdc;
    PAINTSTRUCT ps;
    TEXTMETRIC  tm;
    int         x, y, i;

    switch(msg) {
        case WM_CREATE:
            bold   = 0;
            itc = FALSE;
            hdc    = GetDC(hjan);
            hFonte = CreateFont(32,0,0,0,bold,itc,0,0,0,0,0,0,0,"Arial Black");
            SelectObject(hdc, hFonte);
            GetTextMetrics(hdc, &tm);
            xCar = tm.tmAveCharWidth;
            yCar = tm.tmHeight;
            ReleaseDC(hjan, hdc);
            return 0;
        case WM_SIZE:
            cxCliente = LOWORD(lParam);
            cyCliente = HIWORD(lParam);

            xBuf = max(1, cxCliente/xCar);
            yBuf = max(1, cyCliente/yCar);
            if(pBuffer!=NULL) {
                free(pBuffer);
            }
            if((pBuffer=(char *)malloc(xBuf*yBuf))==NULL) {
                MessageBox(hjan,"Insuficiente",name,MB_ICONEXCLAMATION|MB_OK);
            }
            else {
                for(y=0; y<yBuf; y++)
                    for(x=0; x<xBuf; x++)
                        BUFFER(x, y) = ' ';
            }
            xPi=0;
            yPi=0;
            if(hjan==GetFocus())
                SetCaretPos(xPi*xCar,yPi*yCar);
            return 0;
        case WM_SETFOCUS:
            CreateCaret(hjan, NULL, xCar, yCar);
            SetCaretPos(xPi*xCar, yPi*yCar);
            ShowCaret(hjan);
            return 0;
        case WM_KILLFOCUS:
            HideCaret(hjan);
            DestroyCaret();
            return 0;
        case WM_KEYDOWN:
            switch(wParam) {
                case VK_HOME:
                    xPi = 0;
                    break;
                case VK_END:
                    xPi = xBuf - 1;
                    break;
                case VK_PRIOR:
                    yPi = 0;
                    break;
                case VK_NEXT:
                    yPi = yBuf - 1;
                    break;
                case VK_LEFT:
                    xPi = max(xPi - 1,0);
                    break;
                case VK_RIGHT:
                    xPi = min(xPi+1, xBuf-1);
                    break;
                case VK_UP:
                    yPi = max(yPi - 1,0);
                    break;
                case VK_DOWN:
                    yPi = min(yPi+1, yBuf-1);
                    break;
                case DELETE:
                    for(x=xPi; x<xBuf-1; x++) {
                        BUFFER(x, yPi) = BUFFER(x+1, yPi);
                    }
                    BUFFER(xBuf-1, yPi) = ' ';
                    ShowCaret(hjan);
                    break;
            }
            SetCaretPos(xPi*xCar, yPi*yCar);
            InvalidateRect(hjan, NULL, TRUE);
            return 0;
        case WM_CHAR:
            for(i=0; i<(int)LOWORD(lParam); i++) {
                switch(wParam) {
                    case '\b':
                        if(xPi>0) {
                            xPi--;
                            SendMessage(hjan, WM_KEYDOWN, VK_DELETE, 1L);
                        }
                        break;
                    case '\t':
                        do {
                            SendMessage(hjan, WM_CHAR, ' ', 1L);
                        }
                        while(xPi%8 != 0);
                        break;
                    case '\n':
                        if(++yPi==yBuf) {
                            yPi=0;
                        }
                        break;
                    case '\r':
                        xPi = 0;
                        if(++yPi==yBuf) {
                            yPi = 0;
                        }
                        break;
                    case '\x1B':
                        for(y=0; y<yBuf; y++)
                            for(x=0; x<xBuf; x++)
                            BUFFER(x, y) = ' ';
                        xPi = 0;
                        yPi = 0;
                        InvalidateRect(hjan, NULL, FALSE);
                        break;
                    default:
                        BUFFER(xPi, yPi) = (char) wParam;
                        HideCaret(hjan);
                        ShowCaret(hjan);
                        if(++xPi==xBuf) {
                            xPi = 0;
                            if(++yPi==yBuf) {
                                yPi = 0;
                            }
                        }
                        break;
                }
            }
            SetCaretPos(xPi*xCar, yPi * yCar);
            InvalidateRect(hjan, NULL, TRUE);
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case IDM_FONTE1:
                    bold = 0;
                    itc = FALSE;
                    break;
                case IDM_FONTE2:
                    bold   = FW_BOLD;
                    itc = FALSE;
                    break;
                case IDM_FONTE3:
                    bold   = 0;
                    itc = FALSE;
                    break;
                case IDM_FONTE4:
                    bold   = FW_BOLD;
                    itc = FALSE;
                    break;
                case IDM_SAIR:
                    if(IDOK==MessageBox(hjan,"Deseja sair?",name,MB_OKCANCEL|0x00000030L)) {
                        SendMessage(hjan, WM_CLOSE, 0, 0L);
                    }
                    break;
            }
            InvalidateRect(hjan, NULL, TRUE);
            return 0;
        case WM_PAINT:
            hdc   = BeginPaint(hjan, &ps);
            hFonte=CreateFont(32,0,0,0,bold, itc, 0, 0, 0, 0, 0, 0, 0, "Arial Black");
            SelectObject(hdc, hFonte);
            xBuf  = max(1, cxCliente/xCar);
            yBuf  = max(1, cyCliente/yCar);
            for(y=0; y<yBuf; y++) {
                TextOut(hdc, 0, y*yCar, &BUFFER(0, y), xBuf);
            }
            EndPaint(hjan, &ps);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            DeleteObject((HGDIOBJ) hFonte);
            return 0;
        case WM_CLOSE:
            DestroyWindow(hjan);
            return 0;
    }
    return DefWindowProc(hjan, msg, wParam, lParam);
}




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

#define IDM_FONTE1 0
#define IDM_FONTE2 1
#define IDM_FONTE3 2
#define IDM_FONTE4 3
#define IDM_FONTE5 4
#define IDM_SAIR   5
#define IDI_ICON   6
/********************** Arquivo: resource.rc ***********************/ #include "afxres.h" #include "resource.h" IDI_ICON ICON "texto1.ico" Texto1 MENU { MENUITEM "&Sair ^S", IDM_SAIR POPUP "&Fonte" { MENUITEM "&Normal\tF1", IDM_FONTE1 MENUITEM "&N&egrito\tF2", IDM_FONTE2 MENUITEM "&Itálico\tF3", IDM_FONTE3 MENUITEM "Negrito Itálic&o\tF4", IDM_FONTE4 } } Texto1 ACCELERATORS { VK_F1, IDM_FONTE1, VIRTKEY VK_F2, IDM_FONTE2, VIRTKEY VK_F3, IDM_FONTE3, VIRTKEY VK_F4, IDM_FONTE4, VIRTKEY "^S", IDM_SAIR } Texto2 CURSOR "texto1.cur" Texto2 ICON "texto1.ico"

Comentários