(非托管C++) 绘制文本我已经成功绘制PNG文件,我可以在桌面拖动一个透明分层窗口,但现在我的问题是在透明分层窗口绘制文本C++ GDI +的透明分层窗口
这里是我的代码和我在中间绘制文本的尝试,需要注意的是我使用的screenDC而不是使用一个在WM_PAINT消息是很重要的
[编辑]的意见后 更新的代码,现在我'只是试图在获取HBITMAP版本之前在位图上写入文本,这一次我需要使用 我正在使用DrawString becau SE的TextOut()不是GDI +,我希望的DrawString真的是GDI +洛尔 仍然没有工作,虽然,不知道我在做什么错
void Draw() // draws a frame on the layered window AND moves it based on x and y
{
HDC screenDC(NULL); // grab screen
HDC sourceDC(CreateCompatibleDC(screenDC));
POINT pos = {x,y}; // drawing location
POINT sourcePos = {0,0}; // top left of image
SIZE size = {100,100}; // 100x100 image
BLENDFUNCTION blendFunction = {0};
HBITMAP bufferBitmap = {0};
Bitmap* TheBitmap = crnimage; // crnimage was already loaded earlier
// ------------important part goes here, my attempt at drawing text ------------//
Gdiplus::Graphics Gx(TheBitmap);
// Font* myFont = new Font(sourceDC);
Font myFont(L"Arial", 16);
RectF therect;
therect.Height = 20;
therect.Width = 180;
therect.X = 0;
therect.Y = 0;
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
format.GenericDefault();
Gdiplus::SolidBrush GxTextBrush(Gdiplus::Color(255, 255, 0,255));
WCHAR thetext[] = L"Sample Text";
int stats = Gx.DrawString(thetext, -1, &myFont, therect, &format, &GxTextBrush);
if(stats) // DrawString returns nonzero if there is an error
msgbox(stats);
stats = Gx.DrawRectangle(&Pen(Color::Red, 3), therect);
// the rectangle and text both draw fine now
// ------------important part goes here, my attempt at drawing text ------------//
TheBitmap->GetHBITMAP(0, &bufferBitmap);
HBITMAP oldBmpSelInDC;
oldBmpSelInDC = (HBITMAP)SelectObject(sourceDC, bufferBitmap);
// some alpha blending
blendFunction.BlendOp = AC_SRC_OVER;
blendFunction.SourceConstantAlpha = wndalpha;
blendFunction.AlphaFormat = AC_SRC_ALPHA;
COLORREF colorKey(RGB(255,0,255));
DWORD flags(ULW_ALPHA);
UpdateLayeredWindow(hWnd, screenDC, &pos, & size, sourceDC, &sourcePos,
colorKey, &blendFunction, flags);
// release buffered image from memory
SelectObject(sourceDC, oldBmpSelInDC);
DeleteDC(sourceDC);
DeleteObject(bufferBitmap);
// finally release the screen
ReleaseDC(0, screenDC);
}
我一直在努力写我的分层窗口上的文本我知道有几种方法可以去做 (很遗憾,我不知道如何)
我看到的常见选项是在位图上绘制文本,然后渲染位图本身
- 使用GDI +加载一个位图
- 创建从位图
- 使用的DrawString一个图形对象写入文本位图
- 处置Graphics对象
- 使用位图保存方法来保存结果文件
显然,人们还可以使图形从DC对象,然后画上了DC的文字,而我又没有线索如何做到这一点
TextOut后的UpdateLayeredWindow调用将立即擦除文本。确实非常重要的是,您只需绘制WM_PAINT消息处理程序。我看不到你绕过这个的原因。 – 2011-01-24 16:46:25
除了Hans所说的外,`TextOut`是一个GDI函数,而不是GDI +,并且对透明度一无所知。所以它会创建与DC背景颜色混合的文本,而不是任何应该显示的内容。 – 2011-01-24 19:17:00