首页 > 解决方案 > 如何获取我在 HWND 上绘制的所有像素代码?

问题描述

我构建了一些简单的 Direct2d 程序。

我画了一些东西,我需要我的 HWND 的像素代码。

我的 Direct2d 程序可能有 1920 * 1080 * 4 字节的像素代码,

但我不知道如何联系他们。

所以这是我的代码和程序截图,请任何人帮助我。

如何打印所有像素数据的十六进制代码

图片

#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <d2d1.h>
#include <dwrite.h>
#include <iostream>
#include <ctime>
#include <atlconv.h>
#include <string>

#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")

ID2D1Factory* factory = NULL;
ID2D1HwndRenderTarget* target = NULL;

ID2D1SolidColorBrush* solid_brush;
IDWriteFactory* w_factory;
IDWriteTextFormat* w_format;
IDWriteTextLayout* w_layout;

using namespace std;

HWND hwnd;

int s_width = GetSystemMetrics(SM_CXSCREEN);
int s_height = GetSystemMetrics(SM_CYSCREEN);

clock_t start, endclock;
double result;
int fps = 0;
char fpsbuffer3[30];
char spectorbuffer[30];


void DrawString(string str, float fontSize, float x, float y, float r, float g, float b, float a)
{
    RECT re;
    GetClientRect(hwnd, &re);
    FLOAT dpix, dpiy;
    dpix = static_cast<float>(re.right - re.left);
    dpiy = static_cast<float>(re.bottom - re.top);

    USES_CONVERSION;
    wstring bb(A2W(str.c_str()));
    HRESULT res = w_factory->CreateTextLayout(bb.c_str(), bb.size(), w_format, 1920, 1080, &w_layout);

    if (SUCCEEDED(res))
    {
        DWRITE_TEXT_RANGE range = { 0, str.length() };
        w_layout->SetFontSize(fontSize, range);
        solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
        target->DrawTextLayout(D2D1::Point2F(x, y), w_layout, solid_brush);
        w_layout->Release();
        w_layout = NULL;
    }
}

void DrawBox(float x, float y, float width, float height, float thickness, float r, float g, float b, float a, bool filled)
{
    solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
    if (filled)  target->FillRectangle(D2D1::RectF(x, y, x + width, y + height), solid_brush);
    else target->DrawRectangle(D2D1::RectF(x, y, x + width, y + height), solid_brush, thickness);
}

void DrawLine(float x1, float y1, float x2, float y2, float thickness, float r, float g, float b, float a) {
    solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
    target->DrawLine(D2D1::Point2F(x1, y1), D2D1::Point2F(x2, y2), solid_brush, thickness);
}

void DrawCircle(float x, float y, float radius, float thickness, float r, float g, float b, float a, bool filled)
{
    solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
    if (filled) target->FillEllipse(D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius), solid_brush);
    else target->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius), solid_brush, thickness);
}

void DrawEllipse(float x, float y, float width, float height, float thickness, float r, float g, float b, float a, bool filled)
{
    solid_brush->SetColor(D2D1::ColorF(r, g, b, a));
    if (filled) target->FillEllipse(D2D1::Ellipse(D2D1::Point2F(x, y), width, height), solid_brush);
    else target->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(x, y), width, height), solid_brush, thickness);
}

void Draw_Edge_String(string str, float fontSize, float x, float y, float r, float g, float b, float a)
{
    DrawString(str, fontSize, x + 1, y, 0, 0, 0, 1);
    DrawString(str, fontSize, x - 1, y, 0, 0, 0, 1);
    DrawString(str, fontSize, x, y + 1, 0, 0, 0, 1);
    DrawString(str, fontSize, x, y - 1, 0, 0, 0, 1);
    DrawString(str, fontSize, x, y, r, g, b, a);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch (iMessage) {
    case WM_CREATE:
        return 0;
    //case WM_PAINT:
        //return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

void render()
{
    UpdateWindow(hwnd);
    WINDOWINFO info;
    ZeroMemory(&info, sizeof(info));
    info.cbSize = sizeof(info);

    D2D1_SIZE_U siz;
    siz.height = s_height;//((info.rcClient.bottom) - (info.rcClient.top));
    siz.width = s_width;//((info.rcClient.right) - (info.rcClient.left));
    SetWindowPos(hwnd, NULL, 0, 0, s_width, s_height, SWP_SHOWWINDOW);
    target->Resize(&siz);


    target->BeginDraw();
    target->Clear(D2D1::ColorF(1.0, 0, 0, 1.0));


    DrawCircle(300, 300, 100, 1, 0, 1, 0, 1, 1);


    endclock = clock();
    result = (double)(endclock - start);
    fps++;
    if (result > 1000)
    {
        sprintf(fpsbuffer3, "Fps: %d", fps);
        fps = 0;
        start = endclock;
    }
    std::string a = string(fpsbuffer3);
    DrawString(a, 20, siz.width - 100 + 2, 2, 0, 0, 0, 1);
    DrawString(a, 20, siz.width - 100, 0, 1, 1, 1, 1);


    //in here I want to print my pixel code. :( 



    target->EndDraw();
}


int main()
{
    WNDCLASS wc;

    wc.cbClsExtra = NULL;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"classname";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wc);


    HWND hwnd = CreateWindow(L"classname", L"windowname", WS_POPUP, 0, 0, s_width, s_height, NULL, NULL, GetModuleHandle(NULL), NULL);

    D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
    HRESULT hr = factory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_HARDWARE,
            D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN,         D2D1_ALPHA_MODE_PREMULTIPLIED)),
    D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(200, 200),
        D2D1_PRESENT_OPTIONS_IMMEDIATELY), &target);

    target->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 0.0f), &solid_brush);
    target->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
    DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&w_factory));

    hr = w_factory->CreateTextFormat(L"-2002", NULL, DWRITE_FONT_WEIGHT_REGULAR,
    DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 15.0f, L"ko-KR", &w_format);




    ShowWindow(hwnd, SW_SHOW);

    MSG messages = { 0 };

    start = clock();
    while (messages.message != WM_QUIT) {
        if (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        else {
            render();
        }
    }
}

标签: cdirect2d

解决方案


推荐阅读