Ir ao conteúdo

C++ Como fazer um programa que inverte as cores em c++


Posts recomendados

Postado

@Arthur160 CapturaTelaInvertida() continua do tipo inteiro:

3 horas atrás, Arthur160 disse:
int CapturaTelaInvertida()

Tem que retornar HBITMAP.

 

3 horas atrás, Arthur160 disse:
int x1 = GetSystemMetrics(SM_XVIRTUALSCREEN);
int y1 = GetSystemMetrics(SM_YVIRTUALSCREEN);
int x2 = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int y2 = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int w = x2 - x1;
int h = y2 - y1;

Isso fica dentro da função.

 

3 horas atrás, Arthur160 disse:
DeleteObject(CapturaTelaInvertida());

O retorno da função não é salvo em nenhum HBITMAP, você não tem que deletar nada.

 

Dê uma lida no código que eu tinha escrito, talvez te ajude a entender:

#define ID_IMAGE 1

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

HWND hWnd;
HBITMAP hbit;

int WindowX(void);
int WindowY(void);
HBITMAP GetScreenShotInvertedColor(void);

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

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
{
    hWnd = GetConsoleWindow();

    hbit = GetScreenShotInvertedColor();

    MSG  msg;
    WNDCLASSW wc = {0};
    wc.lpszClassName = L"Edit control";
    wc.hInstance     = hInstance;
    wc.hbrBackground = CreateSolidBrush(RGB(0,0,0));
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Inverter",
                  WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_BORDER,
                  0, 0, WindowX(), WindowY(),
                  0, 0, hInstance, 0);

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

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND bk;
    switch (msg)
    {
        case WM_CREATE:
            {
                bk = CreateWindowW(L"Static", L"", WS_CHILD | WS_VISIBLE | SS_BITMAP,
                                   0, 0, WindowX(), WindowY(), hwnd,
                                   (HMENU) ID_IMAGE, NULL, NULL);

                SendMessage(bk, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hbit);

                SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);

                ShowWindow(hwnd, SW_MAXIMIZE);

                ShowWindow(hWnd, SW_MINIMIZE);
                //won't hide the window without SW_MINIMIZE
                ShowWindow(hWnd, SW_HIDE);
            }
            break;
        case WM_CLOSE:
            {
                MessageBeep(MB_OK);
                if (MessageBox(hwnd, "Really quit?", "Inverter", MB_OKCANCEL) == IDOK)
                {
                    DestroyWindow(hwnd);
                } else {
                    return 0;
                }
            }
            break;
        case WM_DESTROY:
            {
                // Fechar o programa
                PostQuitMessage(0);
                // Fechar a console
                PostMessage(hWnd, WM_CLOSE, 0, 0);
            }
            break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

int WindowX(void)
{
	RECT desktop_rect_;
	HWND desktop_ = GetDesktopWindow();
	GetWindowRect(desktop_, &desktop_rect_);
	return desktop_rect_.right;
}

int WindowY(void)
{
	RECT desktop_rect_;
	HWND desktop_ = GetDesktopWindow();
	GetWindowRect(desktop_, &desktop_rect_);
	return desktop_rect_.bottom;
}

HBITMAP GetScreenShotInvertedColor(void)
{
    int x1, y1, x2, y2, w, h;

    x1 = GetSystemMetrics(SM_XVIRTUALSCREEN);
    y1 = GetSystemMetrics(SM_YVIRTUALSCREEN);
    x2 = GetSystemMetrics(SM_CXVIRTUALSCREEN);
    y2 = GetSystemMetrics(SM_CYVIRTUALSCREEN);
    w = x2 - x1;
    h = y2 - y1;

    HDC hScreen = GetDC(NULL);
    HDC hDC = CreateCompatibleDC(hScreen);
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);
    HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
    BitBlt(hDC, 0, 0, w, h, hScreen, x1, y1, NOTSRCCOPY);

    SelectObject(hDC, old_obj);
    DeleteDC(hDC);
    ReleaseDC(NULL, hScreen);

    return hBitmap;
}

alt+f4 pra fechar 🤪

Postado

Agora eu entendi.

A Função não deve ser int e sim, HBITMAP.

 

E também as funções WindowX() e WindowY() devem ser declaradas manualmente e eu antes achava que elas eram declaradas na própia API do Windows mesmo.

Mas tinha dado um erro ao coloca-lás e depois achei que você tinha feito algo para usar essas funções,

mas, agora entendi, você que declarou elas manualmente.

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!