PrintScreen MENU DISCARDABLE


/** A palavra de Deus ilumina nossa mente, e purifica nossos corações! */

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

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

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, PSTR szCmdLine, int iCmdShow) {
    static TCHAR nome[] = TEXT("PrintScreen");
    HACCEL       hAccel;
    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(hI, IDI_ICON);
    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName  = nome;
    wndclass.lpszClassName = nome;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Error!"), nome, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindow(
        nome, T, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hI, NULL
    );

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    hAccel = LoadAccelerators(hI, nome);

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

void InvertBlock(HWND hwndScr, HWND hwnd, POINT ptBeg, POINT ptEnd) {
    HDC hdc;
    hdc = GetDCEx(hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
    ClientToScreen(hwnd, &ptBeg);
    ClientToScreen(hwnd, &ptEnd);
    PatBlt(hdc,ptBeg.x,ptBeg.y,ptEnd.x-ptBeg.x,ptEnd.y-ptBeg.y,DSTINVERT);
    ReleaseDC(hwndScr, hdc);
}

HBITMAP CopyBitmap(HBITMAP hBitmapSrc) {
    BITMAP  bitmap;
    HBITMAP hBitmapDst;
    HDC     hdcSrc, hdcDst;
    GetObject(hBitmapSrc, sizeof(BITMAP), &bitmap);
    hBitmapDst = CreateBitmapIndirect(&bitmap);
    hdcSrc = CreateCompatibleDC(NULL);
    hdcDst = CreateCompatibleDC(NULL);
    SelectObject(hdcSrc, hBitmapSrc);
    SelectObject(hdcDst, hBitmapDst);
    BitBlt(hdcDst,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcSrc,0,0,SRCCOPY);
    DeleteDC(hdcSrc);
    DeleteDC(hdcDst);
    return hBitmapDst;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static BOOL    bCapturing, bBlocking;
    static HBITMAP hBitmap;
    static HWND    hwndScr;
    static POINT   pb, ptE;
    BITMAP         bm;
    HBITMAP        hBitmapClip;
    HDC            hdc, hdcM;
    int            iEnable;
    PAINTSTRUCT    ps;
    RECT           rt;
    switch(msg) {
        case WM_LBUTTONDOWN:
            if(!bCapturing) {
                if(LockWindowUpdate(hwndScr = GetDesktopWindow())) {
                    bCapturing = TRUE;
                    SetCapture (hwnd);
                    SetCursor(LoadCursor(NULL, IDC_CROSS));
                }
                else
                    MessageBeep(0);
            }
            return 0;
        case WM_RBUTTONDOWN:
            if(bCapturing) {
                bBlocking = TRUE;
                pb.x = LOWORD(lParam);
                pb.y = HIWORD (lParam) ;
                ptE = pb;
                InvertBlock(hwndScr, hwnd, pb, ptE);
            }
            return 0;
        case WM_MOUSEMOVE:
            if(bBlocking) {
                InvertBlock(hwndScr, hwnd, pb, ptE);
                ptE.x = LOWORD(lParam);
                ptE.y = HIWORD(lParam);
                InvertBlock(hwndScr, hwnd, pb, ptE);
            }
            return 0;
        case WM_LBUTTONUP:
        case WM_RBUTTONUP:
            if(bBlocking) {
                InvertBlock(hwndScr, hwnd, pb, ptE);
                ptE.x = LOWORD(lParam);
                ptE.y = HIWORD(lParam);
                if(hBitmap) {
                    DeleteObject(hBitmap);
                    hBitmap = NULL;
                }
                hdc = GetDC(hwnd);
                hdcM = CreateCompatibleDC(hdc);
                hBitmap=CreateCompatibleBitmap(hdc,abs(ptE.x-pb.x),abs(ptE.y - pb.y));
                SelectObject(hdcM, hBitmap);
                StretchBlt(hdcM,0,0,abs(ptE.x-pb.x),abs(ptE.y-pb.y),hdc,pb.x,pb.y,ptE.x-pb.x,ptE.y-pb.y,SRCCOPY);
                DeleteDC(hdcM);
                ReleaseDC(hwnd, hdc);
                InvalidateRect(hwnd, NULL, TRUE);
            }
            if(bBlocking || bCapturing) {
                bBlocking = bCapturing = FALSE;
                SetCursor(LoadCursor (NULL, IDC_ARROW));
                ReleaseCapture();
                LockWindowUpdate(NULL);
            }
            return 0;
        case WM_INITMENUPOPUP:
            iEnable=IsClipboardFormatAvailable((CF_BITMAP)?MF_ENABLED:MF_GRAYED);
            EnableMenuItem((HMENU) wParam, IDM_EDIT_PASTE, iEnable);
            iEnable = hBitmap ? MF_ENABLED : MF_GRAYED;
            EnableMenuItem((HMENU) wParam, IDM_EDIT_CUT,    iEnable);
            EnableMenuItem((HMENU) wParam, IDM_EDIT_COPY,   iEnable);
            EnableMenuItem((HMENU) wParam, IDM_EDIT_DELETE, iEnable);
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case IDM_EDIT_CUT:
                case IDM_EDIT_COPY:
                    if(hBitmap) {
                        hBitmapClip = CopyBitmap(hBitmap);
                        OpenClipboard(hwnd);
                        EmptyClipboard();
                        SetClipboardData(CF_BITMAP, hBitmapClip);
                    }
                    if(LOWORD(wParam) == IDM_EDIT_COPY)
                        return 0;
                case IDM_EDIT_DELETE:
                    if(hBitmap) {
                        DeleteObject(hBitmap);
                        hBitmap = NULL;
                    }
                    InvalidateRect(hwnd, NULL, TRUE);
                    return 0;
                case IDM_EDIT_PASTE:
                    if(hBitmap) {
                        DeleteObject(hBitmap);
                        hBitmap = NULL;
                    }
                    OpenClipboard(hwnd);
                    hBitmapClip = GetClipboardData(CF_BITMAP);
                    if(hBitmapClip) {
                        hBitmap = CopyBitmap(hBitmapClip);
                    }
                    CloseClipboard();
                    InvalidateRect(hwnd, NULL, TRUE);
                    return 0;
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            if(hBitmap) {
                GetClientRect(hwnd, &rt);
                hdcM = CreateCompatibleDC(hdc);
                SelectObject(hdcM, hBitmap);
                GetObject(hBitmap, sizeof(BITMAP), (PSTR) &bm);
                SetStretchBltMode(hdc, COLORONCOLOR);
                StretchBlt(hdc,0,0,rt.right,rt.bottom,hdcM,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
                DeleteDC(hdcM);
            }
            EndPaint(hwnd, &ps);
            return 0;
        case WM_DESTROY:
            if(hBitmap)
                DeleteObject(hBitmap);
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}


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

#define IDI_ICON                        1
#define IDM_EDIT_CUT                    40001
#define IDM_EDIT_COPY                   40002
#define IDM_EDIT_PASTE                  40003
#define IDM_EDIT_DELETE                 40004


#define T "StretchBlt(hdcM,0,0,abs(ptE.x-pb.x),abs(ptE.y-pb.y),hdc,pb.x,pb.y,ptE.x-pb.x,ptE.y-pb.y,SRCCOPY);"


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

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

IDI_ICON ICON              "project1.ico"

PrintScreen MENU DISCARDABLE
BEGIN
    POPUP "&Edit"
    BEGIN
        MENUITEM "Cu&t\tCtrl+X",                IDM_EDIT_CUT
        MENUITEM "&Copy\tCtrl+C",               IDM_EDIT_COPY
        MENUITEM "&Paste\tCtrl+V",              IDM_EDIT_PASTE
        MENUITEM "De&lete\tDelete",             IDM_EDIT_DELETE
    END
END

PrintScreen ACCELERATORS DISCARDABLE
BEGIN
    "C",          IDM_EDIT_COPY,        VIRTKEY, CONTROL, NOINVERT
    "V",          IDM_EDIT_PASTE,       VIRTKEY, CONTROL, NOINVERT
    VK_DELETE,    IDM_EDIT_DELETE,      VIRTKEY,          NOINVERT
    "X",          IDM_EDIT_CUT,         VIRTKEY, CONTROL, NOINVERT
END

Comentários