在位图上绘制符号时出现问题

发布于 2024-11-26 00:56:45 字数 1233 浏览 2 评论 0原文

#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
    HBITMAP hbmp = CreateBitmap(50,50, 1, 1, NULL);
    HDC hdc = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
    HBRUSH hbrush = CreateSolidBrush(Color::White);
    HPEN hpen = CreatePen(PS_SOLID, 1, Color::Black);
    HFONT hfont = CreateFont(50, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, 
                  ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, 
                  ANTIALIASED_QUALITY, DEFAULT_PITCH, TEXT("Times New Roman"));


    HGDIOBJ hbmpOld   = SelectObject(hdc, hbmp);
    HGDIOBJ hbrushOld = SelectObject(hdc, hbrush);
    HGDIOBJ hpenOld   = SelectObject(hdc, hpen);
    HGDIOBJ hfontOld  = SelectObject(hdc, hfont);

    SetTextColor(hdc, Color::Black);
    SetBkColor(hdc, Color::White);
    SetBkMode(hdc, OPAQUE);
    RECT rect={50,0,50,0};

    FillRect(hdc, &rect, hbrush);

    DrawText(hdc, "1", -1, &rect, DT_LEFT);


    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();

    getchar();

}

但在剪贴板中的这段代码之后,我只有黑色方块,没有符号。

#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")

using namespace std;
using namespace Gdiplus;


int main()
{
    HBITMAP hbmp = CreateBitmap(50,50, 1, 1, NULL);
    HDC hdc = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
    HBRUSH hbrush = CreateSolidBrush(Color::White);
    HPEN hpen = CreatePen(PS_SOLID, 1, Color::Black);
    HFONT hfont = CreateFont(50, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, 
                  ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, 
                  ANTIALIASED_QUALITY, DEFAULT_PITCH, TEXT("Times New Roman"));


    HGDIOBJ hbmpOld   = SelectObject(hdc, hbmp);
    HGDIOBJ hbrushOld = SelectObject(hdc, hbrush);
    HGDIOBJ hpenOld   = SelectObject(hdc, hpen);
    HGDIOBJ hfontOld  = SelectObject(hdc, hfont);

    SetTextColor(hdc, Color::Black);
    SetBkColor(hdc, Color::White);
    SetBkMode(hdc, OPAQUE);
    RECT rect={50,0,50,0};

    FillRect(hdc, &rect, hbrush);

    DrawText(hdc, "1", -1, &rect, DT_LEFT);


    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();

    getchar();

}

But after this code in clipboard i have only black square without symbol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-12-03 00:56:45

除了颜色名称之外,您似乎没有使用 GDI+。我会使用 GDI RGB 宏来代替:白色 = RGB(255,255,255),黑色 = RGB(0,0,0)

您还将把所有选择的内容保留到 DC 中。在打开剪贴板之前,将所有旧对象重新选择回 DC。

You don't appear to be using GDI+ except for the color names. I'd use the GDI RGB macro instead: white = RGB(255,255,255), black = RGB(0,0,0).

You're also leaving everything selected into the DC. Reselect all the old objects back into the DC before you open the clipboard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文