在位图上绘制符号的问题

时间:2011-07-23 12:20:23

标签: c++ bitmap gdi+ clipboard symbols

#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();

}

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

1 个答案:

答案 0 :(得分:0)

除颜色名称外,您似乎没有使用GDI +。我会改用GDI RGB宏:white = RGB(255,255,255),black = RGB(0,0,0)

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