A variável szBuffer recebe os valores de GetRValue, GetGValue e GetBValue através da função wsprintf - Post modificado em 04/06/2024

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

#define ID_TIMER 1

void FindWindowSize(int *, int *);

char txt[] = {"Qual é a Cor?"};

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static COLORREF cr, crLast;
    static HDC hdcScreen;
    HDC hdc;
    PAINTSTRUCT ps;
    POINT pt;
    RECT rc;
    TCHAR szBuffer[16];
    
    switch(msg) {
        case WM_CREATE:
            hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
            SetTimer(hwnd, ID_TIMER, 100, NULL);
            return 0;
        case WM_TIMER:
            GetCursorPos(&pt);
            cr = GetPixel(hdcScreen, pt.x, pt.y);
            SetPixel(hdcScreen, pt.x, pt.y, 0);
            if(cr != crLast) {
                crLast = cr;
                InvalidateRect(hwnd, NULL, FALSE);
            }
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rc);
            wsprintf(
                szBuffer, TEXT(" %02X %02X %02X "),
                GetRValue(cr), GetGValue(cr), GetBValue(cr)
            );
            DrawText(
                hdc,
                szBuffer,
                -1,
                &rc,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER
            );
            EndPaint(hwnd, &ps);
            return 0;

        case WM_DESTROY: {
            DeleteDC(hdcScreen);
            KillTimer(hwnd, ID_TIMER);
            PostQuitMessage(0);
            break;
        }
		
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

void FindWindowSize(int * pcxWindow, int * pcyWindow) {
    HDC hdcScreen;
    TEXTMETRIC tm;
    hdcScreen = CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
    GetTextMetrics(hdcScreen, &tm);
    DeleteDC(hdcScreen);
    *pcxWindow=2*GetSystemMetrics(SM_CXBORDER)+12*tm.tmAveCharWidth;
    *pcyWindow=2*GetSystemMetrics(SM_CYBORDER)+GetSystemMetrics(SM_CYCAPTION)+2*tm.tmHeight;
}

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR lpCmdLine, int iCS) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG msg; /* A temporary location for all messages */
    int cxWindow, cyWindow;

    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize		 = sizeof(WNDCLASSEX);
    wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
    wc.hInstance	 = hI;
    wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
    wc.hbrBackground = (HBRUSH)(COLOR_ACTIVECAPTION+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon		 = LoadIcon(hI, IDI_ICON);
    wc.hIconSm		 = LoadIcon(hI, IDI_ICON); 

    if(!RegisterClassEx(&wc)) {
        MessageBox(
            NULL,
            "Window Registration Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }
	
    FindWindowSize(&cxWindow, &cyWindow);

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,"WindowClass",txt,WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, cxWindow, cyWindow,
        NULL,NULL,hI,NULL
    );

    if(hwnd == NULL) {
        MessageBox(
            NULL,
            "Window Creation Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }

    while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
        TranslateMessage(&msg); /* Translate key codes to chars if present */
        DispatchMessage(&msg); /* Send it to WndProc */
    }
    return msg.wParam;
}

resource.html
/* -- Arquivo: resource.h -- */


#define IDI_ICON 1

resourcerc.html
/* -- Arquivo: resource.rc -- */


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

IDI_ICON ICON "project1.ico"

Comentários